// GLOBAL COLOR VARIABLE 
var NS = window.NS || {};
DEFAULT_COLOR = "WHITE"  // FOR MANDATORY BUT EMPTY
EMPTY_COLOR = "#FF6666"  // FOR MANDATORY BUT EMPTY
WRONG_COLOR = "YELLOW"  // FOR WRONG VALUESvalidate
NS.ALLOW_FORM_SUBMIT=true; // For stopping form to submit.
NS.ALLOW_FORM_FILL=true; // For stopping form to fill or maximize.
NS.ALLOW_LINE_ADD=true; // For stopping form line to add just like that..
MAND_ARG_MISSING = 'Mandatory Argument Missing';
var W = window; //Window element
var D = document; // document element
function getEl(elementId,doc){
	try{
		if(!doc) doc = window.document;
		if (!elementId) {
	//		err(MAND_ARG_MISSING);
			alert(Gz(MAND_ARG_MISSING));
		}else{
			var handle = doc.getElementById(elementId);
			return handle;
		}
	}catch(e){}
}
function validate(form, responseContainerId, panelAction){
	if(! NS.ALLOW_LINE_ADD) {return false;}
	var returnMsg = true;
	var mandMsg = false;
	var errMsg = "";
//	var form = getEl(formId)
	var flds = form.elements
	for(i=0; i<flds.length;i++){
		fld = flds[i]
		switch(fld.className){
			case "txtSM" :
			case "txtNM" :
			case "txtBM" :
			case "txtLM" :
			case "txtHM" :
			case "txtNtM" :
			case "noteArM" :
			case "txtAddrM" :
			case "txtAraM" :if (fld.value=="") {
						  	fld.style.backgroundColor = EMPTY_COLOR;
						  	mandMsg=true;
						  	returnMsg = false
						  }else {
						  	fld.style.backgroundColor = DEFAULT_COLOR;
						  }
						  break;	
			case "txtFlNM" :if (fld.value=="") {
						  	fld.style.backgroundColor = EMPTY_COLOR;
						  	mandMsg=true;
						  	returnMsg = false
						  }else {
						  		var str=fld.value;
						  		var restricted=new Array('/','\\',':','*','?','\"','<','>','|','\'','.','\&');
								for(var count=0;count<restricted.length;count++){
									if(str.indexOf(restricted[count])>-1){
										fld.style.backgroundColor = WRONG_COLOR;		
										errMsg += ""  + fld.value+" "+ NOT_ALLOW_CHAR+"\n";
										returnMsg = false							
									}
								}
							  	fld.style.backgroundColor = DEFAULT_COLOR;
						  }
						  break;
			case "intSMC" :
			case "intNMC" :
			case "decSMC" :
			case "decNMC" :
			case "currSM":
			case "currM" :if (fld.value=="") {
						  	fld.style.backgroundColor = EMPTY_COLOR;
						  	mandMsg=true;
						  	returnMsg = false
						  }else {
						  	fld.style.backgroundColor = DEFAULT_COLOR;
						  }
						  break;

			case "phM" :if (fld.value.length>0 && fld.value.length<10){
						  	fld.style.backgroundColor = WRONG_COLOR;
						  	errMsg += "Invalid phone number ("+fld.value+") \n";
						  	returnMsg = false
						  }else {
						  	fld.style.backgroundColor = DEFAULT_COLOR;
						  }
						  break;

			case "intSM" :
			case "intNM" :if (fld.value=="") {
						  	fld.style.backgroundColor = EMPTY_COLOR;
						  	mandMsg=true;
						  	returnMsg = false
/*						  }else
						  if (isNaN(fld.value) || fld.value.indexOf(".")!=-1){
						  	fld.style.backgroundColor = WRONG_COLOR;
						  	errMsg += ""  + fld.value+" "+ NOT_INT+"\n";
						  	returnMsg = false
*/						  }else {
						  	fld.style.backgroundColor = DEFAULT_COLOR;
						  }
						  break;

			case "decSM" :
			case "decNM" :if (fld.value=="") {
						  	fld.style.backgroundColor = EMPTY_COLOR;
						  	mandMsg=true;
						  	returnMsg = false
/*						  }else
						  if (isNaN(fld.value)){
						  	fld.style.backgroundColor = WRONG_COLOR;
						  	errMsg += ""  + fld.value+" "+ NOT_DEC+"\n";
						  	returnMsg = false
*/						  }else {
						  	fld.style.backgroundColor = DEFAULT_COLOR;
						  }
						  break;				
			
			case "dtM" :if (fld.value=="") {
						  	fld.style.backgroundColor = EMPTY_COLOR;
						  	mandMsg=true;
						  	returnMsg = false
						  }
			case "dt" : var dateStr = fld.value;
						if(dateStr=="") break;
					  	var dt = getDateFromString(dateStr, dtFmt)
					  	if(dt == "Invalid Date"){
					  		fld.style.backgroundColor = WRONG_COLOR;
					  		errMsg += ""  + fld.value+" "+ DATE_FORMAT_ERROR+"("+dtFmt+")\n";
					  		returnMsg = false;
					  	}else{
					  		fld.style.backgroundColor = DEFAULT_COLOR;
					  	} 
					   break;
			
			case "cmbUM" : 			  
			case "cmbSM" :			  
			case "cmbBM" :			  
			case "cmbM" :if (fld.value==-1) {
						  	fld.style.backgroundColor = EMPTY_COLOR;
						  	mandMsg=true;
						  	returnMsg = false
						  }else {
						  	fld.style.backgroundColor = DEFAULT_COLOR;
						  }
						  break;
						  
/*			case "intS" :
			case "intN" :if (isNaN(fld.value) || fld.value.indexOf(".")!=-1 && fld.value!=""){
						  	fld.style.backgroundColor = WRONG_COLOR;
						  	errMsg += ""  + fld.value+" "+ NOT_INT+"\n";
						  	returnMsg = false
						  }else {
						  	fld.style.backgroundColor = DEFAULT_COLOR;
						  }
						  break;
						 
			case "decS" :
			case "decN" :if (isNaN(fld.value) && fld.value!=""){
						  	fld.style.backgroundColor = WRONG_COLOR;
						  	errMsg += ""  + fld.value+" "+ NOT_DEC+"\n";
						  	returnMsg = false
						  }else {
						  	fld.style.backgroundColor = DEFAULT_COLOR;
						  }
						  break;
*/			
			case "dtTG" :
						if (fld.value=="") {
						  	fld.style.backgroundColor = EMPTY_COLOR;
						  	mandMsg=true;
						  	returnMsg = false
						 	break; 	
						  }	
						var tmpDt = getDateFromString(fld.value, dtFmt)
						var today = new Date();
						if (tmpDt<today){
						  	fld.style.backgroundColor = WRONG_COLOR;
						  	errMsg += ""  + fld.value+" "+ NOT_GREATOR_DATE+"\n";
						  	returnMsg = false;
						  }else {
						  	fld.style.backgroundColor = DEFAULT_COLOR;
						  }
						  break;
			case "emailM" :
						if (fld.value=="") {
						  	fld.style.backgroundColor = EMPTY_COLOR;
						  	mandMsg=true;
						  	returnMsg = false
						  }else{	
							 var emailReg = /\w{2,}@\w{2,}.\w{2,}/;
							 var isEmail = emailReg.test(fld.value);
							 if(!isEmail){
							  	fld.style.backgroundColor = WRONG_COLOR;
							  	errMsg += "Invalid email ("+fld.value+") \n";
							  	returnMsg = false;
							 }else{
								fld.style.backgroundColor = DEFAULT_COLOR; 
							 }
						 }
						 break;
		 
		}
	}
	if(mandMsg || errMsg!=""){
	/*
		var errorMsgDiv = getEl('errorMsg');
		if(! errorMsgDiv){
		 	 errorMsgDiv = D.createElement("div");
			 errorMsgDiv.id = 'errorMsg';
			 form.parentNode.appendChild(errorMsgDiv);
		}
		 errorMsgDiv.className='valid';
		 if(mandMsg==true) errMsg= (errMsg!="")?""  + MAND_FILL + "\n" + errMsg:""  + MAND_FILL + "\n" ; 
		 errorMsgDiv.innerHTML = (!returnMsg)?errMsg:"";
	*/
		 if(mandMsg==true) errMsg= (errMsg!="")?""  + MAND_FILL + "\n" + errMsg:""  + MAND_FILL + "\n" ; 
		 var alStr = (!returnMsg)?errMsg:"";
		 alert(alStr)
		 NS.ALLOW_FORM_SUBMIT=false;
	 }else{
		 NS.ALLOW_FORM_SUBMIT=true;
//		if(errorMsgDiv) errorMsgDiv.parentNode.removeChild(errorMsgDiv);
	}
	if((responseContainerId || (responseContainerId=='')) && returnMsg){
	  if(responseContainerId=='popout'){
	  	
		var paramStr = "width="+RPOW.w;
		paramStr += ",height="+RPOW.h;
		paramStr +=",resizable=1,scrollbars=yes";

	  	var winName = "rpt" + Math.random();
	  	var p = secWindow("id:"+winName,"size:RPOW");
//	  	var p = window.open("",winName,paramStr);
//	  	p.document.write(PG_LOADING);
		p.body.contentWindow.document.write(PG_LOADING);
//	  	form.target= winName;
		
	  	form.target= p.body.id;
	  	return returnMsg;
	  	
	  }
	  returnMsg = false; //This is made false to prevent the form to submit...Manish: 
	  if(NS.ALLOW_FORM_SUBMIT){
//	  	ajaxpage(form.action, responseContainerId,'', dataStr);

		var con = new NS.util.Ajax.thread();
		con.url = form.action;
//		con.title="Hello";
		con.form = form;

		con.allowCache = false;
		var cPanel = NS.util.Panel.focused; // panel of form
		con.onLoad = function(){
			if(con.exception) {
				NS.FORM_SUBMIT_SUCCESS=false;
				if(currPanel){
				 	currPanel.panelElement.body.innerHTML = con.responseText;
				 	currPanel.activate();
				 }
			}else{
				var containerTar = getEl(responseContainerId);
				if(containerTar){
					containerTar.innerHTML = con.responseText;
//					getScriptFromText(con.responseText);
					getScriptFromTarget(containerTar);
				}else if(responseContainerId) eval(con.responseText);

				if(panelAction && currPanel){
					panelAction = panelAction.split(":");
					if(panelAction[0]=="panel"){
						if(panelAction[1]=="close"){
							cPanel.close();
						}
						if(panelAction[1]=="minimize"){
							cPanel.minimize();
						}
					}
				}

			}
		}
		
		con.get();

	  }
	  
	}
	return returnMsg;
}

/*
 * Submits the form after checking onsubmit of the form;
 * Should be fired from a field
 * Warning: Not applicable where btn action is checked
 */
function submitThisForm(el){
	try{
		var form = el.form
		if(form.onsubmit.call(form))form.submit();
	}catch(e){le(e)}
}
