/*

****************************************************************************************************
****************************************************************************************************

Author: Stuart Vaughan
Date: 19/06/2002
Version: 1.00
Desc:
This file has been put together in the attempt to start a Javascript library for Citrus Internet.

Author: Steve McDonald
Date: 17/09/2002
Version: 1.10
Desc:
The file was updated so the CheckField function is now called with an integer relating to the type
of check to be performed.

Author: Steve McDonald
Date: 01/05/2003
Version: 1.20
Desc:
Added 5 more check types (see checkfield documentation below).
Modified checkselect to include validation for select multiple. 
Added maxLength function for mimicking maxlength attrubite of input types of text with textarea controls.
Usage: <textarea onkeypress="maxLength(this, 2000);" onblur="maxLength(this, 2000);">

Author: Steve McDonald
Date: 07/08/2003
Version: 1.30
Desc:
Modified CheckSelect to include a third parameter for specifing minimum number of required selections with select-multiple controls.
Note: this parameter is optional and can be excluded from the parameter list.

Author: Steve McDonald
Date: 08/08/2003
Version: 1.30
Desc:
Removed zero length check from CheckInt. If numeric value required in field then use both CheckInt and CheckNotEmpty

Author: Steve McDonald
Date: 14/08/2003
Version: 1.30
Desc:
Corrected the english handling for the error message in CheckRadio.
Reformatted the version history for easier updating.
Also reformatted some of the code I'd previously added.
Reduced the amount of code required in main CheckField function.

Author: Steve McDonald
Date: 9/9/2003
Version: 1.30
Desc:
Added focus() to CheckRadio() when returning false.
Added handling in CheckCheckBox() for an array of checkbox controls with the same name.

Author: Steve McDonald
Date: 18/9/2003
Version: 1.30
Desc:
Fixed bug in CheckField() switch() statement - case 11 missing break;

****************************************************************************************************
****************************************************************************************************

*/

// ****************************
// START - FORM VALIDATION CODE
// ****************************

// CheckField: Top Level JS function for form validation.
// First argument passed must be one of these values:
//
// 1 - Check for zero length
// 2 - Check for valid email
// 3 - Check for integer value
// 4 - Check for radio button selected
// 5 - Check for select option selected
// 6 - Check for zero length text (formatting excluded) in Active-X instance
// 7 - Check for 2 equal values
// 8 - Check for specific length
// 9 - Check to ensure only alpha-numeric characters being used
// 10 - Check to ensure checkbox is selected
//
// Typically the second argument is the form element object
// while the third argument is a string related to the error
// message that will be displayed, however CheckField() allows
// for a variable number of arguments to be passed for custom
// checking types. Add new second level functions and respective
// case statements to CheckField() as required.

function CheckField() {
	switch (arguments[0]) {

		case 1 : // check for zero length
			return CheckNotEmpty(arguments[1], arguments[2]); break;

		case 2 : // check for valid email
			return CheckEmail(arguments[1], arguments[2]); break;

		case 3 : // check for integer value (second level function not yet written)
			return CheckInt(arguments[1], arguments[2]); break;

		case 4 : // check for radio button selected
			return CheckRadio(arguments[1], arguments[2]); break;

		case 5 : // check for select option selected
			arguments[3] = !arguments[3] ? '' : arguments[3];
			return CheckSelect(arguments[1], arguments[2], arguments[3]); break;

		case 6 : // check for zero length text in Active-X instance
			return CheckActiveX(arguments[1], arguments[2]); break;

		case 7 : // check for equality
			return CheckEquality(arguments[1], arguments[2], arguments[3], arguments[4]); break;

		case 8 : // check for specified length
			return CheckSetLength(arguments[1], arguments[2], arguments[3], arguments[4]); break;

		case 9 : // check for alpha-numeric chars only
			return CheckAlphaNumeric(arguments[1], arguments[2]); break;

		case 10 : // check for checkbox selected
			return CheckCheckBox(arguments[1], arguments[2]); break;

		case 11 : // check select has options
			return CheckOptions(arguments[1], arguments[2], arguments[3], arguments[4]); break;

		default:
			return false;

	}
}

function CheckNotEmpty (oCtl, sName)
{
	if (oCtl.value.length == 0)
	{
		alert ("Please enter " + sName + ".");
		// Check if the Control Can accept the focus
		if (!oCtl.disabled&&oCtl.type!='hidden')
		{
			oCtl.focus();
		}
		return false;
	}
	else
	{
		return true;
	}
}

function CheckEmail (oCtl, sName)
{
// Checks whether sEmail contains a valid email address
	var strEmailAddress = oCtl.value
	if (strEmailAddress == "" || strEmailAddress.length > 50 || !(strEmailAddress.match(new RegExp("^[^@\ ]+@[0-9A-Za-z\-\_]+([\.]([0-9A-Za-z\-\_]{1,}))+$"))))
	{
		alert ("Please enter " + sName);
		oCtl.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function CheckInt(oCtl, sName) {
	if (isNaN(oCtl.value)) {
		alert('Please ensure that ' + sName + ' is numeric');
		oCtl.focus();
		return false;
	} else {
		return true;
	}
}

function CheckSelect(oCtl, strName, intMin) {

	var intSel = 0;
	for (var i = 0; i < oCtl.options.length; i++) {
		if (oCtl.options[i].selected) intSel++;
	}

	if ((oCtl.selectedIndex == 0 && oCtl.type == 'select-one') || ((oCtl.selectedIndex == -1 || (intMin.toString().length > 0 && intMin > 1 && intSel < intMin)) && oCtl.type == 'select-multiple')) {
		alert ('Please choose' + (intMin.toString().length > 0 ? ' at least ' + intMin + ' item' + (intMin > 1 ? 's' : '') : '') + ' from the ' + strName + ' list.');
		oCtl.focus();
		return false;
	} else {
		return true;
	}

}

function CheckRadio (oCtl, sName)
{
	var bExist;

// Check if an Option is selected
	bExist = false;
	for (i=0; i < oCtl.length;i++)
	{
		if (oCtl[i].checked)
		{
			bExist = true;
		}
	}
// Check if on a MAC
	if ((navigator.userAgent.indexOf('Mac')!=-1))
	{
		bExist = true;
	}
// OK
	if (!bExist)
	{
		alert('Please choose a' + ('aeiou'.indexOf(sName.charAt(0).toLowerCase()) > -1 ? 'n ' : ' ') + sName);
		if (oCtl.length) {
			oCtl[0].focus();
		} else {
			oCtl.focus();
		}
		return false;
	}
	else
	{
		return true;
	}
}

function CheckActiveX(intEditLiveInstance, sName) {
	if (eval('editLive' + intEditLiveInstance + '.getText().length == 0')) {
		alert('Please enter ' + sName + '.');
		return false;
	} else {
		return true;
	}
}

function CheckEquality(oCtl1, oCtl2, sName1, sName2) {
	if (oCtl1.value != oCtl2.value) {
		alert('Please ensure ' + sName1 + ' and ' + sName2 + ' contain the same value');
		oCtl1.focus();
		return false;
	} else {
		return true;
	}
}

function CheckSetLength(oCtl, iMin, iMax, sName) {
	if (oCtl.value.length < iMin || oCtl.value.length > iMax) {
		if (iMin == iMax) {
			alert('Please ensure ' + sName + ' is ' + iMin + ' characters in length');
		} else {
			alert('Please ensure ' + sName + ' is between ' + iMin + ' and ' + iMax + ' characters in length');
		}
		oCtl.focus();
		return false;
	} else {
		return true;
	}
}

function CheckAlphaNumeric(oCtl, sName) {
	if (!oCtl.value.match(new RegExp('/([a-z][A-Z][0-9])+/'))) {
		alert('Please ensure ' + sName + ' contains letters and numbers only');
		oCtl.focus();
		return false;
	} else {
		return true;
	}
}

function CheckCheckBox(oCtl, sName) {
	if (oCtl.length) {
		var blnChecked = false;
		for (var i = 0; i < oCtl.length; i++) {
			blnChecked = (oCtl[i].checked || blnChecked);
		}
		if (!blnChecked) {
			alert(sName);
			oCtl[0].focus();
			return false;
		} else {
			return true;
		}
	} else {
		if (!oCtl.checked) {
			alert(sName);
			oCtl.focus();
			return false;
		} else {
			return true;
		}
	}
}

function CheckOptions(oCtl, sName, iMin, oAddCtl) {
	if (oCtl.options.length < iMin) {
		var sPlural = iMin == 1 ? '' : 's';
		alert('Please ensure the ' + sName + ' list contains at least ' + iMin + ' item' + sPlural);
		oAddCtl.focus();
		return false;
	} else {
		return true;
	}
}

function maxLength(oCtl, intLength) {
	if (!isNaN(intLength) && oCtl.value.length > intLength) {
		oCtl.value = oCtl.value.substr(0, intLength);
	}
}

// **************************
// END - FORM VALIDATION CODE
// **************************