/* javascript form validation functions */

function initError(myError,myFieldName) { //set error message if not passed
	if (typeof(myError)=='undefined'||trim(myError)=='')
		myError='Error in '+myFieldName+' field input';
	return myError;
}
function initOptions(myOptions) { //set options array if not passed
	if (typeof(myOptions)=='undefined'||trim(myOptions)=='')
		myOptions='required';
	return myOptions;
}

function checkText(myInput,myError,myOptions) { //check text boxes
	myError=initError(myError,myInput.name);
	myOptions=initOptions(myOptions);
	myValue=trim(myInput.value);
	optionsArray=myOptions.split(',') //convert options string to an array
	inputValid=true;
	for (i=0;inputValid&&i<optionsArray.length;i++) { //loop over validation options
		thisOptionArray=optionsArray[i].split('=');
		if (thisOptionArray[0]=='required') //required
			inputValid=(myValue!='');
		else if (thisOptionArray[0]=='alphanumeric') //alphanumeric (can be blank)
			inputValid=(/^\w*$/.test(myValue));
		else if (thisOptionArray[0]=='alpha') //alpha (can be blank)
			inputValid=(/^[a-zA-Z]*$/.test(myValue));
		else if (thisOptionArray[0]=='username') //username (can be blank)
			inputValid=(/^[\w@\.\-]*$/.test(myValue));
		else if (thisOptionArray[0]=='float') //numeric (can be blank)
			inputValid=(!isNaN(myValue));
		else if (thisOptionArray[0]=='posInteger') //positive integer (can be blank)
			inputValid=(/^\d*$/.test(myValue));
		else if (thisOptionArray[0]=='integer') //any integer (can be blank)
			inputValid=(/^-?\d*$/.test(myValue));
		else if (thisOptionArray[0]=='length') //fixed-length
			inputValid=(myValue.length==thisOptionArray[1]);
		else if (thisOptionArray[0]=='RGB') //rgb value (can be blank)
			inputValid=(/^[a-fA-F0-9]{6}$|^$/.test(myValue));
		else if (thisOptionArray[0]=='email') //email address (can be blank)
			inputValid=(/^(([\w\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+){0,1}/.test(myInput))&&
						myInput.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
		else if (thisOptionArray[0]=='windowsfile') //Windows folder name (can be blank)
			inputValid=(!(/[\\:*?"<>|]/.test(myValue)));
		else if (thisOptionArray[0]=='mobile') //mobile phone in format 0123456789 (can be blank)
			inputValid=(/^[0-9]{10}$|^$/.test(myValue));
		else if (thisOptionArray[0]=='austPhone') //Australian land phone in format 00-12345678 (can be blank)
			inputValid=(/^[0-9]{2}\-[0-9]{8}$|^$/.test(myValue));
	}
	setError(inputValid,myError);
}

function checkRadioOrCheckbox(myInput,myError) { //check radio buttons
	myError=initError(myError,myInput.name);
	inputValid=false;
	if (typeof(myInput.length)=='undefined'&&myInput.checked)
		inputValid=true;
	else {
		for (i=0;i<myInput.length;i++) {
			if (myInput[i].checked) {
				inputValid=true;
				break;
			}
		}
	}
	setError(inputValid,myError);
}

function checkSelect(myInput,myError) { //check single and multiple select lists
	myError=initError(myError,myInput.name);
	inputValid=((myInput.selectedIndex!=-1)&&(myInput.options[myInput.selectedIndex].value!='')&&(myInput.options[myInput.selectedIndex].value!='Select'))
	setError(inputValid,myError);
}

function checkDate(myDD,myMM,myYY,myError,myOptions,otDD,otMM,otYY) { //check date selects (can be blank)
	var currDay = new Date();
	var myDate=new Date();
	var otherDate=new Date();
	myError=initError(myError,"date");
	myOptions=initOptions(myOptions);
	optionsArray=myOptions.split(',') //convert options string to an array
	
	thisDD=myDD.options[myDD.selectedIndex].value;
	thisMM=myMM.options[myMM.selectedIndex].value;
	thisYY=myYY.options[myYY.selectedIndex].value;
	otherDD=typeof(otDD)=='undefined'?currDay.getDate():otDD;
	otherMM=typeof(otMM)=='undefined'?currDay.getMonth():otMM;
	otherYY=typeof(otYY)=='undefined'?currDay.getFullYear():otYY;
	inputValid=true;

	if (thisDD!=''&&thisMM!=''&&thisYY!='') {
		daysInMonth=[31,(thisYY%4)?28:29,31,30,31,30,31,31,30,31,30,31];
		inputValid=(thisDD<=daysInMonth[thisMM-1])
		myDate.setFullYear(thisYY,thisMM-1,thisDD);
	}
	if (otherDD!=''&&otherMM!=''&&otherYY!='') {
		otherDate.setFullYear(otherYY,otherMM-1,otherDD);
	}
	
	for (i=0;inputValid&&i<optionsArray.length;i++) { //loop over validation options
		thisOptionArray=optionsArray[i];
		if (thisOptionArray=='required') //required
			inputValid=(thisDD!='' && thisMM!='' && thisYY!='');
		if (thisOptionArray=='present') //date on the present
			inputValid=(myDate >= currDay);
		if (thisOptionArray=='today') //date in today
			inputValid=(myDate == currDay);
		if (thisOptionArray=='past') //date on the past
			inputValid=(myDate < currDay);
		if (thisOptionArray=='>') //date > other day
			inputValid=(myDate > otherDate);
		if (thisOptionArray=='>=') //date >= other day
			inputValid=(myDate >= otherDate);
		if (thisOptionArray=='<') //date < other day
			inputValid=(myDate < otherDate);
		if (thisOptionArray=='<=') //date <= other day
			inputValid=(myDate <= otherDate);
		if (thisOptionArray=='!=') //date != other day
			inputValid=(myDate != otherDate);
		if (thisOptionArray=='==') //date == other day
			inputValid=(myDate == otherDate);
		if (!inputValid) {
			if (thisOptionArray=='required')
				myError = myError + " " + thisOptionArray + " " + thisDD + "/" + thisMM + "/" + thisYY;
			else
				myError = myError + "  should be " + thisOptionArray + " " + otherDD + "/" + otherMM + "/" + otherYY;
		}
	}	
	
	setError(inputValid,myError);
}

function checkMatch(myInput1,myInput2,myError) { //check that two strings match
	myError=initError(myError,myInput2.name);
	inputValid=(trim(myInput1.value)==trim(myInput2.value));
	setError(inputValid,myError);
}

function trim(myString) {
	return myString.replace(/^\s*/,'').replace(/\s*$/,'');
}

function setError(inputValid,myError) {
	if (!inputValid)
		err_msg+='   - '+myError+'\n';
}

function validationResults() {
	if (err_msg!='') {
		err_msg='Form validation error.  Please correct the following and try again:\n'+err_msg;
		alert(err_msg);
		return false;
	}
	return true;
}
