﻿ // JScript File
/*
 Created By : Kedarnath Nayak
 This file is to be included(<scrict src="ajaxscript.js"/>) in all the pages whereever dropdownlist is to be filled without postback
 File Name            :      ajaxscript.js
 Description          :      To fill the dropdowns without postback and showing the layers without postback
 Modification History: 
   
*/
		  var xmlHttp=null;
		  var fillctlname;   //DropDownlist to be filled
	      var fillctl;       //Use in the function GetLayerInfo() For using in  the  ShowLayerInfo()
	      var qurl;          //Use in the function GetLayerInfo() for using in the ShowLayerInfo()
	      var pctlname;      //Use in the function GetLayerInfo() for using in the ShowLayerInfo()
	      var plstartno;      //Use in the GetLayerInfo1() for hinding the starting layer no
	      var layno;          //Use in the GetLayerInfo1() for hiding this no of layers


		   function GetXmlHttpObject() //Creates the XmlHttpObject
				{ 
				    
					var objXMLHttp=null;
					if (window.XMLHttpRequest)	
						{
							objXMLHttp=new XMLHttpRequest()
						}
					else if (window.ActiveXObject)
						{
							
							objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
						}
						
					return objXMLHttp;
				} 
				
				function stateChanged() //This function is called for populating the DropDownlists  by function fillselect()
				{ 
				  	
					    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
					    { 
					            var str=xmlHttp.responseText.toString();
//					            alert(str);
					            var select2=document.getElementById(fillctlname.id);
					            var ar=new Array();
					            ar=str.split('|');
					            if(ar.length>1)  //If More than one element retured then add it to the dropdown lists
					            {
               					    RemoveAllOptions(fillctlname); //Remove all the Options 
						            for(var i=0;i<ar.length-1;i++)
						            {
						                var e=document.createElement("option");
						                var el=ar[i].split(',')
						                e.value=el[0];
						                e.innerText=el[1];
						                select2.appendChild(e);
        						        
						            } //End For
                                }   //End if(ar.length>1)
                                else if(ar.length==1) //If No Record Returned
                                {
                                  RemoveAllOptions(fillctlname);  //Remove all the Options 
                                }  //End Else If
                            
				        }  //End of xmlHttp.readyState
				    
				    
				}
				
				function fillselect(ctlname,pfillctlname,url) //This function is called in the onChange event of the dropdownlist
				{
			       fillctlname=pfillctlname;
			      
			       if(document.getElementById(ctlname.id).selectedIndex>0)
			       {
			           var uid=document.getElementById(ctlname.id).value;
			           xmlHttp=GetXmlHttpObject();
    				   
				       if (xmlHttp==null) //If XmlHttpObject Could not be created
					    {
						    alert ("Browser does not support HTTP Request")
						    return
					    }   //End if if (xmlHttp==null)
    					
				        url=url+uid;
				        //alert(url);
				        xmlHttp.open("GET",url,false)
				        xmlHttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
				   	    xmlHttp.onreadystatechange=stateChanged
					    xmlHttp.send(null)
					}
					else if (document.getElementById(ctlname.id).selectedIndex==0)
					 {
					    RemoveAllOptions(fillctlname);
					 }    
					    return false;
				}
				
				
				function GetLayerInfo(gctlname,url,pfillctl,pqurl) /* Retrieve the no.of data for the layers to be shown */
				{
				    xmlHttp=GetXmlHttpObject();
				    fillctl=pfillctl;
					qurl=pqurl;
					pctlname=gctlname;
					//alert(document.getElementById(gctlname.id).id);
				    var paramvalue=document.getElementById(gctlname.id).value;
				   if (xmlHttp==null)
					{
						alert ("Browser does not support HTTP Request")
						return
					} 
				    url=url+paramvalue;
				    url=url+'&opt1=layer&opt2=layers'
				 //   alert(url);
				    xmlHttp.open("GET",url,false)
				    xmlHttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
			        xmlHttp.onreadystatechange=ShowLayerInfo
					xmlHttp.send(null)
				}
				
				function GetLayerInfoDyn(gctlname,trname,tdname,url,pfillctl,pqurl) /* Retrieve the no.of data for the layers to be shown */
				{
				
				    xmlHttp=GetXmlHttpObject();
				    fillctl=pfillctl;
					qurl=pqurl;
					tr=trname;
					td=tdname;
					pctlname=gctlname;
					//alert(document.getElementById(gctlname.id).value);
				    var paramvalue=document.getElementById(gctlname.id).value;
				   if (xmlHttp==null)
					{
						alert ("Browser does not support HTTP Request")
						return
					} 
				    url=url+paramvalue;
				    url=url+'&opt1=layer&opt2=layers'
				    
				   // alert(url);
				    xmlHttp.open("GET",url,false)
				    xmlHttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
			        xmlHttp.onreadystatechange=ShowLayerInfoDyn()
			         alert(tr);
					xmlHttp.send(null)
				}
				
				function ShowLayerInfoDyn()   //Displays or the Hides no. of Layers
				{ 
//				  alert(xmlHttp.readyState);
                    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
					{ 
					        var str=xmlHttp.responseText.toString();
					        var values=str.split(',');
					        //hidealllayersDyn(tr);
					         for(var i=0;i<3;i++) //HIde all the layers
					                {
					              
					                  document.getElementById(tr+i).style.display="None";
					                }
					        alert(values[1]-1)
					        for(var i=0;i<values[1]-1;i++) //display the no. of layers
					        {
					          document.getElementById(tr+i).style.display="";
					          //alert(document.getElementById('LTR'+i).id);
					        }
					         if(values.length==1)
					         { 
					           for(var i=0;i<3;i++) //HIde the no. of layers when first item(--select--) is selected
					                {
					                  document.getElementById(tr+i).style.display="None";
					                }
					         }
//					         document.getElementById('LCAP0').innerText=values[2];
//					         document.getElementById('LCAP1').innerText=values[3];
//					         document.getElementById('LCAP2').innerText=values[4];
                            for(var i=0;i<3;i++) //HIde the no. of layers when first item(--select--) is selected
                             {
                                var ctlno = parseInt(i) + parseInt(plstartno);
					            document.getElementById(td+ctlno).innerText=values[i+2];
                             }
					         fillselect(pctlname,fillctl,qurl);
					}
					alert(tr);
				}
				
                function GetLayerInfo1(gctlname,url,pfillctl,pqurl,startno,totno) /* Retrieve the no.of data for the layers to be shown along with the starting number of the layers*/
				{
				   
				    xmlHttp=GetXmlHttpObject();
				    fillctl=pfillctl;
					qurl=pqurl;
					plstartno=startno;
					pctlname=gctlname;
					layno=totno;
				    var paramvalue=document.getElementById(gctlname.id).value;
				   if (xmlHttp==null)
					{
						alert ("Browser does not support HTTP Request")
						return
					} 
				    url=url+paramvalue;
				    url=url+'&opt1=layer&opt2=layers'
				    xmlHttp.open("GET",url,false)
				    xmlHttp.onreadystatechange=ShowLayerInfo1
					xmlHttp.send(null)
				}
				
				function ShowLayerInfo1()   //Displays or the Hides no. of Layers
				{
				  if (xmlHttp.readyState==4)
					{ 
					        var str=xmlHttp.responseText.toString();
					        var values=str.split(',');
					        //document.getElementById("hidLayers").value=values[1];
					        //alert(values[1]);
					        //alert(document.getElementById("hidLayers").value);
					        hidealllayers1();
					        for(var i=0;i<values[1]-1;i++) //display the no. of layers
					        {
					            var ctlno = parseInt(i) + parseInt(plstartno);
					            document.getElementById('LTR'+ctlno).style.display="";
					        }
					         if(values.length==1)
					         { 
					           for(var i=0;i<3;i++) //HIde the no. of layers when first item(--select--) is selected
					                {
					                   var ctlno = parseInt(i) + parseInt(plstartno);
					                    document.getElementById('LTR'+ctlno).style.display="None";
					                    //document.getElementById('LTR'+i).style.display="None";
					                }
					         }
					         
					         for(var i=0;i<3;i++) //HIde the no. of layers when first item(--select--) is selected
                             {
                                var ctlno = parseInt(i) + parseInt(plstartno);
					            document.getElementById('LCAP'+ctlno).innerText=values[i+2];
                             }
                             
					         //document.getElementById('LCAP0').innerText=values[2];
					         //document.getElementById('LCAP1').innerText=values[3];
					         //document.getElementById('LCAP2').innerText=values[4];
					         
					         fillselect(pctlname,fillctl,qurl);
					}
				}
				
				function ShowLayerInfo()   //Displays or the Hides no. of Layers
				{
				    if (xmlHttp.readyState==4)
					{ 
					        var str=xmlHttp.responseText.toString();
					        var values=str.split(',');
					        hidealllayers();
					        //alert(values[1]-1)
					        for(var i=0;i<values[1]-1;i++) //display the no. of layers
					        {
					          document.getElementById('LTR'+i).style.display="";
					          //alert(document.getElementById('LTR'+i).id);
					        }
					         if(values.length==1)
					         { 
					           for(var i=0;i<3;i++) //HIde the no. of layers when first item(--select--) is selected
					                {
					                  document.getElementById('LTR'+i).style.display="None";
					                }
					         }
					         document.getElementById('LCAP0').innerText=values[2];
					         document.getElementById('LCAP1').innerText=values[3];
					         document.getElementById('LCAP2').innerText=values[4];
					         fillselect(pctlname,fillctl,qurl);
					}
				}
				
				
				function RemoveAllOptions(fillctlname)
				{
//				try
//				{
				  for(var i=document.getElementById(fillctlname.id).length;i>0;i--)
					  {
					    document.getElementById(fillctlname.id).remove(i);
//					    if (document.getElementById(fillctlname.id).options[i]!=null)
//					        document.getElementById(fillctlname.id).options[i]=null;
					  } //End For
					  if(document.getElementById(fillctlname.id).length==1)
					     document.getElementById(fillctlname.id).options[0].selectedindex=0;
//                }
//                catch(e)
//                {
//                }
//        lctl = document.getElementById(fillctlname.id);
//		var opt = document.createElement("option");
//		opt.value= "0";
//		opt.innerText = "-Select-";
//		lctl.appendChild(opt);

				}
				
				function hidealllayers()
				{
				   for(var i=0;i<3;i++) //HIde all the layers
					                {
					                  document.getElementById('LTR'+i).style.display="None";
					                }
				}
				function hidealllayersDyn(trname)
				{
				   for(var i=0;i<3;i++) //HIde all the layers
					                {
					                  document.getElementById(trname+i).style.display="None";
					                }
				}
				function hidealllayers1()
				{
				   for(var i=0;i<layno;i++) //HIde all the layers
                   {
                        var ctlno = parseInt(i) + parseInt(plstartno)
                        document.getElementById('LTR'+ctlno).style.display="None";
                   }
				}
				
				
				 function wait(millis) 
                    {
                        var date = new Date();
                        var curDate = null;
                        do { curDate = new Date(); } 
                        while(curDate-date < millis);
                    } 
                
    var flag;
    var valOut = "";
  function Exists(ctl,qno,url,msg,fl)
    { 
   
        flag = fl;
        var val=ctl.value;
        val = val.toUpperCase();
        Destctl = ctl;
        Message=msg;
        xmlHttp=GetXmlHttpObject();
        url=url + val + '&Qno='+qno;
       //alert(url);
        xmlHttp.open("GET",url,false)
        xmlHttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
        xmlHttp.onreadystatechange=ShowExists;
        xmlHttp.send(null);
        return valOut;
    }
    
    function ShowExists()
    {
 
        if (xmlHttp.readyState == 4)
        {
            var output = xmlHttp.responseText;
           
               //alert(output);
            if (flag == 0)
            {
                if (output > 0)
                {
                    alert(Message +' Already Exists');
                    Destctl.value="";
                    Destctl.focus();
                    valOut = '';
                }
            }
            else if (flag == 1)
            {
                if (output == 0)
                {
                    alert(Message +' does not Exists');
                    Destctl.value="";
                    Destctl.focus();
                    valOut = "false";
                }
                else
                {
                    valOut = "true";
                }
            }
            if (flag == 2)
            {
                if (output != '')
                {
                    valOut = output;
                    return output;
                }
            }
        }
    }
    
//   function test()
//   {
//    alert('test1');
//   } 
    
  var FillControl;
    
    function GetOutput(ctl,fillctl,qno,url)
    { 
        FillControl = fillctl;
        var val=ctl.value;
        xmlHttp=GetXmlHttpObject();
        url=url + "?id=" + val + '&Qno='+qno;
        xmlHttp.open("GET",url,false)
        xmlHttp.onreadystatechange=ShowOutput;
        xmlHttp.send(null)
    }
    
    
      
    
    function ShowOutput()
    {
        if (xmlHttp.readyState==4)
        {
            var val = xmlHttp.responseText;
            
              if (FillControl.type == "text")
                FillControl.value = val;
            else
               FillControl.innerText = val;
                
             if (val == '')
              FillControl.innerText = '';
             else
              FillControl.innerText = val;


        }
    }
    
//     function GetOutput1(ctl, fillctl,hidectl, qno, url)
//    {
//        FillControl = document.getElementById(fillctl);
//        HideControl = document.getElementById(hidectl);
//        var val=ctl.value;
//        xmlHttp=GetXmlHttpObject();
//        url=url + "?id=" + val + '&Qno='+qno;
//      // alert(url);
//        xmlHttp.open("GET",url,false)
//        xmlHttp.onreadystatechange=ShowOutput1;
//        xmlHttp.send(null)
//    }
//    
//    function ShowOutput1()
//    {
//        if (xmlHttp.readyState==4)
//        {
//            var val = xmlHttp.responseText;
//         // alert(val);
//            if (FillControl.type == "text")
//                FillControl.value = val;
//            else
//                FillControl.innerText = val;
//           if (val == '')
//               {
//                HideControl.style.display="";
//                FillControl.style.display="None";
//               }
//           else
//               {
//                HideControl.style.display="None";
//                FillControl.style.display="";
//               }
//        }
//    }