/* common.js */

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
}

/* validates an email address for proper format */
function checkEmail(emailString) {
  var validEmail=false;
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailString.value)) {
    validEmail=true;
  }
  return validEmail;
}

function validateInput(theForm) {
  if (theForm.first_name.value=="") {
    alert("Please enter your first name before proceeding");
    theForm.first_name.focus();
    return false;
  }
  if (theForm.last_name.value=="") {
    alert("Please enter your last name before proceeding");
    theForm.last_name.focus();
    return false;
  }
  if (theForm.email_address.value=="") {
    alert("Please enter your email address before proceeding");
    theForm.email_address.focus();
    return false;
  } else {
    var validEmail = checkEmail(theForm.email_address);
    if (!validEmail) {
      alert("Please your complete email address, i.e. username@company.com");
      theForm.email_address.focus();
      return false;    
    }
  }

  /****** commented out per PP's instructions (Jan-2003) ******
 if (theForm.state.selectedIndex==0) {
    alert("Please select your state before proceeding");
    theForm.state.focus();
		theForm.country.options[0].selected=true;
    return false;
  } else {
		determineCountry(theForm);
	}

  if (theForm.phone_npa.value=="") {
    alert("Please enter your area code before proceeding");
    theForm.phone_npa.focus();
    return false;
  }
  if (theForm.phone_nxx.value=="") {
    alert("Please enter your phone number before proceeding");
    theForm.phone_nxx.focus();
    return false;
  }
  if (theForm.phone_line.value=="") {
    alert("Please enter your phone number before proceeding");
    theForm.phone_line.focus();
    return false;
  }
  if (theForm.street_address1.value=="") {
    alert("Please enter your street address before proceeding");
    theForm.street_address1.focus();
    return false;
  }
  if (theForm.city.value=="") {
    alert("Please enter your city before proceeding");
    theForm.city.focus();
    return false;
  }
  if (theForm.zip_code5.value=="") {
    alert("Please enter your zip code before proceeding");
    theForm.zip_code5.focus();
    return false;
  }

}

function determineCountry(theForm) {
	if ( theForm.state.selectedIndex>0 && theForm.state.selectedIndex<54 ) {
		theForm.country.options[1].selected=true;
	} else {
		theForm.country.options[2].selected=true;
	}
****** end of comment (Jan-2003) ******/
}  

/*** ADDED in JAN, 2004 ***/
function determineCountry(countryDropdown) {
	if (countryDropdown.length==14) {
			document.forms.registrationForm.country.value = "CANADA";
	} else if (countryDropdown.length==54) {
			document.forms.registrationForm.country.value = "USA";
	} else if (countryDropdown.length>54) {
		if ( countryDropdown.selectedIndex>54 ) {
			document.forms.registrationForm.country.value = "CANADA";
		} else {
			document.forms.registrationForm.country.value = "USA";
		}
	}
}

function validateInput2004( theForm,fieldArray,requiredArray ) {
	
	//trim all fields in fieldArray
	for (i=0; i<fieldArray.length; i++) {
		theForm[fieldArray[i]].value = trim(theForm[fieldArray[i]].value);
	}
	
	//validate required fields
	validCount=0;
	for (i=0; i<requiredArray.length; i++) {
		if ( validateRegistrationField( theForm,requiredArray[i] ) ) {
			validCount++;
		} else {
			//return control to the page
			return false;
		}
	}
	
	//validate opt-in check
	if (theForm.opt_in_flag.checked) {
		//check email address
		if (theForm.email_address.value=="") {
			alert("Your e-mail address is required in order to receive periodic e-mail newsletters, special promotions, site updates.");
			theForm.email_address.focus();
			return false;
		} else {
			//validate format
			emailRE = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
			if ( !( emailRE.test(theForm.email_address.value) ) ) {
				alert("The e-mail address you entered is not properly formatted.\nIt needs to be in the \"jsmith@aol.com\" format.");
				theForm.email_address.focus();
				return false;
			}
		}		
	}
	
	if (validCount==requiredArray.length) {
		return true;
	} else {
		return false;
	}

}

/*** the following function is called from validateInput2004
     and should not be called directly ***/
function validateRegistrationField( theForm,fieldName ) {
	switch(fieldName) {
		case "first_name" :
			if (theForm[fieldName].value=="") {
				alert("Please enter your first name.");
				theForm[fieldName].focus();
				return false;
			}
			return true;
			//break;
			
		case "last_name" :
			if (theForm[fieldName].value=="") {
				alert("Please enter your last name.");
				theForm[fieldName].focus();
				return false;
			}
			return true;
			//break;
			
		case "street_address1" :
			if (theForm[fieldName].value=="") {
				alert("Please enter your street address (line 1).");
				theForm[fieldName].focus();
				return false;
			}
			return true;
			//break;
			
		case "street_address2" :
			if (theForm[fieldName].value=="") {
				alert("Please enter your street address (line 2).");
				theForm[fieldName].focus();
				return false;
			}
			return true;
			//break;
			
		case "city" :
			if (theForm[fieldName].value=="") {
				alert("Please enter your city.");
				theForm[fieldName].focus();
				return false;
			}
			return true;
			//break;
			
		case "state" :
			if (theForm[fieldName].selectedIndex==0) {
				alert("Please select your state or province.");
				theForm[fieldName].focus();
				return false;
			}
			return true;
			//break;
			
		case "zip_code5" :
			if (theForm[fieldName].value=="") {
				alert("Please enter your zip or postal code.");
				theForm[fieldName].focus();
				return false;
			} else {
				//check proper formatting
				if (theForm.country.value=="USA") {
					usZipRE = /(^\d{5}$)|(^\d{5}-\d{4}$)/; //08810[-2402]
					if ( !( usZipRE.test(theForm[fieldName].value) ) ) {
						alert("The US zip code you entered is not properly formatted.\nIt needs to be in the \"12345\" format.");
						theForm[fieldName].select();
						return false;
					}
				} else if (theForm.country.value=="CANADA") {
					canadaPostalCodeRE = /^\D{1}\d{1}\D{1}\ ?\d{1}\D{1}\d{1}$/; //L1H 8N9 or L1H8N9 
					if ( !( canadaPostalCodeRE.test(theForm[fieldName].value) ) ) {
						alert("The Canadian postal code you entered is not properly formatted.\nIt needs to be in the \"A5A 5A5\" format");
						theForm[fieldName].select();
						return false;
					}
				}
			}
			return true;
			//break;
			
		case "phone" :
			if (theForm[fieldName].value=="") {
				alert("Please enter your phone number.");
				theForm[fieldName].focus();
				return false;
			} else {
				//check proper formatting
				phoneRE = /^\d{3}\-\d{3}\-\d{4}$/; //123-456-7890
				if ( !( phoneRE.test(theForm[fieldName].value) ) ) {
					alert("The phone number you entered is not properly formatted.\nIt needs to be in the \"123-456-7890\" format.");
					theForm[fieldName].select();
					return false;
				}
			}
			return true;
			//break;
			
		case "email_address" :
			if (theForm[fieldName].value=="") {
				alert("Please enter your email address.");
				theForm[fieldName].focus();
				return false;
			} else {
				//validate format
				emailRE = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
				if ( !( emailRE.test(theForm[fieldName].value) ) ) {
					alert("The e-mail address you entered is not properly formatted.\nIt needs to be in the \"jsmith@aol.com\" format.");
					theForm[fieldName].focus();
					return false;
				}
			}
			return true;
			//break;

		case "registration_rules" :
			if ( !(theForm[fieldName].checked) ) {
				alert("Make sure to read and agree to the registration rules before proceeding.");
				theForm[fieldName].focus();
				return false;
			}
			return true;
			//break;
	}
}

function validateFileExtension( theField,extensionArray,fileExt ) {	

	theField.value = trim(theField.value);
	/*** NO NEED TO REQUIRE THIS FIELD
	if ( theField.value=="" ) {
		alert("Please select a file to upload.");
		theField.focus();
		return false;
	}
	***/
	var f = theField.value.split('.');
	
	if ( f.length<2 ) {
		
		alert("The file you are about to upload needs an extension, please correct it or try a different file.");
		theField.focus();
		return false;
		
	} else {
		
		if ( extensionArray.length>0 ) {
			
			//check valid extensions against the uploaded file
			valid = false;
			for ( i=0;i<extensionArray.length;i++ ) {
				if ( theField.value.lastIndexOf( extensionArray[i] ) > -1 ) {
					valid = true;
					break;
				}
			}
			
			if (valid) {
				return true;
			} else {
				alert("The file you are trying to upload is not in a valid format. " + fileExt);
				theField.focus();
				return false;
			}
			
		} else {
			
			//any extension is valid
			return true;
			
		}
		
	}
	
}