/* FORM VALIDATOR */

//onerror=handleErr;
var txt="";

function handleErr(msg,url,l)
{
	txt="There was an error on this page.\n\n";
	txt+="Error: " + msg + "\n";
	txt+="URL: " + url + "\n";
	txt+="Line: " + l + "\n\n";
	txt+="Click OK to continue.\n\n";
	alert(txt);
	return true;
}

function Validator(pName) {
	// VALIDATOR CONSTRUCTOR
	this.name				= pName;			// NAME OF THIS CLASS. MUST BE THE SAME AS OF THE HOLDER
	
	// MESSAGE LIST
	this.msgList 			= new Array();		// LIST OF ERROR MESSAGES PER FORM
	this.requiredFields 		= new Array();		// LIST OF REQUIRED FIELDS WHICH CANNOT BE LEFT BLANK
	
	this.thisFuncQueue		= new Array();		// FUNCTION QUEUE: LIST OF FUNCS TO RUN AT ONCE

	// CUSTOM MESSAGE LIST
	// 0 INDEX DENOTES DEFAULT TEXT
	this.c_msgList = new Object();
	this.c_msgList.onEmpty		= {0 : "This information is required."};
	// INVALID E-MAIL ADDRESS
	this.c_msgList.onInvalid	= {0: "Please enter a valid value."};

	
	// THIS OBJECT
	var thisObj = this;
	
	
	// FUNCTION HOLDER
	if(!window.FUNCHLD) window.FUNCHLD = new Array();
	var thisInd = FUNCHLD.length;
	FUNCHLD[thisInd] = this;
	
	
	
	// --------.--------.--------.--------.--------
	// FUNCTIONS
	// THIS FUNCTION IS TO CHECK FOR ERRORS
	// THE MESSAGE TO BE POSTED HAS THE CLASSID frm_message
	this.postMsg_q = function(pObjName, pMsg) {
		//do_check(pName, pMsg);
		thisObj.thisFuncQueue[thisObj.thisFuncQueue.length] = function() { thisObj.postMsg(pObjName, pMsg);}
	}
	
	
	// POST MESSAGE
	this.postMsg = function(pObj, pMsg) {
		// FIND THE PARENT ROW OF THAT FORM. THE CLASS NAME IS frm_row.
		// CONTINUE GOING TO PARENT BY PARENT UNTIL CLASSNAME IS frm_row OR TAG IS body
		if(pObj.charCodeAt) {
			var objFrm = document.getElementById("f_" + pObj);
			var objName = pObj;
		}
		else{
			var objFrm = pObj;
			var objName = thisObj.returnObjName(pObj);
		}
		
		//var objfrm	= document.getElementById("f_" + pObjName);
		var objParRow = objFrm;
		do {
			objParRow = objParRow.parentNode;
		} while(
			objParRow 								&&
			(" "+objParRow.className+" ").indexOf(" frm_row ") 	<  0 
		);
		
		// NOW HUNT FOR ALL SIBLINGS WITH CLASSNAME IS frm_msg
		if(objParRow) {
			var objMsg = objParRow.getElementsByTagName("DIV");
			for(var i=0; i<objMsg.length; i++) {
				if(objMsg[i].className == "frm_msg") 
					objMsg = objMsg[i];
			}
			
			
			
			// POST THE MESSAGE
			// PROCESS MESSAGE
			pMsg = processMsg (objName, pMsg);
			
			// THIS WILL EMBED A NEW DIV ELEMENT TO THE objMsg
			// THIS WILL CHECK FIRST THE objMsgText IF IT IS EXISTING
			var objMsgText = document.getElementById("frmmsg_" + objName);
			if(pMsg) {
				// INSERT A NEW DIV ELEMENT CONTAINING ERROR TO THE pMsg
				if(!objMsgText) {
					objMsgText = document.createElement("DIV");
					objMsgText.id = ("frmmsg_" + objName);
					objMsg.appendChild(objMsgText);
				}
				objMsgText.innerHTML = pMsg;
			}
			// REMOVE THE objMsgText IF pMsg IS BLANK
			else if(pMsg=="") {
				if(objMsgText) {
					objMsg.removeChild(objMsgText);
				}
				
				// CHECK THE OK BUTTON
			}
			//if(objName=="FirstName") alert(objMsgText);
			
			
			
	
			// CHECK STATUS OF POST
			// DISAPPEAR CLASS OF tr
			var t_strnew = objParRow.className + " ";
			t_strnew = t_strnew.replace(" hasmsg ", "");
			t_strnew = t_strnew.replace("  ", " ")
	
			// CLEAR NOTIFICATION CLASS
			if(objMsg.firstChild == null) {
				//objMsg.style.display = "none";
				objParRow.className = t_strnew ;
			}
			// ADD NOTIFICATION CLASS
			else {
				objParRow.className = t_strnew + " hasmsg";
			}
		}
		
	}
	
	// POST SPECIAL MESSAGE AS INDICATED IN this.c_msgList.cattype
	// POST DEFAULT TEXT FOR DEFAULT
	function processMsg (pObjName, pMsg) {
		// GATHER TEXT
		var t_errmsg =  (pMsg ? pMsg : "");
		var t_ConvType = "";
		
		// CHOOSE WHICH TO USE DEPENDING ON THE <!e:ERRTYPE> TAG
		// SEARCH TEXT WITH TAGS
		var allMatches = pMsg.match(/(<!e:[^<][A-Za-z0-9_]+>)/g);
		// PRINT ALL MATCHES
		if(allMatches) {
			var t_s_mspl = allMatches[0].split(":",2);
			var t_s_mspl = t_s_mspl[1].substr(0, t_s_mspl[1].length-1);
			
			t_ConvType = "on" + t_s_mspl;
		}
		
		
		// PROCESS MESSAGE REPLACEMENT
		// FOR DEFAULT
		if(t_ConvType && thisObj.c_msgList[t_ConvType]) {
			var t_errmsg = thisObj.c_msgList[t_ConvType][pObjName];
			if(!t_errmsg) 
				t_errmsg = thisObj.c_msgList[t_ConvType][0];
		}
		
		// RETURN PROCESSED TEXT
		return t_errmsg;
	}


	
	// ADD ERROR MESSAGE TO LIST:
	// THIS WILL BE USED WHEN FORM WAS LOADED.
	this.addMsg = function(pObjName, pMsg) {
		thisObj.msgList[pObjName] = pMsg;
	}
	
	// ADD TO LIST THE FORMS WHICH SHOULD NOT BE BLANK
	this.addRequiredFields = function(pObjName) {
		thisObj.requiredFields[pObjName] = true;
	}
	
	
	
	// RUN ALL THE QUEUED FUNCTIONS
	this.runFuncQueue = function() {
		for(var i=0; i<thisObj.thisFuncQueue.length; i++) {
			// TRY TO RUN THAT QUEUED FUNCTION
			try {
				thisObj.thisFuncQueue[i]();
			} catch(e) {
			}
		}
		
		// CLEAR FUNCTION QUEUE
		thisObj.thisFuncQueue = new Array();
	}
	
	
	
	// EXPERIMENTAL:
	// TO FIND IF pObj IS STRING, GET THE pObj[0]. IF TRUE, THEN THE pObj IS A STRING
	// VALIDATION FORM
	// CHECKING OF FORMS AFTER EDITING IS DONE HERE

	this.check = function(pObj) {
		// FIRST: CHECK THE LIST IF THE FORM IS REQUIRED TO BE FILLED
		// OBJECT NAME: THE NAME OF A DEFAULT FORM OBJECT IS f_OSKS
		// CHECK pObj IF IT IS AN OBJECT OR A STRING
		if(pObj.charCodeAt) {
			var objFrm = document.getElementById("f_" + pObj);
			var objName = pObj;
		}
		else{
			var objFrm = pObj;
			var objName = thisObj.returnObjName(pObj);
		}

		// CLEAR
		var t_errmsg = "";
		
		// CHECK FOR ERRORS
		if(thisObj.requiredFields[objName] && objFrm.value == "") 
			t_errmsg = "<!e:Empty>";
		
		// EXTERNAL CUSTOM CHECKING CAN BE CREATED BY CREATING
		// this.checkMore(pName) FUNCTION
		// this.checkMore MUST RETURN AN ERROR MESSAGE, IF ANY
		if(t_errmsg=="") {
			if(thisObj.checkMore)
				t_errmsg = thisObj.checkMore(objFrm);
		}
		

		// POST MESSAGE
		thisObj.postMsg(objFrm, t_errmsg);
	}



	// PREPARE THE OBJECT FOR BEING USED
	this.prepareObject = function(pObj) {
		// CHECK pObj'S STATUS
		if(pObj.charCodeAt) {
			var objFrm = document.getElementById("f_" + pObj);
			var objName = pObj;
		}
		else{
			var objFrm = pObj;
			var objName = thisObj.returnObjName(pObj);
		}
		
		// SET OBJECT PROPERTIES FOR THE OBJECT
		var func = function() {
			setTimeout("FUNCHLD["+thisInd+"].check('" + objName + "');", 100);
		}
		objFrm.onchange = 
		objFrm.onblur = func;

		// CLEAR PREVIOUS onclick EVENT
		objFrm.onclick = null;
		
		// RUN THE objFrm.onclick IF objFrm IS TYPE RADIO OR CHECKBOX
		if(objFrm.type=="radio" || objFrm.type.toLowerCase()=="checkbox") {
			objFrm.onclick = func;
			func();
		}
		
		// THIS FUNCTION WILL NEVER BE REPEATED AGAIN
	}
	
	
	
	// RETURN NAME OF OBJECT
	this.returnObjName = function(pObj) {
		var objName = pObj.name.split("_", 2);
		objName = objName[1];
		
		return objName;
	}
}

