

var validators = new Array();


function AddValidator(obj, check, msg) {
	val = new Object();
	val.objName = obj;
	val.obj = document.getElementsByName(obj);
	val.check = check;
	val.msg = msg.split("|");
	validators[validators.length] = val;
}


function CheckRequired(objs) {
	ok = true;
	for (jj = 0; jj < objs.length ; jj++ )	{
		if ( (objs[jj].type == "text") || (objs[jj].type == "textarea")) {
			ok = (objs[jj].value.length > 0);
		}
		if ( (objs[jj].type == "radio") || ( objs[jj].type == "checkbox")){
			if ( jj == 0) ok = false;
			if ((ok == false) && (objs[jj].checked))	 ok = true;
		}
	}	

	return ok;
}
function CheckMin(objs, value) {
	ok = true;
	cnt = 0;
	for (jj = 0; jj < objs.length ; jj++ )	{
		if ( (objs[jj].type == "text") || (objs[jj].type == "textarea")) {
			cnt = (objs[jj].value.length);
		}
		if ( (objs[jj].type == "radio") || ( objs[jj].type == "checkbox")){
			if (objs[jj].checked) cnt++;
		}
	}	
	ok =(cnt >= value);
	return ok;
}

function CheckMax(objs, value) {
	ok = true;
	cnt = 0;
	for (jj = 0; jj < objs.length ; jj++ )	{
		if ( (objs[jj].type == "text") || (objs[jj].type == "textarea")) {
			cnt = (objs[jj].value.length);
		}
		if ( (objs[jj].type == "radio") || ( objs[jj].type == "checkbox")){
			if (objs[jj].checked) cnt++;
		}
	}	
	ok =(cnt <= value);
	return ok;
}

function performValidate() {
	msg = "";
	for (i = 0;i < validators.length ; i++ ) {
	   valids = validators[i].check.split(";");
	   for (j = 0; j < valids.length ; j++ ) {
		  vals = valids[j].split("=");
		  switch (vals[0])   {
			case "req":
			case "required": 
				if (! CheckRequired(validators[i].obj) ) msg = msg + validators[i].msg[j] + "\n";
			    break;
			case "min":
				if (! CheckMin(validators[i].obj, vals[1]) ) msg = msg + validators[i].msg[j] + "\n";
			    break;
			case "max":
				if (! CheckMax(validators[i].obj, vals[1]) ) msg = msg + validators[i].msg[j] + "\n";
			    break;
		  }
	   }
	}

	return msg;
}
