/*
		******************************************************
		Filename:		validates.js
		Type:				Javascript Functions
		Purpose:			Validation checks.
		Author:			Tony Morgan
		Date:				17th Jan 2001
		******************************************************


		Example validation control - put this bit at the top of your form:

		======================================================================================================================================
		=	// this first function checks the login form...
		=	function check(thisForm) {
		=		messageStr = "The following errors occurred:\n\n";
		=
		=		valid = true;
		=		valid = requiredField(thisForm.username, "You must enter a username.", valid);
		=		valid = minLength(thisForm.password, "You must enter a password of at least 5 characters.", 5, valid);
		=
		=		if (valid == false)	alert(messageStr);
		=		errorFlag = 0;
		=		return valid;
		=	}
		======================================================================================================================================
		
		Functions:
		
		======================================================================================================================================
		=	isMoney( obj, msg, cstate, integer )						Checks that it's a number and rounds to nearest
		=																			penny. If 'integer' is true (boolean) then it 
		=																			converts amount to an integer, else converts it 
		=																			to 2 decimal places.
		=																			
		=	requiredField( obj, msg, cstate )							Checks that anything has been entered in the 
		=																			specified fileds.
		=																			
		=	isaNum( obj, msg, cstate )										Checks that it's a positive integer.
		=	
		=	minLength( obj, msg, minlen, cstate )						Ensures the selected field is longer than or equal
		=																			to the minimum length.
		=																			
		=	maxLength( obj, msg, maxlen, cstate )						Ensures the selected field is no longer than the
		=																			maximum length.
		=																			
		=	validSame( obj1, obj2, msg, cstate )						Ensures 2 fileds are equal (good for confirming 
		=																			passwords).
		=																			
		=	validEmail( obj, msg, cstate )								Ensures that the selected field contains an '@' and#
		=																			a '.'.
		=																			
		=	validDate( objDay, objMonth, objYear, msg, cstate )	Checks that three fileds make up a valid date.
		=
		=  requiredTextarea( obj, msg, initialValue, cstate )		Checks that a textarea contains text.  Set 
		=																			'initialValue' to blank ('') if there is none.
		=
		=  requiredSelect( obj, msg, cstate )							Checks that a select box's value is not 0.
		=																			
		======================================================================================================================================
*/


// this flags keeps track of the first error and so sets the focus on that input type first
errorFlag = 0;

function errorMsg( str )
	{
	messageStr += str + "\n"
	return false;
}

function isMoney( obj, msg, cstate, integer ) {
	if (isNaN(obj.value)) {
		if (errorFlag == 0) {
			obj.focus();
			errorFlag = 1
		}
		return errorMsg(msg);
	}
	if (integer) obj.value = Math.round(obj.value * 100);
	else obj.value = Math.round(obj.value * 100)/100;
	return cstate;
}

function requiredField( obj, msg, cstate ) {
	if( obj.value.length == 0 ) {
		if (errorFlag == 0) {
			obj.focus();
			errorFlag = 1
		}
		return errorMsg(msg);
	}
	return cstate;
}

function requiredTextarea( obj, msg, initialValue, cstate ) {
	if( obj.value.length <= 1 || obj.value == initialValue ) {
		if (errorFlag == 0) {
			obj.focus();
			errorFlag = 1
		}
		return errorMsg(msg);
	}
	return cstate;
}

function requiredSelect( obj, msg, cstate ) {
	selectedPosition = obj.selectedIndex;
	if( obj.options[selectedPosition].value == 0 || selectedPosition == 0 ) {
		if (errorFlag == 0) {
			obj.focus();
			errorFlag = 1
		}
		return errorMsg(msg);
	}
	return cstate;
}

function isaNum( obj, msg, cstate ) {
	if( obj.value <= 0 ) {
		if (errorFlag == 0) {
			obj.focus();
			errorFlag = 1
		}
		return errorMsg(msg);
	}
	return cstate;
}

function minLength( obj, msg, minlen, cstate ) {
	if( obj.value.length < minlen ) {
		if (errorFlag == 0) {
			obj.value = "";
			obj.focus();
			errorFlag = 1
		}
		return errorMsg(msg);
	}
	return cstate;
}

function maxLength( obj, msg, maxlen, cstate ) {
	if( obj.value.length > maxlen ) {
		if (errorFlag == 0) {
			obj.value = "";
			obj.focus();
			errorFlag = 1
		}
		return errorMsg(msg);
	}
	return cstate;
}

function validSame( obj1, obj2, msg, cstate ) {
	if( obj1.value != obj2.value ) {
		obj2.value = "";
		if (errorFlag == 0) {
			obj2.focus();
			errorFlag = 1
		}
		return errorMsg(msg);
	}
	return cstate;
}

function validEmail( obj, msg, cstate ) {
	if( obj.value.length == 0 ) {
		if (errorFlag == 0) {
			obj.focus();
			errorFlag = 1
		}
		return errorMsg(msg);
	}
	else {
		tempmail = "" + (obj.value);

		if((tempmail.indexOf("@") == -1) || (tempmail.indexOf(".") == -1) || (tempmail.indexOf('/') > -1) || (tempmail.indexOf('\\') > -1) ) {
			if (errorFlag == 0) {
				obj.focus();
				errorFlag = 1
			}
			return errorMsg(msg);
		}
	}
	return cstate;
}

function validDate( objDay, objMonth, objYear, msg, cstate ) {

	_day   = 0 + objDay.options[objDay.selectedIndex].value;
	_month = objMonth.options[objMonth.selectedIndex].value;
	_year  = 0 + objYear.options[objYear.selectedIndex].value;

	// Validate the date
	if( _month == "Sept" || _month == "Apr" || _month == "Jun" || _month == "Nov" ) {
		if( _day > 30 ) {
			if (errorFlag == 0) {
				objDay.focus();
				errorFlag = 1
			}
			return errorMsg(msg + "There are only 30 days in the selected month!");
		}
	}
	if( _month == "Feb" ) {
		//Is it a leap year (modulus 4)
		if( (_year % 4) == 0 ) {
			if(_day > 29 ) {
				if (errorFlag == 0) {
					objDay.focus();
					errorFlag = 1
				}
				return errorMsg(msg + "There are 29 days in February of that year.");
			}
		}
		else {
			if( _day > 28 ) {
				if (errorFlag == 0) {
					objDay.focus();
					errorFlag = 1
				}
				return errorMsg(msg + "There are 28 days in February of that year.");
			}
		}
	}
	return cstate;
}