﻿// 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- \"" + controlText + "\" must be 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- \"" + controlText + "\" must be completed.";
			result = false;
		}
		else if (!IsValidEmail(control.value))
		{
			sErrMessage += "\n\t- \"" + controlText + "\" must be 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- \"" + controlText + "\" must be completed.";
			result = false;
		}
		else if (!IsNumeric(control.value))
		{
			sErrMessage += "\n\t- \"" + controlText + "\" must only contain 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- \"" + controlText + "\" must only contain 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; 
}