String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
function isValidPallet(plt){
	plt=plt.trim().replace(/ /g,"");
	return /^\d{1,2}$/.test(plt);
}

function isOneToSixAlphaNumeric(alphanumericChar)
{

	alphanumericChar=alphanumericChar.trim();
	if(alphanumericChar.length == 0 || alphanumericChar.search(/[^a-zA-Z0-9 ]/g) != -1 || alphanumericChar.length>6)
	{
		return false;
	}
	else
		return true;
}

function isCheckSelected(checkname){
	
	checks=document.getElementsByName(checkname);
	checked=false;
	for(var i=0;i<checks.length;i++){
		if(checks[i].checked)checked=true;
		
	}
	return checked;	
}
function isZip(s)
{
	//reZip = new RegExp(/(^\d{5}$)|(^[A-Za-z]{1}\d{1}[A-Za-z]{1}\d{1}[A-Za-z]{1}\d{1}$)/);
	reZip = new RegExp(/(^\d{5}$)|(^[A-Za-z]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$)/);
	if (!reZip.test(s)) {
		return false;
	}else{
		return true;
	}

}
function checkRating(){

	/*
	if(!isOneToSixAlphaNumeric(document.getElementById("OrigZip").value)){alert ("Please enter alphanumeric(1-6 characters) for Origin postal code"); return false;}


	if(!isOneToSixAlphaNumeric(document.getElementById("DestZip").value)){alert ("Please enter alphanumeric(1-6 characters) for Destination postal code"); return false;}
	*/
	//freight term must selected 
	if(!isCheckSelected("billterm")){alert("Please select Freight terms  ");return false;}

	//USD 

	//Orig and Destination code must be entered.
	if(!document.getElementById("OrigZip").value.trim()){alert("Please enter Origin postal code "); return false;}
	if(!document.getElementById("DestZip").value.trim()){alert("Please enter Destination postal code "); return false;}

	//Proper Combination Of Class WeightHaz
	if(improperCombinationOfClassWeightHaz()){alert(improperCombinationOfClassWeightHaz()); return false;}
	
	//alert("Form validated ..");
	
	//Zip codes validation .
	
	if(!isZip(document.getElementById("OrigZip").value)){
		alert("Origin postal code is not valid (e.g ##### or @#@#@# or @#@ #@#).\n# = Number.\n@ = Any Alphanumeric character.");
		document.getElementById("OrigZip").focus();
		return false;
	}
	if(!isZip(document.getElementById("DestZip").value)){
		alert("Destination postal code is not valid (e.g ##### or @#@#@# or @#@ #@#).\n# = Number.\n@ = Any Alphanumeric character.");
		document.getElementById("DestZip").focus();
		return false;
	}
	
	/*
	if(!isOneToSixAlphaNumeric(document.getElementById("OrigZip").value)){alert ("Please enter alphanumeric(1-6 characters) for Origin postal code"); return false;}

	if(!isOneToSixAlphaNumeric(document.getElementById("DestZip").value)){alert ("Please enter alphanumeric(1-6 characters) for Destination postal code"); return false;}
	*/
	//checking palletes entries
	pallets=document.getElementById("pallets").value;
	if(pallets!=null && pallets.trim()!=""){
		if (!isValidPallet(pallets)){alert("Pallets can contain max two digits "); return false;};
	}

	//Checking if discounts are filled.
	//betweenZeroHundred   ltldiscount   mindiscount
	ltldiscount=document.getElementById("ltldiscount").value;

	if(ltldiscount!=null && ltldiscount.trim()!=""){
		if (!betweenZeroHundred(ltldiscount)){alert("LTL discount must be between 0 and 99.99"); return false;};
	}

	mindiscount=document.getElementById("mindiscount").value;
	if(mindiscount!=null && mindiscount.trim()!=""){
		if (!betweenZeroHundred(mindiscount)){alert("Minimum discount must be between 0 and 99.99"); return false;};
	}

	absmincharge=document.getElementById("absmincharge").value;
	/*
	if(absmincharge!=null && absmincharge.trim()!=""){
		if (!betweenZeroHundred(absmincharge)){alert("Minimum Absolute-min charge must be between 0 and 99.99"); return false;};
	}
	*/
	if(absmincharge!=null && absmincharge.trim()!=""){
		if (!betweenZeroThousand(absmincharge)){alert("Minimum Absolute-min charge must be between 0 and 999.99"); return false;};
	}
}

function improperCombinationOfClassWeightHaz(){
	//At least one class must be selected 
	notincluded=new Array();
	classes=new Array();
	for(i=0;i<4;i++){
		elem=document.getElementById("Clky["+i+"]");
		if(elem.value.trim()!="")
			classes[i]=elem.value;
		else 
			notincluded.push(i);
		
	}
	if(classes.length==0) return "Please select at least one class";

	//At least one weight must be selected 
	weights=new Array();
	for(i=0;i<4;i++){
		elem=document.getElementById("Weight["+i+"]");
		if(elem.value.trim()!="")
			weights[i]=elem.value;
	}
	if(weights.length==0) return "Please select at least one Weight";


	//Proper combination must be in Weight and Class 

	for (var i in classes)
	{
		if(weights[i]==null){return "Please select proper combination of class and weight"}
	}

	for (var i in weights)
	{
		if(classes[i]==null){return "Please select proper combination of class and weight"}
	}
	//Alert all validations suceeded.

	//Hazes should Come in proper combination 
	hazs=new Array();
	for(i=0;i<4;i++){
		elem=document.getElementById("Haz["+i+"]");
		if(elem.value.trim()!="")
			hazs[i]=elem.value;
		
	}
	if(weights.length==0) return "Please select at least one Weight";



	for(var i in notincluded){//alert(i);
		if(notincluded[i] in hazs) {
			
			return  "Please remove improper haz entry not associated with weight and class ";
		}
	}
	return false;
}

function fillhaz(check){

	var hazid;

	hazid= check.id;

	if(document.getElementById(hazid).value != "X")
	{
		document.getElementById(hazid).value="X";
		//document.getElementById(hazid).style.backgroundColor="#f00";;
		document.getElementById(hazid).style.color="#f00";
		document.getElementById(hazid).style.fontSize="16px";
	}else{
		document.getElementById(hazid).value=""
		document.getElementById(hazid).style.backgroundColor="#fff";	
				
	}
}

//TODO write better ........ltldiscount        mindiscount          
function betweenZeroHundred(strString)
   //  check for valid numeric strings	
   {

   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
	if(blnResult){
		flt=parseFloat(strString); 
		return (flt>=0 && flt<99.99);
	}
   return blnResult;
}

function betweenZeroThousand(strString)
   //  check for valid numeric strings	
   {

   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
	if(blnResult){
		flt=parseFloat(strString); 
		return (flt>=0 && flt<999.99);
	}
   return blnResult;
}

function handleEvent(event,hazid){

	if(event.keyCode == "46")
	{
		document.getElementById(hazid).value="";
		document.getElementById(hazid).style.backgroundColor="#fff";	
	}else{
		document.getElementById(hazid).value="X";
		document.getElementById(hazid).style.color="#f00";
		document.getElementById(hazid).style.fontSize="16px";	
		document.getElementById(hazid).style.backgroundColor="#fff";	
	}

}
