/*
	JavaScript Include File  - for Form Verification
	StarNet Systems Pty Ltd
	Usage:
	
	// example function 
	function DefineFields(f) {
		f.Contact_ID.Required = true;			// specify form object is required
		f.Contact_ID.Label = "Contact Name";	// (optional) specify a label in english for object
		f.cboCollege_1.NumberRequired = true;
		f.cboCollege_1.Label = 'College Preference 1';
		f.txtField.MaxLength = 100;				// check's field is < max length
		f.txtField.DateAus = true;			// check date is valid for dd/mm/yyyy
		f.txtField.DateUs = true;			// check date is valid for mm/dd/yyyy 
		return CheckForm(f);
	}
	
	// add this to the form header tag
	onSubmit="return DefineFields(this)"
					
*/
function CheckForm(f) {
	var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;		// true if ie
	var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false; // true if windows
	var bReturn = true;	
	var sErr = '', sNumErr = '', sLenErr = '', sDateErr = '';
	var num, e, sElementName;
	for (var i = 0; i < f.length; i++) {
		e = f.elements[i];
		if (e.Label != null && e.Label != '')
			s = e.Label;
		else
			s = e.name.substr(3);
			
		if (e.Required) 
			{	// check required text field
			if (e.value == '' || e.value == null) 
				sErr += '     ' + s + ' \n';
			}

		else if (e.NumberRequired) {	// check for required number field
			// value must be a number greater or = to 0
			if (e.value != null)
				// non select box (& select in ie)
				num = parseInt(e.value);
			else if (e.options.length > 0) {
				// select box
				num = parseInt(e.options[e.selectedIndex].value)
			}
			if ( isNaN(num) == false && num >= 0 )	
				; // ok
			else 
				sErr += '     ' + s + ' \n';
		}
//below commented out section - see SC 159376, strange validation error!!
	/*	else if (e.min != null || e.max != null ) {	// check number is within the range
			num = parseFloat(e.value);
			if (isNaN(num))
				sNumErr += 'The field ' + s + ' must be a number.\n';
			else if ((e.min != null && num < e.min) || (e.max != null && num > e.max) ) 
				sNumErr += 'The field ' + s + ' must be a number between ' + e.min + ' and ' + e.max + '.\n';
		}*/
//commented out to here for SC 159376
		
		if (e.MaxLength) {		// check for max length
			if (e.value.length > e.MaxLength)
				sLenErr += '     ' + s + '   (length:' + e.value.length + ' max:' + e.MaxLength + ')    \n';
		}
		
		if (e.DateAus) {
			if (!ValidDateAus(e.value))
				sDateErr += '     ' + s + '  (required format: dd/mm/yyyy) \n';
		} 
		else if (e.DateUs) {
			if (!ValidDateUs(e.value))
				sDateErr += '     ' + s + '  (required format: mm/dd/yyyy) \n';
		}
	}
	
	if (sErr != '' || sNumErr != '' || sLenErr != '' || sDateErr != '' ) {
		if (sErr != '')
			sErr = 'Warning:   The following required field(s) are empty:      \n\n' + sErr + '\n';
		if (sNumErr != '')
			sErr += sNumErr;
		if (sLenErr != '')
			sErr += 'Warning:   The following field(s) are too long:      \n\n' + sLenErr + '\n';
		if (sDateErr != '')
			sErr += 'Warning:   The following date field(s) do not contain valid dates:      \n\n' + sDateErr + '\n';
		alert(sErr + '\nPlease correct these fields and re-submit the form.');
		bReturn = false;
	}
	return bReturn;
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

// checks if date passed is in valid dd/mm/yyyy format
function ValidDateAus (sDate) {
    if (sDate.length == 10) {
		var day  = sDate.substring(0,2);
		var month = sDate.substring(3,5);
		var year  = sDate.substring(6,10);
		return ValidDate(day, month, year);		
    }
    else 
        return false; // invalid length
}

// checks if date passed is in valid mm/dd/yyyy format
function ValidDateUs(sDate) {
    if (sDate.length == 10) {
		var month = sDate.substring(0,2);
		var day = sDate.substring(3,5);
		var year  = sDate.substring(6,10);
		return ValidDate(day, month, year);
    }
    else 
        return false; // invalid length
}

// checks if date sections are valid 
function ValidDate(sDay, sMonth, sYear) {
	var test = new Date(sYear,sMonth-1,sDay);
	if (sYear == y2k(test.getYear()) && 
			(sMonth-1 == test.getMonth()) && 
			(sDay == test.getDate())) {
		return true;
	}
	else 
		return false;	
}

function GetValue(e) {
	if (e.value != null)
		return e.value;	// non select box (& select in ie)
	else if (e.options != null) 
		if (e.options.length > 0) 
			return e.options[e.selectedIndex].value;
		else
			return '';
	else
		return '';
}

// NotUnique():
//	return true if any of the parameter list have the same value
// 	usage: 
//		if ( NotUnique(cboCollege_1, cboCollege_2, cboCollege_3) )
//			// display err
function NotUnique() {
	var val_i, val_j;
	for (var i = 0; i < arguments.length; i++ ) {
		if (arguments[i].options.length > 0)
			val_i = arguments[i].selectedIndex;
		else
			val_i = arguments[i].value;
		for (var j = 0; j < arguments.length; j++) {
			if (arguments[j].options.length > 0)
				val_j = arguments[j].selectedIndex;
			else
				val_j = arguments[j].value;
			if ( val_i == val_j && arguments[i].name != arguments[j].name )
				return true;
		}
	}
	return false;
}

// CtrlNotUnique()
//	returns true if element part are not unique
//	usage:
//		if ( CtrlNotUnique(f, 'cboCollege_') )
//			// display error
function CtrlNotUnique(f, sElementPart) {
	var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;	
	var val_i, val_j;
	for (var i = 0; i < f.length; i++ ) {
		if (f.elements[i].name.indexOf(sElementPart) >= 0) {
			if (f.elements[i].options.length > 0)
				val_i = f.elements[i].selectedIndex;
			else
				val_i = f.elements[i].value;
			for (var j = 0; j < f.length; j++) {
				if (f.elements[j].name.indexOf(sElementPart) >= 0) {
					if (f.elements[j].options.length > 0)
						val_j = f.elements[j].selectedIndex;
					else
						val_j = f.elements[j].value;				
					if ( val_i == val_j && f.elements[i].name != f.elements[j].name )
						return true; // duplicate selection/value found
				}
			}
		}
	}
	return false;
}

function CheckChars()
{
	var txtBoxToCheck = event.srcElement;
	if (txtBoxToCheck.value.length > txtBoxToCheck.MaxLength*1)
	{		
		txtBoxToCheck.value=txtBoxToCheck.value.substring(0,txtBoxToCheck.MaxLength*1);
		alert("Please restrain your input to " + txtBoxToCheck.MaxLength + " or less characters")
		return false
	}
}

function LimitChars()
{
	var txtBoxToCheck = event.srcElement;
	if (txtBoxToCheck.value.length==txtBoxToCheck.maxlength*1)
		return false;
}




