//  Remove leading and trailing spaces from string
function trim1(str)
{
  var i,j;
  i = 0;
  j=str.length-1;
  str = str.split("");
  while(i < str.length)
  {
    if(str[i]==" "){ str[i] = ""; }
    else{ break; }
    i++;
  }
  while(j > 0)
  {
	if(str[j]== " "){ str[j]=""; }
    else{ break; }
    j--;
  }
  return str.join("");
}

// Check that the test string contains something other than just whitespace
function isEmpty(testStr){var reWhitespace=/^\s+$/;return(testStr==null||testStr.length==0||reWhitespace.test(testStr))}
// Check that the test string contains valid e-mail formatting
function isEmail(testStr){var reEmail=/^.+\@.+\..+$/;return(!isEmpty(testStr)&&reEmail.test(testStr))}
// Check that the test string contains valid U.S. ZIP code formatting
function isUSZip(testStr){var reUSZip=/(^\d{5}$)|(^\d{5}-\d{4}$)/;return(!isEmpty(testStr)&&reUSZip.test(testStr))}
// Check that the test string contains valid Canadian postal code formatting
function isCAPostal(testStr){var reCAPostal=/^((\D{1}\d{1}\D{1})+(\s?|\-?)+(\d{1}\D{1}\d{1}))$/;return(!isEmpty(testStr)&&reCAPostal.test(testStr))}
// Check that the test string contains only numbers
function isNumeric(testStr){var reInteger=/^\d+$/;return(!isEmpty(testStr)&&reInteger.test(testStr))}
// Check that the test string contains only numbers and is the correct length
function isNumLn(testStr,len){return(testStr.length==len&&isNumeric(testStr))}
// Check that the test string contains only numbers and is within the correct length range
function isNumLnRng(testStr,lenLow,lenUp){return((testStr.length>=lenLow&&testStr.length<=lenUp)&&isNumeric(testStr))}
// Display an error message to the user if the form data is invalid
function isInvalid(fieldObj,msgText){alert(msgText);fieldObj.focus()}

function validatezipform(formObj){
	if(formObj.country)
	{
		if(formObj.country.value == "US"&& !isUSZip(trim1(formObj.postalcode.value))){	isInvalid(formObj.postalcode,"Please enter a valid zip code");	return false}
		if(formObj.country.value == "CA"&& !isCAPostal(trim1(formObj.postalcode.value))){	isInvalid(formObj.postalcode,"Please enter a valid postal code");	return false}
	}
	else
		if(!isUSZip(trim1(formObj.postalcode.value)) && !isCAPostal(trim1(formObj.postalcode.value))){isInvalid(formObj.postalcode,"Please enter a valid zip code");	return false}
}
function validateaddressform(formObj){
	if(isEmpty(formObj.address.value)&&isEmpty(formObj.city.value)&&isEmpty(formObj.state.value)&&isEmpty(trim1(formObj.postalcode.value))){isInvalid(formObj.address,"Please enter an address. Submit either a street address and a city and state  or  street address and a zip code");return false}
	if(!isEmpty(formObj.address.value)&&(isEmpty(formObj.city.value)&&isEmpty(formObj.state.value)&&isEmpty(trim1(formObj.postalcode.value)))){isInvalid(formObj.address,"Please enter street address and a city and state OR street address and a zip code");return false}
	if(	!isEmpty(formObj.address.value)&&	((!isEmpty(formObj.city.value) && isEmpty(formObj.state.value))||(isEmpty(formObj.city.value) && !isEmpty(formObj.state.value)))){isInvalid(formObj.address,"Please enter a street address and a city and state  or  street address and a zip code as a minimum. The more information, the more specific your results");return false}
	if(((!isEmpty(formObj.city.value) && isEmpty(formObj.state.value))||(isEmpty(formObj.city.value) && !isEmpty(formObj.state.value)))){		isInvalid(formObj.city,"Please enter a city and state ");	return false}
	//if(!isEmpty(trim1(formObj.postalcode.value))&&!isUSZip(trim1(formObj.postalcode.value))){isInvalid(formObj.postalcode,"Please enter a valid zip code");	return false}
	
	if(formObj.country)
	{
		if(formObj.country.value == "US"&& !isUSZip(trim1(formObj.postalcode.value))){	isInvalid(formObj.postalcode,"Please enter a valid zip code");	return false}
		if(formObj.country.value == "CA"&& !isCAPostal(trim1(formObj.postalcode.value))){	isInvalid(formObj.postalcode,"Please enter a valid postal code");	return false}
	}
	else
		if(!isUSZip(trim1(formObj.postalcode.value)) && !isCAPostal(trim1(formObj.postalcode.value))){isInvalid(formObj.postalcode,"Please enter a valid zip code");	return false}

}

function newWindow(filePath,winName,winProperties){ // Opens new browser window with event-specified parameters. Called by any event handler, usually onClick.
	var newWin = window.open(filePath,winName,winProperties); // Example call: <A HREF="javascript:newWindow('window.html','new','width=300,height=300,scrollbars=no')">
	newWin.focus(); // Makes a pre-existing window of the same name jump to the front instead of staying hidden.
}

