//Global XMLHTTP Request object
var XmlHttp;
var i,j= 0;
var controlIndex=1;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttpFS()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}
 
//Gets called when country combo box selection changes
function OnFlightStatusOriginChange(cIndex,languageCode,PleaseSelect1) 
{

    controlIndex=cIndex;
     
     
	var countryList = document.getElementById(FlightStatusId+'_cmbOrigin');

    if(countryList==null)
    {
      countryList = document.getElementById(FlightStatusId+'_cmbOrigin');
    }
   
    if(countryList!=null)
	{
	    //Getting the selected country from country combo box.
	     var selectedCountry = countryList.options[countryList.selectedIndex].value;
          
  	    // URL to get states for a given country
	    var requestUrl = "/sites/Etihad/_layouts/Etihad/AjaxServer.aspx?mode=FlightStatus&SelectedAirport=" + selectedCountry + "&LanguageCode=" + languageCode ;
	    CreateXmlHttpFS();

	    // If browser supports XMLHTTPRequest object
	    if(XmlHttp)
	    {
		    //Setting the event handler for the response
		    //XmlHttp.onreadystatechange = HandleResponseFS;
    		
		    //Initializes the request object with GET (METHOD of posting), 
		    //Request URL and sets the request as asynchronous.
		    XmlHttp.open("GET", requestUrl, false);
    		
		    //Sends the request to server
		    XmlHttp.send(null)	
		    HandleResponseFS(PleaseSelect1);	
	    }
    }
}


//Called when response comes back from server
function HandleResponseFS(PleaseSelect2)
{
   
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			ClearAndSetCountryListItemsFS(XmlHttp.responseXML.documentElement,PleaseSelect2);
			
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetCountryListItemsFS(countryNode,PleaseSelect3)
{
   destList1 = document.getElementById(FlightStatusId+'_cmbDestination');
  
   if(destList1!=null)
   { 
  	//Clears the dest combo box contents.
	for (var count = destList1.options.length-1; count >-1; count--)
	{
	  destList1.options[count] = null;
	}
 
	var destNodes = countryNode.getElementsByTagName('AirportName1');

	var destNodesValue = countryNode.getElementsByTagName('DestinationAirportCode');
		
	var textValue; 
	var optionItem;
	var value;
	
	/*Start of Code for Please Select*/
	if(PleaseSelect3!=null)
	{
	optionItemPS = new Option( PleaseSelect3, "AS",  false, false);
    destList1.options[destList1.length] = optionItemPS;
    }
	/*End of Please Select Code*/
        
	//Add new dests list to the dest combo box.
	for (var count = 0; count < destNodes.length; count++)
	{
   		//textValue = GetInnerTextFS(destNodes[count]);
        textValue =destNodes[count].firstChild.nodeValue ;
        
   		//value = GetInnerTextFS(destNodesValue[count]);
   		value = destNodesValue[count].firstChild.nodeValue ;
   		
		optionItem = new Option( textValue, value,  false, false);
		
        destList1.options[destList1.length] = optionItem;
	} 
}

if(document.getElementById(FlightStatusId+'_cmbDestination')!= null )    
{ 
    var destList = document.getElementById(FlightStatusId+'_cmbDestination');
    
  	//Clears the dest combo box contents.
	for (var count = destList.options.length-1; count >-1; count--)
	{
	  destList.options[count] = null;
	}
 
	var destNodes = countryNode.getElementsByTagName('AirportName1');

	var destNodesValue = countryNode.getElementsByTagName('DestinationAirportCode');
		
	var textValue; 
	var optionItem;
	var value;
	
	/*Start of Code for Please Select*/
	if(PleaseSelect3!=null)
	{
	optionItemPS = new Option( PleaseSelect3, "AS",  false, false);
    destList.options[destList.length] = optionItemPS;
    }
	/*End of Please Select Code*/
		
	//Add new dests list to the dest combo box.
	for (var count = 0; count < destNodes.length; count++)
	{
   		//textValue = GetInnerTextFS(destNodes[count]);
        textValue =destNodes[count].firstChild.nodeValue ;
   		//value = GetInnerTextFS(destNodesValue[count]);
   		value = destNodesValue[count].firstChild.nodeValue ;
		optionItem = new Option( textValue, value,  false, false);
        destList.options[destList.length] = optionItem;
	}
	
}


//var destList2 = document.getElementById("cmbOrigin"+ (abc+1));
//if(destList2 != null)
//{
//for(l=0;l<destList1.length;l++)
//{
//    if(destList2[l].value==GetInnerTextFS(destNodesValue[0]))
//    {
//        destList2.selectedIndex = l ;
//        var m = abc + 1;

//CountryListOnChange(m,'en') ;
//        break;
//    }
//}
//}

}
   


function DestinationListOnChange(z)
{

}


//Returns the node text value 
function GetInnerTextFS (node)
{
    
	 return (node.textContent || node.innerText || node.text) ;
	//return(node.textContent);
	//return(node.innerHTML);
	//return(node.text);
}









