﻿// JScript File
    
    //mySpan.attachEvent("onclick", fpChgBackground)
    function setLayout()
    {
         if(window.navigator.appName == 'Microsoft Internet Explorer')
          { 
             document.attachEvent("onreadystatechange", setLayoutList);
             window.attachEvent("onresize", setLayoutList);
       
          }
        else    
           {
                window.addEventListener("load" , setLayoutList ,false);
                window.addEventListener("resize", setLayoutList,true);
            
           }
        
    }
    
    function setLayoutList()
    {
       setScroll(); 
      
       //alert("in lauout");
        var objTH = document.getElementById("trHeader");
        if(objTH == null)
              return;
              
        if(document.getElementById("_ListTable") ==null)
           return;
                 
        var objTR =  document.getElementById("_ListTable").rows[0];
        if(objTR == null)
              return;
        
        if(objTR.style.display !='none')
        {
            for(var cnt=0;cnt< objTR.cells.length-1 ;cnt++)
            {
               //alert(objTH.cells[cnt].offsetWidth  + ">>" + objTH.cells[cnt].offsetWidth );
               if(objTH.cells[cnt].offsetWidth > objTH.cells[cnt].offsetWidth )
               {     
                    objTR.cells[cnt].width= objTH.cells[cnt].offsetWidth;
               }
               else
               {
                 if(objTR.cells[cnt].offsetWidth >0)
                     objTH.cells[cnt].width = objTR.cells[cnt].offsetWidth;
               }
            }
        }
       
      
    }
    
    function setScroll()
    {
       if(window.frameElement != null)  
       {
         try{   
               if(window.navigator.appName == 'Microsoft Internet Explorer')
                    document.getElementById("_divListTable").style.height = window.frameElement.offsetHeight -95+ "px";
               else 
                    document.getElementById("_divListTable").style.height = window.frameElement.offsetHeight -96+ "px";
               
             if(   parseInt(document.getElementById("_divListTable").style.height) < (document.getElementById("_ListTable").offsetHeight))
                    {
                          document.getElementById("_ListTable").style.width ="98%";
                          document.getElementById("_divListTable").style.overflowY="scroll";
                          //alert("in1");
                    }
             else
                {
                    document.getElementById("_ListTable").style.width ="100%";
                   // alert("in2");
                }       
             }catch(e){}
       }
    }
    
    
        function sortList(fieldname,sortdirection)
        {
            document.getElementById("_sort").value = fieldname; 
             document.getElementById("_sortDir").value = sortdirection; 
             document.forms[0].submit();
        }
        function movePage(intRsPage)
        {	
            document.getElementById("_page").value  = intRsPage;
	    	document.forms[0].submit();
	    }
	    function _setListLayout()
	    {
	     //var obj = window.parent.document.getElementById("frm_Main") ;
	     var obj = window;
	     //alert(window.document.location.href); 
	     var obj1=document.getElementById("_divList") ;
    	 if(obj !=null)
    	 {
    	    
    	   // alert(obj.screen.availHeight); 
    	   // alert(obj.screen.height )
    	    //alert( obj.offsetWidth );
            //alert( obj.offsetHeight ); 
            //alert( obj.style.posHeight ); 
            //alert( obj.style.posWidth );
         }     
        //obj1.style.height = obj.offsetHeight - 100 ;
	    }
	    
	   function fnStandardActionSearchText(event,JSFunction,FunctionDoBeforeStandardAction,CallBackFunction,ValidateFormBeforeSubmit,doStandardAction)
	   {
	       // alert("in");
	        
	        if((event.which == 13) || (event.keyCode==13)  )
	        {
	             //alert("inEnter");
	             var objbtn;
	             var iid = event.srcElement.id ;
	            if(event.srcElement.id == "_search1")
	               objbtn = "document.getElementById('pafSearch1')";
	            else if(event.srcElement.id == "_search2")
	               objbtn = "document.getElementById('pafSearch2')";
	            else if(event.srcElement.id == "_search3")
	               objbtn = "document.getElementById('pafSearch3')";
	            else
	               objbtn = "document.getElementById('pafSearch0')";
	               
	            var strFunctionCall ="fnStandardActionSearch(" + objbtn+",'" + JSFunction +"','" + FunctionDoBeforeStandardAction +"','" + CallBackFunction +"',"+ ValidateFormBeforeSubmit +"," + doStandardAction +")";
	            try
	            {
	               eval(strFunctionCall);
	            }catch(e){}
	            //return;
	            //fnStandardActionSearch(objbtn,JSFunction,FunctionDoBeforeStandardAction,CallBackFunction,ValidateFormBeforeSubmit,doStandardAction);
	        }
	   }
	    
	    function fnStandardActionSearch(srcSearchBtn,JSFunction,FunctionDoBeforeStandardAction,FunctionDoAfterStandardaction,CallBackFunction,ValidateFormBeforeSubmit,doStandardAction)
        {
           if(JSFunction !="")
	      {
	         try{eval(JSFunction+"();");}catch(e){}
	         if(CallBackFunction!='')
	         {
	            if(ValidateFormBeforeSubmit)
	                __PAFValidatePostBack(CallBackFunction,"",document.forms[0]);
	            else
	                __PAFPostBack(CallBackFunction,"");
	         }
	      }
	      else
	      {       var val_FunctionDoBeforeStandardAction=true;
	              if(FunctionDoBeforeStandardAction !="")
	                   { 
	                      try{val_FunctionDoBeforeStandardAction = eval(FunctionDoBeforeStandardAction+"();");}catch(e){} 
	                   }     
	              if (!val_FunctionDoBeforeStandardAction) return;
    	        
	            //alert(document.getElementById("_search").value)
	            //********************* new change by Mrunal -- to allow multiple search groups
	             var currentSearch =new String(); 
	             var currentSearchGp ="";
	             var newSearchVal="";             
	             
	             if(srcSearchBtn.id =="pafSearch0")
	               { currentSearchGp = "_gp0:" ;  newSearchVal= document.getElementById("_search0").value;}
	             if(srcSearchBtn.id =="pafSearch1")
	               { currentSearchGp = "_gp1:" ; newSearchVal=document.getElementById("_search1").value; }
	             if(srcSearchBtn.id =="pafSearch2")
	               { currentSearchGp = "_gp2:" ;  newSearchVal = document.getElementById("_search2").value; }
	             if(srcSearchBtn.id =="pafSearch3")
	               { currentSearchGp = "_gp3:" ; newSearchVal = document.getElementById("_search3").value;}
	             
	             currentSearch= document.getElementById("_search").value;
	             //alert(currentSearchGp);
	             //alert(currentSearch);
	             var currentSearchInd = currentSearch.indexOf(currentSearchGp);
                    if(currentSearchInd!=-1 )
                    {
                      var tempIndex;
                       if( currentSearchInd==0)//case present at first
                       {
                          tempIndex=currentSearch.indexOf(";");
                          if(tempIndex == -1)
		                    currentSearch="";
	                      else	
                          currentSearch = currentSearch.substring( tempIndex+1);
                            
                       }
                       else if(currentSearch.indexOf(";",currentSearchInd )==-1)//present at last
                       {
                          currentSearch=currentSearch.substr(0, currentSearchInd-1);
                       }
                       else //in betwwen 2 
                       {
                          tempIndex=currentSearch.indexOf(";",currentSearchInd);
                          currentSearch =  currentSearch.substring(0,currentSearchInd) + currentSearch.substring(tempIndex+1); ;
                       }
                      // alert(currentSearch);
                    }
                      if(newSearchVal != "")
                      {
                        if(currentSearch =="")
                            currentSearch=currentSearchGp + newSearchVal;
                        else
                            currentSearch = currentSearch +";" + currentSearchGp + newSearchVal;
                      }
                    // alert(currentSearch);
	               document.getElementById("_search").value=currentSearch;
	            
	             //**************************************
	             
	             
	              
	               
	               if(document.getElementById("_lstSelectedId") != null)
	                 document.getElementById("_lstSelectedId").value="";
	               if(document.getElementById("_lstAction") != null)
	                 document.getElementById("_lstAction").value="";
	               if(document.getElementById("_lsteditId") != null)
	                 document.getElementById("_lsteditId").value="";
	               if(document.getElementById("_lstNewCount") != null)
	                 document.getElementById("_lstNewCount").value="0";
	         
	             
	         if(ValidateFormBeforeSubmit)
	           {
	              if(validateForm(document.forms[0]))
                  {
                       if(FunctionDoAfterStandardaction !="")
	                    { 
	                        try{eval(FunctionDoAfterStandardaction+"();");}catch(e){} 
	                    }     
	            
                     __PAFPostBack(CallBackFunction,"");
                  }  
	           }
	           else
	            {
	                  if(FunctionDoAfterStandardaction !="")
	                    { 
	                        try{eval(FunctionDoAfterStandardaction+"();");}catch(e){} 
	                    }     
	            
	                __PAFPostBack(CallBackFunction,"");
	            }
	         
	      }
	      
	         
        }
        
        function fnSetFilterValue(objSource,cnt)
        {
        
             var newFileter = "flt"+cnt+":";
             var newFilertValue;
           if(objSource.type=="select-one")
            {
                if(objSource.value != "")
                     newFilertValue = newFileter +  objSource.value; 
                else
                   newFilertValue= newFileter + "null";
            }
            else
             {
                if(objSource.type=="checkbox") //  || objSource.type="radio")
                   {
                        if(objSource.checked)
                        {
                         if(objSource.value == "" )
                            newFilertValue = newFileter + "1";
                         else
                         {
                          if(objSource.value == "1" )
                                newFilertValue = newFileter  + "1";
                          else
                              newFilertValue = newFileter + "0";
                          }
                        }
                        else
                        {
                              newFilertValue = newFileter + "null";
                        }
                   }     
                   else
                         newFilertValue = newFileter + objSource.value;
             } 
               var curFilter = new String();
                    curFilter = document.getElementById("hfilterField").value;
                    
            if(curFilter == "")
                   document.getElementById("hfilterField").value = newFilertValue ;
            else
              {      
                      var startpos = curFilter.indexOf(newFileter)
                      if(startpos!=-1)
                      {
                            var endpos = curFilter.indexOf(";",startpos) ;
                            if(endpos == -1 && startpos==0)
                            {
                              document.getElementById("hfilterField").value = newFilertValue
                            }
                            else if( endpos == -1 && startpos >0)
                            {
                              curFilter = curFilter.substring(0,startpos-1);
                              document.getElementById("hfilterField").value = curFilter + ";" + newFilertValue;
                            }
                            else if(startpos>0 && endpos!=-1)
                            {
                              curFilter = curFilter.substring(0,startpos) +   curFilter.substring(endpos+1,curFilter.length);
                              document.getElementById("hfilterField").value = curFilter + ";" +  newFilertValue;
                            }
                            else if(startpos==0 && endpos!=-1)
                            {
                              curFilter =  curFilter.substring(endpos+1,curFilter.length );
                              document.getElementById("hfilterField").value = curFilter + ";" +  newFilertValue;
                            }
                       }
                      else
                            document.getElementById("hfilterField").value = curFilter + ";" +  newFilertValue ;
                   }
                   
           
             
             // alert(document.getElementById("hfilterField").value );
              
             document.forms[0].submit(); 
           //}
           //catch()
        }
        
	    function fnCustomAction(JSFunction,CallBackFunction,ValidateFormBeforeSubmit)
	    {
	      
	      if(JSFunction !="")
	      {
	         //try{eval(JSFunction+"();")==false) return false;}catch(e){}
	          try{
	          if( eval(JSFunction+"()")==false) return false;
	         }catch(e){}
	      }   
	            if(ValidateFormBeforeSubmit)
	                __PAFValidatePostBack(CallBackFunction,"",document.forms[0]);
	            else
	                __PAFPostBack(CallBackFunction,"");
	
	    }
	    
	    function fnStandardAction(stdBtn,JSFunction,FunctionDoBeforeStandardAction,FunctionDoAfterStandardaction,CallBackFunction,ValidateFormBeforeSubmit,doStandardAction)
	    {
	             
	      if(JSFunction !="")
	      {
	         try{
	          if( eval(JSFunction+"()")==false) return false;
	         }catch(e){}
	         if(CallBackFunction!='')
	         {
	            if(ValidateFormBeforeSubmit)
	                __PAFValidatePostBack(CallBackFunction,"",document.forms[0]);
	            else
	                __PAFPostBack(CallBackFunction,"");
	         }
	      } 
	      else
	      {
	          var val_FunctionDoBeforeStandardAction=true;
	          if(FunctionDoBeforeStandardAction !="")
	               { 
	                  try{val_FunctionDoBeforeStandardAction = eval(FunctionDoBeforeStandardAction+"();");}catch(e){} 
	               }     
	          if (!val_FunctionDoBeforeStandardAction) return;
	          
	          if(doStandardAction ==  true)
	            { 
	              switch(stdBtn)
	              {
	                 case "AddNew" ://--
	                       if(document.getElementById("_TargetFrame") ==null )
                           {  fnAddNew();
                             return;  
                           }
                           else
                           {    OpenDetail('0');return;
                           }
                           break;
	                 case "Cancel" :
    	               if(document.getElementById("_lstSelectedId") != null)
	                     document.getElementById("_lstSelectedId").value="";
	                   if(document.getElementById("_lstAction") != null)
	                     document.getElementById("_lstAction").value="";
	                   if(document.getElementById("_lsteditId") != null)
	                     document.getElementById("_lsteditId").value="";
	                   if(document.getElementById("_lstNewCount") != null)
	                     document.getElementById("_lstNewCount").value="0";
	                     document.getElementById("_lstAction").value="";break;
	                 case "Delete" :
	                       document.getElementById("_lstSelectedId").value =  getSelectedId();
	                       if( document.getElementById("_lstSelectedId").value  =="")
	                       {    alert("No Record Selected");
	                            return; 
	                       }   
	                       document.getElementById("_lstAction").value="delete";
	                       break;
	                 case "Save" :
	                    document.getElementById("_lstAction").value="save";break;
	                 case "SaveandList" :
	                    document.getElementById("_lstAction").value="savelist";break;
	              }
	            }
	           
	           if(ValidateFormBeforeSubmit)
	           {
	              if(validateForm(document.forms[0]))
                  {
                       if(FunctionDoAfterStandardaction !="")
	                    { 
	                        try{ if(eval(FunctionDoAfterStandardaction+"();")==false) return;}catch(e){} 
	                    }     
	            
	                 
                     __PAFPostBack(CallBackFunction,"");
                  }  
	           }
	           else //of validateForm
	            {
	                  if(FunctionDoAfterStandardaction !="")
	                    { 
	                       try{ if(eval(FunctionDoAfterStandardaction+"();")==false) return;}catch(e){} 
	                    }     
	                     
	                __PAFPostBack(CallBackFunction,"");
	            }
	 
	      }//else
	     
	    }
	    
	    
	    
	    
	 
	   
	    
	    
	    function getSelectedId()
	    {
	       var i,obj,strId;
	       strId="";
	       for(i=0;i<document.forms[0].elements.length;i++)
	        {
	           obj=document.forms[0].elements[i];
		       if((obj.id).indexOf('chkId_')!= -1 && (obj.tagName).toUpperCase()=="INPUT" && (obj.type).toUpperCase()=="CHECKBOX" )
		       {
		         if(obj.checked)
		         {
		            if(strId=="")
		                strId+=(obj.id).replace('chkId_','');
		            else
		                strId+= "," + (obj.id).replace('chkId_','');
		                 
		         }
		       }
	        }
	        return strId; 
	    }
	    
        function OpenDetail(keyVal)
        {
           if(document.getElementById("_TargetFrame") ==null && keyVal=='0')
           { fnAddNew();
             return;  
           }
           
           var target  = document.getElementById("_TargetFrame").value;
           var strURL = document.getElementById("_LinkURL").value;
           var strKey = document.getElementById("_KeyName").value + "=" + keyVal;
           if(strURL.indexOf("?") == -1)
	           strURL += "?" + strKey ;
	       else
	           strURL += "&" + strKey ;
	      
	        
	        if(document.getElementById("_page")!=null)
	            strURL += "&_page=" + document.getElementById("_page").value;
	        if(document.getElementById("_search")!=null)
	            strURL += "&_search=" + document.getElementById("_search").value;
	        if(document.getElementById("hfilterField")!=null)
	            strURL += "&_filter=" + document.getElementById("hfilterField").value;
	        if(document.getElementById("_sort")!=null)
	            strURL += "&_sort=" + document.getElementById("_sort").value;
	        if(document.getElementById("_sortDir")!=null)
	            strURL += "&_sortDir=" + document.getElementById("_sortDir").value;
	     
	       
	        if(target!="")
	        {
	          try{ 
	                //eval(target).src= strURL; 
	                //window.parent.document.getElementById("iframe1").src = strURL;
	                   if(target == "_blank")
    	                    target = window.open();
    	                else
	                        target = eval(target);
	                    
                       target.location.href = strURL;           
	                
	            }
	          catch(e){}
	        }
	        else
	            document.location.href = strURL;
	        
        }
                
	    
function deleteRow(objImg)
{
  document.getElementById("_ListTable").deleteRow(objImg.parentElement.parentElement.rowIndex  ) ; 
  setNewRowsId();
}

 function setNewRowsId()
 {	    
    var strNewIds = new String(); 
    strNewIds="";
    var lstRows = document.getElementById("_ListTable").rows  ;
    for(var rcount=0; rcount<lstRows.length  ; rcount++)
     {
        if( (lstRows[rcount].id).indexOf("ltr_new_") != -1)
        {
            if( strNewIds !="") strNewIds += ",";
            strNewIds += (lstRows[rcount].id).replace("ltr_new_","");
        }   
     }
     document.getElementById("_lstnewId").value =strNewIds;
    //alert( document.getElementById("_lstnewId").value );
 }   
	    
 function fnAddNew()
 {
    //alert(document.getElementById("_lstNewCount").value);
    var newCount = eval(document.getElementById("_lstNewCount").value)  + 1; 
    document.getElementById("_lstNewCount").value  = newCount;
    strId = "ltr_" + "new_" + document.getElementById("_lstNewCount").value;
    if(document.getElementById(strId)==null)
    {
        var lstTr = document.getElementById("_ListTable").rows[document.getElementById("_ListTable").rows.length -1];
        var ObjTR  = document.createElement("TR");
        lstTr.parentNode.insertBefore( ObjTR,lstTr);
        ObjTR.id= strId;
        objTempTR = document.getElementById("tr_editTemplate");
        for(var cnt=0;cnt< objTempTR.cells.length ;cnt++)
        {
            var ObjTD  = document.createElement("TD");
            if(objTempTR.cells[cnt].style.display =='none')
                ObjTD.style.display ='none';
            ObjTD.className  = "Row";
            ObjTR.appendChild(ObjTD)
	
        }
    }
    else
    {
      document.getElementById(strId).style.display = '';
    }   
    fnEditDetail("new_" + document.getElementById("_lstNewCount").value);
    setScroll();
    setNewRowsId();
 }
 function fnEditDetail(strId)
 {
    //var cnt=startColumn;
    var isCalendarControl;
    var CalendarField;
    var isNew = false;
    if(strId.indexOf("new_") != -1)
		isNew = true;
		
    objTR = document.getElementById("ltr_" + strId);
    objTempTR = document.getElementById("tr_editTemplate");
    
    for(var cnt=0;cnt< objTR.cells.length ;cnt++)
    {
        isCalendarControl=false;
        CalendarField="";
        if(objTempTR.cells[cnt].innerHTML =="")
        {  if(isNew)
				objTR.cells[cnt].innerHTML ="&nbsp;"
           continue;
        }         
        var objvalue;
        var str = new String(); 
        if(isNew)
            objvalue = objTR.cells[cnt].innerHTML;
        else 
            objvalue = objTR.cells[cnt].firstChild.innerHTML;
            
       
       
        
        if(objTempTR.cells[cnt].firstChild.id =='img_delNewRow' && (!isNew) )
        {
            str ="";
            objTR.cells[cnt].innerHTML = str;
             continue;
        }
        else
        {
            str = objTempTR.cells[cnt].innerHTML; 
            while(str.indexOf( "_templateFld") != -1)
                str = str.replace("_templateFld" ,"_" + strId ); 
                
        }
        if(str.indexOf( "_CalendartemplateFld") > -1)
        {
            str = objTempTR.cells[cnt].innerHTML; 
            while(str.indexOf( "_CalendartemplateFld") != -1)
                str = str.replace("_CalendartemplateFld" ,"_" + strId ); 
          
          isCalendarControl=true;
          CalendarField = objTempTR.cells[cnt].firstChild.id;
          CalendarField = CalendarField.replace("_CalendartemplateFld","");
          
        }
        objTR.cells[cnt].innerHTML = str;
        if(isCalendarControl)
           eval( 'Sys.Application.add_init(function() {  $create(AjaxControlToolkit.CalendarBehavior, {"button":$get("'+CalendarField +'_'+strId+'_htmlImage"),"format":"dd-MM-yyyy","id":""}, null, null, $get("'+ CalendarField +'_'+strId+'"));});');
                                                        
     	if(objTR.cells[cnt].firstChild.tagName == "SPAN")
     	objTR.cells[cnt].removeChild(objTR.cells[cnt].firstChild );
        var newObj = objTR.cells[cnt].firstChild ; 
     	if(newObj != null)
     	    newObj.disabled=false;
     	if(newObj != null && (!isNew))
        {	
           //alert(objTR.cells[cnt].innerHTML);
           //alert("-"+newObj.id+"-");
            if(newObj.tagName.toLowerCase() == 'select')    
			{
			   for(var optcnt=0; optcnt<newObj.options.length ; optcnt++)
			   {
			      if(newObj.options[optcnt].innerHTML == objvalue)
			        newObj.options[optcnt].selected =true;
			   }			
			}
			else if	(newObj.tagName.toLowerCase() == 'input' && newObj.type.toLowerCase() =='checkbox')
			   {
			      if(objvalue =="1" || objvalue.toLowerCase() =="true" || objvalue.toLowerCase() =="yes" )
						newObj.checked = true;
			   }
			else if (isCalendarControl)
			  {
			    if(objvalue !="&nbsp;")
			        newObj.value = objvalue;
			  }
		    else
			   newObj.value = objvalue;
			
	    }
    }
    if(! isNew) 
    {
        if(document.getElementById("_lsteditId").value =="")
            document.getElementById("_lsteditId").value = strId;
        else       
            document.getElementById("_lsteditId").value +="," + strId;
    }       
 }
 

