/*
	Form validation control script.
	
	Developed by Mark A. McNally for netXtra Ltd. (http://www.netxtra.net)
	
	Version : 1.5
	Date : 19/06/2009
	
*/
var frmBorder = '2px solid #900';
var frmBgColor = '#ffffff';	
var frmBorderOrig = 'solid 1px #cccccc';
var frmBgColorOrig = '#ffffff';
var textColorOrig = '#333333';
var textWarningColor = '#900';
var fi = new Array();
var errNull = ' is required';
var direction = false;

function $get(el) {
	var obj = document.getElementById(el);
	if (obj) {
		return obj;		
	} else {
		return null;
	}
} 

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.
		7a = Must be a number lesser than lowerVal field.
		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
		10a = "OTHER" if checked then check against other 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
		12 = "CheckBoxArray" looks to see if any in a list of checkboxes, supplied in other field as csv list, have been selected, assumes 
			 items are in semantic list elements, name should the last element in the list.
			 i.e. 
	*/
	if (!direction) {
		fi.reverse();	
		direction=true;
	}
	
	for (i=0;i<fi.length;i++) {
		var ob = $get(fi[i].name);
		if (ob) {
			if (fi[i].type!='9'&&fi[i].type!='10'&&fi[i].type!='10a'&&fi[i].type!='11'&&fi[i].type!='12'&&fi[i].type!='12a'&&fi[i].type!='c1') {		
				ob.style.backgroundColor = frmBgColorOrig;					
				ob.style.border = frmBorderOrig;	
				removeError(ob);
			} else if (fi[i].type=='10a') {
				var o = $get(fi[i].other);
				o.style.backgroundColor = frmBgColorOrig;					
				o.style.border = frmBorderOrig;	
				removeError(o);
			} else if (fi[i].type=='12'||fi[i].type=='12a') {
				removeError(ob.parentNode.parentNode);
			} else if (fi[i].type=='c1') {
				var usb = fi[i].upperVal.split(',');
				var lsb = fi[i].lowerVal.split(',');
				var uyb = $get(usb[1]);
				var umb = $get(usb[0]);
				var lyb = $get(lsb[1]);
				var lmb = $get(lsb[0]);		
				uyb.style.backgroundColor = frmBgColorOrig;					
				uyb.style.border = frmBorderOrig;		
				umb.style.backgroundColor = frmBgColorOrig;					
				umb.style.border = frmBorderOrig;	
				lyb.style.backgroundColor = frmBgColorOrig;					
				lyb.style.border = frmBorderOrig;		
				lmb.style.backgroundColor = frmBgColorOrig;					
				lmb.style.border = frmBorderOrig;	
				removeError(ob.parentNode);
			}
		}
	}	
	
	for (i=0;i<fi.length;i++) {
		var em = $get(fi[i].name);
		
		if (em!=null) {
			if (fi[i].type == '1') {
				if (em.value!='') {
					if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(em.value)) {
						insertError(em,fi[i].msg);
						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=='') {
					insertError(em,fi[i].msg);
					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)) {
					insertError(em,fi[i].msg);
					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) {
					insertError(em,fi[i].msg);
					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) {
					insertError(em,fi[i].msg);
					em.value = '';					
				}				
			}
			if (fi[i].type == '6') {
				var x = $get(fi[i].Equal);
				if (em.value!=x.value) {
					insertError(em,fi[i].msg);
					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)) {
					insertError(em,fi[i].msg);
					errors = fi[i].msg + '\n' + errors;
					em.focus();
					em.style.border = frmBorder;
					em.style.backgroundColor = frmBgColor;		
				}				
			}
			if (fi[i].type == '7a') {
				var emMore = $get(fi[i].upperVal).value;				
				if (isNaN(em.value)||isNaN(emMore)||Number(em.value)>Number(emMore)) {
					insertError(em,fi[i].msg);
					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) {
						insertError(em,fi[i].msg);
						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;
					insertError(em,fi[i].msg);
					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) {
						insertError(em,fi[i].msg);
						errors = fi[i].msg + '\n' + errors;
						x.focus();
						x.style.border = frmBorder;
						x.style.backgroundColor = frmBgColor;
					}									
				}				
			}
			if (fi[i].type == '10a') {
				var x = $get(fi[i].other);
				var emX = $get(fi[i].name);
				if (emX.checked) {
					if (x.value==''||x.value==0) {
						if (window['alreadyFlagged10a'] != fi[i].other) {
							insertError(x,fi[i].msg);		
							errors = fi[i].msg + '\n' + errors;
							x.focus();
							x.style.border = frmBorder;
							x.style.backgroundColor = frmBgColor;						
						}
						window['alreadyFlagged10a'] = fi[i].other;
					}	
				}
			}
			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) {
					insertError(em,fi[i].msg);
					errors = fi[i].msg + '\n' + errors;
					em.style.border = frmBorder;
					em.style.backgroundColor = frmBgColor;					
				}					
			}
			if (fi[i].type == '12') {
				var checkboxes = fi[i].other.split(',');
				var tmp = false;
				var tmpObj;
				
				for (j=0;j<=(checkboxes.length-1);j++) {
					if (checkboxes[j]!='') {
						tmpObj = $get(checkboxes[j]);
						if (tmpObj.checked) {
							tmp = true;
						}
					}
				}
				if (!tmp) {
					insertError(em.parentNode.parentNode,fi[i].msg);
					errors = fi[i].msg + '\n' + errors;		
				}			
			}
			if (fi[i].type == '12a') {
				var checkboxes = fi[i].other.split(',');
				var other = $get(fi[i].Equal);
				var tmp = false;
				var tmpObj;
				
				for (j=0;j<=(checkboxes.length-1);j++) {
					if (checkboxes[j]!='') {
						tmpObj = $get(checkboxes[j]);
						if (tmpObj.checked) {
							tmp = true;
						}
					}
				}
				if (other.value!='') {
					tmp = true;	
				}
				if (!tmp) {
					insertError(em.parentNode.parentNode,fi[i].msg);
					errors = fi[i].msg + '\n' + errors;		
				}			
			}
			if (fi[i].type == 'c1') {
				var upperSelectBoxes = fi[i].upperVal.split(',');
				var lowerSelectBoxes = fi[i].lowerVal.split(',');
				var uy = $get(upperSelectBoxes[1]);
				var um = $get(upperSelectBoxes[0]);
				var ly = $get(lowerSelectBoxes[1]);
				var lm = $get(lowerSelectBoxes[0]);
				
				var upperDate = new Date();
				upperDate.setFullYear(uy.value,(um.value-1),1);
				var lowerDate = new Date();
				lowerDate.setFullYear(ly.value,(lm.value-1),1);
				
				if (lowerDate>upperDate) {
					insertError(em.parentNode,fi[i].msg);
					errors = fi[i].msg + '\n' + errors;
					um.focus();
					um.style.border = frmBorder;
					um.style.backgroundColor = frmBgColor;	
					uy.style.border = frmBorder;
					uy.style.backgroundColor = frmBgColor;	
					lm.style.border = frmBorder;
					lm.style.backgroundColor = frmBgColor;	
					ly.style.border = frmBorder;
					ly.style.backgroundColor = frmBgColor;						
				}	
			}
		}
	}
	if (errors=='') {
		return true;
	} else {
		alert(errors + '\nPlease complete/ammend 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 insertError(obj,msg) {
	var parent = obj.parentNode;
	
	var div = document.createElement('div');
	div.classname = 'alert';
	div.className = 'alert';
	
	var p = document.createElement('p');
	setText(p,msg);
	div.appendChild(p);
	parent.appendChild(div);
}
function removeError(obj) {
	var parent = obj.parentNode;
	var divs = parent.getElementsByTagName('div');
	
	for (h=0;h<=(divs.length-1);h++) {
		if (divs[h].className=='alert') {
			parent.removeChild(divs[h]);			
		}
	}
	
}
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 addIsANumberCheck(n,m) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'3','0','0','','',''));
		
}
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 addOtherFieldCheck(n,m,e,o) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'8','0','0',e,o,''));
		
}
function addIfFieldSelectedCheckOther(n,m,o) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'10a','0','0','',o,''));
		
}
function addYesNoCheck(n,m) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'9','','','','',''));
		
}
function addLowerThanCheck(n,m,l) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'7','',l,'','',''));
		
}
function addLowerThanOtherFieldCheck(n,m,u) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'7a',u,'','','',''));
		
}
function addCheckboxArrayCheck(n,m,o) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'12','0','0','',o,''));
		
}
function addCheckboxArrayCheckWithOtherField(n,m,o,e) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'12a','0','0',e,o,''));
		
}
function addCustomLessThanCheck(n,m,u,l) {
	var tmp;
	
	tmp = fi.push(new formItems(n,m,'c1',u,l,'','',''));
		
}
function setText(el,str) {
	if ('string' == typeof el.textContent) {
		 el.textContent = str;
	} else if ('string' == typeof el.innerText) {
		el.innerText = str;
	}
}
