/* 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(myValue));
		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!='')
	setError(inputValid,myError);
}

function checkDate(myDD,myMM,myYY,myError,myOptions) { //check date selects (can be blank)
	if (typeof(myError)=='undefined'||trim(myError)=='')
		myError='Invalid date';
	if (typeof(myOptions)=='undefined')
		myOptions='';
	thisDD=myDD.options[myDD.selectedIndex].value;
	thisMM=myMM.options[myMM.selectedIndex].value;
	thisYY=myYY.options[myYY.selectedIndex].value;
	inputValid=true;
	if ((thisDD==''||thisMM==''||thisYY=='')&&myOptions=='required')
		inputValid=false;
	else 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])
	}
	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;
}