
// ###################################################################
//  this file contains all the user defined validation routines
//  you are free to add routines to this file *IF* you think
//  the routines will be used by others.  if the routine is
//  specific to your page, please put the validation routine
//  directly on your page
//
//  the routines are broken up into groups by input type:
//		- text
//		- select
//		- radio
//		- etc
//
// ###################################################################


// ###################################################################
// these are the PASSWORD validation functions
// ###################################################################

// this should work just as well on regular text fields, but untried as of yet...
function VALIDpasswd(pass1, pass2FieldName) {
	if(pass1)
	{
		var pass2 = eval("document.theForm." + pass2FieldName);
		if(pass1.value == pass2.value) {
			return true;
		}
	}
	return false;
}

// ###################################################################
// these are the TEXTBOX validation functions
// ###################################################################

function VALIDmaxLength(field, theLengthToMatch) {
	if(field.value.length > theLengthToMatch) {
		return false;
	}
	return true;
}

function VALIDminLength(field, theLengthToMatch) {
	if(field.value.length < theLengthToMatch) {
		return false;
	}
	return true;
}

// works on any string
function VALIDnotEquals(text, valueToMatch) {
	if(text.value != valueToMatch) {
		return true;
	}
	return false;
}

function VALIDtoUpperCase(textField, ignoreMe) {
	textField.value = textField.value.toUpperCase();
	return false;
}


function VALIDnotEqualsIgnoreCase(text, valueToMatch) {
	if(text.value.toUpperCase() != valueToMatch.toUpperCase()) {
		return true;
	}
	return false;
}


//  works on any string
function VALIDequals(text, valueToMatch) {
	if(text.value == valueToMatch) {
		return true;
	}
	return false;
}

function VALIDequalsIgnoreCase(text, valueToMatch) {
	if(text.value.toUpperCase() == valueToMatch.toUpperCase()) {
		return true;
	}
	return false;
}



// only works on numeric srrings 
function VALIDgreaterThan(text, valueToMatch) {
	if (testIsNumeric(text.value)) {
		if (parseFloat("0" + text.value) > parseFloat("0" + valueToMatch)) {
			return true;
		}
		return false;
	}
}

// only works on numeric strings (validate number first)
function VALIDlessThan(text, valueToMatch) {
	if (testIsNumeric(text.value)) {
		if (parseFloat("0" + text.value) < parseFloat("0" + valueToMatch)) {
			return true;
		}
		return false;
	}
}

//  check for legal characters (in id's etc)...
//  this is basically the opposite of VALIDillegalChars.
//  here, if a character does NOT match one of the valid characters, it returns false.
function VALIDlegalChars(field, legalChars) {
	var testString = field.value;
outside:
	for (n = 0; n < testString.length; n++) {
		for (o = 0; o < legalChars.length; o++) {
			if (testString.charAt(n) == legalChars.charAt(o)) {
				continue outside;
			}
		}
			return false;
	}
	return true;
}

//  check for illegal characters (in id's etc)
function VALIDillegalChars (field, illegalChars) {

	var testString = field.value;
	for (z = 0; z < testString.length; z++) {
		for (q = 0; q < illegalChars.length; q++) {
			if (testString.charAt(z) == illegalChars.charAt(q)) {
				return false;
			}
		}
	}
	return true;
}

//	default values for VALIDlegalChars...
//  a-z, 0-9, and _
//	if third parameter is true, then include uppercase characters.  if false, do NOT allow uppercase characters.

function VALIDdefaultLegalChars (field, uppercase) {
	defaultChars = "abcdefghijklmnopqrstuvwxyz1234567890_"
	if (uppercase) defaultChars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	if (VALIDlegalChars(field, defaultChars)) return true;
	return false;
}

//  -----
// CheckEmailAddress()
// This checks the email address for invalid characters, illogical
// formatting, and likely invalid usernames or domainnames. It returns
// a 'true' for addresses which pass the test and a 'false' for those
// which don't.
//  -----
function VALIDemail(field, ignoreMe) {

	username 		= field.value;
	startPos 		= 0;
	endPos   		= username.length;
	validFlag    	= false;
	dotFlag      	= false;
	invalidChars 	= false;
	countBefore	= 0;
	countAfter	= 0;

	// this list is used to check for characters not allowed in an email
	// address. You can add as many extra characters as you like to the list
	// and the function will then reject any address with one of the
	// specified characters.
	invalidCharList = " $^&*()+=\'\":;?";
	
	for(i = startPos; i<endPos; i++) {
		c = username.substring(i, i+1);

		// first perform the simple test
		if(c=="@") validFlag = true;

		if(validFlag == true && dotFlag == false) countAfter++;
		if(validFlag == false) countBefore++;

		// check for at least one dot after the @ sign
		if(validFlag==true && c==".") dotFlag = true;

		// now check for silly characters in the address
		for(ii = 0; ii < (invalidCharList.length - 1); ii++) {
			if(c == invalidCharList.substring(ii, ii+1)) invalidChars = true;
		}
	}

	// check the flags and counters to determine if we have a valid one
	if(countAfter == 1) {
 	// no 1 letter domain names
		return false;
	}
	if(countBefore == 1) {
	// no 1 letter usernames
		return false;
	}

	// this is the only valid pass
	if(validFlag == true && dotFlag == true && invalidChars == false) {
			return true;
	}
	// everything else is invalid
	return false;
}

function testIsNumeric(testString) {

	decimalCount = 0;
	//  return false if there is nothing in the field
	if (testString.length < 1) {
		return false;
	}
	// look at each letter in the text box
	for (i = testString.length; i >= 0 ; i--) {

	//  see if each letter is a '.' (decimal) - return false if there are more than one
		if (testString.charAt(i) == "." ) {
			if (decimalCount == 0) {
				decimalCount += 1;
				continue;
			} else {
				return false;
			}
		}

		//  see if each letter is a letter, and return false if a letter is found
		if (isNaN(testString.charAt(i))) {
			return false;
		}

		//  if the character is a space, return false
		if (testString.charAt(i) == ' ') {
			return false;
		}
	}

	// none of these things return false, so it must be true
	return true;
}

//	this method determines whether a field contains a valid price
//	i.e. a positive number with two or less digits after the decimal
//
// the parameter "changeIt" is a boolean value:
//
//	a value of *true* tells the method to round the field
//	to two decimal places (if it a positive number)...  
//	so a value of 653.3893 becomes 653.39, and
//	a value of 592 becomes 592.00
//
//	if "changeIt" is false, the method will round to two decimal places ONLY if
//	the value of the field is already two decimal places or less...
//	e.g. 893.94 stays 893.94
//	23 becomes 23.00
//	11.4 becomes 11.40
//	55.576 returns false on the validation - as if it was not a valid number
//	because it has three digits after the decimal (i.e. '576')
//
//	NOTE:  if you want a field to keep a value of 55.576 (i.e. not return false)
//	just use positiveNumber...  8^)

function VALIDprice(field, changeIt) {

//  do is numeric and all that good stuff for a positive number...
	if (!VALIDpositiveNumber(field, "ignoreMe")) {
		return false;
	}
	var stringy = field.value;
	var stringyNum = parseFloat(stringy);

	if (stringy.lastIndexOf(".") >= 0) {
		if(stringy.lastIndexOf(".")+3 < stringy.length && !changeIt) {
			return false;
		}
		var stringyNum100 = stringyNum * 100;
		stringyNum100 = Math.round(stringyNum100);
		stringyNum = stringyNum100/100;
	}
	stringy=stringyNum + "";

	if(stringy.lastIndexOf(".") == -1) {
		field.value = stringyNum + ".00";
		return true;
	}
	if(stringy.length - stringy.lastIndexOf(".") == 2) {
		field.value = stringyNum + "0";
		return true;
	}
	if(stringy.length - stringy.lastIndexOf(".") == 3) {
		field.value = stringyNum;
		return true;
	}
}

// -----
// positiveNumber()
// This script checks a string to ensure that the text contained within
// is numeric and positive.  It uses the format '123456.7890'.  It will
// not allow commas (,) or more than one deciaml point.  The script returns
// a 'true' for strings which pass the test and a 'false' for those
// which don't.
// -----
function VALIDpositiveNumber(field, ignoreMe) {
	var testString = field.value;
	var response;
	response = testIsNumeric(testString);
	if (!response) {
		return false;
	}
	return true;
}

// this is the same as positive number, but it makes sure in is an integer too...
function VALIDpositiveInteger(field, ignoreMe) {
	var testString=field.value;
	var response;
	response = testIsNumeric(testString);
	//  see if we got a number back - if not, return false
	if (!response) {
		return false;
	}
	// since we have a number, check for a decimal, and return false if we ever find one
	if (response) {
		for (h = 0; h < testString.length ; h++) {
			if (testString.charAt(h) == "." ) {
				return false;
			}
		}
	}
	//  we never found a '.' so we can return true
	return true;
}

function VALIDinteger(field, ignoreMe) {
	var testString=field.value;
	var response;

	//  we don't care if there's a '-'...  if there is, remove it and send the rest to isNumeric
	if (testString.charAt(0) == "-") {
		testString = testString.substring(1,testString.length);
	}
	response = testIsNumeric(testString);
	//  see if we got a number back - if not, return false
	if (!response) {
		return false;
	}
	// since we have a number, check for a decimal, and return false if we ever find one
	if (response) {
		for (h = 0; h < testString.length ; h++) {
			if (testString.charAt(h) == "." ) {
				return false;
			}
		}
	}
	//  we never found a '.' so we can return true
	return true;
}

//  checks to see if the string is a valid number (positive or negative, '.' or no '.')
function VALIDnumber(field, ignoreMe) {
	var testString = field.value;
	var response;
	//  we don't care if there's a '-'...  if there is, remove it and send the rest to isNumeric
	if (testString.charAt(0) == "-") {
		testString = testString.substring(1,testString.length);
	}
	response = testIsNumeric(testString);
	//  if we got a good response, return true
	if (response) {
		return true;
	}
	//  if not, return false
	return false;
}

// allows only negative numbers
function VALIDnegativeNumber(field, ignoreMe) {
	var testString = field.value;
	var response;
	if (testString.charAt(0) != "-") {
		return false;
	} else {
		testString = testString.substring(1,testString.length);
	}
	response = testIsNumeric(testString);
	//  if we got a good response, return true
	if (response) {
		return true;
	}
	//  if not, return false
	return false;
}

// string must be numeric, an integer, and negative
function VALIDnegativeInteger(field, ignoreMe) {

	var testString = field.value;
	var response;
	// if it doesn't start with '-', return false
	if (testString.charAt(0) != "-") {
		return false;
	} else {
		testString = testString.substring(1,testString.length);
	}
	response = testIsNumeric(testString);

	if (!response) {
		return false;
	}

	for (h = 0; h < testString.length ; h++) {
		if (testString.charAt(h) == "." ) {
			return false;
		}
	}
	//  we never found a '.' so we can return true
	return true;
}

// this method simply checks to see if there is anything entered into a field
function VALIDisNull(fieldValue, ignoreMe) {

	if (fieldValue) {
		if (fieldValue.value.length == 0) {
			return true;
		} else {
			return false;
		}
	}
}


// this method simply checks to see if there is anything entered into a field
function VALIDnn(fieldValue, spacesValid) {
	if (fieldValue) {
	var testString = fieldValue.value
		if (spacesValid == "spacesNotValid") {
			while (testString.charAt(0) == " ") {
				testString = testString.slice(1,fieldValue.value.length);
			}
		}
		if (testString.length == 0) {
			return false;
		} else {
			return true;
		}
	}
}

// tests to see wheather a field is a valid hex colour (eg #A1B2C3)
function VALIDhexColour (field, ignoreMe) {
	var testString = field.value.toUpperCase();
	validCharList = "ABCDEF";
//  if the string doesn't start with a '#', return false
	if (testString.charAt(0) != "#") {
		return false;
	}

//  the string should be 7 digits long (one '#' and three sets of two characters)
	if (testString.length != 7) {
		return false;
	}

//  look at each character
	for (a = 1; a < testString.length; a++) {
		var testChar = testString.charAt(a);
		var found = false;
//  if it is numberic (0-9), it's okay and go to the next number
		if (testIsNumeric(testChar)) {
//			for (h = 0; h < testString.length ; h++) {
				if (testChar == "." ) {
					return false;
				}
//			}
			continue;
		}

//  the char isn't numeric, so check if it is 'A-F'
		for(x = 0; x < (validCharList.length); x++) {
//  if it isn't 'A-F', return false
			if(testChar == validCharList.substring(x, x+1)) {
				found = true;
				break;
			}
		}
		if (!found) {
			return false;
		}
	}
//  we never returned false, so it must be true
	return true;
}

function VALIDresellerHexColour(field, ignoreMe) {
	var testString = field.value;

	if (testString == "null") {
		return true;
	}

	if (testString == "") {
		return true;
	}

	if ((VALIDhexColour(field, ignoreMe))) {
		return true;
	}

	return false;
}


// ###################################################################
//  these are the functions that work on SELECT statements
// ###################################################################

// a debugging method that alerts the selected indexes from a select statement
function alertSelectValues(cboParameter) {
	var foundIndex;
	var foundIndexString = "";
	foundIndex = getComboValues(cboParameter);
	if (foundIndex.length >= 1) {
		for (a = 0; a < foundIndex.length; a++) {
			foundIndexString += "\n" + foundIndex[a];
		}
	} else {
		foundIndexString = "none";
	}
	alert("the matching index(es) are: " + foundIndexString);
}

// -----
// these 4 methods define the minimum, maximum and exact number of selctions from a select box
// -----

function VALIDminNumberOfItems(field, counter) {
	return (field.length>=counter);
}

//  returns true if there are 'X' or more number of selections made from a SELECT statement
function VALIDminNumberOfSelects(cboParameter, numberOfSelections) {
	var numberSelected;
	numberSelected = getSelectValues(cboParameter).length;

	if (numberOfSelections > numberSelected) {
		return false;
	}
	return true;
}

//  returns true if there are 'X' or less number of selections made from a SELECT statement
function VALIDmaxNumberOfSelects(cboParameter, numberOfSelections) {
	var numberSelected;
	numberSelected = getSelectValues(cboParameter).length;
	if (numberOfSelections < numberSelected) {
		return false;
	}
	return true;
}

//  returns true if there are exactly 'X' number of selections made from a SELECT statement
function VALIDexactNumberOfSelects(cboParameter, numberOfSelections) {
	var numberSelected;
	numberSelected = getSelectValues(cboParameter).length;
	if (numberOfSelections == numberSelected) {
		return false;
	} 
	return true;
}

//  returns true if the index selected from a select group matches the parameter
function VALIDselect(Parameter, desiredIndex) {
	var foundIndex;
	var foundIndexString = "";
	foundIndex = getSelectValues(Parameter);
	if (foundIndex.length >= 1) {
		for (a = 0; a < foundIndex.length; a++) {
			if (desiredIndex == foundIndex[a]) {
				return true;
			} else {
				foundIndexString = -1;
			}
		}
	}
	return false;
}

function getSelectValues(Parameter) {
	var array = new Array();
	for (r = 0; r < Parameter.length; r++) {
		if (Parameter[r].selected == true) {
			array[array.length] = r;
		}
	}
	return array;
}

// ###################################################################
//  these are the CHECKBOX methods
// ###################################################################

//  returns true if a checkbox is true
function VALIDcheckBoxTrue(getData, ignoreMe) {
	if (getData.checked == true) {
		return true;
	} else {
		return false;
	}
}

//  returns true if a checkbox is false
function VALIDcheckBoxFalse(getData, ignoreMe) {
	if (getData.checked == false) {
		return true;
	} else {
		return false;
	}
}

function getAllCheckedValues(checkGroup) {
//	alert(checkGroup.length);
	var checkedArray = new Array();
	for (b = 0; b < checkGroup.length; b++) {
		if (checkGroup[b].checked) {
			checkedArray[checkedArray.length] = checkGroup[b].value;
		}
	}
	return checkedArray;
}

// ###################################################################
//  these are the RADIO GROUP methods
// ###################################################################

// tests to see if any of the radio buttons are selected
function VALIDradioSelected (buttonGroup, ignoreMe) {
	if (getRadioValue(buttonGroup) < 0) {
		return false;
	}
	return true;
}

//  tests to see if the value selected on the form matches a predefined value (ie the parameter)
function VALIDradioValue(buttonGroup, valueToMatch) {
	returnValue = getRadioValue(buttonGroup);
	if (returnValue == valueToMatch) {
		return true;
	}
	return false;
}

function getRadioText(buttonGroup) {

	returnValue = getRadioValue(buttonGroup);
	return buttonGroup(returnValue).value;
}

//  gets the value of a radio group and returns it as an integer
function getRadioValue(buttonGroup) {
	var selectedIndex;
	for (i=0; i< buttonGroup.length; i++) {
		if (buttonGroup[i].checked == true) {
			selectedIndex = i;
		}
	}
	if (selectedIndex >= 0) {
		return selectedIndex;
	} else {
		return -1;
	}
}

//  a degugging function to alert the selected value (index) of a radio group
function testRadio(buttonGroup) {
	alert(getRadioValue(buttonGroup));
}


//####################################
// weird validations...
//####################################

function VALIDOtherValidation(firstField, secondFieldName) {

	var secondField = eval("document.forms[0]." + secondFieldName);
	var secondFieldValue;
	if (secondField.value) {
		secondFieldValue = secondField.value;
	} else {
		secondFieldValue = "";
	}
	var firstFieldType = firstField[0].type;

	var OthersArray = new Array();
	OthersArray[0] = "Other";
	OthersArray[1] = "other";

// this validates for radio buttons...
	if (VALIDradioSelected (firstField, "ignoreMe")) {
		if (firstFieldType == "radio") {
		var firstFieldValue = getRadioText(firstField);
			for (i = 0; i < OthersArray.length; i++) {
//	if they match, keep secondField value...
				if (OthersArray[i] == firstFieldValue) {
					return true;
				}
			}
//	else, make 2ndField value = 1stField value
				secondField.value = firstFieldValue;
		}
//	end radio validation and begin validation for checkboxes
	} else if (firstFieldType == "checkbox") {
		var allChecked = getAllCheckedValues(firstField);
		if (allChecked.length >= 0) {
			var foundString = "";
			var toAdd = ""
//	get an array of all selected checkboxes
			for (f = 0; f < allChecked.length; f++) {
				for (m = 0; m < OthersArray.length; m++) {
					if (allChecked[f] == OthersArray[m]) {
						toAdd = secondFieldValue;
						break;
					}
				toAdd = allChecked[f];
				}
				if (f == 0) {
					foundString = toAdd;
				} else {
					foundString += "," + toAdd;
				}
			}
			secondField.value = foundString;
			}
		// end checkbox validation ad begin select
		}	else {

	var firstFieldValue = getSelectValues(firstField);
		if (firstFieldValue.length >= 0) {
			var foundStringy = "";
			var toAddy = ""
//	get an array of all selected 
			for (r = 0; r < firstFieldValue.length; r++) {
				for (mr = 0; mr < OthersArray.length; mr++) {
					if (firstField[firstFieldValue[r]].value == OthersArray[mr]) {
						toAdd = secondFieldValue;
						break;
					}
				toAdd = firstField[firstFieldValue[r]].value;
				}
				if (r == 0) {
					foundString = toAdd;
				} else {
					foundString += "," + toAdd;
				}
			}
			secondField.value = foundString;
			}
		// end checkbox validation ad begin select
	}		//	end radio validation

	return false;

}

function VALIDbankAccountNumber(field, ignoreMe) {

    var fieldvalue  = new String(field.value);
    var fieldsize   = field.value.length;
    
    if (VALIDpositiveInteger(field, ignoreMe) == false) 
    	return false;
    	
    if(fieldsize == 9) {
		return VALIDelevenProof(field, ignoreMe);
	}
	return true;
}

function VALIDelevenProof(field, ignoreMe) {

    var fieldvalue  = new String(field.value);
    var fieldsize   = field.value.length;

    if (VALIDpositiveInteger(field, ignoreMe) == false) 
    	return false;

    if(fieldsize == 9) 
    {
		var total = 0;
		for(i=1; i<=fieldsize; i++) {
			total = total + (parseInt(fieldvalue.substr(fieldsize-i, 1)))*i;
		}
	
		if(total>0 && (total%11)==0) {
			return true;
		}
		else {
			return false;
		}
    }
    return true;        
}                               

// EOF


