function showform(id)
{
	el = document.getElementById(id);
	if (el.style.display == "none")
	{
		el.style.display = "";
	} else {
		el.style.display = "none";
	}
}

function hideform(id)
{
	el = document.getElementById(id);
	el.style.display = "none";
}


// Function to check & validate billing & shipping addresses
function disableForm(field) {

   	if (field.b_firstname && field.b_firstname.value == "") {
        alert("Please complete billing first name field.");
        field.b_firstname.focus();
        return false;
       }

   	if (field.b_lastname && field.b_lastname.value == "") {
        alert("Please complete billing last name field.");
        field.b_lastname.focus();
        return false;
       }

   	if (field.b_address1 && field.b_address1.value == "") {
        alert("Please complete billing address1.");
        field.b_state.focus();
        return false;
       }

   	if (field.b_city && field.b_city.value == "") {
        alert("Please complete billing city.");
       field.b_city.focus();
        return false;
       }

   	if (field.b_state && field.b_state.value == "") {
        alert("Please complete billing state.");
        field.b_state.focus();
        return false;
       }

   	if (field.b_zip && field.b_zip.value == "") {
        alert("Please complete billing zip.");
        field.b_zip.focus();
        return false;
       }

   	if (field.b_country && field.b_country.value == "") {
        alert("Please complete billing country.");
        field.b_country.focus();
        return false;
       }

   	if ((field.b_daytel_area && field.b_daytel_area.value == "") || (field.b_daytel_prefix && field.b_daytel_prefix.value == "") || (field.b_daytel_suffix && field.b_daytel_suffix.value == "") ) {
        alert("Please complete billing day time phone number.");
        field.b_daytel_area.focus();
        return false;
       }

   	if ((field.b_nighttel_area && field.b_nighttel_area.value == "") || (field.b_nighttel_prefix && field.b_nighttel_prefix.value == "") || (field.b_nighttel_suffix && field.b_nighttel_suffix.value == "") ) {
        alert("Please complete billing night time phone number.");
        field.b_nighttel_area.focus();
        return false;
       }

	if(field.same_bill_to && field.same_bill_to.checked == false)
	{
   		if (field.firstname && field.firstname.value == "") {
        	alert("Please complete shipping first name field.");
        	field.firstname.focus();
        	return false;
       	}

   		if (field.lastname && field.lastname.value == "") {
        	alert("Please complete shipping last name field.");
       		field.lastname.focus();
        	return false;
       	}

   		if (field.address1 && field.address1.value == "") {
        	alert("Please complete shipping address1.");
        	field.state.focus();
        	return false;
       	}

	   	if (field.city && field.city.value == "") {
	        alert("Please complete shipping city.");
	        field.city.focus();
	        return false;
	       }

	   	if (field.state && field.state.value == "") {
	        alert("Please complete shipping state.");
	        field.state.focus();
	        return false;
	       }

	   	if (field.zip && field.zip.value == "") {
	        alert("Please complete shipping zip.");
	        field.zip.focus();
	        return false;
	       }

	   	if (field.country && field.country.value == "") {
	        alert("Please complete shipping country.");
	        field.country.focus();
	        return false;
	       }

	   	if ((field.daytel_area && field.daytel_area.value == "") || (field.daytel_prefix && field.daytel_prefix.value == "") || (field.daytel_suffix && field.daytel_suffix.value == "") ) {
	        alert("Please complete shipping day time phone number.");
	        field.daytel_area.focus();
	        return false;
	       }

	   	if ((field.nighttel_area && field.nighttel_area.value == "") || (field.nighttel_prefix && field.nighttel_prefix.value == "") || (field.nighttel_suffix && field.nighttel_suffix.value == "") ) {
	        alert("Please complete shipping night time phone number.");
	        field.nighttel_area.focus();
	        return false;
	       }
    }

	return true;
}

function trimField(field)
{
   	field.b_firstname.value = trim(field.b_firstname.value);
   	field.b_lastname.value = trim(field.b_lastname.value);
   	field.b_company.value = trim(field.b_company.value);
   	field.b_address1.value = trim(field.b_address1.value);
   	field.b_address2.value = trim(field.b_address2.value);
   	field.b_city.value = trim(field.b_city.value);
   	field.b_zip.value = trim(field.b_zip.value);
   	field.firstname.value = trim(field.firstname.value);
   	field.lastname.value = trim(field.lastname.value);
   	field.company.value = trim(field.company.value);
   	field.address1.value = trim(field.address1.value);
   	field.address2.value = trim(field.address2.value);
   	field.city.value = trim(field.city.value);
   	field.zip.value = trim(field.zip.value);

   	return true;
}

function trim(inputString) {
   	// Removes leading and trailing spaces from the passed string. Also removes
   	// consecutive spaces and replaces it with one space. If something besides
   	// a string is passed in (null, custom object, etc.) then return the input.
   	if (typeof inputString != "string") { return inputString; }
   	var retValue = inputString;

   	while(retValue.indexOf(",")!=-1){
	  	retValue = retValue.replace(","," "); // remove one or multiple commas
   	}

	while(retValue.indexOf("\""," ")!=-1){
		retValue = retValue.replace("\""," "); // remove one or multiple doubel quote
	}

   	var ch = retValue.substring(0, 1);

   	while (ch == " ") { // Check for spaces at the beginning of the string
      	retValue = retValue.substring(1, retValue.length);
      	ch = retValue.substring(0, 1);
   	}

   	ch = retValue.substring(retValue.length-1, retValue.length);
   	while (ch == " ") { // Check for spaces at the end of the string
      	retValue = retValue.substring(0, retValue.length-1);
      	ch = retValue.substring(retValue.length-1, retValue.length);
   	}

   	while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      	retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   	}

   	return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function replaceSpcChar(field){
	// Remove Comma
	if(field.value.indexOf(",")!=-1)
	  	alert("Please take note that comma is not allowed and it will be removed."); // alert user regarding comma usage

	while(field.value.indexOf(",")!=-1){
	  	field.value = field.value.replace(","," "); // remove one or multiple commas
	}

	// Remove Double Quote
	if(field.value.indexOf("\"")!=-1)
	  	alert("Please take note that double quote is not allowed and it will be removed"); // alert user regarding comma usage

	while(field.value.indexOf("\""," ")!=-1){
	  	field.value = field.value.replace("\""," "); // remove one or multiple doubel quote
	}
} // Ends the "replaceComma" function

function detectPOBOX(field){

	value1 = field;

	myRe=/[p][\.\,\/\>\s]*[o|0][\.\,\>\s]*[b][\.\,\>\s]*[o|0]*[\.\,\>\s]*[x][\.\,\>\s]*/gi;
	myRe.exec(value1);

	iPobox = myRe.lastIndex;

	if (value1 != null && iPobox > 0) {
		alert("We do not ship to P.O. Box Address");
		myRe.exec(value1);
		return false;
	}

	value2 = field;

	myRe=/post[\s]*office[\s]*box/gi;
	myRe.exec(value2);
	iPobox = myRe.lastIndex;

	if (value2 != null && iPobox > 0) {
		alert("We do not ship to P.O. Box Address");
		myRe.exec(value2);
		return false;
	}

	return true;
}

function verifyForm(field, flag)
{
	check1 = true;
	check2 = true;
	check3 = true;
	check4 = true;
	check5 = 1;

	if (flag == 0) check5 = 0;

	check1 = trimField(field);
	if(field.same_bill_to && field.same_bill_to.checked == true)
	{
		check2 = detectPOBOX(field.b_address1.value);
		check3 = detectPOBOX(field.b_address2.value);
	} else {
		check2 = detectPOBOX(field.address1.value);
		check3 = detectPOBOX(field.address2.value);
	}

	if (check5 != 0)
		check4 = disableForm(field);

	return (check1 && check2 && check3 && check4);
}

function verifyPPAddress(field1, field2)
{
	check1 = true;
	check2 = true;

	check1 = detectPOBOX(field1);
	check2 = detectPOBOX(field2);

	return (check1 && check2);
}