// File:			formValidation.js
// Version:			1.2.6
// Creation Date:	12/09/2002
// Author:			Kiora, Inc
// Disclaimer:		The Code in this page is property of Kiora, Inc and cannot be used 
//					in part or total without express consent from Kiora's principals.
//					To request permission to use this file email support@kiora.com

// -- Declare Regular Expression Variables
var emailRegExp = /^([a-zA-Z0-9_\-\.]+)@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$/;
var phoneRegExp = /^((\([1-9]{1}\d{2}\))|([1-9]{1}\d{2})){1}[\s|\-]?\d{3}[\s|\-]?\d{4}$/;
var phoneRegExp = /^.*$/;
var numbersRegExp = /^\d+$/;								// -- Any combination of numbers. Can start with one or more leading 0
var integerRegExp = /(^[0]$)|(^-?[1-9](\d{1,8})?$)/;
var smallIntRegExp = /(^[0]$)|(^-?[1-9]{1}(\d{1,3})?$)|(^-?[1-2]{1}\d{4}$)|(^-?[3]{1}[0-1]{1}\d{3}$)|(^-?[3]{1}[2]{1}[0-6]{1}\d{2}$)|(^-?[3]{1}[2]{1}[7]{1}[0-5]{1}\d{1}$)|(^-?[3]{1}[2]{1}[7]{1}[6]{1}[0-7]{1}$)/;
var tinyIntRegExp = /(^[0]$)|(^[1-9]{1}(\d{1,2})?$)|(^[2]{1}[0-4]{1}\d{1}$)|(^[2]{1}[5]{1}[0-5]{1}$)/;
var decimalRegExp = /^-?(((\d+(\.\d{1,3})?)|((\d*\.)?\d{1,3})))$/;
var dateRegExp = /^((0?[1-9]{1})|(1{1}[0-2]{1})){1}\/{1}((0?[1-9]{1})|([1-2]{1}\d{1})|(3{1}[0-1]{1})){1}\/{1}([1-9]{1}\d{3}){1}$/;
var timeRegExp = /^((0|1){1}\d{1}\:{1}(0|1|2|3|4|5){1}\d{1})|(2{1}(0|1|2|3){1}\:{1}(0|1|2|3|4|5){1}\d{1})$/;
var allowAllRegExp = /^.*$/;
var removeSpacesRegExp = /^(\s*)$/;
var leaveNumbersRegExp = /\D/g;
var takeOutSglQuotes = /\'|\‘|\’/g;
var takeOutDblCurlyQuotes = /\“|\”/g;

// -- Format phone number
function phoneFormat(str){
	if((trimNNChars(str.value)).length == 10){
	    sTemp = "(" + sTemp.substring(0, 3) + ") " + sTemp.substring(3, 6) + "-" + sTemp.substring(6, 10);
		str.value = sTemp ;
		return true;
	}
	return false;
}

// -- Date leap year test
function validateDate(inputStr){
	var delim1 = inputStr.indexOf("/");
	var delim2 = inputStr.lastIndexOf("/");
	var mm = parseInt(inputStr.substring(0,delim1),10);
	var dd = parseInt(inputStr.substring(delim1 + 1,delim2),10);
	var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10);
	if (mm == 2) {
		if (yyyy % 4 > 0 && dd > 28) {
			return false;
		} else if (dd > 29) {
			return false;
		}
		return true;
	}
	return true;
}

// -- Remove all spaces
function trimAll(strValue){
	return strValue.replace(removeSpacesRegExp, '');
}

// -- Replace all single quotes
function rplcSnglQuotes(strValue){
	return strValue.replace(takeOutSglQuotes, '`');
}

// -- Replace all double curly quotes
function rplcDblCurlyQuotes(strValue){
	return strValue.replace(takeOutDblCurlyQuotes, '"');
}

// -- Remove all non-numeric characters
function trimNNChars(strValue){
	return strValue.replace(leaveNumbersRegExp, '');
}

// -- Check For Valid Data Type
function validate(str, regx, req){
	if((trimAll(str.value)).length == 0){				// -- Is it empty?
		str.value='';
		if (req == '1'){								// -- If required return false, else return true
			return false;
		}
		return true;
	}
	result = eval(regx).test(str.value);												// -- Not empty. Run data type specific validation.
	str.value = rplcSnglQuotes(str.value);												// -- Replace all single quotes with an acute accent.
	str.value = rplcDblCurlyQuotes(str.value);											// -- Replace all double curly quotes with simple double quotes.
	//if (regx == 'phoneRegExp' && result){												// -- If data type is Phone...
	//	return phoneFormat(str);
	//}else if (regx == 'dateRegExp' && req == '0' && str.value == 'mm/dd/yyyy'){		// -- If data type is Date and not required...
	if (regx == 'dateRegExp' && req == '0' && str.value == 'mm/dd/yyyy'){				// -- If data type is Date and not required...
		return true;
	}else if (regx == 'dateRegExp' && result){											// -- If data type is Date...
		return validateDate(str.value);
	}
	return result;
}

// -- Check For length of General Text
function validateTxtLen(str, maxLen, req){
	if((trimAll(str.value)).length == 0){				// -- Is it empty?
		if (req == '1'){								// -- If required return false, else return true
			return false;
		}
		str.value='';
		return true;
	}
	if(str.value.length > maxLen){						// -- Not empty, is lenght higher than allowed max lenght
		return false;
	}
	str.value = rplcSnglQuotes(str.value);				// -- Replace all single quotes with an acute accent.
	str.value = rplcDblCurlyQuotes(str.value);			// -- Replace all double curly quotes with simple double quotes.
	return true;
}

// -- Check to meake sure one checkbox or one radio is checked
function oneIsChecked(object){
	if (object.length){									// -- If there are multiple objects with the same name
		for (i=0; i<object.length; i ++){
			if (object[i].checked){
				return true;
			}
		}
	}else{												// -- If there is only one
		if (object.checked){
			return true;
		}
	}
	return false;
}

// -- Check to meake sure one Option on a select box is selected
function oneIsSelected(object){
	if (object.selectedIndex != '-1'){									// -- If there is one selected option
		return true;
	}
	return false;
}

// -- Check for apropriate file extension on a file input field
function checkFileExt(str, regx, req){
	if((trimAll(str.value)).length == 0){				// -- Is it empty?
		if (req == '1'){								// -- If required return false, else return true
			return false;
		}
		str.value='';
		return true;
	}
	if (str.value.lastIndexOf('.')){
		lastDot = str.value.lastIndexOf('.') + 1;					// -- Find the last dot in the string
		fileExt = str.value.substring(lastDot, str.value.length);	// -- Get the file extension
		return eval(regx).test(fileExt.toLowerCase());				// -- Match the file ext against the valid ones
	}
	return false;
}

// -- Display Error message
function dspError(msg, fld){
	alert(msg);
	if (fld.type){
		fld.focus();
		if (fld.type == 'text'){
			fld.select();
		}
	}
	return false;
}