// JavaScript Document

function sameInfo(){
 //if(document.coForm.checkbox.checked) {
  document.coForm.Shipping_First_Name.value = document.coForm.Billing_First_Name.value;
  document.coForm.Shipping_Last_Name.value = document.coForm.Billing_Last_Name.value;
  document.coForm.Shipping_Address_1.value = document.coForm.Billing_Address_1.value;
  document.coForm.Shipping_Address_2.value = document.coForm.Billing_Address_2.value;
  document.coForm.Shipping_City.value = document.coForm.Billing_City.value;
  document.coForm.sstate.value = document.coForm.bstate.value;
  document.coForm.szip.value = document.coForm.bzip.value;
  document.coForm.szip4.value = document.coForm.bzip4.value;

}


function trim(inputString) {
	if (typeof inputString != "string") {
		return inputString;
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	
	while (ch == " ") {
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") {
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf(" ") != -1) {
		retValue = retValue.substring(0, retValue.indexOf(" ")) + retValue.substring(retValue.indexOf(" ")+1, retValue.length);
	}
	return retValue;
}


function validatetask(thevalue, thename) {
	var nowcont = true;
	var none = /\?|\!|\<|\>|\[|\]|\{|\}|\@|\$|\%|\*/;
	var none2 = /\<|\>|\[|\]|\{|\}|\@|\$|\%|\*/;
	var emailFilter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w+)$/;
	var filter5dig = /\d{5}/;
	var filter4dig = /\d{4}/;
	var filter3dig = /\d{3}/;
	var thefield;
	if (thename == "Billing_First_Name") {
		thefield = document.coForm.Billing_First_Name;
	}
	if (thename == "Billing_Last_Name") {
		thefield = document.coForm.Billing_Last_Name;
	}
	if (thename == "Billing_Address_1") {
		thefield = document.coForm.Billing_Address_1;
	}
	if (thename == "Billing_Address_2") {
		thefield = document.coForm.Billing_Address_2;
	}
	if (thename == "Billing_City") {
		thefield = document.coForm.Billing_City;
	}
	if (thename == "Shipping_First_Name") {
		thefield = document.coForm.Shipping_First_Name;
	}
	if (thename == "Shipping_Last_Name") {
		thefield = document.coForm.Shipping_Last_Name;
	}
	if (thename == "Shipping_Address_1") {
		thefield = document.coForm.Shipping_Address_1;
	}
	if (thename == "Shipping_Address_2") {
		thefield = document.coForm.Shipping_Address_2;
	}
	if (thename == "Shipping_City") {
		thefield = document.coForm.Shipping_City;
	}
	
	
	if(thename == "Billing_First_Name" || thename == "Billing_Last_Name" || thename == "Shipping_First_Name" || thename == "Shipping_Last_Name" || thename == "Billing_Address_1" || thename == "Billing_Address_2" || thename == "Shipping_Address_1" || thename == "Shipping_Address_2" || thename == "Billing_City" || thename == "Shipping_City") {
		if(nowcont==true && trim(thevalue) == "" && thename != "Billing_Address_2" && thename != "Shipping_Address_2"){
		
		document.getElementById("coMessage").innerHTML ="Please enter a value for " + thename;
		thefield.focus();
		nowcont=false;
		
	  	
	      }
	  if(nowcont==true && trim(thevalue) != "" && none.test(thevalue)) {
		 
		 document.getElementById("coMessage").innerHTML =thename + " contains invalid characters.  Please review and correct this field.";
		 thefield.focus();
		 nowcont=false;
		 
	     }
	}
	
	if(thename == "email") {
		
		if(nowcont==true && trim(thevalue) == "" ){
		
			document.getElementById("coMessage").innerHTML ="Please enter an Email Address";
			document.getElementById("coForm").email.focus();
			nowcont=false;
	      }
		 if (nowcont==true &&  trim(thevalue) != ""  && !emailFilter.test(thevalue)) {
		document.getElementById("coMessage").innerHTML = "The Email Address entered is not valid.  Please review and correct this field.";
 		document.getElementById("coForm").email.focus();
		 nowcont=false; 
		 }
		  
	}
	
	if(nowcont==true && thevalue == "" && thename == "bstate"){
		document.getElementById("coMessage").innerHTML ="Please select a Billing State";
			document.getElementById("coForm").bstate.focus();
			nowcont=false;
		
	}
	if(nowcont==true && thevalue == "" && thename == "sstate"){
		document.getElementById("coMessage").innerHTML ="Please select a Shipping State";
			document.getElementById("coForm").sstate.focus();
			nowcont=false;
		
	}
	if(thename == "bzip") {
		if(nowcont == true && thevalue == "") {
			document.getElementById("coMessage").innerHTML ="Please enter a Billing Zip Code";
			document.getElementById("coForm").bzip.focus();
			nowcont=false;
		}
		 if (nowcont==true &&  trim(thevalue) != ""  && !filter5dig.test(thevalue)) {
		document.getElementById("coMessage").innerHTML = "The Billing Zip Code Field contains invalid characters.  Please enter only numbers.";
 		document.getElementById("coForm").bzip.focus();
		 nowcont=false;
	 }
   }
	 if (nowcont==true &&  trim(thevalue) != ""  && !filter4dig.test(thevalue) && thename=="bzip4") {
		document.getElementById("coMessage").innerHTML = "The Billing Zip Code Field contains invalid characters.  Please enter only numbers.";
 		document.getElementById("coForm").bzip4.focus();
		 nowcont=false;
	 }
	 
	 if(thename == "szip") {
		if(nowcont == true && thevalue == "") {
			document.getElementById("coMessage").innerHTML ="Please enter a Shipping Zip Code";
			document.getElementById("coForm").szip.focus();
			nowcont=false;
		}
		 if (nowcont==true &&  trim(thevalue) != ""  && !filter5dig.test(thevalue)) {
		document.getElementById("coMessage").innerHTML = "The Shipping Zip Code Field contains invalid characters.  Please enter only numbers.";
 		document.getElementById("coForm").szip.focus();
		 nowcont=false;
	 }
   }
	 if (nowcont==true &&  trim(thevalue) != ""  && !filter4dig.test(thevalue) && thename=="szip4") {
		document.getElementById("coMessage").innerHTML = "The Shipping Zip Code Field contains invalid characters.  Please enter only numbers.";
 		document.getElementById("coForm").szip4.focus();
		 nowcont=false;
	 }
	 
	 if(thename == "areacode" && nowcont==true &&  trim(thevalue) != ""  && !filter3dig.test(thevalue)) {
		document.getElementById("coMessage").innerHTML = "The Phone Number contains invalid characters.  Please enter only numbers.";
 		document.getElementById("coForm").areacode.focus();
		nowcont=false;
	 }
	  if(thename == "prefix" && nowcont==true &&  trim(thevalue) != ""  && !filter3dig.test(thevalue)) {
		document.getElementById("coMessage").innerHTML = "The Phone Number contains invalid characters.  Please enter only numbers.";
 		document.getElementById("coForm").prefix.focus();
		nowcont=false;
	 }
	 if(thename == "suffix" && nowcont==true &&  trim(thevalue) != ""  && !filter4dig.test(thevalue)) {
		document.getElementById("coMessage").innerHTML = "The Phone Number contains invalid characters.  Please enter only numbers.";
 		document.getElementById("coForm").suffix.focus();
		nowcont=false;
	 }
	 if(thename == "faxareacode" && nowcont==true &&  trim(thevalue) != ""  && !filter3dig.test(thevalue)) {
		document.getElementById("coMessage").innerHTML = "The Fax Number contains invalid characters.  Please enter only numbers.";
 		document.getElementById("coForm").faxareacode.focus();
		nowcont=false;
	 }
	  if(thename == "faxprefix" && nowcont==true &&  trim(thevalue) != ""  && !filter3dig.test(thevalue)) {
		document.getElementById("coMessage").innerHTML = "The Fax Number contains invalid characters.  Please enter only numbers.";
 		document.getElementById("coForm").faxprefix.focus();
		nowcont=false;
	 }
	 if(thename == "faxsuffix" && nowcont==true &&  trim(thevalue) != ""  && !filter4dig.test(thevalue)) {
		document.getElementById("coMessage").innerHTML = "The Fax Number contains invalid characters.  Please enter only numbers.";
 		document.getElementById("coForm").faxsuffix.focus();
		nowcont=false;
	 }
	 if(document.getElementById("coForm").cardmsg) {
		 	if(thename="cardmsg" && nowcont==true && thevalue != "" && none2.test(thevalue)){
		document.getElementById("coMessage").innerHTML ="Your card message contains invalid characters.  Please enter only letters, numbers and punctuation marks.";
			document.getElementById("coForm").cardmsg.focus();
			nowcont=false;
			}
	}
	
	return nowcont;
}



function getformvalues (fobj, valfunc) {
	
	
	var str = "";
	aok = true;
	var val;
	
	for (var i = 0; i < fobj.elements.length; i++) {
		if (valfunc) {
			if (aok == true) {
				val = valfunc(fobj.elements[i].value, fobj.elements[i].name);
				if (val == false) {
					aok = false;
				}
			}
		}
		str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
	}
	return str;
}

function submitform(theform, serverPage, objID, valfunc) {
	
	var file = serverPage;
	var str = getformvalues(theform, valfunc);
	
	if (aok == true) {
		obj = document.getElementById(objID);
		document.getElementById("coMessage").innerHTML = "";
		//document.getElementById("theForm").style.display="none";
		processajax (serverPage, obj, "post", str);
	}
}

