var visibilityGroup = 'let||sale';
var frmBorder = 'solid 1px red';
var frmBgColor = 'yellow';	
var frmBorderOrig = 'solid 1px #7F9DB9';
var frmBgColorOrig = '#ffffff';
var textColorOrig = '#333333';
var textWarningColor = 'red';


function chkForm(list) {
	var errors = '';
	var fi = new Array();
	var errNull = ' must be defined';
	//var frmBorder = 'solid 1px red'; Currently set globally for other functions use
	//var frmBgColor = 'yellow';
	if (!list) {
		list = '1';
	}
	/*
	 List your form elements from the bottom up, this way the focus will return to the uppermost field

		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.
	*/
	if (list=='1') {
		fi[0] = new formItems('type','You must select an enquiry type.','2','0','0','','0');
		fi[1] = new formItems('country_ref','You must select your country.','2','0','0','','0');
		fi[2] = new formItems('email','Email must be completed and valid.','1','0','0','','Email');
	} 

	for (i=0;i<fi.length;i++) {
		var em = document.getElementById(fi[i].name);
		if (visibilityGroup.indexOf(fi[i].visibilityGroup)<=0) {
		
			em.style.border = frmBorderOrig;
			em.style.backgroundColor = frmBgColorOrig;
			
			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 = document.getElementById(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 (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,v,e) {
	this.name = n;
	this.msg = m;
	this.type = t;
	this.upperVal = u;
	this.lowerVal = l;
	this.visibilityGroup = v;
	this.Equal = e;
}

function countChars() {
	var s = document.getElementById('summary');
	var t = document.getElementById('charNumber');

	t.innerHTML = 'Characters left ' + (200-s.value.toString().length);

	if (s.value.toString().length>200) {
		s.style.border = frmBorder;
		s.style.backgroundColor = frmBgColor;
		t.style.color = textWarningColor;
	} else {
		s.style.border = frmBorderOrig;
		s.style.backgroundColor = frmBgColorOrig;
		t.style.color = textColorOrig;

	}
}

