﻿ // 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: 
 //Modification by samita Sahoo on 21th august to add new function "fillDynQuery" for more than one ajax implementation in a page.
*/
		  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
				{
			//alert(url);
			//alert(pfillctlname);
			       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;
				        xmlHttp.open("GET",url,false)
				        xmlHttp.onreadystatechange=stateChanged
					    xmlHttp.send(null)
					
					}
					else if (document.getElementById(ctlname.id).selectedIndex==0)
					 {
					    RemoveAllOptions(fillctlname);
					 }    
					    return false;
				}
				
				
				//Modification by samita Sahoo on 13th august to add one more parameter ordname to fill data by order
					function fillselect1(ctlname,pfillctlname,url,ordname) //This function is called in the onChange event of the dropdownlist
				{
				//alert(url);
			    //alert(pfillctlname);
			       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+ "&check=" + ordname;
                        //alert(url);
				        xmlHttp.open("GET",url,false)
				        xmlHttp.onreadystatechange=stateChanged
					    xmlHttp.send(null)
					
					}
					else if (document.getElementById(ctlname.id).selectedIndex==0)
					 {
					    RemoveAllOptions(fillctlname);
					 }    
					    return false;
				}
				//End
				
				
				
				//'*********************FUNCTION TO FILL DROPDOWN LIST IN A PAGE WITH MORE THAN ONE AJAX IMPLEMENTATION*******
					
					function fillDynQuery(ctlname,pfillctlname,url,ordname,sesname) //This function is called in the onChange event of the dropdownlist
				{
				
				//alert(sesname);
			    //alert(pfillctlname);
			       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+ "&check=" + ordname +"&var=" + sesname;
				        //url=url+uid+ "&check=" + ordname;
				        // alert(url);
				        xmlHttp.open("GET",url,false)
				        xmlHttp.onreadystatechange=stateChanged
					    xmlHttp.send(null)
					
					}
					else if (document.getElementById(ctlname.id).selectedIndex==0)
					 {
					    RemoveAllOptions(fillctlname);
					 }    
					    return false;
				}
				//End
				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;
				    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.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
			        xmlHttp.onreadystatechange=ShowLayerInfo
					xmlHttp.send(null)
				}
				
                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();
					        for(var i=0;i<values[1]-1;i++) //display the no. of layers
					        {
					          document.getElementById('LTR'+i).style.display="";
					        }
					         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)
				{
				  for(var i=document.getElementById(fillctlname.id).length;i>0;i--)
					  {
					      document.getElementById(fillctlname.id).options[i]=null;
					  } //End For
					  if(document.getElementById(fillctlname.id).length==1)
					     document.getElementById(fillctlname.id).options[0].selectedindex=0;
				}
				
				function hidealllayers()
				{
				   for(var i=0;i<3;i++) //HIde all the layers
					                {
					                  document.getElementById('LTR'+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);
                    } 
                
