function trim(sString) {
	while (sString.substring(0,1) == ' ' || sString.substring(0,1) == '\n'
	|| sString.substring(0,1) == '\r' || sString.substring(0,1) == '\t') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ' ||
	sString.substring(sString.length-1, sString.length) == '\n' ||
	sString.substring(sString.length-1, sString.length) == '\r' ||
	sString.substring(sString.length-1, sString.length) == '\t') {
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}

function IsInt(sText) { 
	var ValidChars = "0123456789"; 
	var Char;
	if(sText=="") {
		return false;
	}
	for (i = 0; i < sText.length; i++) {
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			return false;
		}
	}
	return true;
}

function trimFormInputs() {
	document.custform.FirstName.value = trim(document.custform.FirstName.value);
	document.custform.LastName.value = trim(document.custform.LastName.value);
	document.custform.Email.value = trim(document.custform.Email.value);
	document.custform.PhoneArea.value = trim(document.custform.PhoneArea.value);
	document.custform.PhoneFirst.value = trim(document.custform.PhoneFirst.value);
	document.custform.PhoneLast.value = trim(document.custform.PhoneLast.value);
	document.custform.PhoneExt.value = trim(document.custform.PhoneExt.value);
	document.custform.ShipName.value = trim(document.custform.ShipName.value);
	document.custform.ShipAddress1.value = trim(document.custform.ShipAddress1.value);
	document.custform.ShipAddress2.value = trim(document.custform.ShipAddress2.value);
	document.custform.ShipCity.value = trim(document.custform.ShipCity.value);
	document.custform.ShipState.value = trim(document.custform.ShipState.value);
	document.custform.ShipZipCode.value = trim(document.custform.ShipZipCode.value);
}

function checkCustForm() {
	//trim all the input boxes
	trimFormInputs();
	
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(document.custform.Email.value)) {
		alert('Invalid email address');
		return false;
	}
	
	if (document.custform.FirstName.value == "") {
		alert('Please Enter a First Name');
		return false;
	}
	
	if (document.custform.LastName.value == "") {
		alert('Please Enter a Last Name');
		return false;
	}
	
	filter = /\d{3}/;
	if (!filter.test(document.custform.PhoneArea.value)) {
		alert('Invalid Area Code');
		return false;
	}
	
	filter = /\d{3}/;
	if (!filter.test(document.custform.PhoneFirst.value)) {
		alert('Invalid Phone Number');
		return false;
	}
	
	filter = /\d{4}/;
	if (!filter.test(document.custform.PhoneLast.value)) {
		alert('Invalid Phone Number');
		return false;
	}
	
	if (document.custform.PhoneExt.value != "" && !IsInt(document.custform.PhoneExt.value)) {
		alert('Invalid Phone Extension');
		return false;
	}
	
	if (document.custform.ShipName.value == "") {
		alert('Please Enter a Shipping Name');
		return false;
	}
	
	if (document.custform.ShipAddress1.value == "") {
		alert('Please Enter a Shipping Address');
		return false;
	}
	
	if (document.custform.ShipCity.value == "") {
		alert('Please Enter a City');
		return false;
	}
	
	filter = /[a-zA-Z]{2}/;
	if (!filter.test(document.custform.ShipState.value)) {
		alert('Please Enter a State Abbreviation');
		return false;
	}
	
	filter = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	if (!filter.test(document.custform.ShipZipCode.value)) {
		alert('Invalid Zip Code');
		return false;
	}
	
	//set the hidden value so that when we post back the server
	//will know if js is enabled and this validation was not bypassed
	document.custform.jsenabled.value = "true";
	
	return true;
}

function toggleElement(elName) {
	document.getElementById(elName).style.display = (document.getElementById(elName).style.display == 'none')?'':'none';
}

// Use to tab to next field with phone numbers
function AutoTab(obj,len,nextField) {
	var fieldLength=obj.value.length;
	if (fieldLength == len) {
		nextField.focus();
	}
}
