﻿// JScript File

//****************************************************************
//File Name     :AjaxScripts.js.js  (containing all the function used to generate the AJAX request and parse the response XML.
//Function Types: 1. Ajax  (Function generate the Ajax request and Get the HttpXML Response. Please Note , Should not make any any in those Functions)
//                   1.1 show_data
//                   1.2 stateChangeHandler
//                   1.3 xmlHttp_Get
//                   1.4 GetXmlHttpObject
//
//                2. Non- Ajax (Functions used for pasing the XML data and filling data on the page. User can change those or make new functions to parse the XML )
//                   2.1 FillControl
//                   2.2 FillTable
//                   2.3 FillDropDown
//                   2.4 FillTab
// 
// Type         : Java Script file
//
// Author       : Md. Akmal Hussain
//*********************************************************************** 

    var xmlHttp; 
    //var requestURL = 'http://localhost/AjaxTest/Default.aspx?q=';   // Uncomment when testing with IIS or metion the IP when deploying on Web Server
    var requestURL = '../Master_Ajax.aspx?q='; // Uncomment it executing on asp.net web server  
    var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
    var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
    var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
    var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 
    var target = '';
    var fName='';
    var strs;
    var tab
    var strId ;
   function show_data(valueToSearch,valueFrom,valueCriteria, valueFromCache, targetName,formsName,tabFilling,Id, Name, Code)
    { 
      //****************************************************************
      //Function Name :show_data  (to form and send Ajax Request
      //Parameters    : valueToSearch (Query String)
      //                targetName (Where to Fill)    
      //                formsName  (Which is the Form) 
      //                tabFilling  (Weather value is filling the Tab (Y/N)
      //
      // Type         : Ajax function
      //
      // Author       : Md. Akmal Hussain
      //***********************************************************************    
        strId = Id;       
       
        var strName= valueToSearch; //Assigninng the Value to variable which will be used for Query String
		    if (strName.length > 0)
		    { 
                //Append the name to search for to the requestURL 
                var url = requestURL + strName + '&q2=' + valueFrom + '&q3=' + valueCriteria + '&q4=' + valueFromCache + '&q5=' + Name + '&q6=' + Code; 
                target = targetName;  // Assigning all the value to global variable for this file
                fName= formsName;
                tab=tabFilling;
              
             
            //Create the xmlHttp object to use in the request 
            //stateChangeHandler will fire when the state has changed, i.e. data is received back 
            // This is non-blocking (asynchronous)
			
			xmlHttp = GetXmlHttpObject(stateChangeHandler); 
            
            //Send the xmlHttp get to the specified url 
            xmlHttp_Get(xmlHttp, url); 
            
            
            } 
         
    } 

    //stateChangeHandler will fire when the state has changed, i.e. data is received back 
    // This is non-blocking (asynchronous) 
    function stateChangeHandler() 
    { 
        
      //****************************************************************
      //Function Name :stateChangeHandler  
      //Parameters    :
      //
      //
      // Type         : Ajax function               
      //               
      //Author        : Md. Akmal Hussain               
      //
      //*********************************************************************** 
        //readyState of 4 or 'complete' represents that data has been returned 
        
     
        if ((xmlHttp.readyState == 4) || (xmlHttp.readyState == 'complete'))
        {
            if (xmlHttp.status == 200)
            {                if (tab=="Y") // Request has been generated to fill the tab or not
                {
                    FillTab();
                }
                else
                {
                    FillDropDown();  
                }  
            }
             else
            {
                alert('There was a problem retrieving the data:\n'+xmlHttp.statusText);
            } 
         }      
      }   
    
    // XMLHttp send GET request 
    function xmlHttp_Get(xmlhttp, url) 
    { 
      //****************************************************************
      //Function Name :xmlHttp_Get : to get the Http values
      //Parameters    :xmlhttp (xml response from http)
      //               url (where to put request) 
      //
      // Type         : Ajax function               
      //
      //Author        : Md. Akmal Hussain               
      //
      //*********************************************************************** 
        xmlhttp.open('GET', url, true); 
        xmlhttp.send(null); 
    } 

    function GetXmlHttpObject(handler) 
    { 
      //****************************************************************
      //Function Name :GetXmlHttpObject (to Get hte xml Resonse according to the browser type)
      //Parameters    :xmlhttp (xml response from http)
      //               url (where to put request) 
      //
      // Type         : Ajax function
      //               
      //Author        : Md. Akmal Hussain               
      //
      //*********************************************************************** 
       
        var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

        //Depending on the browser, try to create the xmlHttp object 
        if (is_ie){ 
            //The object to create depends on version of IE 
            //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
            var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
             
            //Attempt to create the object 
            try{ 
                objXmlHttp = new ActiveXObject(strObjName); 
                objXmlHttp.onreadystatechange = handler; 
            } 
            catch(e){ 
            //Object creation errored 
                alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
                return; 
            } 
        } 
        else if (is_opera){ 
            //Opera has some issues with xmlHttp object functionality 
            alert('Opera detected. The page may not behave as expected.'); 
            return; 
        } 
        else{ 
            // Mozilla | Netscape | Safari 
            objXmlHttp = new XMLHttpRequest(); 
            objXmlHttp.onload = handler; 
            objXmlHttp.onerror = handler; 
           
        } 
         
        //Return the instantiated object 
        return objXmlHttp; 
    } 

    function UseValue(strVal){ 
        document.frmStuff.txtName.value = strVal; 
    } 



 function FillControl(control)
 {
    //****************************************************************
      //Function Name :FillControl (To Identify where to fill the result )
      //Parameters    :control (Which type of control to fill in case of List/Dropdown or Table)
      //               
      //
      // Type         : Non -Ajax function
      //               
      //Author        : Md. Akmal Hussain               
      //
      //*********************************************************************** 
    
    if (control=='T') // for Table control
        {
            FillTable();
        }
     else
     {
            FillDropDown();
     }
        
 
 }


    
    
    
    function FillDropDown()
    {
      //****************************************************************
      //Function Name :FillDropDown (To Fill the Dropdown /listbox)
      //Parameters    :
      //               
      //
      // Type         : Non -Ajax function
      //               
      //Author        : Md. Akmal Hussain               
      //
      //*********************************************************************** 
        
       // code for IE
		if (window.ActiveXObject)
		  {
	          xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		      xmlDoc.async=false;
              xmlDoc.load(xmlHttp.responseXML);
              
              // Adding the XML Parsing here
               var strFName = fName;
               var strEName = target;   //xSel.childNodes[1].firstChild.nodeValue;
               //var objDDL = document.forms[fName].elements(target);Commented by Vinod due to Error

               var objDDL =  document.getElementById(target); 
               objDDL.options.length = 0;
               var xRows = xmlDoc.getElementsByTagName('Entry');
               var strCityIdCodeName; 
               var theText;
               var theValue ;
               var option ;
               
               for(i=0;i<xRows.length;i++) 
                {
                    theText = xRows[i].childNodes[0].firstChild.nodeValue;
                    theValue = xRows[i].childNodes[1].firstChild.nodeValue;
                    theValue = theValue+'~'+theText;
                    
                    try
                    {
                        //objDDL.add(option,null); Commented by Vinod due to Error
                       // objDDL.add(option);                                
                        strCityIdCodeName = theValue.split('~');
                        var CityId = strCityIdCodeName[0];   
                        
                        if(CityId==strId)
                        {
                            option = new Option(theText,theValue,false,true);
                            objDDL[objDDL.length] =option;
                            
                        }                        
                        else
                        {
                            option = new Option(theText,theValue,false,false);
                            objDDL[objDDL.length] =option;
                           
                        }
                         
                    }
                    catch (e)
                    {
                        objDDL.add(option,-1);
                    }
                }
          
              
		  }
			// code for Mozilla, Firefox, Opera, etc.
		else if (document.implementation && document.implementation.createDocument)
		  {
			  xmlDoc= xmlHttp.responseXML;
		  	  var objDDL =  document.getElementById(target);
              objDDL.options.length = 0;
              var xRows = xmlDoc.getElementsByTagName('Entry');
              for(i=0;i<xRows.length;i++)
                {
            
                    var theText = xRows[i].childNodes[0].firstChild.nodeValue;
                    var theValue = xRows[i].childNodes[1].firstChild.nodeValue;
                    var option = new Option(theText,theValue);
                    try
                    {
                        objDDL.add(option,null);
                    }catch (e){
                        objDDL.add(option,-1);
                    }
                }
  		   }
    }
    
    function FillTab()
    {
                
        
		if (window.ActiveXObject) // code for IE
		{
	      
	          xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		      xmlDoc.async=false;
              xmlDoc.load(xmlHttp.responseXML);
        }
        else if (document.implementation && document.implementation.createDocument) // for Mozilla and Netscape
        {
              xmlDoc= xmlHttp.responseXML;
        }
          
          //XML Parsing here
           var strFName = fName;
           var strEName = target;   
           //-------------------------------------
            document.getElementById('OwnedBy').value = xmlDoc.getElementsByTagName('OwnedBy')[0].firstChild.nodeValue;
            document.getElementById('TravelFromDate').value = xmlDoc.getElementsByTagName('TravelFromDate')[0].firstChild.nodeValue ;
            document.getElementById('AgencyName').value = xmlDoc.getElementsByTagName('AgencyName')[0].firstChild.nodeValue ;
            document.getElementById('TravelToDate').value = xmlDoc.getElementsByTagName('TravelToDate')[0].firstChild.nodeValue ;
            document.getElementById('XdeptDate').value = xmlDoc.getElementsByTagName('XdeptDate')[0].firstChild.nodeValue ;
            document.getElementById('Nights').value = xmlDoc.getElementsByTagName('Nights')[0].firstChild.nodeValue ;
            document.getElementById('ExpiryDate').value = xmlDoc.getElementsByTagName('ExpiryDate')[0].firstChild.nodeValue ;
            document.getElementById('SalesRep').value = xmlDoc.getElementsByTagName('SalesRep')[0].firstChild.nodeValue ;
            document.getElementById('TravlingFrom').value = xmlDoc.getElementsByTagName('TravlingFrom')[0].firstChild.nodeValue ;
            document.getElementById('TotalAdult').value = xmlDoc.getElementsByTagName('TotalAdult')[0].firstChild.nodeValue ;
            document.getElementById('AgencyContactPerson').value = xmlDoc.getElementsByTagName('AgencyContactPerson')[0].firstChild.nodeValue ;
            document.getElementById('DocumentProf').value = xmlDoc.getElementsByTagName('DocumentProf')[0].firstChild.nodeValue ;
            document.getElementById('TotalChild').value = xmlDoc.getElementsByTagName('TotalChild')[0].firstChild.nodeValue ;
            document.getElementById('AgencyAddress').value = xmlDoc.getElementsByTagName('AgencyAddress')[0].firstChild.nodeValue ;
            document.getElementById('TotalInfant').value = xmlDoc.getElementsByTagName('TotalInfant')[0].firstChild.nodeValue ;
            document.getElementById('TotalAdult').value = xmlDoc.getElementsByTagName('TotalAdult')[0].firstChild.nodeValue ;
            document.getElementById('FileOk').checked = xmlDoc.getElementsByTagName('FileOk')[0].firstChild.nodeValue ;
          
           // Filling the Drop down from XML here          
            var objDDL = document.getElementById('MarktSrc');
            objDDL.options.length = 0;
            var xRows = xmlDoc.getElementsByTagName('MarktSrc');
            for(i=0;i<xRows.length;i++)
            {
                var theText = xRows[i].childNodes[1].firstChild.nodeValue;
                var theValue = xRows[i].childNodes[0].firstChild.nodeValue;
                var option = new Option(theText,theValue);
                try
                {
                    objDDL.add(option,null);
                }catch (e){
                    objDDL.add(option,-1);
                }
            }
          
    }
    

