
function OpenWindow( url, width, height, options, name ) {
	if ( ! width ) width = 325;
	if ( ! height ) height = 450;
	if ( ! options ) options = "scrollbars=no,menubar=no,toolbar=no,location=no,status=yes,resizable=no,x=0,y=0";
	if ( ! name ) name = "outsideSiteWindow";
	var newWin = window.open( url, name, "width=" + width + ",height=" + height + "," + options );
}

//checkout form
function billEqShip(f) {

	if(f.billeqship.checked == true) {
		f.shipAddress.value = f.billAddress.value;
		f.shipCity.value = f.billCity.value;
		f.shipPostal.value = f.billPostal.value;
		var thisIndex = f.billProvince.options[f.billProvince.selectedIndex].index;
		if (thisIndex == 2){
		f.shipProvince.options[1].selected = true;
			}
		else {
		f.shipProvince.options[0].selected = true;
}
		//f.billProvince.options[f.billProvince.selectedIndex].index;
		f.shipAddress.disabled = true;
		f.shipCity.disabled = true;
		f.shipPostal.disabled = true;
		f.shipProvince.disabled = true;
	}
	else {
		f.shipAddress.value = "";
		f.shipCity.value = "";
		f.shipPostal.value = "";
		f.shipProvince.options[0].selected = true;
		f.shipAddress.disabled = false;
		f.shipCity.disabled = false;
		f.shipPostal.disabled = false;
		f.shipProvince.disabled = false;
	}
}
	
//checkout form
function resetFields(f) {
	//alert('reset');
	for (i = 0; i < f.elements.length; i++) {
	
		if (f.elements[i].disabled == true) {
			f.elements[i].disabled = false;
		}
	
	}

}

/*
//
start form field validation functions
//
*/

// General function that returns false if "fieldname" is not a number.
function checknumber(fieldname) {
	//alert(fieldname);
	//var strStripped = ignoreSpaces(fieldname.value);
	//alert(strStripped);
	var strStripped = fieldname.value;
	var numPattern = /\d/;
	var nonNumPattern = /\D/;

	var isNumber = ( strStripped.match(numPattern) == null )? false : true;
	var isNonNumber = ( strStripped.match(nonNumPattern) == null )? false : true;

	var fieldval = ( isNumber && !isNonNumber ) ? true : false;
	return fieldval;
}

// General function that returns a string with its spaces removed.
function ignoreSpaces(string) {
	//alert("string:" + string);
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++) {
		temp += splitstring[i];
	}
	return temp;
}

// General function that returns false if "fieldname" is empty.
function checkfield(fieldname) {
	var strStripped = ignoreSpaces(fieldname.value);
	//alert(strStripped.length);
	var fieldval = (strStripped.length == '') ? false: true;
	return fieldval;
}

// validate dropdown
function checkoption(fieldname) {
	var fieldval = (fieldname.options[fieldname.selectedIndex].value == 0)? false: true;
	return fieldval;
}

// validate float
function checkfloat(fieldname) {
	//var strStripped = ignoreSpaces(fieldname.value);
	var strStripped = fieldname.value;
	var floatPattern = /^(\d+\.?\d*|\.\d+)$/;
	var isFloat = ( strStripped.match(floatPattern) == null )? false : true;
	return isFloat;
}
// validate email
function checkemail(fieldname) {
	var strStripped = ignoreSpaces(fieldname.value);
	// var emailPattern = /\w{1,}@\w{1,}\.\w{1,}/;			// Pattern is "j@g.f"
	var emailPattern = (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);			// Pattern is "j@g.f"
	var isEmail = ( strStripped.match(emailPattern) == null )? false : true;
	return isEmail;
}

// validate url
function checkurl(fieldname) {
	var strStripped = ignoreSpaces(fieldname.value);
	var isURL = true;

	if (strStripped.length != 0) {
		isURL = ( 
			(fieldname.value.indexOf("http://") == 0) || 
			(fieldname.value.indexOf("https://") == 0) ||
			(fieldname.value.indexOf("ftp://") == 0) ||
			(fieldname.value.indexOf("ftps://") == 0)
		)? true : false;

	}		
	return isURL;
}

// validate radio
function checkradio(fieldname) {
	var numradio = fieldname.length;
	var onechecked = false;
	for (counter = 0; counter < numradio; counter++) {
		if ( fieldname[counter].checked ) {
			onechecked = true;
		}
	}
	return onechecked;
}


// Check that a Canadian postal code is valid
function isValidPostalcode(postalcode) {
	if (postalcode.search) {
		postalcode = removeSpaces(postalcode);
		
		if (postalcode.length == 6 && postalcode.search(/^\w\d\w\d\w\d$/) != -1) return true;
		else if (postalcode.length == 7 && postalcode.search(/^\w\d\w\-d\w\d$/) != -1) return true;
		else return false;
	}
	return true;
}

// Remove all spaces from a string
function removeSpaces(string) {
	var newString = '';
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != ' ') newString += string.charAt(i);
	}
	return newString;
}

function isValidZip(field) {
	var valid = "0123456789-";
	var hyphencount = 0;
	
	field = removeSpaces(field);
	
	if (field.length!=5 && field.length!=10) {
		//alert("Please enter your 5 digit or 5 digit+4 zip code.");
		return false;
	}
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") {
			//alert("Invalid characters in your zip code.  Please try again.");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
			//alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
	   }
	}
	return true;
}

function checkunicode(field) {
	var thecode = field.charCodeAt(0);
	if (thecode == 163) {	// British pound symbol
		return true;
	} else if (thecode == 165) {	// Japanese yen symbol
		return true;
	} else if (thecode >= 196 && thecode <= 255) {	// various letters with accents
		return true;
	} else {	// other special characters
		return false;
	}
}

function checktest(field){
	//alert(field);
	
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.#()-_@,:& "
	var ok = "yes";
	var temp;

	// check string if it contains invalid character.
	for (var i=0; i<field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (valid.indexOf(temp) == "-1" && !checkunicode(temp)) {
			ok = "no";
		}
	}


	if (ok == "no") {
		return false;
	} else {
		return true;
	}
}

function checkdomain(field){
	//alert(field);
	
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
	var ok = "yes";
	var temp;
	for (var i=0; i<field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}

	if (ok == "no") {
		return false;
	   }
	else {
		return true;
	}
}
	
function ignoreSpaces(string) {
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	temp += splitstring[i];
	return temp;
}
/*
//
end form field validation functions
//
*/
