	
	// --- Page scope variables used for Browser Type Detection
	var m_boolIE4
	var m_boolNS4
	var m_boolMac

	// --- Main entry point for this include (called at the end of this file) 
	function intelygisClientSideValidations_Main()
	{
	
		// --- Set browser type page scope vars (Function in XX) 
		setBrowserTypeVariables();
				
	}
	
	// --- Detect the user's browser
	function setBrowserTypeVariables()	// v1.000
	{
		
		// alert(navigator.appVersion);
		
		m_boolNS4 = false;
		m_boolIE4 = false;
		
		// TO DO: Clean up this function - Better browser detection
		// and note - IE on mac uses [] while IE on the PC uses ()
		m_boolMac = (navigator.appVersion.indexOf("Mac") != -1);
		m_boolWindows = (navigator.appVersion.indexOf("Windows") != -1);		
		
		// m_boolNS4 = (document.layers != null);

		if(m_boolMac)
		{
			m_boolNS4 = true;	
		}
		else
		{
			if (document.all != null)
				m_boolIE4 = true;
			else
				m_boolNS4 = true;
		}
			
		return;	
	}
	
	// --- Retrieve reference to a form object
	function getForm(strFormName)	//  v1.000
	{
		if (m_boolIE4)
			return document.forms(strFormName)
		
		if (m_boolNS4)
			return document.forms[strFormName]
			
	}
	
	// --- Clear contents of a form 
	function clearForm(strFormName)		// v1.000
	{
		getForm(strFormName).reset();
		
		return;
	}
	
	function clearFormWithHiddenClearField(strFormName, strClearFieldName)
	{
		var formElements = getFormElements(strFormName)
		var clearField = getElement(formElements, strClearFieldName)
		
		clearField.value = true;
		simpleSubmitForm(strFormName);
		
		return;
	}
	
	// --- Submit a form
	function submitForm(strFormContextKey, strFormName, boolConfigureBeforeSubmission) //  v1.000
	{
		var boolValidated = false;
		var boolConfigured = true;
		
		// --- Call the form's validation function
		boolValidated = eval("validate" + strFormContextKey + "('" + strFormName + "')")
		
		if(boolValidated)
		{
			// --- Call form's configuration function if required
			if(boolConfigureBeforeSubmission)
				boolConfigured = eval("configure" + strFormContextKey + "('" +  strFormName + "')");
				
			// --- If configuration successfull, submit the form
			if(boolConfigured)	
				getForm(strFormName).submit();
		}
	}
	
	function simpleSubmitForm(formName)
	{
		getForm(formName).submit();
	}
	
	// --- Move from one field to another to ensure pick up last change before submission
	function moveFocus(objElements, strField1, strField2)
	{
		getElement(objElements, strField1).focus();
		getElement(objElements, strField2).focus();
	}
	
	// --- Retreieve reference to the elements collection of a form
	function getFormElements(strFormName)	//  v1.000
	{
		if (m_boolIE4)
			return document.forms(strFormName).elements
		
		if (m_boolNS4)
			return document.forms[strFormName].elements
	}
	
	// --- Retreieve a reference to an element in an elements collection
	function getElement(objElements,strElementName)	//  v1.000
	{
		if (m_boolIE4)
			return objElements(strElementName)
		
		if (m_boolNS4) {
			return objElements[strElementName]
		}
			
	}
	
	// --- Verifies a form element has a value
	function required(objElement, strUserFriendlyFieldName)	//  v1.000
	{
	
		// --- Dispatch based on the Element's type
		switch (objElement.type)
		{
			case "text":
				if (! validateTextbox(objElement, strUserFriendlyFieldName)) return false;
				break;
				
			case "password":
				if (! validateTextbox(objElement, strUserFriendlyFieldName)) return false;
				break;
			
			case "select-one":
				if (! validateSelect(objElement, strUserFriendlyFieldName)) return false;
				break;
			
			case "textarea":
				if (! validateTextbox(objElement, strUserFriendlyFieldName)) return false;
				break;
			
			default:
				switch (objElement[0].type )
				{
					case "radio":
						if (! validateRadioButton(objElement, strUserFriendlyFieldName)) return false;
						break;	
				}	
				
				break;
		}
		
		return true;
	}

	// --- Verifies a text box has a value
	function validateTextbox(objElement, strUserFriendlyFieldName)
	{
		if (objElement.value == "") 
			return failedRequired(objElement, strUserFriendlyFieldName)
		else
			return true
	}

	// --- Verifies a select (drop down) has a value
	function validateSelect(objElement, strUserFriendlyFieldName)
	{
		if (objElement.options[objElement.selectedIndex].value == "")
			return failedRequired(objElement, strUserFriendlyFieldName)
		else
			return true
	}
	
	// --- Verifies a checkbox has a value
	function validateCheckbox(objElement, strUserFriendlyFieldName)
	{
		if (!objElement.checked)
			return failedRequired(objElement, strUserFriendlyFieldName)
	}
	
	// --- Verifies a radio button has a value
	function validateRadioButton(objElement, strUserFriendlyFieldName)
	{
		var i
		
		for(i=0; i <= (objElement.length - 1); i++)
			if(objElement[i].checked) return true;
			
		return failedRequired(objElement[0], strUserFriendlyFieldName)
	}
	
	// --- Inform the user, a required field does not have a value
	function failedRequired(objElement, strUserFriendlyFieldName)
	{
		alert(strUserFriendlyFieldName + " is a required field.  Please complete this field before submitting.");
		objElement.focus();
		return false;
	}
	
	// --- Validate the size (length) of a value is less than a given value
	function validateSize(objElement, intLength, strUserFriendlyFieldName, boolAllowEmpty)
	{

		var strvalue;
		
		strvalue = objElement.value;
		
		if ((strvalue == "") && (boolAllowEmpty)) return true;
		
		if (strvalue.length <= intLength) return true;
	
		alert (strUserFriendlyFieldName + " can contain a maximum of " + intLength + " characters.  It currently contains " + strvalue.length + ".");
		objElement.focus();
		return false;
		
	}
	
	// --- Retrieves the value of a radio button
	function getRadioButtonValue(objElement)
	{
	
		var i
		
		for(i=0; i <= (objElement.length - 1); i++)
			if(objElement[i].checked) return objElement[i].value;
			
		return "";
		
	}
	
	// --- Verifies an email address has an @ and a .
	function validateEmail(objElement, strUserFriendlyFieldName, boolAllowEmpty)	// v1.00
	{
		var strvalue;
		
		strvalue = objElement.value;
		
		if ((strvalue == "") && (boolAllowEmpty)) return true;
			
		if (strvalue.match(/.@./)) return true;
		
		alert ("Please enter " + strUserFriendlyFieldName + " in the format aaa@aaaa.aaa");
		objElement.focus();
		return false;
	}
	
	function isSomething(val)
	{
		if(val != "") 
			return true 
		else
			return false
	}
	
	function validateNumeric(objElement, intLength, strUserFriendlyFieldName, boolAllowEmpty)
	{
		var strvalue;
		var strS;
		
		strvalue = objElement.value;
		
		if ((strvalue == "") && (boolAllowEmpty)) return true;
			
		if (strvalue.length == intLength)
		{ 
			if (isPosInteger(strvalue)) return true;
		}
		
		if(intLength == 1) 
			strS = "" 
		else 
			strS = "s"
			 
		alert ("Please enter " + intLength + " digit" + strS + " in the " + strUserFriendlyFieldName + " field.");
		objElement.focus();
		return false;
	}

	function isPosInteger(inputVal) {
		
		inputStr = inputVal.toString()
		
		for (var i = 0; i < inputStr.length; i++) 
		{
			var oneChar = inputStr.charAt(i)
				
			if (oneChar < "0" || oneChar > "9") return false;
	
		}
		return true
	}
	
	function validatePostalCode(objElement, strUserFriendlyFieldName, boolAllowEmpty)
	{
		var strvalue;
		var boolPassed

		objElement.value = objElement.value.toUpperCase();
		strvalue = objElement.value;
		
		if ((strvalue == "") && (boolAllowEmpty)) return true;
			
		boolPassed = false;
		
		if (strvalue.length == 7)
		{
			if (strvalue.match(/\D\d\D\s\d\D\d/)) boolPassed = true;	
		}
		else if (strvalue.length == 5)
		{
			if (strvalue.match(/\d\d\d\d\d/)) boolPassed = true;
		}
		
		if (!boolPassed)
		{
			alert ("Please enter " + strUserFriendlyFieldName + " in the format A1A 1A1 or 12345.");
			objElement.focus();
		}

			
		return boolPassed;
	}
	
	function validateAllPhoneNumberParts(objAreaCode, objStart, objEnd, strUserFriendlyFieldName, boolAllowEmpty)
	{
		var boolAreaCode;
		var boolStart;
		var boolEnd;
		  
		boolAreaCode = isSomething(objAreaCode.value);
		boolStart = isSomething(objStart.value);
		boolEnd = isSomething(objEnd.value);
		
		if (boolAreaCode && boolStart && boolEnd) return true
		if (!(boolAreaCode || boolStart || boolEnd)) return true
			
		alert ("Please complete all 3 " +  strUserFriendlyFieldName + " fields.");
		objAreaCode.focus();
		return false;
	}
	
	function validateDate(objElement, strUserFriendlyFieldName, boolAllowEmpty)
	{
		var strvalue;
		var strMessage;
		
		var strDay, strMonth, strYear;
		var intDay, intMonth
	
		strvalue = objElement.value;

		if ((strvalue == "") && (boolAllowEmpty)) return true;
			
		if (strvalue.length == 8)
		{
			
			if((strvalue.substring(2,3) == "/") && (strvalue.substring(5,6) == "/"))
			{
				
				strDay = strvalue.substring(3,5);
				strMonth = strvalue.substring(0,2);
				strYear = strvalue.substring(6,8);
		
				if ((isPosInteger(strDay)) && (isPosInteger(strMonth)) && (isPosInteger(strYear)))
				{	
					
					// --- Bug in Microsoft JavaScript - have to remove leading zeros before calling parse int
					intDay = parseInt(removeLeadingZeros(strDay));
					intMonth = parseInt(removeLeadingZeros(strMonth));
					
					if (!isIntegerInRange(intMonth, 1, 12))
					{	
						alert ("The month for " + strUserFriendlyFieldName + " must be between 1 and 12.");
						objElement.focus();
						return false;
					}
					
					if (!isIntegerInRange(intDay, 1, getDaysInMonth(strMonth)))
					{	
						alert ("The day specified for " + strUserFriendlyFieldName + " is out of the range of days in the specified month.");
						objElement.focus();
						return false;
					}
						
					return true;	
				}
			}
		}
		
		alert ("Please enter " + strUserFriendlyFieldName + " in the format mm/dd/yy. For example 10/31/01 for October 31, 2001.");
		objElement.focus();
		return false;

	}
	
	function removeLeadingZeros(val)
	{
	   while (val.charAt(0) == '0')
	      val = val.substring(1, val.length);

	   return val;
	} 
	
	function getDaysInMonth(strMonth)
	{
		switch (strMonth)
		{
			case "01": return 31;
			case "02": return 29;
			case "03": return 31;
			case "04": return 30;
			case "05": return 31;
			case "06": return 30;
			case "07": return 31;
			case "08": return 31;
			case "09": return 30;
			case "10": return 31;
			case "11": return 30;
			case "12": return 31;
			
		}
	}
	
	function isIntegerInRange(inputValue, minValue, maxValue) {
		
		if ((inputValue >= minValue) && (inputValue <= maxValue)) return true;
		
		return false;
	}
		
	function getSelectValue(objElement)
	{
		return objElement.options[objElement.selectedIndex].value
	}
	
	function validateCreditCardInfo(objElements, boolRequired)	// --- v1.000
	{
	
		// --- If any credit card info is supplied then all of it must be supplied
		var boolCheckCreditCardInfo = boolRequired;
		
		if (getRadioButtonValue(getElement(objElements, "CreditCardType")) != "") boolCheckCreditCardInfo = true;
		if (getElement(objElements, "CreditCardAccountNumber").value != "") boolCheckCreditCardInfo = true;
		if (getSelectValue(getElement(objElements, "CreditCardExpiryMonth")) != "") boolCheckCreditCardInfo = true;
		if (getSelectValue(getElement(objElements, "CreditCardExpiryYear")) != "") boolCheckCreditCardInfo = true;
		
		if(boolCheckCreditCardInfo)
		{

			if (!required(getElement(objElements,"CreditCardType"), "Credit Card Type")) return false;
			if (!required(getElement(objElements,"CreditCardAccountNumber"), "Credit Card Account Number")) return false;
			if (!required(getElement(objElements,"CreditCardExpiryMonth"), "Credit Card Expiry Month")) return false;
			if (!required(getElement(objElements,"CreditCardExpiryYear"), "Credit Card Expiry Year")) return false;
		
		}
		
		return true;
		
	}
	
	// --- Call main entry point for this include
	intelygisClientSideValidations_Main();
	
	