/********************
* Field population methods
*********************/
function setTextFieldValue(formName,fieldName,value) {
	eval("document."+formName+"."+fieldName+".value=value");
}
//selects the checkboxes in the group fieldName with the values of cookieAry
function setCheckboxSelc(formName,fieldName,cookieAry,othersFieldName) {
	if (cookieAry=='') return;
	var ary=cookieAry.split(',');
	var length = eval("document."+formName+"."+fieldName+"."+"length");
	for (var j=0;j<ary.length;j++) {
		for (var i=0;i<length;i++) {
			if (eval("document."+formName+"."+fieldName+"["+i+"].value")==ary[j]) {
				eval("document."+formName+"."+fieldName+"["+i+"].checked=true");
				//return;
			}
			else if (othersFieldName) { //value is not one of the predetermined checkbox values
				eval("document."+formName+"."+fieldName+"["+(length-1)+"].checked=true"); //set the last value of the checkbox gp ('Others') to selected
				eval("document."+formName+"."+othersFieldName+".value=ary[ary.length-1]");
			}
		}
	}
}
//selects the radio button in the group fieldName with the value cookieValue
function setRadioSelc(formName,fieldName,cookieValue,othersFieldName) {
	if (!cookieValue) return;
	var length = eval("document."+formName+"."+fieldName+"."+"length");
	for (var i=0;i<length;i++) {
		if (eval("document."+formName+"."+fieldName+"["+i+"].value")==cookieValue) {
			eval("document."+formName+"."+fieldName+"["+i+"].checked=true");
		}
		else if (othersFieldName) { //value exists but is not one of the predetermined radio values
			eval("document."+formName+"."+fieldName+"["+(length-1)+"].checked=true"); //set the last value of the radio gp ('Others') to selected
			eval("document."+formName+"."+othersFieldName+".value=cookieValue");
		}
	}
}
//selects the item in the DDL fieldName with the value cookieValue
function setDDLSelc(formName,fieldName,cookieValue,othersFieldName) {
	if (cookieValue=='') return;
	var length = eval("document."+formName+"."+fieldName+"."+"length");
	for (var i=0;i<length;i++) {
		if (eval("document."+formName+"."+fieldName+"["+i+"].value")==cookieValue) {
			eval("document."+formName+"."+fieldName+"["+i+"].selected=true");
			return;
		}
		else if (othersFieldName) { //value exists but is not one of the predetermined DDL values
			eval("document."+formName+"."+fieldName+"["+(length-1)+"].selected=true;"); //set the last value of the DDL ('Others') to selected
			eval("document."+formName+"."+othersFieldName+".value=cookieValue");
		}
	}
}


/********************
* Retrieval methods
*********************/
function getTextFieldValue(formName,fieldName) {
	var value = eval("document."+formName+"."+fieldName+".value");
	return value;
}
//returns as an array the VALUES of the selected checkboxes in the group fieldName, except 'Others', since we want to record the textfield value of 'Others'
function getCheckboxSelc(formName,fieldName) {
	var length = eval("document."+formName+".elements.length");
	var aryCheckboxSelc= new Array();
	for (var i=0;i<length;i++) {
		if (eval("document."+formName+".elements["+i+"].type=='checkbox'") &&
		   	eval("document."+formName+".elements["+i+"].name")==fieldName &&
		   	eval("document."+formName+".elements["+i+"].checked")) {
			aryCheckboxSelc.push(eval("document."+formName+".elements["+i+"].value"));
		}
	}
	if (aryCheckboxSelc.length>0) return aryCheckboxSelc;
	else return '';
}
//returns the VALUE of the selected radio button in the group fieldName
function getRadioSelc(formName,fieldName) {
	var length = eval("document."+formName+".elements.length")
	for (var i=0;i<length;i++) {
		if (eval("document."+formName+".elements["+i+"].type=='radio'") &&
		   	eval("document."+formName+".elements["+i+"].name")==fieldName &&
		   	eval("document."+formName+".elements["+i+"].checked")) {
			return eval("document."+formName+".elements["+i+"].value");
		}
	}
	return '';
}
//returns the VALUE of the selected item in the DDL fieldName
function getDDLSelc(formName,fieldName) {
	var index = eval("document."+formName+"."+fieldName+"."+"selectedIndex");
	var value = eval("document."+formName+"."+fieldName+"."+"options["+index+"].value");
	return value;
}


/********************
* Validation methods
*********************/
function chkTextfieldHasValue(formName,fieldName) {
	if(Trim(eval("document."+formName+"."+fieldName+".value"))=='') return false;
	else return true;
}
//checks that at least one DDL item is selected, and not the default/first value
function chkDDLSelc(formName,fieldName) {
	var index = eval("document."+formName+"."+fieldName+"."+"selectedIndex");
	if (index==0) return false;	
	else return true;
}
//checks that at least one radio button is selected
function chkRadioSelc(formName,fieldName) {
	var length = eval("document."+formName+".elements.length")
	for (var i=0;i<length;i++) {
		if (eval("document."+formName+".elements["+i+"].type=='radio'") &&
		   	eval("document."+formName+".elements["+i+"].name")==fieldName &&
		   	eval("document."+formName+".elements["+i+"].checked")) {
			return true;
		}
	}
	return false;
}
//checks that at least one checkbox is selected
function chkCheckboxSelc(formName,fieldName) {
	var length = eval("document."+formName+".elements.length")
	for (var i=0;i<length;i++) {
		if (eval("document."+formName+".elements["+i+"].type=='checkbox'") &&
		   	eval("document."+formName+".elements["+i+"].name")==fieldName &&
		   	eval("document."+formName+".elements["+i+"].checked")) {
			return true;
		}
	}
	return false;
}
//checks that email field is not empty, and format is of the form a@b.c
function chkEmail(formName,fieldName,compulsory) {
	var value = eval("document."+formName+"."+fieldName+".value")
	if(Trim(value)=='' && compulsory.toLowerCase()=='y') return false;
	else {
		str = new String(value);
		if(Trim(str)!='') {
			posAt = str.indexOf('@');
			posDot = str.indexOf('.', posAt);
			if (posAt!= -1 && posDot!= -1 && posDot>posAt && posDot-posAt>1) return true; //if can find @ and . and . is after @ and there is at least one character between @ and .
			else return false;
		}	
	}
	return true;
}
//checks that the value in fieldName is numeric
function chkNumeric(formName,fieldName) {
	if (event.keyCode<45 || event.keyCode>57 || event.keyCode==46) { 
		alert("Only digits to be entered");
		event.returnValue = false;
	}
}
//checks that the value in the DDL fieldName equals reqValue. prompts user otherwise, without allowing any values to be entered
function chkDDLSelcSpecific(formName, fieldName, reqValue) {
	if (getDDLSelc(formName, fieldName)!=reqValue) {
		alert("Please choose '"+reqValue+"' to enter data")
		event.returnValue = false;
	}
}
//checks that the value in the DDL fieldName equals reqValue. empties resetFieldName otherwise
function chkDDLReset(formName, fieldName, reqValue, resetFieldName) {
	if (getDDLSelc(formName, fieldName)!=reqValue)
		eval("document."+formName+"."+resetFieldName+".value=''");
	return false;
}
//checks that the value in the selected radio fieldName equals reqValue. prompts user otherwise, without allowing any values to be entered
function chkRadioSelcSpecific(formName, fieldName, reqValue) {
	if (getRadioSelc(formName, fieldName)!=reqValue) {
		alert("Please choose '"+reqValue+"' to enter data")
		event.returnValue = false;
	}
}
//empties value in resetFieldName
function chkRadioReset(formName, resetFieldName) {
	eval("document."+formName+"."+resetFieldName+".value=''");
}
//checks that the value in the selected radio fieldName equals reqValue. prompts user otherwise, without allowing any values to be entered
function chkCheckboxSelcSpecific(formName, fieldName, reqValue) {
	var found=false;
	var ary=getCheckboxSelc(formName, fieldName);
	for (var i=0; i<ary.length; i++) {
		if (ary[i]==reqValue) {
			found=true;
			break;
		}
	}
	if (!found) {
		alert("Please choose '"+Trim(reqValue)+"' to enter data")
		event.returnValue = false;
	}
}
//checks that the checkbox is unselected, and empties resetFieldName value
function chkCheckboxReset(formName, resetFieldName) {
	if ("document."+formName+"."+resetFieldName+".checked==false")
		eval("document."+formName+"."+resetFieldName+".value=''");
}
//checks that the date value in fieldName is of the format DD/MM/YYYY
function chkDateFormat(formName, fieldName) {
	var value=eval("document."+formName+"."+fieldName+".value");
	var aryDate=value.split("/");
	var day=aryDate[0];
	var month=aryDate[1];
	var year=aryDate[2];
	//if (
}
//converts value of fieldName into uppercase
function convertUppercase(formName, fieldName) {
	var field=eval("document."+formName+"."+fieldName);
	field.value=field.value.toUpperCase();
}




function Trim(S) {
	reg=/\s/ig;
	var c= new String();
	var D = new String();
	D = S;
	var newstring=new String("");
	for (i=1; i<=D.length; i++){
		c=D.charAt(i-1);
		if (c.search(reg) == "-1") newstring=newstring.concat(c);
	}
	return newstring;
}