<!--

showFormAlerts = true;

////////////////////////////////////
// INDIVIDUAL CHECKING FUNCTIONS
////////////////////////////////////
function checkForChars(theString, checkChars){
	if (theString.match(checkChars)) {
		return true;
	} else {
	 return false;
	}
}


function validateText(elValue, conA, conB, conC) {
	var rtnTextOkay = true;

	// conA = text to check for match
	// conB = "case" = match case, else not
	// conC = "not" = invert check (ie. NOT equal to...)	

	//Put in temporarily since it .toLowerCase seems to match everything! 
	conB = 'case';
		
	/*
	var textOne = elValue;
	textOne = textOne.toLowerCase;
	var textTwo = conA;
	textTwo = conA.toLowerCase;
	
	alert (textOne + ' == ' + textTwo + ' : ' + (textOne == textTwo));
	*/
	
	//See if we are to compare case or not
	if (conB == 'case') {
		if (conC = 'not') {
			if (elValue == conA) { if (showFormAlerts) { alert ('You must enter something.'); rtnTextOkay = false; } }
			else { rtnTextOkay = true; }
		} else {
			if (elValue != conA) { if (showFormAlerts) { alert ('You must enter ' + conA + '.'); rtnTextOkay = false; } }
			else { rtnTextOkay = true; }
		}
	} else {
		if (elValue.toLowerCase != conA.toLowerCase) { if (showFormAlerts) { alert ('You must enter ' + conA + '.'); rtnTextOkay = false; } }
		else { rtnTextOkay = true;}
	}
	
	return rtnTextOkay;
}



function validatePhone(elValue, conA, conB, conC) {
	var rtnTextOkay = true;

	// conA, conB, conC = not currently used	
	
	if (elValue == '' || elValue.length < 6) {
		rtnTextOkay = false;
		alert('Please enter a telephone number.');
	} else {

		//For each character in the string:
		for (var i=0; i < elValue.length; i++) {
			//If the character is less than 0 and greater than 9...
			if ((elValue.substring(i, i+1) < '0') || (elValue.substring(i, i+1) > '9')) {
				//... then it is false UNLESS it contains one of these characters which are permitted.
				if (checkForChars(elValue.substring(i, i+1), '[+()]')) {
					rtnTextOkay = true; 
				} else {
					rtnTextOkay = false; 
					alert('Please enter only numbers and the following symbols: + ( ).');
					//Break so we don't repeat the error for each incorrect character encountered.
					break;
				}
			} else { 
				rtnTextOkay = true }
		}
	}

	// '/[\!\@\#\$\%\^\&\*]/'
	
	return rtnTextOkay;
}



function validateEmail(elValue, conA, conB, conC) {
	var rtnTextOkay = true;

	// conA, conB, conC = not currently used	
	
	if (elValue == '') {
		rtnTextOkay = false;
		alert('Please enter your email address.');
	} else {
		if (checkForChars(elValue, '[@]') && checkForChars(elValue, '[.]')) {
			rtnTextOkay = true; 
		} else {
			rtnTextOkay = false;
			alert('Please check your email address.');
		}
	}

	// '/[\!\@\#\$\%\^\&\*]/'
	
	return rtnTextOkay;
}


function validateInt(elValue, conA, conB, conC) {
	var rtnIntOkay = true;
	
	elValue = parseInt(elValue);

	if (conA == null || conA == '') conA = 0;
	if (conB == null || conB == '') conB = 32767;
	
	if (isNaN(elValue)) {
		if (showFormAlerts) { alert ('Not a number'); rtnIntOkay = false; }
	} else {
	//Now we check it's really an INT and meets our conditions
		if (elValue % 1 != 0) {
			if (showFormAlerts) { alert ('Not an integer'); rtnIntOkay = false; }
		} else { //Number IS an integer. Now check conditions...
			if (elValue < conA || elValue > conB) { 
				if (showFormAlerts) { alert ('Number must be between ' + conA + ' and ' + conB + '.'); rtnIntOkay = false; } }
		}
	}
	
	return rtnIntOkay;
}

function validateLong(elValue, conA, conB, conC) {
	var rtnIntOkay = true;
	
	if (conA == null || conA == '') conA = 0;
	if (conB == null || conB == '') conB = 2147483647;
	
	if (isNaN(elValue)) {
		if (showFormAlerts) { alert ('Not a number'); rtnIntOkay = false; }
	} else {
	//Now we check it meets our conditions
		if (elValue < conA || elValue > conB) { 
			if (showFormAlerts) { alert ('Number must be between ' + conA + ' and ' + conB + '.'); rtnIntOkay = false; } }
	}
	
	return rtnIntOkay;
}

function validateDouble(elValue, conA, conB, conC) {
	var rtnIntOkay = true;
	
	if (conA == null || conA == '') conA = 0;
	if (conB == null || conB == '') conB = 1.79769313486231e308;
	// (max negative number is –4.94065645841247E–324
	
	if (isNaN(elValue)) {
		if (showFormAlerts) { alert ('Not a number'); rtnIntOkay = false; }
	} else {
	//Now we check it meets our conditions
		if (elValue < conA || elValue > conB) { 
			if (showFormAlerts) { alert ('Number must be between ' + conA + ' and ' + conB + '.'); rtnIntOkay = false; } }
	}
	
	return rtnIntOkay;
}

function validateDate(elValue, conA, conB, conC) {
	var rtnDateOkay = true;
	
	//dates are of the format dd/mm/yy
	if (conA == null || conA == '') conA = '01/01/1500';
	if (conB == null || conB == '') conB = '31/12/9999';

	// The first and last '/' opens & closes the search string,
	// the '\D' specified to search for any non-numeric character.
	var delimeter = /\D/;
	var dateArray = elValue.split(delimeter);

	rtnDateOkay = validDate(dateArray[2],dateArray[1],dateArray[0]);

	if (rtnDateOkay == false) {
		alert('Not a valid date. Please enter dates in the following format: dd/mm/yyyy.');
	};

	return rtnDateOkay;
}


///////////////////////////////////////
// END OF INDIVIDUAL CHECKING FUNCTIONS
///////////////////////////////////////

function validateMe(elValue, elType, conA, conB, conC, elName) {
	/*
	conA = first constraint
	conB = second constraint
	conC = third constraint (not used - left here if needed in the future)
	       It could be used to pass custom error messages.
	
	elName = used to set cell colour on errors
	elValue = value from the element (using this.value)
	elType = element type. One of the following...
	
	text: conA = 'the Text you waNt tO maTcH', conB = case senstive or not (use 'case' for case sensitive or leave blank).
	int: conA = +ve or -ve integer, conB +ve or -ve integer. If blank, conA is 0 and conB is +ve integer max.
	double: (as int, above)
	date: Will automatically verify it is a valid date. conA = lower date range, conB = upper date range or both blank to accept any date.
	*/

	isValid = true;

	switch (elType) {
		case 'text' :
			//Check if conA is empty. If if is then we have nothing to check.
			if (conA != null) {
				isValid = validateText(elValue, conA, conB, conC);
			}
			break;
		case 'email':
			isValid = validateEmail(elValue, conA, conB, conC);
			break;
		case 'phone':
			isValid = validatePhone(elValue, conA, conB, conC);
			break;
		case 'int':
			isValid = validateInt(elValue, conA, conB, conC);
			break;
		case 'long':
			isValid = validateDouble(elValue, conA, conB, conC);
			break;
		case 'double':
			isValid = validateDouble(elValue, conA, conB, conC);
			break;
		case 'date':
			isValid = validateDate(elValue, conA, conB, conC);
			break;
		default:
			alert ('Error - incorrect check type encountered');
			break;
	
	}
	

	if (!isValid) {
		document.all[elName].style.background = '#FFCCCC'; 
		document.all[elName].focus();
	}
	else { document.all[elName].style.background = ''; }
	
	return isValid;

}



function validateForm(theForm) {
	//Set an attribute for this form and set it to TRUE since we will be using optimistic checking.
	formValid = true;
	
	var objEle = theForm.tags("input");
	var elValue, elType, conA, conB
	
	//Cycle through each element in the form:
	for (i = 0, n = objEle.length; i < n; i++) {
		elType = objEle[i].getAttribute("checkType");
		
		//If the attribute exists then we know something has to be checked
		if (elType != null) {
			elValue = objEle[i].value;
			elType = objEle[i].getAttribute("checkType");
			conA = objEle[i].getAttribute("checkConA");
			conB = objEle[i].getAttribute("checkConB");
			conC = objEle[i].getAttribute("checkConC");
			
			//The next lines sets whether the over all form is valid or not.
			//If there is a single error (ie. FALSE is returned), then any amount
			//of TRUEs and a FALSE is always FALSE.
			formValid = formValid && validateMe(elValue, elType, conA, conB, conC, objEle[i].name);
		}
	}
	
	return formValid;
}

//-->