/*************************************************************
Copyright (c) 2001 onwards 
Mian-Guan Lim (mg@mirageglobe.com)
http://www.mirageglobe.com

Free to use for commercial or personal applications
if copyright message remains intact.

Tested with 
- IE6
- Netscape 6.1

Validator ver 1.12
*************************************************************/


//variables

var bgBad = "#99CCFF";									// css background applied if error found
var bgGood = "#FFFFFF";									// css background applied if no error found
var msgShow = "Please complete the following mandatory fields:"; 	// message in pop up window
var foundError = false;									// boolean to determine if error is found


//main function
//********************************************************************
function Validate(checktype, obj, msg, obj2) {
	if(obj) setColor(obj, bgGood);
	switch (checktype){
	case "start" :		funcstart(obj);						break;
	case "end" : 		return funcend();					break;
	case "empty" :		return valempty(obj,msg); 			break;
	case "email" :		return valemail(obj,msg); 			break;
	case "numeric" :	return valnumeric(obj,msg); 		break;
	case "comp" : 		return valcomp(obj,msg,obj2);		break;
	default :
		alert ("Invalid checks");
	}
}

//settings sub functions
//********************************************************************
function setColor(obj, bgcolour) {
	if (obj.style) obj.style.backgroundColor = bgcolour;
}

function funcstart(obj) {
	if (obj){
		bgBad = obj;
	};
	msgShow = "Please complete the following fields:";
	foundError = false;
}

function funcend() {
	if (foundError == true){
		alert (msgShow); 
		return false;
	}
	else {
	return true;
	}
}


//validation sub functions
//********************************************************************
function valempty(obj,msg){
	if (obj.value == "") {
		setColor(obj, bgBad);
		if (foundError == false) obj.focus(); foundError=true;
		msgShow = msgShow + msg;
		return true;
	}
	else {
		return false;
	}
}

function valcomp(obj,msg,obj2){
	if (obj2.value == obj.value) {
		return false;
	}
	else {
		setColor(obj, bgBad);
		if (foundError == false) obj.focus(); foundError=true;
		msgShow = msgShow + msg;
		return true;
	}
}

function valnumeric(obj,msg){
	var strValidChars = "0123456789";
	var strChar;
	var strValid = true;

	if (obj.value != ""){
	   //  test strString consists of valid characters listed above
	   for (i = 0; i < obj.value.length && strValid == true; i++) {
	   		strChar = obj.value.charAt(i);
			  	if (strValidChars.indexOf(strChar) == -1){
				 	strValid = false;
				}
		}
	}
	else{
		strValid == true;
	}

	if (strValid == true){
		return true;
	}
	else{
		setColor(obj, bgBad);
		if (foundError == false) obj.focus(); foundError=true;
		msgShow = msgShow + msg;
		return false;
	}
}

function valnumericempty(obj,msg){
	var strValidChars = "0123456789.-";
	var strChar;
	var strValid = true;

	if (obj.value != ""){
	   //  test strString consists of valid characters listed above
	   for (i = 0; i < obj.value.length && strValid == true; i++) {
	   		strChar = obj.value.charAt(i);
			  	if (strValidChars.indexOf(strChar) == -1){
				 	strValid = false;
				}
		}
	}
	else{
		strValid == true;
	}

	if (strValid == true){
		return true;
	}
	else{
		setColor(obj, bgBad);
		if (foundError == false) obj.focus(); foundError=true;
		msgShow = msgShow + msg;
		return false;
	}
}

function valemail(obj,msg){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.value)){
return true;
}else{
	if (foundError == false) obj.focus(); foundError=true;
		msgShow = msgShow + msg;
return false;
}}

