<!--


/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

/*
=============================================================

=============================================================
*/

function isInvalidChar(str){
	var invalidExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\-\!\?\<\>\"\;\:\,\'\.\{\}\`\~\=\|]/;
	if(str.search(invalidExp) != -1) {
		return true;
	}
	return false;
}


/*
===================================================
Validate email
===================================================
*/
/*
function validateEmail(){
var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
str = document.getElementById('email').value;
if(str.match(emailRegEx)){
document.form.submit();
}else{
alert('Please enter a valid email address.');
return false;
}
}
*/






   	var submitcount=0;
   
 function validRequired(formField)
	{
	var result = true;
		formField.focus();
		result = false;

	return result;
	}

 function doublecheck()
	{
 	// double check	
	var result =true;
	
	
	if(Trim(document.form.loginname.value).length!=0) {
    document.getElementById("UserNameID").innerHTML ='';
	};
	if(Trim(document.form.password.value).length!=0) {
    document.getElementById("PasswordID").innerHTML ='';
	};
	if(Trim(document.form.confirmpassword.value).length!=0) {
    document.getElementById("ConfirmPasswordID").innerHTML ='';
	};
	if(Trim(document.form.password.value)==Trim(document.form.confirmpassword.value)) {
    document.getElementById("ConfirmPasswordID").innerHTML ='';
	};
	if(Trim(document.form.hinttypecode.value).length!=0) {
    document.getElementById("HintID").innerHTML ='';
	};
	if(Trim(document.form.hint.value).length!=0) {
    document.getElementById("AnswerID").innerHTML ='';
	};
	if(Trim(document.form.email.value).length!=0) {
    document.getElementById("EmailID").innerHTML ='';
	};
	if(Trim(document.form.firstname.value).length!=0) {
    document.getElementById("FirstNameID").innerHTML ='';
	};
	if(Trim(document.form.lastname.value).length!=0) {
    document.getElementById("LastNameID").innerHTML ='';
	};
	if(Trim(document.form.street.value).length!=0) {    /*|| (Trim(document.form.street2.value).length!=0))*/
    document.getElementById("StreetID").innerHTML ='';
	};
	if(Trim(document.form.city.value).length!=0) {
    document.getElementById("CityID").innerHTML ='';
	};
	if((document.form.country.value!='0000' || document.form.country.value!='0001') && (Trim(document.form.state.value).length==0)) {
    document.getElementById("StateID").innerHTML ='';
	};
	if((document.form.country.value=='0000' || document.form.country.value=='0001') && (Trim(document.form.state.value).length!=0)) {
    document.getElementById("StateID").innerHTML ='';
	};
	if(Trim(document.form.zip.value).length!=0) {
    document.getElementById("ZipID").innerHTML ='';
	};
	if(Trim(document.form.workphone.value).length!=0) {
    document.getElementById("DaytimePhoneID").innerHTML ='';
	};
	// shipto
	if(Trim(document.form.shiptofirstname.value).length!=0) {
    document.getElementById("SFirstNameID").innerHTML ='';
	};
	if(Trim(document.form.shiptolastname.value).length!=0) {
    document.getElementById("SLastNameID").innerHTML ='';
	};
	if(Trim(document.form.shiptostreet1.value).length!=0) {     /*|| (Trim(document.form.shiptostreet2.value).length!=0))*/ 
    document.getElementById("SStreetID").innerHTML ='';
	};
	if(Trim(document.form.shiptocity.value).length!=0) {
    document.getElementById("SCityID").innerHTML ='';
	};
	if((document.form.shiptocountry.value!='0000' || document.form.shiptocountry.value!='0001') && (Trim(document.form.shiptostate.value).length==0)) {
    document.getElementById("SStateID").innerHTML ='';
	};
	if((document.form.shiptocountry.value=='0000' || document.form.shiptocountry.value=='0001') && (Trim(document.form.shiptostate.value).length!=0)) {
    document.getElementById("SStateID").innerHTML ='';
	};
	if(Trim(document.form.shiptozip.value).length!=0) {
    document.getElementById("SZipID").innerHTML ='';
	};
	if(Trim(document.form.shiptophone.value).length!=0) {
    document.getElementById("SDaytimePhoneID").innerHTML ='';
	};
	if(Trim(document.form.accounttypecode.value).length!=0) {
    document.getElementById("ProfessionID").innerHTML ='';
	};
	return result;
	}

/*
=============================================================
Check Fields
=============================================================
*/
function checkfields()
{
	//Validating the email field
    var re10digit=/^\d{10}$/ //regular expression defining a 10 digit number
	var re=/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	
	
	doublecheck();
	
	if(Trim(document.form.loginname.value).length==0) {
    alert("Missing required fields or invalid data. Please enter a user name.");
	document.form.loginname.value='';
    validRequired(document.form.loginname);
	}	
	else if (isInvalidChar(document.form.password.value)){
    alert("Invalid data. Passwords may only contain letters and numbers.");
	document.form.password.value='';
	validRequired(document.form.password);
	}
	else  if ((Trim(document.form.password.value).length<7) || (Trim(document.form.password.value).length>12)) {
	alert("Invalid data. Please provide a 7-12 character password.");
 	document.form.password.value='';
 	validRequired(document.form.password);
    }
	else  if(Trim(document.form.confirmpassword.value).length==0) {
    alert("Missing required fields or invalid data. Please confirm your password.");
	document.form.confirmpassword.value='';
	validRequired(document.form.confirmpassword);
	}
	else  if(document.form.password.value!=document.form.confirmpassword.value) {
	alert("Missing required fields or invalid data. Your passwords do not match, please re-enter.");
	document.form.password.value='';
	document.form.confirmpassword.value='';
    //validRequired(document.form.confirmpassword);
	validRequired(document.form.password);
	}	
    else  if(Trim(document.form.hinttypecode.value).length==0) {
    document.getElementById("HintID").innerHTML ='<font color=red size=1><b>Please select a password reminder question</b></font>';
	validRequired(document.form.hinttypecode);
    }
	else  if(Trim(document.form.hint.value).length==0) {
    document.form.hint.value='';
	document.getElementById("AnswerID").innerHTML ='<font color=red size=1><b>Please provide an answer to the question you selected</b></font>';
	validRequired(document.form.hint);
	}
	
	
	else if (! document.form.email.value.match(re)) {
    alert("Missing required fields or invalid data. Please provide a valid e-mail address that includes one (@), and at least one (.) Spaces and special characters are not valid.");
	validRequired(document.form.email);
	//document.form.email.value='';
	document.form.email.focus();
    document.form.email.select();
    return false;
    }
		
	else  if(Trim(document.form.accounttypecode.value).length==0) {
    alert("Missing required fields or invalid data. Please specify a profession.");
	validRequired(document.form.accounttypecode);
	}
	
	else if(Trim(document.form.firstname.value).length==0) {
	alert("Missing required fields or invalid data. Please provide your first name for billing.");
    document.form.firstname.value='';
	validRequired(document.form.firstname);
	}
	else  if(Trim(document.form.lastname.value).length==0) {
	alert("Missing required fields or invalid data. Please provide your last name for billing.");
    document.form.lastname.value='';
	validRequired(document.form.lastname);
	}
	
	// new code w/o 2nd address line

	else  if(Trim(document.form.street.value).length==0) {
	alert("Missing required fields or invalid data. Please provide a street address for billing.");
    document.form.street.value='';
	validRequired(document.form.street);
	}
	
	
// old code with 2nd address line

	/*
	else  if ((Trim(document.form.street2.value).length==0) && (Trim(document.form.street.value).length==0)) {
    document.form.street.value='';
	//document.form.street2.value='';
	document.getElementById("StreetID").innerHTML ='<font color=red size=1><b>Please provide an address</b></font>';
	validRequired(document.form.street);
	}
	else  if ((Trim(document.form.street2.value).length>0) && (document.form.street.value.toUpperCase().indexOf('BOX')>0)) {
    document.form.street.value='';
	document.form.street2.value='';
	document.getElementById("StreetID").innerHTML ='<font color=red size=1><b>PO Box address info must be entered on the next line when a regular street address is provided</b></font>';
	validRequired(document.form.street);
	}
	*/
	
	else  if(Trim(document.form.city.value).length==0) {
	alert("Missing required fields or invalid data. Please provide a city for billing.");
    document.form.city.value='';
	validRequired(document.form.city);
	}
	
	else  if(Trim(document.form.country.value).length==0) {
    document.getElementById("CountryID").innerHTML ='<font color=red size=1><b>Please provide a country</b></font>';
	validRequired(document.form.country);
	}
	else  if ((document.form.country.value=='0000' || document.form.country.value=='0001') && (Trim(document.form.state.value).length==0)) {
	alert("Missing required fields or invalid data. Please provide a state for billing.");
	validRequired(document.form.state);
	}
	else  if(Trim(document.form.zip.value).length==0) {
    alert("Missing required fields or invalid data. Please provide a zip code for billing.");
	document.form.zip.value='';
	validRequired(document.form.zip);
	}
	
	// check if numbers match 10 digits and value is not null
	else if (Trim(document.form.workphone.value).search(re10digit)==-1) { //if match failed
	alert("Missing required fields or invalid data. Phone must be 10 digits. Omit country code, parentheses, spaces and hyphens.");
	//document.form.workphone.value='';
	document.form.workphone.focus;
	document.form.workphone.select();
	validRequired(document.form.workphone);
	}


// shipto
	else  if(Trim(document.form.shiptofirstname.value).length==0) {
    alert("Missing required fields or invalid data. Please provide your first name for shipping or check the box above if shipping to the same address as your billing address.");
	document.form.shiptofirstname.value='';
	validRequired(document.form.shiptofirstname);
	}
	else  if(Trim(document.form.shiptolastname.value).length==0) {
    alert("Missing required fields or invalid data. Please provide your last name for shipping.");
	document.form.shiptolastname.value='';
	validRequired(document.form.shiptolastname);
	}	
	
	// new code w/o 2nd address line
	else  if(Trim(document.form.shiptostreet1.value).length==0) {
    alert("Missing required fields or invalid data. Please provide a street address for shipping.");
	document.form.shiptostreet1.value='';
	validRequired(document.form.shiptostreet1);
	}
	
	// old code with 2nd address line

	/*else  if ((Trim(document.form.shiptostreet1.value).length==0) && (Trim(document.form.shiptostreet2.value).length==0)) {
    document.form.shiptostreet1.value='';
	document.form.shiptostreet2.value='';
	document.getElementById("SStreetID").innerHTML ='<font color=red size=1><b>Please provide an address</b></font>';
validRequired(document.form.shiptostreet1);
	}
	else  if ((Trim(document.form.shiptostreet2.value).length>0) && (document.form.shiptostreet1.value.toUpperCase().indexOf('BOX')>0)) {
    document.form.shiptostreet1.value='';
	document.form.shiptostreet2.value='';
	document.getElementById("SStreetID").innerHTML ='<font color=red size=1><b>PO Box address info must be entered on the next line when a regular street address is provided</b></font>';
validRequired(document.form.shiptostreet1);
	}*/

	else  if(Trim(document.form.shiptocity.value).length==0) {
    alert("Missing required fields or invalid data. Please provide a city for shipping.");
	document.form.shiptocity.value='';
	validRequired(document.form.shiptocity);
	}	
	
	else  if(Trim(document.form.shiptocountry.value).length==0) {
    document.getElementById("SCountryID").innerHTML ='<font color=red size=1><b>Please provide a country</b></font>';
validRequired(document.form.shiptocountry);
	}
	else  if ((document.form.shiptocountry.value=='0000' || document.form.shiptocountry.value=='0001') && (Trim(document.form.shiptostate.value).length==0)) {
	alert("Missing required fields or invalid data. Please provide a state for shipping.");
	validRequired(document.form.shiptostate);
	}
	else  if(Trim(document.form.shiptozip.value).length==0) {
    alert("Missing required fields or invalid data. Please provide a zip code for shipping.");
	document.form.shiptozip.value='';
	validRequired(document.form.shiptozip);
	}	
	
	// check if numbers match 10 digits and value is not null
	else if (Trim(document.form.shiptophone.value).search(re10digit)==-1) //if match failed 
	{
	alert("Missing required fields or invalid data. Phone must be 10 digits. Omit country code, parentheses, spaces and hyphens.");
	// document.form.shiptophone.value='';
	document.form.shiptophone.focus;
	validRequired(document.form.shiptophone);
	}
	
	
	else 
	{

		if (Trim(document.form.street.value).length==0)
		{
		   document.form.street.value=document.form.street2.value;
		   document.form.street2.value='';
		};
		   
		if (Trim(document.form.shiptostreet1.value).length==0)
		{
			document.form.shiptostreet1.value=document.form.shiptostreet2.value;
		   	document.form.shiptostreet2.value='';
		};
		   
		//alert('!!!!!!!!!!!!!!!!');
		if (submitcount > 0) 
   		{
        	alert("Please wait, your request is being processed.")
        	return false;
   		}
		
		if (submitcount == 0)
       	{
			submitcount++
			//alert('submitcount='+submitcount);
            //alert("Return True");
			return true;
	   	}
	}
	return false;
}

/*
=============================================================
Copy Address
=============================================================
*/
function copyAddress() 
{
	document.form.shiptofirstname.value=document.form.firstname.value
	document.form.shiptolastname.value=document.form.lastname.value
	document.form.shiptoagencyname.value=document.form.company.value

	//if (Trim(document.form.street.value).length==0)
	//{
		//document.form.street.value=document.form.street2.value;
		//document.form.street2.value='';
	//}
	
	document.form.shiptostreet1.value=document.form.street.value
	//document.form.shiptostreet2.value=document.form.street2.value
	document.form.shiptocity.value=document.form.city.value
	document.form.shiptostate.value=document.form.state.value
	document.form.shiptocountry.value=document.form.country
	document.form.shiptozip.value=document.form.zip.value
	document.form.shiptocountry.value=document.form.country.value
	document.form.shiptophone.value=document.form.workphone.value
}

// -->

