/// <summary>
/// Ensures that the given list box has a selected item other than 0
/// </summary>
/// <returns>True if the objects value is a valid city</returns>
function ValidateItemSelected(myObject, errorMessage) {
  success = false;
  if(myObject.selectedIndex == 0) {
    alert(errorMessage);
    myObject.focus();
  } else {
    success = true;
  }
  return success;
}


/// <summary>
/// Ensures that the given object has a value other than blank
/// </summary>
/// <returns>True if the objects value is a valid city</returns>
function ValidateNotEmpty(myObject, errorMessage) {
  success = false;
  if(myObject.value == "") {
    alert(errorMessage);
    myObject.focus();
  } else {
    success = true;
  }
  return success;
}


/// <summary>
/// Checks an objects value and display an error message if a valid city was not found
///   If the field is required, an error message will appear for an empty string
///   If a valid city was not given, the focus is set to the object allowing the user to correct the issue
/// </summary>
/// <returns>True if the objects value is a valid city</returns>
function ValidateCity(myObject, required) {
  success = false;
  if(myObject.value == "") {
    if(required) {
      alert("Please enter your city.");
      myObject.focus();
    } else {
      success = true;
    }
  } else if(IsValidCity(myObject.value)) {
    success = true;
  } else {
    alert("Please enter a valid city.");
    myObject.focus();
    myObject.select();
  }
  return success;
}

function ValidateDecimal(myObject, type, required)
{
	var success=false;
	if (myObject.value=="" && required)
	{
		alert("Please enter the " + type);
		myObject.focus();
	}
	else if(IsValidDecimal(myObject.value))
	{
		return true;
	}
	else
	{
		alert("Please enter the " + type);
		myObject.focus();
	}
}


/// <summary>
/// Checks an objects value and display an error message if a valid date was not found
///   If the field is required, an error message will appear for an empty string
///   If a valid date was not given, the focus is set to the object allowing the user to correct the issue
///   Dates have a type string that can be blank, it is used to increase the error description so rather than just saying date, you can say requested date, completed date, birth date, etc.
/// </summary>
/// <returns>True if the objects value is a valid date</returns>
function ValidateDate(myObject, type, required) {
  success = false;
  if(myObject.value == "") {
    if(required) {
      alert("Please enter a " + type + " date.");
      myObject.focus();
    } else {
      success = true;
    }
  } else if(IsValidDate(myObject.value)) {
    success = true;
  } else {
    alert("Please enter a valid " + type + " date");
    myObject.focus();
    myObject.select();
  }
  return success;
}


/// <summary>
/// Checks an objects value and display an error message if a valid e-mail address was not found
///   If the field is required, an error message will appear for an empty string
///   If a valid e-mail address was not given, the focus is set to the object allowing the user to correct the issue
/// </summary>
/// <returns>True if the objects value is a valid e-mail address</returns>
function ValidateEMailAddress(myObject, required, errorMessage, errorValidMessage) {
  success = false;
  if(myObject.value == "") {
    if(required) {
      if(errorMessage == "") {
				alert("Please enter your email address.");
			} else {
				alert(errorMessage);
			}
      myObject.focus();
    } else {
      success = true;
    }
  } else if(IsValidEMailAddress(myObject.value)) {
    success = true;
  } else {
		if(errorValidMessage == "") {
			alert("Please enter your full email address (example: Jsmith@aol.com).");
		}else{
			alert(errorValidMessage);
		}
    myObject.focus();
    myObject.select();
  }
  return success;
}

function ValidateMinLength(myObject, length, errorMessage) {
  success = false;
  if(myObject.value.length < length) {
    alert(errorMessage);
    myObject.focus();
  } else success = true;
  return success
}

/// <summary>
/// Checks an objects value and display an error message if a valid integer was not found
///   If the field is required, an error message will appear for an empty string
///   If a valid integer was not given, the focus is set to the object allowing the user to correct the issue
///   Dates have a type string that will be used in the error message as the name of the integer, for example "number of files".
/// </summary>
/// <returns>True if the objects value is a valid integer</returns>
function ValidateInteger(myObject, type, required) {
  success = false;
  if(myObject.value == "") {
    if(required) {
      alert("Please enter the " + type);
      myObject.focus();
    } else {
      success = true;
    }
  } else if(IsValidInteger(myObject.value)) {
    success = true;
  } else {
    alert("Please enter a valid " + type);
    myObject.focus();
    myObject.select();
  }
  return success;
}

/// <summary>
/// Checks an objects value and display an error message if a valid integer was not found
///   If the field is required, an error message will appear for an empty string
///   If a valid integer was not given, the focus is set to the object allowing the user to correct the issue
///   Dates have a type string that will be used in the error message as the name of the integer, for example "number of files".
/// </summary>
/// <returns>True if the objects value is a valid integer</returns>
function ValidateYear(myObject, required,errormsg) {
  success = false;
  if(myObject.value == "") {
    if(required) {
      alert(errormsg);
      myObject.focus();
    } else {
      success = true;
    }
  } else if(IsValidInteger(myObject.value)) {
    success = true;
  } else {
    alert(errormsg);
    myObject.focus();
    myObject.select();
  }
  return success;
}

/// <summary>
/// Checks an objects value and display an error message if a valid phone number was not found
///   If the field is required, an error message will appear for an empty string
///   If a valid phone number was not given, the focus is set to the object allowing the user to correct the issue
///   Phone numbers have a type string that can be blank, it is used to increase the error description so rather than just saying phone number, you can say home phone number, work phone number, cell phone number, etc.
/// </summary>
/// <returns>True if the objects value is a valid phone number</returns>
function ValidatePhoneNumber(myObject, type, required, errorMessage,errorValidMessage) {
  success = false;
  if(myObject.value == "") {
    if(required) {
      if(errorMessage==""){
				alert("Please enter a " + type + " phone number");
			}else{
				alert(errorMessage);
			}
      myObject.focus();
    } else {
      success = true;
    }
  } else if(IsValidPhoneNumber(myObject.value)) {
    success = true;
  } else {
    if(errorValidMessage==""){
    	alert("Please enter a valid " + type + " phone number");
		}else{
			alert(errorValidMessage);
		}
    myObject.focus();
    myObject.select();
  }
  return success;
}


/// <summary>
/// Checks an objects value and display an error message if a valid social security number was not found
///   If the field is required, an error message will appear for an empty string
///   If a valid social security number was not given, the focus is set to the object allowing the user to correct the issue
/// </summary>
/// <returns>True if the objects value is a valid social security number</returns>
function ValidateSocialSecurityNumber(myObject, required) {
  success = false;
  if(myObject.value == "") {
    if(required) {
      alert("Please enter a social security number");
      myObject.focus();
    } else {
      success = true;
    }
  } else if(IsValidSocialSecurityNumber(myObject.value)) {
    success = true;
  } else {
    alert("Please enter a valid social security number");
    myObject.focus();
    myObject.select();
  }
  return success;
}


/// <summary>
/// Checks an objects value and display an error message if a valid state was not found
///   If the field is required, an error message will appear for an empty string
///   If a valid state was not given, the focus is set to the object allowing the user to correct the issue
/// </summary>
/// <returns>True if the objects value is a valid state</returns>
function ValidateState(myObject, required) {
  success = false;
  if(myObject.value == "") {
    if(required) {
      alert("Please enter a state");
      myObject.focus();
    } else {
      success = true;
    }
  } else if(IsValidState(myObject.value)) {
    success = true;
  } else {
    alert("Please enter a valid state");
    myObject.focus();
    myObject.select();
  }
  return success;
}


/// <summary>
/// Checks an objects value and display an error message if a valid url was not found
///   If the field is required, an error message will appear for an empty string
///   If a valid url was not given, the focus is set to the object allowing the user to correct the issue
/// </summary>
/// <returns>True if the objects value is a valid url</returns>
function ValidateUrl(myObject, required) {
  success = false;
  if(myObject.value == "") {
    if(required) {
      alert("Please enter a url");
      myObject.focus();
    } else {
      success = true;
    }
  } else if(IsValidUrl(myObject.value)) {
    success = true;
  } else {
    alert("Please enter a valid url");
    myObject.focus();
    myObject.select();
  }
  return success;
}


/// <summary>
/// Checks an objects value and display an error message if a valid zip code was not found
///   If the field is required, an error message will appear for an empty string
///   If a valid zip code was not given, the focus is set to the object allowing the user to correct the issue
/// </summary>
/// <returns>True if the objects value is a valid zip code</returns>
function ValidateZipCode(myObject, required, errorMessage,errorValidMessage) {
  success = false;
  if(myObject.value == "") {
    if(required) {
			if(errorMessage==""){
				alert("Please enter a zip code");
			}else{
				alert(errorMessage);
			}
      myObject.focus();
    } else {
      success = true;
    }
  } else if(IsValidZipCode(myObject.value)) {
    success = true;
  } else {
		if(errorValidMessage==""){
			alert("Please enter a valid zip code");
		}else{
			alert(errorValidMessage);
		}
    myObject.focus();
    myObject.select();
  }
  return success;
}

function ValidateDefaultNotSelected(mySelect, errorMessage)
{
	var defaultValue=mySelect.options[0].value;
	var selectedValue=mySelect.options[mySelect.selectedIndex].value;
	if (defaultValue==selectedValue)
	{
		alert(errorMessage);
		return false;
	}
	else
		return true;
}


/// <summary>
/// Returns true if the given string is a valid city with no special characters (alphanumeric)
/// </summary>
function IsAlphaNumeric(string) {
  var stringRegExp = new RegExp("^[0-9,a-z,A-Z ]+$");
  return stringRegExp.test(string);
}



/// <summary>
/// Returns true if the given string is a valid city with no special characters or numbers
/// </summary>
function IsValidCity(city) {
  var cityRegExp = new RegExp("^[a-z,A-Z ]+$");
  return cityRegExp.test(city);
}


/// <summary>
/// Returns true if the given string is a valid date.
///  Month and day can be shown as 1 or 2 digits.
///  Both 1 and 01 could be used to represent January
///  Year can be 2 or 4 digits.  03 or 2003 are both valid
/// </summary>
function IsValidDate(date) {
  var dateRegExp = new RegExp("^([0-9]{1,2})[\/\.-]([0-9]{1,2})[\/\.-]([0-9]{2}|[0-9]{4})$");
  return dateRegExp.test(date);
}


/// <summary>
/// Returns true if the given string is a valid e-mail address
/// </summary>
function IsValidEMailAddress(emailAddress) {
  var emailRegExp = new RegExp(/^[A-Za-z0-9_]+(\.[A-Za-z0-9_]+)*\@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,3}$/);
  //var emailRegExp = new RegExp(/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/);
  return emailRegExp.test(emailAddress);
}


/// <summary>
/// Returns true if the given string is a valid integer
/// </summary>
function IsValidInteger(number) {
  var integerRegExp = new RegExp("^[0-9]+$");
  return integerRegExp.test(number);
}

/// <summary>
/// match one decimal and digits only
/// </summary>
function IsValidDecimal(number) {
	var decimalRegExp = new RegExp("[^0-9\.]|([\.].*[\.])");
	return !decimalRegExp.test(number);
}

/// <summary>
/// Match optional "$", optional ","s and optional cents "."
/// </summary>
function IsValidMoney(money) {
	var moneyRegExp = new RegExp("^[0-9,]*(($)|([.][0-9]{0,2}$))");
	return moneyRegExp.test(money);
}

/// <summary>
/// Returns true if the given string is a valid phone number in the form ###-###-####
/// </summary>
function IsValidPhoneNumber(phoneNumber) {
  //var phoneRegExp = new RegExp("^[2-9][0-9]{2}-[0-9]{3}-[0-9]{4}$");
  if(!IsValidInteger(phoneNumber) || (phoneNumber.length<10)){
		return false;
  }
		else{
		return true;
	}
  //return phoneRegExp.test(phoneNumber);
}


/// <summary>
/// Returns true if the given string is a valid social security number in the form ###-##-####
/// </summary>
function IsValidSocialSecurityNumber(ssn) {
  var ssnRegExp = new RegExp("^[0-9]{3}-[0-9]{2}-[0-9]{4}$");
  return ssnRegExp.test(ssn);
}


/// <summary>
/// Returns true if the given string is a valid short state with no special characters or numbers of length 2
/// </summary>
function IsValidState(state) {
  var stateRegExp = new RegExp("^[a-z,A-Z]{2}$");
  return stateRegExp.test(state);
}


/// <summary>
/// Returns true if the given string is a valid url
/// </summary>
function IsValidUrl(url) {
  var urlRegExp = new RegExp("[A-Za-z0-9_]{2,}:\/{2}([-[A-Za-z0-9_]]+\.)+[A-Za-z0-9_]+\S*");
  return urlRegExp.test(url);
}


/// <summary>
/// Returns true if the given string is a valid zip code in the form (#####) or (#####-####)
/// </summary>
function IsValidZipCode(zipCode) {
  var zipRegExp = new RegExp("^[0-9]{5}(-[0-9]{4})?$");
  return zipRegExp.test(zipCode);
}


/*****NEED THESE FOR TRIM FUNCTIONS******/	
String.prototype.rightTrim = rightTrim;
String.prototype.leftTrim = leftTrim;
String.prototype.trim = trim;


/// <summary>
/// Trims trailing white space from string.  Uses prototype definition  "String.prototype.rightTrim = rightTrim;".
/// </summary>
function rightTrim() {
	return this.replace(/\s+$/gi, "");
}


/// <summary>
/// Trims leading white space from string. Uses prototype definition  "String.prototype.leftTrim = leftTrim;".
/// </summary>
function leftTrim() {
	return this.replace(/^\s*/gi, "");
}


/// <summary>
/// Trims leading and trailing white space from string. Uses prototype definition  "String.prototype.trim = trim;".
/// </summary>
function trim() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

