var isIE = 0;
if(document.all){
	isIE = 1;
}
function openNewWindow(URLtoOpen, windowName, windowFeatures) {
	var newWindow = window.open(URLtoOpen, windowName, windowFeatures);
}
/* FORM Validation */
var bad_words = new Array();
//trim
function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	}else{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;
	
	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	} //End While
	return strTemp;

} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	
	var iTemp = 0;
	
	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

Array.prototype.contains = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i].toLowerCase() == element.toLowerCase()) {
            return true;
        }
    }
    return false;
};
var isEmail = /[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/;
var isNumber = /[0-9]/;
var isCAPostalCode = /^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/;
var isUSPostalCode = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
var isDate = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/; //mm/dd/yyyy
var isTime = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/; //HH:MM, HH:MM:SS, HH:MM:SS.mm
var isIP = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
var isMoney = /^((\$\d*)|(\$\d*\.\d{2})|(\d*)|(\d*\.\d{2}))$/;
var isSSN = /^\d{3}\-?\d{2}\-?\d{4}$/;
var isCASIN = /^\d{9}$/;//canadian ssn
function validate(myform) {
	var errorList = '';	
	for(var w=0; w < myform.length; w++){
		if(myform[w].id.substring(0,4) == 'req_') {
			if(myform[w].name == 'email_address') {
				if(!isEmail.test(Trim(myform[w].value))) {
					errorList += myform[w].name + ' must be a valid email.'+"\n";
				}else if(Trim(myform[w].value) != Trim(myform.confirm_email_address.value)){
					errorList += 'Confirm Email does not match Email Address.'+"\n";
				}
			}else if(myform[w].name == 'password'){
				if(Trim(myform[w].value).length < 3){
					errorList += myform[w].name + ' must be at least 3 characters long.'+"\n";
				}else if(Trim(myform[w].value) != Trim(myform.confirm_password.value)){
					errorList += 'Confirm Password does not match Password.'+"\n";
				}
			}else if(myform[w].name == 'zip'){
				if(!isUSPostalCode.test(Trim(myform[w].value)) && !isCAPostalCode.test(Trim(myform[w].value))){
					errorList += myform[w].name+' must be a valid postal code.'+"\n";
				}
			}else if(myform[w].name == 'billing_zip'){
				if(!isUSPostalCode.test(Trim(myform[w].value)) && !isCAPostalCode.test(Trim(myform[w].value))){
					errorList += myform[w].name+' must be a valid postal code.'+"\n";
				}
			}else{
				if(Trim(myform[w].value) == '' || (myform[w].type == 'checkbox' && !myform[w].checked)) {
					errorList += myform[w].name + ' is a required field.'+"\n";
				}else{
				}
			}
		}else{
		}
	}
	if(errorList.length>0){
		alert(errorList);
		return(false);
	}else{
		return(true);
	}
}
/* Correct PNGs in IE */
function correctPNG(){
	for(var i=0; i<document.images.length; i++){
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if(imgName.substring(imgName.length-3, imgName.length) == 'PNG'){
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
			img.outerHTML = strNewHTML
			i = i-1;
		}
	}
}

function selectSubCat(countryID, stateID){
	removeAllOptions(document.getElementById(stateID));
	if(document.getElementById(countryID).value == 'CA'){
		addOption(document.getElementById(stateID),"Select", "");
		addOption(document.getElementById(stateID), "Alberta", "AB");
		addOption(document.getElementById(stateID),"British Columbia", "BC");
		addOption(document.getElementById(stateID),"Manitoba", "MB");
		addOption(document.getElementById(stateID),"New Brunswick", "NB");
		addOption(document.getElementById(stateID),"Newfoundland", "NF");
		addOption(document.getElementById(stateID),"Nova Scotia", "NS");
		addOption(document.getElementById(stateID),"Northwest Territories", "NT");
		addOption(document.getElementById(stateID),"Nunavut", "NV");
		addOption(document.getElementById(stateID),"Ontario", "ON");
		addOption(document.getElementById(stateID),"Prince Edward Island", "PE");
		addOption(document.getElementById(stateID),"Quebec", "QC");
		addOption(document.getElementById(stateID),"Saskatchewan", "SK");
		addOption(document.getElementById(stateID),"Yukon", "YK");
	}
	if(document.getElementById(countryID).value == 'US'){
		addOption(document.getElementById(stateID),"Select", "");
		addOption(document.getElementById(stateID),"AL", "AL");
		addOption(document.getElementById(stateID),"AK", "AK");
		addOption(document.getElementById(stateID),"AS", "AS");
		addOption(document.getElementById(stateID),"AZ", "AZ");
		addOption(document.getElementById(stateID),"AR", "AR");
		addOption(document.getElementById(stateID),"CA", "CA");
		addOption(document.getElementById(stateID),"CO", "CO");
		addOption(document.getElementById(stateID),"CT", "CT");
		addOption(document.getElementById(stateID),"DE", "DE");
		addOption(document.getElementById(stateID),"DC", "DC");
		addOption(document.getElementById(stateID),"FM", "FM");
		addOption(document.getElementById(stateID),"FL", "FL");
		addOption(document.getElementById(stateID),"GA", "GA");
		addOption(document.getElementById(stateID),"GU", "GU");
		addOption(document.getElementById(stateID),"HI", "HI");
		addOption(document.getElementById(stateID),"ID", "ID");
		addOption(document.getElementById(stateID),"IL", "IL");
		addOption(document.getElementById(stateID),"IN", "IN");
		addOption(document.getElementById(stateID),"IA", "IA");
		addOption(document.getElementById(stateID),"KS", "KS");
		addOption(document.getElementById(stateID),"KY", "KY");
		addOption(document.getElementById(stateID),"LA", "LA");
		addOption(document.getElementById(stateID),"ME", "ME");
		addOption(document.getElementById(stateID),"MH", "MH");
		addOption(document.getElementById(stateID),"MD", "MD");
		addOption(document.getElementById(stateID),"MA", "MA");
		addOption(document.getElementById(stateID),"MI", "MI");
		addOption(document.getElementById(stateID),"MN", "MN");
		addOption(document.getElementById(stateID),"MS", "MS");
		addOption(document.getElementById(stateID),"MO", "MO");
		addOption(document.getElementById(stateID),"MT", "MT");
		addOption(document.getElementById(stateID),"NE", "NE");
		addOption(document.getElementById(stateID),"NV", "NV");
		addOption(document.getElementById(stateID),"NH", "NH");
		addOption(document.getElementById(stateID),"NJ", "NJ");
		addOption(document.getElementById(stateID),"NM", "NM");
		addOption(document.getElementById(stateID),"NY", "NY");
		addOption(document.getElementById(stateID),"NC", "NC");
		addOption(document.getElementById(stateID),"ND", "ND");
		addOption(document.getElementById(stateID),"MP", "MP");
		addOption(document.getElementById(stateID),"OH", "OH");
		addOption(document.getElementById(stateID),"OK", "OK");
		addOption(document.getElementById(stateID),"OR", "OR");
		addOption(document.getElementById(stateID),"PW", "PW");
		addOption(document.getElementById(stateID),"PA", "PA");
		addOption(document.getElementById(stateID),"PR", "PR");
		addOption(document.getElementById(stateID),"RI", "RI");
		addOption(document.getElementById(stateID),"SC", "SC");
		addOption(document.getElementById(stateID),"SD", "SD");
		addOption(document.getElementById(stateID),"TN", "TN");
		addOption(document.getElementById(stateID),"TX", "TX");
		addOption(document.getElementById(stateID),"UT", "UT");
		addOption(document.getElementById(stateID),"VT", "VT");
		addOption(document.getElementById(stateID),"VI", "VI");
		addOption(document.getElementById(stateID),"VA", "VA");
		addOption(document.getElementById(stateID),"WA", "WA");
		addOption(document.getElementById(stateID),"WV", "WV");
		addOption(document.getElementById(stateID),"WI", "WI");
		addOption(document.getElementById(stateID),"WY", "WY");
	}
}

function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}


function addOption(selectbox, text, value )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;

	selectbox.options.add(optn);
}

function openPopup(){
	var win = window.open('/popup_signUp.php','NewsletterSignUp','width=400,height=250,scrollbars=0,resizable=0,menubar=0');
	if(win)
		win.focus();
}

//use if trying to use transparent pngs in IE
//window.attachEvent("onload", correctPNG);