/*
Current interface compatible with all versions since : 1.0
Works with Browsers: Navigator 4, Navigator 4.5, Netscape 6, IE 4, IE 5, IE 5.5, IE 6.
*/

/*
USAGE GUIDE - Notes
1) All parameters are mandatory.
2) In the examples below, each parameter is described between <>s. eg. replace <Form Name> with the name of your form.
*/

/*
USAGE GUIDE - Basic Functions
To validate a field
	validate("<Form Name>", "<Field Name>", "<Validation Function>", "<Validation Message>");
	Standard Validation Functions are:-
		basic - text, textarea, hidden, or file : Will validate if the field is not blank.
		      - checkboxes or radios : Will validate if at least one is checked.
		      - select : will validate if the value of the selected option is not blank.
		email - text or textarea fields only. Will validate if a valid email address.
		numeric - text or textarea fields only. Will validate if a number.
		Checkbox - Checkbox ; will check if a single box is checked.
		altered - text, textarea, hidden, or file : Will validate if the value is different to the default value.
		        - select : will validate if the selected option is not a defaultSelected option (does not have SELECTED in the option tag)
	NB. Do not put ()s after the function name.
	You can also write your own validation functions - see Advanced Functions, below.
*/

/*
USAGE GUIDE - Advanced Functions
To change the first line of the error message:-
 -	setBaseError("<Form Name>", "<New Error Message>");
To make the form execute javascript just before it submits:-
 - 	setPreSubmitAction("<Form Name>", "<Script to execute>", <true or false - whether the form submits>);

When writing your own functions, be aware of the input and output to the function.
	Input - The function is passed one parameter : The object of the field to be validated.
	Output - The function must return true if the field is valid, false if not.
*/

var errmess
//browser checking
var v4 = (document.all||document.layers) ? true : false;
var ie5 = (document.getElementById && document.all);
var ns6 = (document.getElementById && !document.all);
var buttonClicked = false;

function validate(formName, fieldName, rule, errorText){
	if (v4 || ie5 || ns6){
		var frm = eval("document.forms."+formName)
		var fld = eval("frm."+fieldName);
		//alert(fld.value);
		if (!frm.validFlag){
			frm.validFlag = true;
			frm.onsubmit = f_testform;
			frm.validatedFields = new Array();
			if (!frm.baseErrorMessage) frm.baseErrorMessage = "You must fill in the following field(s)";
		}
		fld.testvalid = f_testvalid;
		fld.errorText = errorText;
		fld.rule = eval(rule);
		frm.validatedFields[frm.validatedFields.length] = fld;
	}
}
function f_testvalid(){
	if (eval(this.rule(this))){
		return true;
	} else {
		errmess += this.errorText+"\n";
		return false;
	}
}
function basic(obj){
	if (!obj.type){
		//must be checkbox(es) or radiobutton(s)
		var tempCt, tempVal
		tempVal = false;
		for (tempCt=0; tempCt<obj.length; tempCt++){
			if (obj[tempCt].checked){
				tempVal = true;
			}
		}
		return tempVal;
	} else if (obj.type=="text"||obj.type=="textarea"||obj.type=="hidden"||obj.type=="file"){
		//must be text, textarea, hidden, or file
		return (obj.value!="");
	} else {
		//must be select box
		return (obj[obj.selectedIndex].value!="");
	}
}

function groupnumber(obj) {
	var result;
	var c = 0;
	for (var i = 0; i < document.Injury_notice.groupmember.length; i++) {
		if (document.Injury_notice.groupmember[i].checked == true) {
			if (document.Injury_notice.groupmember[i].value == "yes") {
				c += 1;
			}
		}
	}
	if (c !== 1) {
		result = true;
	} else {
		if (isNaN(parseFloat(document.Injury_notice.groupnumber.value)) || (document.Injury_notice.groupnumber.value.length !== 6)) {
			result = false;
		} else {
			result = true;
		}
	}
	return result;
}

function superannuation(obj) {
	if (document.Injury_notice.Superannuation.value !== "") {
		if (document.Injury_notice.Super_Dont_Know.checked == true) {
			return false;
		}
	}
	if (document.Injury_notice.Superannuation.value == "") {
		if (document.Injury_notice.Super_Dont_Know.checked == false) {
			return false;
		}
	}
	if (document.Injury_notice.Superannuation.value == "") {
		if (document.Injury_notice.Super_Dont_Know.checked == true) {
			return true;
		}
	}
	if (document.Injury_notice.Superannuation.value !== "") {
		if (document.Injury_notice.Super_Dont_Know.checked == false) {
			return true;
		}
	}
}

//Checks that User name entered is between 4 and 20 characters in length
function username(obj){
	if (obj.value.length < 4 || obj.value.length > 20){
		return false;
	}
	return true;
}

//must be Checkbox
function checkbox(obj){
		return (obj.checked!="");
}

function email(obj){
	//must be text or textentry
	return ((obj.value.indexOf("@")>-1)&&(obj.value.lastIndexOf(".")>obj.value.indexOf("@"))&&(obj.value.indexOf("..")==-1));
}

function onlynumeric(obj){
	return (!isNaN(obj.value));
}

function phnumber(obj){
	//must be text or brackets or space
	
	var k, validity=true;
	if(obj.value.length > 7 ){ 
	for (k=0; k<obj.value.length; k++){
	   if (obj.value.charAt(k) != "("){
		     if (obj.value.charAt(k) != ")"){
    		     if (obj.value.charAt(k) != " "){
							
    		     if (obj.value.charAt(k) != "0"){
						 if (obj.value.charAt(k) != "1"){
						 if (obj.value.charAt(k) != "2"){
						 if (obj.value.charAt(k) != "3"){
						 if (obj.value.charAt(k) != "4"){
						 if (obj.value.charAt(k) != "5"){
						 if (obj.value.charAt(k) != "6"){
						 if (obj.value.charAt(k) != "7"){
						 if (obj.value.charAt(k) != "8"){
						 if (obj.value.charAt(k) != "9"){						 
						 				validity=false;
									return false;
						 }}}}}}}}}}						 						 
 
							}
			    }
			}
	}
	
	return true;
	}
	return false;
}


function numeric(obj){
	//must be text or textentry
	//the function parseInt allows any value after a numeric
	return (!isNaN(parseInt(obj.value)));
}

function altered(obj){
	if (obj.type=="text"||obj.type=="textarea"||obj.type=="hidden"||obj.type=="file"){
		//must be text, textarea, hidden, or file
		return (obj.value!=obj.defaultValue);
	} else {
		//must be select box
		return !(obj[obj.selectedIndex].defaultSelected);
	}
}
function f_testform(){
	var tempCt, validity
	if(buttonClicked == true){
		alert("Please Wait. Your request is being processed.");
		return false;
	}
	buttonClicked = true;
	validity = true;
	errmess = this.baseErrorMessage + "\n\n";
	for (tempCt=0; tempCt<this.validatedFields.length; tempCt++){
		if (!(this.validatedFields[tempCt].testvalid())){
			buttonClicked = false;		
			validity=false;
		}
	}
	if (validity){
		if (this.submitaction) {
			eval(this.submitaction);
			return this.doSubmit;
		} else {
			return true;		
		}
	} else {
		alert(errmess);
		return false;
	}
}
function setBaseError(formName, errorText){
	if (v4 || ie5 || ns6){
		var frm = eval("document.forms."+formName)
		frm.baseErrorMessage = errorText;
	}
}
function setPreSubmitAction(formName, evalString, doSubmit){
	var frm = eval("document.forms."+formName)
	frm.submitaction = evalString;
	frm.doSubmit = doSubmit;
}

