function isEmpty(inputVal) {
	if (inputVal == null || inputVal == "") {
		return true
		}
	return false
}
function ship_countryChange(){
	switcher=getRadioValue("shipToCountrySelection");
	if(switcher==2){
		document.getElementById('ship_US').style.display = 'block';
		document.getElementById('ship_US1').style.display = 'block';
		document.getElementById('ship_Canada').style.display = 'none';
		document.getElementById('ship_Canada2').style.display = 'none';
		document.getElementById('ship_Other').style.display = 'none';
		document.getElementById('ship_Other1').style.display = 'none';
		document.getElementById('ship_Other2').style.display = 'none';
	}else if(switcher==1){
		document.getElementById('ship_US').style.display = 'none';
		document.getElementById('ship_US1').style.display = 'none';
		document.getElementById('ship_Canada').style.display = 'block';
		document.getElementById('ship_Canada2').style.display = 'block';
		document.getElementById('ship_Other').style.display = 'none';
		document.getElementById('ship_Other1').style.display = 'none';
		document.getElementById('ship_Other2').style.display = 'none';
	}else{
		document.getElementById('ship_US').style.display = 'none';
		document.getElementById('ship_US1').style.display = 'none';
		document.getElementById('ship_Canada').style.display = 'none';
		document.getElementById('ship_Canada2').style.display = 'none';
		document.getElementById('ship_Other').style.display = 'block';
		document.getElementById('ship_Other1').style.display = 'block';
		document.getElementById('ship_Other2').style.display = 'block';
	}
}

function isValidEmail(theField) {
// checks for valid email address formatting
	var bad = " :;'"
	if(theField.value.indexOf("@", 0) == -1 || theField.value.indexOf(".", 0) == -1 ){
		 return false;
	}
	for(i=1; i<theField.value.length; i++){
		if(bad.indexOf(theField.value.charAt(i)) != -1 ){
			return false;
		}
	}
	return true;
}

function validateEmail(theField) {
//generates error message
	if (!isValidEmail(theField)){
		alert('The field "'+theField.fieldname+'" requires an email address \nin the form userid@company.com');
		return false;
	}
	return true;
}

function isBlank(s){
// utility function
// returns true if contains only whitespace characters
	if( s=="undefined" ) return true;

	for( var i=0; i < s.length; i++ ){
		var c=s.charAt(i);
		if (( c != ' ')&&( c != '\n') &&(c != '\t' )) return false;
	}
	return true;
}

function validEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false
	 }

	 return true	
}
function validateSignup(form, formtype) {
	if (form.email) {
		if (!validEmail(form.email.value)) {
			alert("Please put in a valid email address.");
			form.email.focus();
			return false;
		} 
		
		if (!validEmail(form.confirm_email.value)) {
			alert("Please confirm your email address.");
			form.confirm_email.focus();
			return false;
		} 
		
		if (form.email.value != form.confirm_email.value) {
			alert("Your email address does not match the confirm email address.");
			form.confirm_email.focus();
			return false;
		}
	}
	
	if (!form.password.value) {
		alert("Please enter a password.");
		form.password.focus();
		return false;
	}
	
	if (!form.confirm_pass.value) {
		alert("Please confirm your password.");
		form.confirm_pass.focus();
		return false;
	}
	
	if (form.password.value != form.confirm_pass.value) {
		alert("Your password did not match the confirmation password.");
		form.confirm_pass.value = "";
		form.confirm_pass.focus();
		return false;
	}	
	
	if (formtype == "wholesale") {
		if (!form.billToCompany.value) {
			alert("Please enter a company name.");
			form.billToCompany.focus();
			return false;
		}
		
		if (!form.billToCompanyURL.value) {
			alert("Please enter a company URL.");
			form.billToCompanyURL.focus();
			return false;
		}		
	}
	
	if (!form.agreed[0].checked) {
		alert("Please read our Terms of Usage policy\nand select that you have read and understood.");
		return false;
	}
	if (isEmpty(form.billToFName.value)) {	
		alert("Please enter your billing address first name.");
		form.billToFName.focus();
		return false;
	}
	if (isEmpty(form.billToLName.value)) {	
		alert("Please enter your billing address last name.");
		form.billToLName.focus();
		return false;
	}
	if (isEmpty(form.billToAddress1.value)) { 		
		alert("Please enter your billing address.");
		form.billToAddress1.focus();
		return false;
	}
	if (isEmpty(form.billToCity.value)) { 			
		alert("Please enter your billing address city.");
		form.billToCity.focus();
		return false;
	}
	
	if (getRadioValue("billToCountrySelection") == 2) {
		if(isEmpty(form.billToZip.value)) { 
			alert("Please enter your postal code.");
			form.billToZip.focus();
			return false;
		}
	} else if (getRadioValue("billToCountrySelection") == 1) {
		if(isEmpty(form.billToZipCanada.value)) { 
			alert("Please enter your postal code.");
			form.billToZipCanada.focus();
			return false;
		}
	}
	
	if (isEmpty(form.billToPhone.value)) {	
		alert("Please enter your billing address phone number.");
		form.billToPhone.focus();
		return false;
	}

	return true;
}
function validateBilling(form) {

	if (isEmpty(form.billToFName.value)) {	
		alert("Please enter your billing address first name.");
		form.billToFName.focus();
		return false;
	}
	if (isEmpty(form.billToLName.value)) {	
		alert("Please enter your billing address last name.");
		form.billToLName.focus();
		return false;
	}
	if (isEmpty(form.billToAddress1.value)) { 		
		alert("Please enter your billing address.");
		form.billToAddress1.focus();
		return false;
	}
	if (isEmpty(form.billToCity.value)) { 			
		alert("Please enter your billing address city.");
		form.billToCity.focus();
		return false;
	}
	if(isEmpty(form.billToZip.value)) { 
		alert("Please enter your postal code.");
		form.billToZip.focus();
		return false;
	}
	if (isEmpty(form.billToPhone.value)) {	
		alert("Please enter your billing address phone number.");
		form.billToPhone.focus();
		return false;
	}
	if (form.email) {
		if (!validEmail(form.email.value)) {
			alert("Please put in a valid email address.");
			form.email.focus();
			return false;
		} else {
			if (form.email.value != form.confirm_email.value) {
				alert("Your email address does not match the confirm email address.");
				form.email.focus();
				return false;
			}
		}
	}
	return true;
}

function validateShipping(form) {

	if (isEmpty(form.shipToFName.value)) {		
		alert("Please enter your shipping address first name");
		form.shipToFName.focus();
		return false;
	}
	if (isEmpty(form.shipToLName.value)) {		
		alert("Please enter your shipping address last name.");
		form.shipToLName.focus();
		return false;
	}
	if (isEmpty(form.shipToAddress1.value)) {		
		alert("Please enter your shipping address.");
		form.shipToAddress1.focus();
		return false;
	}
	if (isEmpty(form.shipToCity.value)) {		
		alert("Please enter your shipping address city.");
		form.shipToCity.focus();
		return false;
	}

	return true;
}

	function getFormGroup(name) {return document.getElementsByName(name);}
	function getRadio(name) {
		elements = getFormGroup(name);
		if (elements)/* loop over all the radio buttons */
		for (i = 0; i < elements.length; i++)
			if (elements[i].checked)
			return elements[i];/* either no group by that name was foundor none were selected */
		return null;
		}
		function getRadioValue(name) {
			element = getRadio(name);
			if (element)
			return element.value;/* there must not have been a radio buttonselected */
			return '';
			}


	function countryChange(){
		switcher=getRadioValue("billToCountrySelection");
		if(switcher==2){
			document.getElementById('US').style.display = 'block';
			document.getElementById('US1').style.display = 'block';
			document.getElementById('Canada').style.display = 'none';
			document.getElementById('Canada2').style.display = 'none';
			document.getElementById('Other').style.display = 'none';
			document.getElementById('Other1').style.display = 'none';
			document.getElementById('Other2').style.display = 'none';
		}else if(switcher==1){
			document.getElementById('US').style.display = 'none';
			document.getElementById('US1').style.display = 'none';
			document.getElementById('Canada').style.display = 'block';
			document.getElementById('Canada2').style.display = 'block';
			document.getElementById('Other').style.display = 'none';
			document.getElementById('Other1').style.display = 'none';
			document.getElementById('Other2').style.display = 'none';
		}else{
			document.getElementById('US').style.display = 'none';
			document.getElementById('US1').style.display = 'none';
			document.getElementById('Canada').style.display = 'none';
			document.getElementById('Canada2').style.display = 'none';
			document.getElementById('Other').style.display = 'block';
			document.getElementById('Other1').style.display = 'block';
			document.getElementById('Other2').style.display = 'block';
		}
	}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
				found = true;
			else
				index++;
		return found;
	}
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
			else i++;
		return index;
	}
	return true;
}

//</script>