/*************************************************************
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 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 "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 valemail(obj,msg){
	if (obj.value != ""){
    	if((obj.value.indexOf('@') == -1)||(obj.value.indexOf('.') == -1)) {
	        setColor(obj, bgBad);
			if (foundError == false) obj.focus(); foundError = true;
			msgShow = msgShow + msg;
			return true;
		} 
		else {
			var atpos = obj.value.indexOf('@');
			var rstring = obj.value.substring(obj.value.indexOf('@')+1,obj.value.length); 		//substring after @ sign
			
			if(atpos < 1) {										// check variable before @ sign
				setColor(obj, bgBad);
				if (foundError == false) obj.focus(); foundError=true;
				msgShow = msgShow + msg;
				return true;
			}
			
			if((rstring.indexOf('@')) >= 0) {					//check variable for 2 @ signs
				setColor(obj, bgBad);
				if (foundError == false) obj.focus(); foundError=true;
				msgShow = msgShow + msg;
				return true;	
			}
			
			if((rstring.indexOf('.')) <= 1) {					//check variable for . after @ sign
				setColor(obj, bgBad);
				if (foundError == false) obj.focus(); foundError=true;
				msgShow = msgShow + msg;
				return true;	
			}
			
			if(((obj.value.charAt(obj.value.length - 1)) == '.')||((obj.value.charAt(obj.value.length - 2)) == '.')){					//check variable for 2 spaces after . sign
				setColor(obj, bgBad);
				if (foundError == false) obj.focus(); foundError=true;
				msgShow = msgShow + msg;
				return true;	
			}
		}
	}
}
