//File modified by Abhishek Pol on 15-Oct-09 to add validation for shipping code. 
function IsNumericVal(numberEntry)
{
	for(i=0;i<numberEntry.length;i++)
		{
			if(numberEntry.charAt(i) < "0")
				return false;
		
			if(numberEntry.charAt(i) > "9")
				return false;
		}
		return true;
}


function trim(str)
{
  var i;
  // strip spaces at the start of the string
  for(i = 0; i < str.length; i++)
    if(str.substr(i, 1) != " ") break;

  if(i < str.length)
    str = str.substring(i, str.length);
  else
    return "" ;

  // strip spaces at the end of the string
  for(i = str.length - 1; i >= 0; i--)
    if(str.substr(i, 1) != " ") break;

  if(i >= 0)
    str = str.substring(0, i + 1);
  else
    return "";

  return str;
}

function fillShippingAddress(the_form) {
	if (withoutSpaces(the_form.shipName.value) == "")
		the_form.shipName.value = the_form.fullName.value
		
	if (withoutSpaces(the_form.shipAddress.value) == "")
		the_form.shipAddress.value = the_form.address1.value
		
	if (withoutSpaces(the_form.shipCity.value) == "")
		the_form.shipCity.value = the_form.city.value
		
	if (withoutSpaces(the_form.shipState.value) == "")
		the_form.shipState.value = the_form.state.value
		
	if (withoutSpaces(the_form.shipZipCode.value) == "")
		the_form.shipZipCode.value = trim(the_form.zipcode.value)
		
	if (withoutSpaces(the_form.shipCountry.value) == "")
		the_form.shipCountry.value = the_form.country.value
		

	return true;
}



function withoutSpaces(numberEntry) {
	cleanNumberEntry = "";
	for(i=0;i<numberEntry.length;i++) {
	  if (numberEntry.charAt(i) != " ") {
		cleanNumberEntry = cleanNumberEntry + numberEntry.charAt(i);
	   }
	}
	return cleanNumberEntry;
}

function validateCard(the_form) {

	if (withoutSpaces(the_form.CardNumber.value) == "") {
		alert("Credit card number is required.");
		the_form.CardNumber.focus();
		return false;
	}
	
	creditCardNumberWithoutSpaces = withoutSpaces(the_form.CardNumber.value);
	if (!IsNumeric(creditCardNumberWithoutSpaces)) {
		alert("Invalid credit card number. Enter numbers only. (Spaces are allowed.)");
		the_form.CardNumber.focus();
		return false;
	}
	
	if ((creditCardNumberWithoutSpaces.length != 16) && (creditCardNumberWithoutSpaces.length != 15) && (creditCardNumberWithoutSpaces.length != 14)) {
    	if (creditCardNumberWithoutSpaces.length < 14) {
	      alert("Not enough numbers for credit card.");
	    } else {
	      alert("Too many numbers for credit card.");
	    }
	    the_form.CardNumber.focus();
	    return false;
	}
	
	if (withoutSpaces(the_form.CardName.value) == "") {
		alert("Name on credit card is required.");
		the_form.CardName.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.CardName.value) == "") {
		alert("Name on credit card is required.");
		the_form.CardName.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.fullName.value) == "") {
		alert("Name is required.");
		the_form.fullName.focus();
		return false;
	}
	
	the_form.email.value = trim(the_form.email.value);
	if (!validEmail(the_form.email.value)) {
		alert("Valid email address is required.");
		the_form.email.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.address1.value) == "") {
		alert("Street address is required.");
		the_form.address1.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.city.value) == "") {
		alert("City is required.");
		the_form.city.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.state.value) == "") {
		alert("State is required.");
		the_form.state.focus();
		return false;
	}
	
	
	if (withoutSpaces(the_form.zipcode.value) == "") {
		alert("ZIP/Postal code is required.");
		the_form.zipcode.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.country.value) == "") {
		alert("Country is required.");
		the_form.country.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.telephone.value) == "") {
		alert("Telephone number is required.");
		the_form.telephone.focus();
		return false;
	}
	
	fillShippingAddress(the_form);
	
	the_form.submit();
	return true;
}

function validateEmail(email)
{
	if(validEmail(email))
		return true;
	else
	{
		alert("Valid email address is required.");
		return false;
	}
}

function validateCard4(the_form) {

	if (withoutSpaces(the_form.shippingMethod.value) == "") {
		alert("Shipping Method is required.");
		the_form.shippingMethod.focus();
		return false;
	}
	//the_form.CardNumber.value = the_form.cn1.value + the_form.cn2.value + the_form.cn3.value + the_form.cn4.value;
	cn = the_form.CardNumber.value;
	//cn.replace(/ /g,'');
	//alert(cn);
	//the_form.CardNumber.value = cn;
	
	if (withoutSpaces(the_form.CardNumber.value) == "") {
		alert("Credit card number is required.");
		the_form.CardNumber.focus();
		return false;
	}
	
	creditCardNumberWithoutSpaces = withoutSpaces(the_form.CardNumber.value);
	if (!IsNumeric(creditCardNumberWithoutSpaces)) {
		alert("Invalid credit card number. Enter numbers only. (Spaces are allowed.)");
		the_form.CardNumber.focus();
		return false;
	}
	
	if ((creditCardNumberWithoutSpaces.length != 16) && (creditCardNumberWithoutSpaces.length != 15) && (creditCardNumberWithoutSpaces.length != 14)) {
    	if (creditCardNumberWithoutSpaces.length < 14) {
	      alert("Not enough numbers for credit card.");
	    } else {
	      alert("Too many numbers for credit card.");
	    }
	    the_form.CardNumber.focus();
	    return false;
	}
	the_form.cn1.value = cn.slice(0,4);
	the_form.cn2.value = cn.slice(5,4);
	the_form.cn3.value = cn.slice(9,4);
	the_form.cn4.value = cn.slice(13,4);
	
	if (withoutSpaces(the_form.csc.value) == "") {
		alert("Card Security Code is required.");
		the_form.csc.focus();
		return false;
	}

	if (withoutSpaces(the_form.CardName.value) == "") {
		alert("Name on credit card is required.");
		the_form.CardName.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.CardName.value) == "") {
		alert("Name on credit card is required.");
		the_form.CardName.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.fullName.value) == "") {
		alert("Name is required.");
		the_form.fullName.focus();
		return false;
	}
	
	the_form.email.value = trim(the_form.email.value);
	if (!validEmail(the_form.email.value)) {
		alert("Valid email address is required.");
		the_form.email.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.address1.value) == "") {
		alert("Street address is required.");
		the_form.address1.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.city.value) == "") {
		alert("City is required.");
		the_form.city.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.state.value) == "") {
		alert("State is required.");
		the_form.state.focus();
		return false;
	}
	
	
	zipCode = withoutSpaces(the_form.zipcode.value);
	if (zipCode == "") {
		alert("ZIP/Postal code is required.");
		the_form.zipcode.focus();
		return false;
	}
	
	shipzipCode = withoutSpaces(the_form.shipZipCode.value);
	if (shipzipCode == "")
	{
		alert("Shipping ZIP/Postal code is required.");
		the_form.shipZipCode.focus();
		return false;
	}
	//alert(zipCode.length);
	//alert(IsNumeric(zipCode));
	//if ((zipCode.length != 5) || (!IsNumeric(zipCode)))
	//{
	//	alert("Shipping ZIP/Postal code must be a 5-digit number.");
	//	the_form.shipZipCode.focus();
	//	return false;
	//}
	
	if (withoutSpaces(the_form.country.value) == "") {
		alert("Country is required.");
		the_form.country.focus();
		return false;
	}
	
	if (withoutSpaces(the_form.telephone.value) == "") {
		alert("Telephone number is required.");
		the_form.telephone.focus();
		return false;
	}
	
	
	
	fillShippingAddress(the_form);

	the_form.ShippingChange.value = "false";

	the_form.submit();
	return true;
}

function validEmail(email) 
{
	//alert("email <" + email + ">");
	email = trim(email);
	//alert("email <" + email + ">");

	var invalidChars = new Array(","," ","/",":",";",",");

	if (email == "") 
	{
		//alert("empty");
		return false;
	}
	
	var pos;
	for(i=0; i < invalidChars.length; i++)
	{
		//alert(invalidChars[i]);
		pos = email.indexOf(invalidChars[i]);
		//alert(invalidChars[i] + " , pos = " + pos);
		if (pos >= 0)
		{
			//alert("invalid char " + invalidChars[i]);
			return false;
		}
	}
	
	pos = email.indexOf("@");
	if(pos <= 0)
	{
		//alert("@ not found, pos = " + pos);
		return false;
	}
	else
	{
		//alert("@ at pos = " + pos);
	}
		
	pos += 2;
	//alert("search for . at pos = " + pos);
	pos = email.indexOf(".", pos);
	if(pos <= 0)
	{
		//alert(". not found, pos = " + pos);
		return false;
	}
	else
	{
		//alert(". at pos = " + pos);
	}

	if (pos+2 > email.length )
	{
		//alert("len = " + email.length);
		return false;
	}
		
	return true;
}

function onShippingChange(the_form)
{
	fillShippingAddress(the_form);
	the_form.ShippingChange.value = "true";
	
	idx = the_form.shippingMethod.options.selectedIndex;
	str = the_form.shippingMethod.options[idx].value;
	if(str.substr(0, 13) == "International")
		alert("There is a shipping expense at cost added\n" + 
			  "to our standard shipping price that will\n" + 
			  "appear in a separate charge once we have\n" + 
			  "mailed your order. If you would like a price\n" + 
			  "quote before you place this order please\n" + 
			  "contact us at sales@nehgs.org");
	
	

	the_form.submit();
}


