String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); }
String.prototype.stripNonDigits = function() { return this.replace(/[^0-9]/g,""); }
String.prototype.stripNonDecimal = function() { return this.replace(/[^0-9.]/g,""); }

function isIE() { return (document.all && !window.opera); }
function isSafari() { return (navigator.userAgent.indexOf("Safari") != -1); }
function isObject(a) { return (a && typeof a == 'object'); }
function isZip (string) { return ( string.search(/\d{5}(-\d{4})?/) != -1); }
function isEmail( string ) { return ( string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1 ); }
function isDigit (string) {	return ( string.search(/^[0-9,\.]+$/) != -1); }
function isPhoneNumber (string) { return ( string.search(/^\(?\d{3}\)?(\s|-)?\d{3}(\s|-)?\d{4}$/) != -1); }
function isInternationalPhoneNumber (string) { return ( string.search(/^\d(\d|-){7,20}/) != -1); }

function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent) break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1) {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}
  
function ajaxManager() {
	var args = ajaxManager.arguments;
	switch(args[0]) {
		case "load_page":
			el = document.getElementById(args[2]);
			l = document.getElementById('loading');
			if(l) {
				l.style.width = el.offsetWidth+'px';
				l.style.height = el.offsetHeight+'px';
				l.style.top = findPosY(el) + 'px';
				l.style.display = 'block';
			}
			if(document.getElementById) {
				var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
			}
			if (x)	{
				x.onreadystatechange = function() {
					if (x.readyState == 4 && x.status == 200) {
						el = document.getElementById(args[2]);
						el.innerHTML = x.responseText;
						l = document.getElementById('loading');
						if(l) l.style.display = '';
					}
				}
				x.open("GET", args[1], true);
				x.send(null);
			}
			break;
	}
}

function getRadioValue(o){
	if(!isObject(o)) return null;
	for (i=0;i < o.length; i++) if (o[i].checked == true) return(o[i].value);
	return null;
}
function displayError(o,s_error) {
	if(o && o.focus) o.focus();
	alert(s_error);
	return false;
}
function browseBy(s) { ajaxManager("load_page",s,"categoriesContainer"); }

function addBookmark(title,url) {
	if (window.sidebar) 
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print) { 
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else if(document.all)
		window.external.AddFavorite(url, title);
}


function enlarge(url) {
  window.open(url, 'Enlarge Image');
}

function updateSubscribeForm(o) {
	if(o.value == 'for news and special offers') o.value = '';
}

function checkSubscribeForm(o) {
	if(!o) return true;
	if(!document.getElementById) return true;
	
	if(!isEmail(o['subscribe_email'].value)) {
		return displayError(o['subscribe_email'],'Please enter a valid email');
	}
	
	return true;
}
