// These functions validate the data in the fields on the form
// Fix bug in Email validation 28-11-03
// Change error texts 2-12-03

function CheckText(oField, sDescript, lMustEnter) {
	if (lMustEnter && oField.value == "") {
		alert(sDescript + ' must be entered.');
		oField.focus();
		return (false);
	}

   return (true);
}


function CheckCombo(oField, sDescript, lMustEnter) {
	if (lMustEnter && oField.selectedIndex <= 0) {
		alert(sDescript + ' must be entered.');
		oField.focus();
		return (false);
	}

   return (true);
}


function CheckDate (oForm, sName, sCaption, lMustEnter) {
	day = oForm[sName+"Day"].selectedIndex;
	month = oForm[sName+"Month"].selectedIndex;
	yearIndex = oForm[sName+"Year"].selectedIndex;
	// Index -1 = not selected, 0 = first (empty)
	if (day <= 0 || month <= 0|| yearIndex <= 0) {
		if (day <= 0 && month <= 0 && yearIndex <= 0) {
			if (!lMustEnter) 
				return true;
			alert('Must specify ' + sCaption);
			return false;
		} else {
			alert(sCaption + ' is not complete');
			return false;
		}
	}
	year = parseInt(oForm[sName+"Year"].options[yearIndex].value);
	//year = (year < 1000) ? 2000 + year : year; 
	month = month - 1;
	var test = new Date();
	test.setMonth(month);
	test.setYear(year);
	test.setDate(day);
	//alert(test.toString() + '::');
	if ( (year == test.getFullYear()) &&
	 (month == test.getMonth()) &&
	 (day == test.getDate()) )
	return true;

	alert('Invalid ' + sCaption + ' field.');
	oForm[sName+"Day"].focus();
	return false;
}


function CheckRadio(oField, sText) {
	// Check an option on radio is selected [Form must have more than 1 option defined]
	var iIndex;
	for (iIndex = 0; iIndex < oField.length; iIndex++) {
		if (oField[iIndex].checked)
      			return (true);
	}

	alert('Please select an option for ' + sText + '.');
	return (false);			// Cannot do a set focus on a radio 
}


function CheckEMail(oField, sDescript, lMustEnter) {

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-0123456789_.@";
	var checkStr = oField.value;

	// Remove trailing spaces
	for (i = checkStr.length - 1; i >= 0; i--) {
	  	ch = checkStr.charAt(i);
		if (ch != ' ') break;
	}
	checkStr = checkStr.substr(0, i + 1);
	
	if (checkStr.length == 0) {
		if (lMustEnter) {
			alert(sDescript + ' must be entered.');
			oField.focus();
			return (false);
		}
		return true;
	}

	if (checkStr.indexOf("@") == -1) {
	  	alert(sDescript + " does not contain an '@'");
		oField.focus();
		return (false);
	}	
	if (checkStr.indexOf(".") == -1) {
	  	alert(sDescript + " does not contain an '.'");
		oField.focus();
		return (false);
	}

	// Check the remainder of the string for valid characters
	for (i = 0;  i < checkStr.length;  i++) {
	  	ch = checkStr.charAt(i);
	  	for (j = 0;  j < checkOK.length;  j++)
	  		if (ch == checkOK.charAt(j)) break;
		if (j == checkOK.length) {
	  		//alert("The " + sDescript + " field contains an invalid character at position " + (i + 1) + "[code: " + ch.charCodeAt(0) + ", len: " + checkStr.length + "]");
	  		alert(sDescript + " contains an invalid character at position " + (i + 1));
			oField.focus();
			return (false);
		}
	}

	return (true);
}


function CheckPeriod(oForm, sStart, sEnd, sDescript, nMax, lMustEnter) {

	both = true;

	// Index -1 = not selected, 0 = first (empty)
	startMonth = oForm[sStart+"Month"].selectedIndex;
	startYear = oForm[sStart+"Year"].selectedIndex;


	if (startMonth <= 0 || startYear <= 0) {
		both = false;
		if (startMonth > 0 || startYear > 0) {
		alert(sDescript + ' start is not complete');
		return false;
		}
		if (lMustEnter) {
		alert('Must specify ' + sDescript + ' start');
		return false;
		}
	}

	if (sEnd == "") return true;

	endMonth = oForm[sEnd+"Month"].selectedIndex;
	endYear = oForm[sEnd+"Year"].selectedIndex;
	if (endMonth <= 0 || endYear <= 0) {
		both = false;
		if (endMonth > 0 || endYear > 0) {
		alert(sDescript + ' end is not complete');
		return false;
		}
		if (lMustEnter) {
		alert('Must specify ' + sDescript + ' end');
		return false;
		}
	}

	if (!both)
		return true;

	startYear = parseInt(oForm[sStart+"Year"].options[startYear].value);
	endYear = parseInt(oForm[sEnd+"Year"].options[endYear].value);
	diff = ((endYear - startYear) * 12) + endMonth - startMonth;
	if (diff < 0) {
		alert(sDescript + " start cannot be after end");
		return (false);
	}
	if (nMax != 0) {
		if (diff >= nMax) {
		alert(sDescript + " maximum range from start to end is " + nMax + " months");
		return (false);
		}
	}
	if (diff == 0 && oForm[sStart+"Day"] != null) {
		startDay = oForm[sStart+"Day"].selectedIndex;
		endDay = oForm[sEnd+"Day"].selectedIndex;
		if (startDay > endDay) {
		alert(sDescript + " start day cannot be after end");
		return false;
		}
	}

	return (true);
}

	 