﻿// Form Validation Scripts
var sErrMessage = "";








function ValidateText(controlName, controlText){
	var result = true;
	var control;
	if (document.getElementById(controlName))
	{
		control = document.getElementById(controlName)
		if (control.value.length == 0)
		{
			sErrMessage += "\n\t- Please ensure that the \"" + controlText + "\" field is completed.";
			result = false;
		}
	}
	
	return result;
}

function ValidateEmail(controlName, controlText){
	var result = true;
	var control;
	if (document.getElementById(controlName))
	{
		control = document.getElementById(controlName)
		if (control.value.length == 0)
		{
			sErrMessage += "\n\t- Please ensure that the \"" + controlText + "\" field is completed.";
			result = false;
		}
		else if (!IsValidEmail(control.value))
		{
			sErrMessage += "\n\t- Please ensure that the \"" + controlText + "\" field contains a valid email.";
			result = false;
		}
	}
	
	return result;
}

function ValidatePhone(controlName, controlText){
	var result = true;
	var control;
	if (document.getElementById(controlName))
	{
		control = document.getElementById(controlName)
		if (control.value.length == 0)
		{
			sErrMessage += "\n\t- Please ensure that the \"" + controlText + "\" field is completed.";
			result = false;
		}
		else if (!IsNumeric(control.value))
		{
			sErrMessage += "\n\t- Please ensure that the \"" + controlText + "\" field contains only numeric characters.";
			result = false;
		}
	}
	
	return result;
}

function ValidateIsNumeric(controlName, controlText){
	var result = true;
	var control;
	if (document.getElementById(controlName))
	{
		control = document.getElementById(controlName)
		if (!IsNumeric(control.value))
		{
			sErrMessage += "\n\t- Please ensure that the \"" + controlText + "\" field contains only numeric characters.";
			result = false;
		}
	}
	
	return result;
}

function IsNumeric(sString){
	
	var sValidChars = "0123456789.- ";
	var sChar;
	var bResult = true;
	
	if (sString.length == 0) return false;
	
	for (i = 0; i < sString.length && bResult == true; i++)
	{
		sChar = sString.charAt(i);
		if (sValidChars.indexOf(sChar) == -1)
		{
			bResult = false;
		}
	}
	return bResult;
}

function IsValidEmail(sEmail){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	if (sEmail.search(validRegExp) == -1) 
	{
	  return false;
	} 
    return true; 
}

String.prototype.endsWith = function(str, ignoreCase) { var thisStr = ignoreCase ? this.toLowerCase() : this; str = ignoreCase ? str.toLowerCase() : str; if (ignoreCase) return thisStr.endsWith(str, false); return this.length>=str.length && this.substring(this.length-str.length) == str; };