//Barry O'Kane
//ezone.co.uk
function validForm(frm,ar) {
	var msg=""; var er="";
	var flds = frm.elements;
	for (var i=0;i<ar.length;i++) {
	 var e = flds[ar[i][1]];
	 var v=""; var ok=true;
	 if (typeof(e)=="object" && (ar[i][3] || (!ar[i][3]&&msg==""))) {
	  switch (e.type) {
		case "text":
		 v=e.value;
		break;
		case "select-one":
		 v=e.options[e.selectedIndex].value;
		break;
	  }		
	  switch (ar[i][0]) {
		case "req": //required
			if (!validString(v,1,99,1)) ok=false;
		break;
		case "email":
			if (!validEmail1(v)) ok=false;
		break;
	  }
	  if (!ok) { msg += "\n" + ar[i][2]; if (er=="") er=e; }
	 }
	}
	if (msg=="") {
		return true;
	} else {
		alert("The following errors occurred with the information you entered:\n" + msg);
		er.focus();
		return false;
	}
	
}

//test for a valid string
// minl=min chars(default 0), maxl=max chars(default 99)
// s=1 will remove all spaces from the string
function validString(t,minl,maxl,s) {
 var ok=false;
 if (typeof(t)=="string") {
  if (!maxl) maxl=99;if (!minl) minl=0;
  if (s) t=t.replace(/\s/g,"");
  var l = t.length;
  if ((l<=maxl) && (l>=minl)) ok=true;
 }
 return ok;
}
//valid date? 
function validDate1(t) {
	if (t=="") return false;
	var re=/(\d{2})\/(\d{2})\/(\d{4})/;
	if (t.search(re)!=-1) return true;
	return false;
}
//valid email? 
function validEmail1(t) {
	if (t=="") return false;
	var re=/\S+@\S+\.\S+/;
	if (t.search(re)!=-1) return true;
	return false;
}
//returns "nan" if t is not valid number between min and max (inclusive)
//otherwise returns the number
//if f=1 tests using float
function validNum(t,min,max,f) {
	(f)?t=parseFloat(t):t=parseInt(t);
	if (t>=min&&t<=max) return t;
	return "nan";
}

