﻿/*
Author: Serge Neroznaque.
Page Type: Client-side Include.
Page Purpose: Contains client-side global routines. Mainly custom form field validators.
Warning: The code is closed and the page shall not be changed by anyone except author.
*/
// <script>
function invalidName(strSubj)
{
	return (strSubj.charAt(0) == " " || strSubj.charAt(strSubj.length - 1) == " ");
}

function checkText(txtRef, strLabel, iLen) // iLen -- is optional parameter. useful when you want to check textareas
{
	if (strLabel == null) strLabel = "TEXT field"
	if (txtRef.value.length == 0) {
		alert("Input a value for \"" + strLabel + "\" before continuing.");
		txtRef.focus();
		return false;
	}
	else if (invalidName(txtRef.value)) {
		alert("\"" + strLabel + "\" can't contain spaces in the start or end.");
		txtRef.focus();
		txtRef.select();
		return false;
	}
	else if (iLen != null && txtRef.value.length > parseInt(iLen)) {
		alert("The field \"" + strLabel + "\" is too long (" + txtRef.value.length + " chars).\Please reduce the content to " + iLen + " chars.");
		txtRef.focus();
		txtRef.select();
		return false;
	}
	return true;
}

function checkNumber(txtRef, strLabel, allowFloat)
{
	var i;
	if (allowFloat) {
		for (i = 0; i < txtRef.value.length; i++){
			var arra = txtRef.value.split(".");
			if (arra.length > 2) {
				alert("Extra decimal divider characters in \"" + strLabel + "\" detected.\nThis field should contain digits and/or dot only.");
				txtRef.focus();
				txtRef.select();
				return false;
			}
			else if (!( txtRef.value.charAt(i) >= "0" && txtRef.value.charAt(i) <= "9" || txtRef.value.charAt(i) == ".")) {
				alert("Invalid characters in \"" + strLabel + "\" detected.\nThis field should contain digits and/or dot only.");
				txtRef.focus();
				txtRef.select();
				return false;
			}
		}
	}
	else {
		for (i = 0; i < txtRef.value.length; i++){
			if (!(txtRef.value.charAt(i) >= "0" && txtRef.value.charAt(i) <= "9")) {
				alert("Invalid characters in \"" + strLabel + "\" detected.\nThis field should contain digits only.");
				txtRef.focus();
				txtRef.select();
				return false;
			}
		}
	}
	return true;
}

// expects string as first parameter
function checkNumberModA(strValue, strLabel, allowFloat)
{
	var i;
	if (strValue == "") return false;

	if (allowFloat) {
		for (i = 0; i < strValue.length; i++){
			var arra = strValue.split(".");
			if (arra.length > 2) {
				return false;
			}
			else if (!( strValue.charAt(i) >= "0" && strValue.charAt(i) <= "9" || strValue.charAt(i) == ".")) {
				return false;
			}
		}
	}
	else {
		for (i = 0; i < strValue.length; i++){
			if (!(strValue.charAt(i) >= "0" && strValue.charAt(i) <= "9")) {
				return false;
			}
		}
	}
	return true;
}

function checkEmail(strValue, strLabel) // expect data in format nnn@nnn.nnn
{
	var objRegExp = new RegExp("^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+[.][A-Za-z]+$");
	 
	if (objRegExp.test(strValue))
		return true;
	alert("Invalid email address in the field \"" + strLabel + "\".");
	return false;
}


function validateFormExModA(frmRef)
// extends validateFormEx. also invokes custom validator specified by "validator" expando
// custom validator must comply with format:
// function customValidator(strValue, strFieldLabel) { return true/false; }
{
	for (var i = 0; i < frmRef.elements.length; i++)
		if (frmRef.elements[i].required || (frmRef.elements[i].value && frmRef.elements[i].value.length > 0)) {
			if (frmRef.elements[i].validator) {
				if (!eval(frmRef.elements[i].validator + "(frmRef.elements[i].value, frmRef.elements[i].required)")) {
					frmRef.elements[i].focus();
					if (frmRef.elements[i].select)
						frmRef.elements[i].select();
					return false;
				}
			}
			else if (frmRef.elements[i].type.indexOf("select") != -1 && frmRef.elements[i].required != null) {
				var valua;
				var iInd = frmRef.elements[i].selectedIndex;

				if (iInd < 0)
					valua = "";
				else
					valua = frmRef.elements[i].options[iInd].value;

				if (valua == "" || valua == "0") {
					alert("Please select a value from \"" + frmRef.elements[i].required + "\".");
					frmRef.elements[i].focus();
					return false;
				}
			}
			else if (!checkText(frmRef.elements[i], frmRef.elements[i].required, frmRef.elements[i].maxlength))
				return false;
		}
	if (frmRef.datIsCool)
		frmRef.datIsCool.value = "Yes";
	return true;
}
