function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
function IsZip(code) {//checks for US code
	return (/^\d{5}$/.test(code));
}
function IsPostal(code) {//checks for canadian postal code
	return (/^[A-Za-z][0-9][A-Za-z](\s|\-){0,1}[0-9][A-Za-z][0-9]$/.test(code));
}
function Validate(form){ //validate the subscription form	
		//trim the values to get rid of any extra spaces before validation
		form.email.value = trim(form.email.value);
		form.name.value = trim(form.name.value);
		form.address.value = trim(form.address.value);
		form.postal.value = trim(form.postal.value);
		form.city.value = trim(form.city.value);		
		var validEmail = (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value));
		var errMsg ="";
		var validAddr = (/^\d{1,}/.test(form.address.value)); //check if address starts with a street number
		if ((validEmail&&form.email.value.length>0)){
			checkSelected = true;
		}
				
		else if ((!validEmail && form.email.value.length>0)) {
			errMsg = "Please enter a valid email address";
			alert(errMsg);
			checkSelected  = false;
			form.email.focus();
			return false;
		}
		else if (form.email.value.length==0) {
			errMsg = "Please enter a valid email address";
			alert(errMsg);
			checkSelected  = false;
			form.email.focus();
			return false;
		}
		if (form.name.value !=""){
			checkSelected = true;
		}
		else {
		
			alert("Please enter your name");
			checkSelected  = false;
			form.name.focus();
			return false;
		
		}	
		if (form.address.value.length>0 && validAddr)
			checkSelected = true;		
		if (form.address.value.length==0 || !validAddr) {
			alert("Please enter a valid address");
			checkSelected  = false;
			form.address.focus();
			return false;
		}
		if ((form.postal.value !="" || form.postal.value.length <8) && (IsZip(form.postal.value) || IsPostal(form.postal.value)))
			checkSelected = true;
		else  {
			alert("Please enter a valid postal code");
			checkSelected  = false;
			form.postal.focus();
			return false;
		}					
		if (checkSelected){		
			return true;		
		}

}
