// Used to verify fields are complete in certain submit forms.

function checkFields(targetform) {
	len = eval("document."+targetform).elements.length

	for (i=0;i<len;i++)
	{
		box = eval("document."+targetform).elements[i];
		if (!box.value && box.id == "req")
		{
			alert("The following field: '" + box.name.replace(/\_/,' ') + "' has not been completed.");
			box.focus()
			return false;
		}
	}
	return true;	
}

function checkCheckbox(targetform, targetfield) {
	if ( eval("document." + targetform + "." + targetfield + ".checked")) {
		return true;
	} else {
		alert("Before the form details are submitted, you must read and agree to the Privacy Policy statement.");
		return false;
	}
}		

function checkPass(targetform) {
	myfield = eval("document."+targetform).mypass.value;
	
	if (myfield == "" && eval("document."+targetform).mypass_confirm.value == "") {
		return true;
	}
	
	if (myfield != eval("document."+targetform).mypass_confirm.value)
	{
		alert("Password and Confirm Password fields do not match !");
		return false;
	}
	
	//  AND ( eval("document."+targetform).mypass.value.length < 8) ) 
	if ( (myfield.length > 0) && (eval("document."+targetform).mypass.value.length < 8) )
	{
		alert("Please ensure the password you entered is greater then 8 characters long");
		return false;
	}
	
	if (!isMinNumeric(myfield) || !isMinAlpha(myfield))
	{
		alert("Please ensure the password contains a mix of at least 2 alpha characters and 2 numeric characters.");
		return false;
	}
	
	return true;
}


function isMinNumeric(txt) 
{
	return (ValidString(txt,'0123456789') >= 2);
}


function isMinAlpha(txt)
{
	return (ValidString(txt,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') >= 2);
}

// idea from http://www.johnwbartlett.com/CF_tipsNtricks/index.cfm?TopicID=90
function ValidString(ChkString,ValidString)
{
	count = 0
	for (i=0; i<ChkString.length; i++)
	{
		if (ValidString.indexOf(ChkString.substring(i,i+1)) != -1) {
			++count
		}
	}
	return count;
}


/* Date Validation Functions
 * Derived from http://www.csua.berkeley.edu/~jgwang/jsfunc02.htm
 */
 
function checkDate(targetform, targetfield) {
	var myDayStr = eval("document." + targetform + "." + targetfield + "_day.value");
	var myMonthStr = eval("document." + targetform + "." + targetfield + "_month.value");
	var myYearStr = eval("document." + targetform + "." + targetfield + "_year.value");
	var myDateStr = myDayStr + '/' + myMonthStr + '/' + myYearStr;
	
	// our month forms start from 1.	
	var myDate = new Date(myYearStr,(myMonthStr-1),myDayStr);
	
	// Tests:
	// Convert the date to a string so we can parse it.
	// var myDate_string = myDate.toGMTString();
	// alert(myDateStr + '\n' + myDate.toString() + '\n' + myDate_string+ '\nMon:' + myDate.getMonth());
	// return false;	

	if ( myDate.getMonth() != (myMonthStr-1) ) {
	  alert( "Invalid " + targetfield + " date speicifed: " + myDateStr + ".");
	  return false;
	} else {
	  return true;
	}
}