/*
Usage: Form objects to be validated at end for mandatory fields
	alt attribute if = must means the field is mandatory
	alt attribute if =tab_must means the field is mandatory for that tab but not for the form
    title attribute must be specified=caption_tabno
*/
    function trim(text) {
		return text.replace(/^\s+/,'').replace(/\s+$/,'');
    }
 
 	function validation(frm) { 		
		var frmlen=frm.length;
		var lbl;
		var tabno; //stores the tab value
		for(var a=0;a<frmlen;a++) {	
			 	if(frm[a].attributes["keyAr"]) {
					if(frm[a].alt=="MUST" && frm[a].attributes["keyAr"].value.length > 0 && !frm[a].disabled) {		
						text=trim(frm[a].value)								
						lbl=frm[a].attributes["keyAr"].value.split("_");
						tabno='tab'+lbl[1]
						if(text=="") {							
							alert(lbl[0]+" cannot be empty");
							changeCont(lbl[1],tabno); //load the correct tab
							if(window.loadTab){
								loadTab(eval(lbl[1])); 
							}
							frm[a].focus();
							return false;
						}		
					}
					 if(frm[a].alt=="DCHECKBOX" && frm[a].attributes["keyAr"].value.length > 0) {
						 text=trim(frm[a].value)
						 checkedvalue = frm[a].checked
						 lbl=frm[a].attributes["keyAr"].value.split("_");
						 tabno='tab'+lbl[1]
						
						if(checkedvalue==false) {
							 alert(lbl[0]+" must be checked/selected ");
							  changeCont(lbl[1],tabno); //load the correct tab
							  if(window.loadTab){
								loadTab(eval(lbl[1])); 
							  }
							  frm[a].focus();
							  return false;
						 }
						 
					 }
					if(frm[a].alt=="PHONE" && frm[a].attributes["keyAr"].value.length > 0 && !frm[a].disabled) {		
						text=trim(frm[a].value)								
						lbl=frm[a].attributes["keyAr"].value.split("_");
						tabno='tab'+lbl[1]
						citycodetext = "";
						if(text.indexOf("-") != -1) {
							arrPhone = text.split("-");
							citycodetext = arrPhone[0] + "-";
						}
						if(text=="" || text == citycodetext) {
							alert(lbl[0]+" cannot be empty");
							changeCont(lbl[1],tabno); //load the correct tab
							if(window.loadTab){
								loadTab(eval(lbl[1])); 
							}
							frm[a].focus();
							return false;
						}		
					}
				} else { // Testing	 to be removed				
					if(frm[a].alt=="MUST" && frm[a].title.length > 0) {
						text=trim(frm[a].value)
						lbl=frm[a].title.split("_");
						tabno='tab'+lbl[1]
						if(text=="") {							
							alert(lbl[0]+" cannot be empty");
							changeCont(lbl[1],tabno); //load the correct tab
							if(window.loadTab){
								loadTab(eval(lbl[1])); 
							}
							frm[a].focus();
							return false;
						}		
					}
					
					 if(frm[a].alt=="DCHECKBOX" && frm[a].title.length > 0) {
						 text=trim(frm[a].value)
						 checkedvalue = frm[a].checked
						 lbl=frm[a].title.split("_");
						 tabno='tab'+lbl[1]
						
						if(checkedvalue==false) {
							 alert(lbl[0]+" must be checked/selected ");
							 changeCont(lbl[1],tabno); //load the correct tab
							 if(window.loadTab){
								loadTab(eval(lbl[1])); 
							 }
							 frm[a].focus();
							 return false;
						 }						 
					 }
				}	// End Testing			
			 }
	   return true;
	}
				
	//function to check if a tab is partially filled and needs to be completed			
	function IsTabFilled(frm,tabno)
	{
	    var frmlen=frm.length;
		var lbl;
		var field_count=0;
		var filled_field_count=0;
		var mandatory_field_count=0;
		var mandatory_filled_field_count=0;
		var percent_filled=0;
		var percent_mandatory_filled=0;
		for(var a=0;a<frmlen;a++)
	    {
			if(frm[a].attributes["keyAr"])
			{

				lbl=frm[a].attributes["keyAr"].value.split("_");
				if(tabno==lbl[1])
				{
				   field_count++;
				   text=trim(frm[a].value)
				   if(text!="") filled_field_count++;
				   if(frm[a].alt=="TAB_MUST")
				   {
					   mandatory_field_count++;
					   if(text!="")
					      mandatory_filled_field_count++;
				   }
			   }
	        }
	   }
		percent_filled=(filled_field_count/field_count)*100;
		percent_mandatory_filled=(mandatory_filled_field_count/mandatory_field_count)*100;
		if(percent_filled>=30||percent_mandatory_filled>=30)
		       return true;
	    else
		       return false;
		   
	}
	
	//function to validate mandatory information in a tab when save/save and add is clicked
	function tab_validation(frm,tabno) {		
		var frmlen=frm.length;
		var lbl;
		var tab='tab'+tabno;		
     	for(var a=0;a<frmlen;a++) {
			if(frm[a].alt=="TAB_MUST")
			{
				if (frm[a].attributes["keyAr"]) {
					lbl=frm[a].attributes["keyAr"].value.split("_");
					if(lbl[1]==tabno) {
						text=trim(frm[a].value)
						if(text==""){
							alert(lbl[0]+" cannot be empty");
							changeCont(tabno,tab);
							if(window.loadTab){
								loadTab(eval(lbl[1])); 
							}
							frm[a].focus();
							return false;
						}		
					}
				} else { // Testing to be removed
					lbl=frm[a].title.split("_");
				 	if(frm[a].alt=="TAB_MUST" && tabno==lbl[1]) {
						text=trim(frm[a].value)
						if(text=="") {
							alert(lbl[0]+" cannot be empty");
							changeCont(tabno,tab);
							if(window.loadTab){
								loadTab(eval(lbl[1])); 
							}
							frm[a].focus();
							return false;
						}		
					}
				} // End Testing
			}

			if(frm[a].alt=="TAB_MUST_PHONE")
			{
				if (frm[a].attributes["keyAr"]) {
					lbl=frm[a].attributes["keyAr"].value.split("_");
					if(lbl[1]==tabno) {
						text=trim(frm[a].value)
						citycodetext = "";
						if(text.indexOf("-") != -1) {
							arrPhone = text.split("-");
							citycodetext = arrPhone[0] + "-";
						}
						if(text=="" || text == citycodetext){
							alert(lbl[0]+" cannot be empty");
							changeCont(tabno,tab);
							if(window.loadTab){
								loadTab(eval(lbl[1])); 
							}
							frm[a].focus();
							return false;
						}		
					}
				}
			}

		}
		return true;
	}	
