/*
	Form validation control script.
	
	Developed by Mark A. McNally for netXtra Ltd. (http://www.netxtra.net)
	
	Version : 1.4
	Date : 13/07/2006
	
*/
var frmBorder = 'solid 1px #993300';
var frmBgColor = 'yellow';	
var frmBorderOrig = 'solid 1px #A7C5E4';
var frmBgColorOrig = '#ffffff';
var textColorOrig = '#555555';
var textWarningColor = '#993300';
var fi = new Array();
var errNull = ' is required';
var direction = false;


function chkForm() {
	var errors = '';
	
	/*
		To check your form elements simply include the below statement in a script block in your page
		for each field you want to check.	
		
		addFormItem('csSurveryFRM_q01','Membership Grade' + errNull + '.','2','0','0','0','','');	

		addFormItem(name,msg,type,upperVal,lowerVal,Equal,other,suffix);
		
			name = ID of input field to be checked
			msg = Message returned to user about the error
			type = What is the type of check
			upperVal = Numeric top line used for checking
			lowerVal = Numeric bottom line used for checking
			Equal = A value used to check if the entered value matches
			other = Other input id
			suffix = A suffix used.
		
		Check types	

		1 = Email format check
		2 = Null value check (not a strict null check) and checks against Equal, can be used where a default value is specified.
		3 = IsNan, will return an error if the value is not a number.
		4 = Number of characters check, uses upper and lower vals to determine if the field length is within the expected range.
		5 = Remove default value, use Equal to store value to remove.
		6 = Must match field, use Equal to store the name of the field it is to check against.
		7 = Must be a number greater than lowerVal.
		8 = "OTHER" if equals this value (stored in other) then check against another field to see if it has content
		9 = "YES/NO" Is selected Check.
		10 = "OTHER" if suffix is checked then check against another field to see if it has content
		11 = "RadioGroup" checks through a range of buttons, the container must have an id that is used as the name added to the FormItems 
			 range, the options must then be numerically numbered and the start value and end value entered in as upperVal and lowerVal
			 i.e. 
	*/
	if (!direction) {
		fi.reverse();	
		direction=true;
	}
	
	for (i=0;i<fi.length;i++) {
		var ob = $get(fi[i].name);
		if (ob) {
			ob.style.backgroundColor = frmBgColorOrig;
			if (fi[i].type!='9'&&fi[i].type!='10'&&fi[i].type!='11') {							
				ob.style.border = frmBorderOrig;	
			} else {
				ob.style.border = 'solid 1px #fff';
			}
		}
	}	
	
	for (i=0;i<fi.length;i++) {
		var em = $get(fi[i].name);
		
		if (em!=null) {
			if (fi[i].type == '1') {
				if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(em.value)) {
					errors = fi[i].msg + '\n' + errors;
					em.focus();
					em.style.border = frmBorder;
					em.style.backgroundColor = frmBgColor;
				}
			}
			if (fi[i].type == '2') {
				if (em.value==fi[i].Equal||em.value=='') {
					errors = fi[i].msg + '\n' + errors;
					em.focus();
					em.style.border = frmBorder;
					em.style.backgroundColor = frmBgColor;					
				}				
			}
			if (fi[i].type == '3') {
				if (isNaN(em.value)) {
					errors = fi[i].msg + '\n' + errors;
					em.focus();
					em.style.border = frmBorder;
					em.style.backgroundColor = frmBgColor;					
				}				
			}
			if (fi[i].type == '4') {
				if (em.value.toString().length<fi[i].lowerVal||em.value.toString().length>fi[i].upperVal) {
					errors = fi[i].msg + '\n' + errors;
					em.focus();
					em.style.border = frmBorder;
					em.style.backgroundColor = frmBgColor;					
				}				
			}
			if (fi[i].type == '5') {
				if (em.value==fi[i].Equal) {
					em.value = '';					
				}				
			}
			if (fi[i].type == '6') {
				var x = $get(fi[i].Equal);
				if (em.value!=x.value) {
					errors = fi[i].msg + '\n' + errors;
					em.focus();
					em.style.border = frmBorder;
					em.style.backgroundColor = frmBgColor;
					x.style.border = frmBorder;
					x.style.backgroundColor = frmBgColor;									
				}				
			}
			if (fi[i].type == '7') {
				if (isNaN(em.value)||Number(em.value)<=Number(fi[i].lowerVal)) {
					errors = fi[i].msg + '\n' + errors;
					em.focus();
					em.style.border = frmBorder;
					em.style.backgroundColor = frmBgColor;		
				}				
			}
			if (fi[i].type == '8') {
				var x = $get(fi[i].Equal);
				if (em.value==fi[i].other) {
					if (x.value==''||x.value==0) {
						errors = fi[i].msg + '\n' + errors;
						x.focus();
						x.style.border = frmBorder;
						x.style.backgroundColor = frmBgColor;
					}									
				}				
			}
			if (fi[i].type == '9') {
				var emY = $get(fi[i].name + "_Yes");
				var emN = $get(fi[i].name + "_No");
				//alert(fi[i].name);
				if (!emY.checked&&!emN.checked) {
					var lbl = fi[i].name;
					errors = fi[i].msg + '\n' + errors;
					emY.focus();
					em.style.border = frmBorder;
					em.style.backgroundColor = frmBgColor;					
				}					
			}
			if (fi[i].type == '10') {
				var x = $get(fi[i].Equal);
				var emX = $get(fi[i].name + fi[i].suffix);
				if (emX.checked) {
					if (x.value==''||x.value==0) {
						errors = fi[i].msg + '\n' + errors;
						x.focus();
						x.style.border = frmBorder;
						x.style.backgroundColor = frmBgColor;
					}									
				}				
			}
			if (fi[i].type == '11') {
				var tmpObj;
				var tmp = false;
				var u = Number(fi[i].upperVal);
				var l = Number(fi[i].lowerVal);
				
				for (j=l;j<=u;j++) {
					tmpObj = $get(fi[i].name + '_' + j);
					if (tmpObj.checked) {
						tmp = true;	
					}
				}
				
				if (!tmp) {
					errors = fi[i].msg + '\n' + errors;
					em.style.border = frmBorder;
					em.style.backgroundColor = frmBgColor;					
				}					
			}
		} else {
			// alert('i null');	
		}
	}
	if (errors=='') {
		return true;
	} else {
		alert(errors + '\nPlease complete/amend the listed field(s) before resubmitting the form');
		return false;
	}		
}
function formItems(n,m,t,u,l,e,o,s) {
	this.name = n; // Name of input field to be checked
	this.msg = m; // Message returned to user about the error
	this.type = t; // What is the type of check
	this.upperVal = u; // Numeric top line used for checking
	this.lowerVal = l; // Numeric bottom line used for checking
	this.Equal = e; // A value used to check if the entered value matches
	this.other = o; // Other input id
	this.suffix = s; // A suffix used.
}
function addFormItem(n,m,t,u,l,e,o,s) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,t,u,l,e,o,s));	
}
function addNullCheck(n,m,e) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'2','0','0',e,'',''));
		
}
function addEmailCheck(n,m) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'1','0','0','','',''));
		
}
function addRadioGroup(n,m,l,u) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'11',u,l,'','',''));
		
}
function addYesNoCheck(n,m) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'9','','','','',''));
		
}
