/********************************************************************************************
 Author: Pier Paolo Manca
 Date: 08/04/2009
 
 Flight pop-up search - AJAX
 Retrieve informations for the search pop-up in the main flight section.
 
 Included by: /cheap-flights-from-dublin/cheap-flights-search.asp
********************************************************************************************/
var xmlHttp;
var commonTop;
var commonBottom;
var parseUrlDestinationType = 0;
var xWithScroll,yWithScroll;
var newObject;

/***********************************************/
/***********************************************/
/*			     HTTP Functions			       */
/***********************************************/
/***********************************************/

function searchDestinations(url, type){ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Your browser does not support AJAX!");
	  return;
	}
	parseUrlDestinationType = type;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged(){ 
	if (xmlHttp.readyState==4){ 
		switch(parseUrlDestinationType){
			case 0:
				document.getElementById("dialog").innerHTML = parseXML(xmlHttp.responseText);
				break;
			case 1:
				document.getElementById("dialog").innerHTML = xmlHttp.responseText;
				break;
		}
	}
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }catch (e){
		  // Internet Explorer
		  	try{
		    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}catch (e){
			    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		    }
	  }
	return xmlHttp;
}

/***********************************************/
/***********************************************/
/*			   Parsing Functions			   */
/***********************************************/
/***********************************************/

function getXmlDOC(text){
	var xmlDoc;
	
	try //Internet Explorer
 	{
		  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		  xmlDoc.async="false";
		  xmlDoc.loadXML(text);
	 }catch(e){
		  try //Firefox, Mozilla, Opera, etc.
		  {
			  parser=new DOMParser();
			  xmlDoc=parser.parseFromString(text,"text/xml");
		  }catch(e){
			  alert(e.message);
			  return;
		  }
	}
	
	return(xmlDoc);
}

function getHTMLDestinations(type){

	var text;
	var htmlTop;
	var commonTop;
	var commonBottom;
		
	commonTop = "<div id='form_search' align='center'>";
	commonTop += "<div align='right' style='border-bottom:1px solid #A48484; padding-bottom:3px;'>";
	commonTop += "<span style='cursor:pointer;' onclick='javascript:searchDestinations(\"/includes/ajax/flight-destinations.html\", 1);'>";
	commonTop += "<strong><img src='/images/icons/small/doubleBack.png' align='texttop' /> Back</strong></span>";
	commonTop += "</div>";
	
	commonBottom = "</div>";
	commonBottom += "<br />";
	commonBottom += "<div align='right'>";
	commonBottom += "&nbsp;";
	commonBottom += "</div>";
	commonBottom += "</div>";
	
	switch(type){
		case 0:
			htmlTop = "<h2>Results:</h2>";
			htmlTop += "<span class='fontSize10px'>Choose your destination and click on it.</span>";
			htmlTop += "<br /><br />";
			htmlTop += "<div id='results'>";
			text = commonTop + htmlTop;
			break;
		case 1:
			htmlTop = "<h2>No Results:</h2>";
			htmlTop += "<br />";
			htmlTop += "<div id='results'>";
			text = commonTop + htmlTop;
			text += "There are no cities matching this criteria. <br />Please try a different city/spelling.";
			text += commonBottom
			break;
		case 2:
			text = commonBottom;
			break;
	}

	return(text);
}

function parseXML(text){
	
	var xmlDoc = getXmlDOC(text);
			
	try{
		var htmlDoc = "";
		var length = 0;
		
		if(xmlDoc.getElementsByTagName("root")[0].childNodes[0]!=null)
			length = xmlDoc.getElementsByTagName("root")[0].childNodes.length;
		
		if(length != 0){
			htmlDoc = getHTMLDestinations(0);
		
			for (i = 0; i < length; i++){
				if(xmlDoc.getElementsByTagName("AirportName")[i].childNodes[0]!=null){
					htmlDoc += "<strong>";
					htmlDoc += "<div id='" + i + "' "; 
							htmlDoc += "style='cursor:pointer; padding:2px;'";
							htmlDoc += "onclick='javascript:changeInputTypeDestination(this.innerHTML);'";
							htmlDoc += "onMouseOver='javascript:this.style.backgroundColor=\"#EFCE77\";'";
							htmlDoc += "onMouseOut='javascript:this.style.backgroundColor=\"transparent\";'";
							htmlDoc += ">";
						htmlDoc += xmlDoc.getElementsByTagName("AirportName")[i].childNodes[0].nodeValue;
						if(xmlDoc.getElementsByTagName("AirportCode")[i].childNodes[0]!=null)
							htmlDoc += " (" + xmlDoc.getElementsByTagName("AirportCode")[i].childNodes[0].nodeValue + ")";
					htmlDoc += "</div>"
					htmlDoc += "</strong>";
				}
			}
		
			htmlDoc += getHTMLDestinations(2);
		}else{
			htmlDoc = getHTMLDestinations(1);			
		}
		
		return (htmlDoc);
	}catch(e){
		return (getHTMLDestinations(1));
	}
}

function changeInputTypeDestination(value) {
 // var oldObject = document.getElementById("to_list");
  var oldObject; 
  if(typeof(document.MainForm.menuArrAptCode) == "object")
  	oldObject = document.MainForm.menuArrAptCode;
  else
  	oldObject = document.getElementById("menuArrAptCode");
  
  if (oldObject != null){
  		newObject = document.createElement('input');
	  	newObject.type = "text";
  		if(oldObject.size) newObject.size = oldObject.size;
		if(oldObject.value) newObject.value = value;
		if(oldObject.name) newObject.name = oldObject.name;
		if(oldObject.id) newObject.id = oldObject.id;
		if(oldObject.className) newObject.className = "textfield hasDatepicker";
		newObject.readOnly = true
		/*newObject.style.marginTop="0px";*/
  
		oldObject.parentNode.replaceChild(newObject,oldObject);
  }else{
	    newObject.value = value;
  }
  
  document.getElementById("ArrAptCode").value = value;  
  $("#dialog").dialog('close');
}


function selectDestinationCity(textCity){
	document.getElementById("menuArrAptCode").Type="text";
	document.getElementById("menuArrAptCode").value=textCity;
	document.getElementById("txtArrAptCode").value=textCity;  
}



