


// generic form validation function
function FormCheck(mbox, useClassForErrors){
	this.mbox = mbox;
	this.useClassForErrors = useClassForErrors;
	
};

FormCheck.prototype.check = function(f, prompt, pwdLength){
	var r = true, el, i, u, t, msg = '', pwd, eColor = '#FFC588', checkboxes = {}, check;
	if (!pwdLength)
		pwdLength = [4,10];
	
	for (i=0; i<f.elements.length; i++){
		el = f.elements[i];

		
//		if (el.tagName == 'SELECT') {
//			console.log(i, el, el.tagName, el.options[el.selectedIndex].value);	
//		}
		
		
		if (!el) {
		    continue;
		}
		check = el.getAttribute('check'); // see what sort of element we're checking, and if it needs to be checked
		t = -1; // clear temporary message variable
		if (!check) {
		    continue; // no 'check' attribute means no checking required
		}
		else if(el.tagName == 'SELECT'){ // if element is a select, just check something has been selected
//			console.log("select:", el);
			if (!el.options[el.selectedIndex].value || el.options[el.selectedIndex].value == '...') {
				t = u ? '' : 'Please fill in all fields';
				u = true;
			}
		}
		else if(el.getAttribute('type') == 'checkbox' || el.getAttribute('type') == 'radio'){
			// if element is a checkbox, check it has been selected
			if (check == 'check-one'){
				// if check-one, need to store whether _one_ of the checkboxes of this name has been selected (see below)
				if (!checkboxes[el.getAttribute('name')])
					checkboxes[el.getAttribute('name')] = {'checked' : false, 'els' : []};
				if (el.checked)
					checkboxes[el.getAttribute('name')]['checked'] = true;
				checkboxes[el.getAttribute('name')]['els'].push(el.parentNode);
			}
			else {
				// otherwise all checkboxes must be selected
				if (!el.checked)
				t = u ? '' : 'Please fill in all fields';
				u = true;
			}
		}
		else if (check == 'email' && !this.isEmail(el.value)) // check for valid email address
			t = 'Email address is not valid';
		else if (
			parseInt(check) == check && el.value.length < check || // check regular element for min length
			check.charAt(0) == '/' && check.charAt(check.length - 1) == '/' && !el.value.match(new RegExp(check.substr(1, check.length - 2))) // test regex
		){ 
			t = u ? '' : 'Please fill in all fields';
			u = true;
		}
		else if (check == 'pwd' && !pwd && (el.value.length < pwdLength[0] || el.value.length > pwdLength[1])) // check pwd for max and min length
			t = 'Password must be between ' + pwdLength[0] + ' and ' + pwdLength[1] + ' characters';
		else if (check == 'pwd' && !pwd)
			pwd = el.value; // if no pwd found yet, store value for later comparison
		else if (check == 'pwd'){
			if (el.value != pwd){ // compare with last pwd field
				t = 'Password fields do not match';
			}
			pwd = ''; // clear pwd for use with next pair of pwd fields
		}
		
		if (this.useClassForErrors) {
			parentLi = el.parentNode;
			count = 0;
			
			while (count < 10 && parentLi.parentNode && (parentLi.tagName != "LI")) {
				parentLi = parentLi.parentNode;
				count++;
			}
//			label = parentLi.getElementsByTagName('label')[0];
//			console.log(parentLi, label);
			parentLi.className = ("" + parentLi.className).replace(/formerror/g, "");
//			label.innerHTML = ("" + label.innerHTML).replace(">>", "");

			if (t != -1){
				parentLi.className += " formerror";
//				label.innerHTML = ">> " + label.innerHTML;
				msg += t ? t + '\n' : '';
				r = false;
			}
		} else {
			// this really is a dumb way to do things, kept for backwards compatibility
			if (t != -1){
				msg += t ? t + '\n' : '';
				el.style.background = eColor;
				r = false;
			}
			else el.style.background = '';
		
			// special case for checkbox - colour around it
			if(el.getAttribute('type') == 'checkbox'){
				if (t != -1)
					el.parentNode.style.background = eColor;
				else
					el.parentNode.style.background = '';
			}
		}
		
	}
	
	for (var i in checkboxes){
		if (!checkboxes[i].checked){
			msg += 'You must select at least one of the highlighted checkboxes\n';
			r = false;
		}
		for (var j = 0; j < checkboxes[i].els.length; j++)
			checkboxes[i].els[j].style.background = checkboxes[i].checked ? '' : eColor;
	}
	
	if (msg && prompt)
			r = confirm('FORM ERROR:\n' + msg + '\n\nClick OK to submit anyway, or cancel to return to the form.');
	else if (msg){
			//console.log(('FORM ERROR:\n' + msg).replace(/\n/,'<br>'));
			if (this.mbox)
				this.mbox.showMessage(('FORM ERROR:\n' + msg).replace(/\n/g,'<br>'), 'neg');
			else
				alert('FORM ERROR:\n' + msg);
	}
	return r;
};

FormCheck.prototype.isEmail = function(m){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(m)) return true;
	return false;
};
FormCheck.prototype.isNum = function(a){
	return typeof a == 'number' && isFinite(a);
};



// used to kill form submission via spec- rather than going _return false_ in your onsubmit handler, go _return killEvent(e)_
function killEvent(e){
 if (e && e.preventDefault) {
    e.preventDefault(); // DOM style
  }
  return false; // IE style
};


