var whitespace = " \t\n\r";


$(document).ready(function() {
	// fix IE6 problems
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		$('#nav li').hover(
			function() {
				$(this).addClass("selected");
			},
			function() {
				$(this).removeClass("selected");
			}
		);
		$('#footer').css('margin-top', '-5px');
	}

	// fix IE7 problems
	if ($.browser.msie && $.browser.version.substr(0,1)==7) {
		$('#header').css('padding-bottom', '0');
		$('#nav').css('padding', '3px 0 0 0');
		$('#footer .box h4').css('margin-top', '-1px');
	}

	// fix IE6 & 7 problems
	if ($.browser.msie && $.browser.version.substr(0,1)<8) {
		$('.b2, .b3, .b4, #header, #content').css('background', '#ddd none');
	}

	// make boxes the same height
	makeBoxesSameHeight();
});

// make boxes the same height
function makeBoxesSameHeight() {
	if($("body[class~='columns3']")) {
		var lhsColumnBoxHeight = $('#lhsColumn .box').height();
		var centerColumnBoxHeight = $('#centerColumn .box').height();
		var rhsColumnBoxHeight = $('#rhsColumn .box').height();

		if(lhsColumnBoxHeight >= centerColumnBoxHeight && lhsColumnBoxHeight >= rhsColumnBoxHeight) {
			$('#centerColumn .box').height(lhsColumnBoxHeight);
			$('#rhsColumn .box').height(lhsColumnBoxHeight);
		}

		else if(centerColumnBoxHeight >= lhsColumnBoxHeight && centerColumnBoxHeight >= rhsColumnBoxHeight) {
			$('#lhsColumn .box').height(centerColumnBoxHeight);
			$('#rhsColumn .box').height(centerColumnBoxHeight);
		}

		else if(rhsColumnBoxHeight >= lhsColumnBoxHeight && rhsColumnBoxHeight >= centerColumnBoxHeight) {
			$('#lhsColumn .box').height(rhsColumnBoxHeight);
			$('#centerColumn .box').height(rhsColumnBoxHeight);
		}
	}
}


//ensure an entered price is valid
function isValidPrice(price) {
	var priceAsString = new String(price);
	var count_decimal = 0;

	// check the price isn't blank
	if(priceAsString.length == 0) {
		return false;
	}

	// check for valid characters (0-9, ., -) and only 1 decimal point and correctly positioned negative sign (ie. at the start)
	for (i=0; i<priceAsString.length; i++) {
		if ((priceAsString.charAt(i) < '0' || priceAsString.charAt(i) > '9') && (priceAsString.charAt(i) != '.')  && (priceAsString.charAt(i) != '-')) {
			return false;
		} else {
			if (i > 0 && priceAsString.charAt(i) == '-') {
				return false;
			}
			if (priceAsString.charAt(i) == '.') {
				count_decimal++;
				if (count_decimal > 1) {
					return false;
				}
			}
		}
	}

	// check there are either 0 or 2 decimal places
	var decimalPosition = priceAsString.indexOf('.');
	if (decimalPosition > -1 && priceAsString.length != decimalPosition + 3) {
		return false;
	}

	return true;
}

// Validate a Credit Card number
function validateCreditCard(s) {
	var v = "0123456789";
	var w = "";
	for (var i=0; i < s.length; i++) {
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
			w += x;
	}
	var j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	var k = Math.floor(j);
	var m = Math.ceil(j) - k;
	var c = 0;
	for (var i=0; i<k; i++) {
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}

// Validate an email address
function isEmail(str) {
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported)
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}

/****************************************************************/
// Returns true if the string passed in is empty
/****************************************************************/
function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

/****************************************************************/
// Returns true if the string passed in contains whitespace
/****************************************************************/
function isWhitespace(s) {
	var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++) {
		// Check that current character isn't whitespace.
		var c = s.charAt(i);

		if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

/****************************************************************/
// Returns true if the string passed in is a valid integer
// valid characters  : -0123456789
// special rules : - : can only appear as the first character
/****************************************************************/
function checkInteger(valueToTest) {
	if (isWhitespace(valueToTest)) return true;
	var i = 0;

	for (i = 0; i < valueToTest.length; i++)
		if ((valueToTest.charAt(i) < '0' || valueToTest.charAt(i) > '9') && (valueToTest.charAt(i) != '.')) {
			return false;
		} else {
			if (i>0 && valueToTest.charAt(i) == '-') {
				return false;
			}
		}
	return true;
}

function checkPositiveInteger(valueToTest) {
	return checkInteger(valueToTest) && valueToTest > 0;
}

/****************************************************************/
// Returns true if the string passed in is a valid float
// valid characters -.0123456789
// special rules : - : can only appear as the first character
//               : . : can only appear once
/****************************************************************/
function checkFloat(valueToTest) {
	var count_decimal = 0;
	if (isWhitespace(valueToTest)) return true;
	var i = 0;

	for (i = 0; i < valueToTest.length; i++)
		if ((valueToTest.charAt(i) < '0' || valueToTest.charAt(i) > '9') && (valueToTest.charAt(i) != '.')  && (valueToTest.charAt(i) != '-')) {
			return false;
		} else {
			if (i>0 && valueToTest.charAt(i) == '-') {
				return false;
			}
			if (valueToTest.charAt(i) == '.')
			{
				count_decimal++;
				if (count_decimal>1) {
					return false;
				}
			}
		}

	return true;
}

// Force positive float
function checkPositiveFloat(valueToTest) {
	return checkFloat(valueToTest) && valueToTest > 0;
}


// Remove leading zero's from a string
function removeLeadingZeros(initialString) {
	return(initialString.replace(/^0+/, ""));
}


// macromedia functions
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

