    // Validator Object
    var valid = new Object();

    // REGEX Elements

        // matches zip codes
        valid.zipCode = /\d{5}(-\d{4})?/;

        // matches $17.23 or $14,281,545.45 or ...
        valid.Currency = /\$\d{1,3}(,\d{3})*\.\d{2}/;

        // matches 5:04 or 12:34 but not 75:83
        valid.Time = /^([1-9]|1[0-2]):[0-5]\d$/;

        //matches email
        valid.emailAddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
        //valid.emailAddress = /^[_a-z0-9\-]+(\.[_a-z0-9\-]+)*@[a-z0-9\-]+(\.[a-z0-9\-]+)+$/;

        // matches phone ###-###-####
        valid.phoneNumber = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;

        // International Phone Number
        valid.phoneNumberInternational = /^\d(\d|-){7,20}/;

        // IP Address
        valid.ipAddress = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;

        // Date xx/xx/xxxx
        valid.Date = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;

        // State Abbreviation
        valid.State = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;

        // Social Security Number
        valid.SSN = /^\d{3}\-\d{2}\-\d{4}$/;
        // must be filled
        valid.noBlank = /^.+/;
        valid.noBlank2 = /^..+/;
        // must be telephone
        valid.phoneNum = /^[0-9\-]+$/;
   function trimAll(sString){
      while (sString.substring(0,1) == ' ')
      {
        sString = sString.substring(1, sString.length);
      }
	  while (sString.substring(sString.length-1, sString.length) == ' ')
	  {
		sString = sString.substring(0,sString.length-1);
	  }
     return sString;
   }
    function validateForm(theForm) {

        var elArr = theForm.elements; 
        var msg;
        // basic validation-------------------------------------------
        for(var i = 0; i < elArr.length; i++) {

           with(elArr[i]) { 

              var v = elArr[i].validator; 

              if(!v) continue; 

              var thePat = valid[v]; 

              var gotIt = thePat.exec(trimAll(value)); 

              if(! gotIt){
                 if (v =="noBlank")
                    msg = "Please fill the required field.";
                 else if(v =="noBlank2")
                    msg = "Please enter at least 2 characters.";
                 else if(v =="phoneNum")
                    msg = "Please enter a valid phone Num. E.g. 0064-9-5234100";
                 else if(v=="emailAddress")
                    msg = "Please enter a valid email address.";
                 else if(v =="datebuy" || v=="datesent")
                    msg = "Please enter a valid date e.g. dd/mm/yyyy";
                 else   
                    msg = "Input error";
                 alert(msg);                  
                 elArr[i].select();
                 elArr[i].focus(); 
                 return false;
              }
           }
        }
        // basic validation  end--------------------------------------------        
       
        // Not both enter for region
      if (theForm.state !=null && theForm.stateCode != null && theForm.shippingState != null && theForm.shippingStateCode !=null)
      {        
        if (theForm.state.value != "" && theForm.stateCode.selectedIndex != 0)
        {
          alert("Please indicate only one region.");
          theForm.state.value = ""
          theForm.state.focus();
          return false;
        }
        else if(theForm.state.value == "" && theForm.stateCode.selectedIndex == 0){
          alert("Please indicate the region.");
          theForm.stateCode.focus();
          return false;
        }
        
        if(theForm.shippingState.value != "" && theForm.shippingStateCode.selectedIndex != 0)
        {
          alert("Please indicate only one region.");
          theForm.shippingState.value = ""
          theForm.shippingState.focus();
          return false;
        }
        else if(theForm.shippingState.value == "" && theForm.shippingStateCode.selectedIndex == 0){
          alert("Please indicate the region.");
          theForm.shippingStateCode.focus();
          return false;        
        }
        //country
        if(theForm.countryCode.selectedIndex==0)
        {
          alert("Please indicate the billing country.");
          theForm.countryCode.focus();
          return false; 
        }
        if(theForm.shippingCountryCode.selectedIndex==0)
        {
          alert("Please indicate the shipping country.");
          theForm.shippingCountryCode.focus();
          return false;         
        }
      }
      // password validation!
      if (theForm.password != null && theForm.password2 != null)
      {  
		var invalid = " "; // Invalid character is a space
		var minLength = 6; // Minimum length
		var pw1 =  theForm.password.value;
		var pw2 =  theForm.password2.value;
		// check for a value in both fields.
		if (pw1 == '' || pw2 == '') {
			alert('Please enter your password twice.');
			theForm.password.focus(); 
		return false;
		}
		// check for minimum length
		if (theForm.password.value.length < minLength) {
			alert('Your password must be at least ' + minLength + ' characters long. Try again.');
			theForm.password.focus();
			return false;
		}
		// check for spaces
		if (theForm.password.value.indexOf(invalid) > -1) {
			alert("Sorry, spaces are not allowed.");
			theForm.password.focus(); 
			return false;
		}
		else {
			if (pw1 != pw2) {
			  alert ("You did not enter the same new password twice. Please re-enter your password.");
			  theForm.password.focus(); 
			  return false;
		    }
		    else {
			   return true;
		    }
        }
       }                   
        return true;
    }
	   // address duplicating -------------------------------------------- 

	var ShipAddress1 = "";
	var ShipCity = "";
	var ShipState = "";
	var ShipStateIndex = 0;
	var ShipZip = "";
	var NonListState = ""

	function ShipToBillPerson(form) {
		if (form.copy.checked) {
			form.shippingAddress.value = form.address.value;
			form.shippingCity.value = form.city.value;
			form.shippingZip.value = form.zip.value;
			form.shippingStateCode.selectedIndex = form.stateCode.selectedIndex;
			form.shippingState.value = form.state.value;
			form.shippingCountryCode.selectedIndex = form.countryCode.selectedIndex;
		} 
		else {
			form.shippingAddress.value = "";
			form.shippingCity.value = "";
			form.shippingZip.value = "";
			form.shippingStateCode.selectedIndex = 0;
			form.shippingState.value = "";
			form.shippingCountryCode.selectedIndex = 0;
		}
	}        
    // address duplicating --------------------------------------------
function ValidateQty(theField,minQty,increment)
{
var pbytQty = theField.value;

	if (isNaN(pbytQty))
	{
	alert('Please enter a number for the quantity.');
	theField.value = minQty;
	theField.focus();
	theField.select();
	return false;
	}

	if ((pbytQty % 1) > 0)
	{
	alert('Please enter an integer for the quantity.');
	theField.value = minQty;
	theField.focus();
	theField.select();
	return false;
	}

	if (pbytQty < minQty)
	{
	alert('The minimum order quantity for this item is ' + minQty + '.');
	theField.value = minQty;
	theField.focus();
	theField.select();
	return false;
	}else{
		var pbytTestIncrement = pbytQty - minQty;
		if ((pbytTestIncrement % increment) > 0)
		{
		alert('You must order this item in increments of ' + increment + '.');
		theField.value = minQty;
		theField.focus();
		theField.select();
		return false;
		}
	}
}