// ************************************************************ //
// * trim()													  * //
// *     Removes leading and trailing spaces from a string    * //
// ************************************************************ //
function trim(s) {
	// Remove leading spaces and carriage returns
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) {
		s = s.substring(1,s.length);
	}

	// Remove trailing spaces and carriage returns
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || 
		    (s.substring(s.length-1,s.length) == '\r')) {
		s = s.substring(0,s.length-1);
	}
	return s;
}

// *************************************************** //
// * trimBox()										 * //
// *      Trims the text of a TextBox				 * //
// *************************************************** //
function trimBox(box) {
	box.value = trim(box.value);
}

// ***************************************************** //
// * stripNonNumeric()								   * //
// *      Removes non-numeric characters from a string * //
// ***************************************************** //
function stripNonNumeric(stringToClean) {
	var c;
	var cleanString = "";
	for( i = 0; i < stringToClean.length; i++ ) {
		c = stringToClean.charAt(i);
		if( isNaN(c) && c!= "." ) continue;
		cleanString += c;
	}
	return cleanString;
}

// *************************************************** //
// * stripNonNumericBox()							 * //
// *      Clears a textbox of non-numeric characters * //
// *************************************************** //
function stripNonNumericBox(box) {
	box.value = stripNonNumeric(box.value);
}

// *************************************************** //
// * validateRequiredField()						 * //
// *      Validates that a control has some value	 * //
// *************************************************** //
function validateRequiredField(control,fieldname,errorMsg) {
	//alert("validating");
	if( control == "undefined" ) {
		//alert("control undefined");
		errorMsg += "** " + fieldname.toUpperCase() + " IS UNDEFINED **";
	}
	else {
		//alert("control not undefined");
		if( trim(control.value).length == 0 )
			errorMsg += "Please enter a value for " + fieldname + ".\r";
	}
	return errorMsg;
}
// *************************************************** //
// * validateZip()									 * //
// *      Validates a zip code is in proper format	 * //
// *************************************************** //
function validateZip(zip,errorMsg,fieldname) {
	var re = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	if( zip != null && zip.length > 0 && 
		zip.search(re) == -1 ) {
		errorMsg += "You have entered an invalid value for " + fieldname + ".\r";
	}
	return errorMsg;
}
// *************************************************** //
// * validateEmail()								 * //
// *      Validates an email						 * //
// *************************************************** //
function validateEmail(email,errorMsg,fieldname) {
	var re = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	if( email == null || trim(email).length == 0 ) {
		return errorMsg;
	}
	else if( email.search(re) == -1 ) {
		errorMsg += "You have entered an invalid value for " + fieldname + ".\r";
	}
	return errorMsg;
}
// *************************************************** //
// * validatePhone()								 * //
// *      Validates a (US) phone number				 * //
// *************************************************** //
function validatePhone(phone,errorMsg,fieldname) {
	var re = /^1?-?\(?[1-9]\d?\d?[\)\. -]? ?[1-9]\d\d[\. -]?\d\d\d\d$/;
	if( phone == null || trim(phone).length == 0 ) {
		return errorMsg;
	}
	else if( phone.search(re) == -1 ) {
		errorMsg += "You have entered an invalid value for " + fieldname + ".\r";
	}
	return errorMsg;
}
// *************************************************** //
// * validateDate()									 * //
// *      Validates a date							 * //
// *************************************************** //
function validateDate(date, nameOfField, errorMsg) {
	var re = /^[01]?\d\/[0123]?\d\/(18|19|20)\d\d$/
	if( date.search(re) == -1 ) {
		errorMsg += nameOfField + " - please enter in mm/dd/yyyy format\r";
	}
	else {
		var arr = date.split("/");
		if( arr[0] > 12 || arr[0] < 1 ||
			arr[1] > 31 || arr[1] < 1 ) {
			errorMsg += nameOfField + " - please enter a valid date\r";
		}
		else if ((arr[0] == "2" && arr[1] > 29) ||
				(arr[0] == "4" && arr[1] > 30) ||
				(arr[0] == "6" && arr[1] > 30) || 
				(arr[0] == "9" && arr[1] > 30) || 
				(arr[0] == "11" && arr[1] > 30) ) {
			errorMsg += nameOfField + " - that month doesn't have " + arr[1] + " days\r";
		}
	}
	return errorMsg;
}