/********************************************************************************************
 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("popUpDestinations").innerHTML = parseXML(xmlHttp.responseText);
				break;
			case 1:
				document.getElementById("popUpDestinations").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'>";
	commonTop += "<span class='pointerCursor' onclick='javascript:searchDestinations(\"/includes/ajax/flight-destinations.html\", 1);'>";
	commonTop += "<strong><img src='/images/icons/small/doubleBack.png' align='texttop' /> Back</strong></span>";
	commonTop += "&nbsp;&nbsp;&nbsp;<span class='pointerCursor' onclick='javascript:HideDestinationsPopup()'>";
	commonTop += "<strong><img src='/images/icons/small/logout.png' align='texttop' /> Close</strong></span>";
	commonTop += "</div><hr size='1' color='#FFD37F' class='margin5px' />";
	commonTop += "<br />";
	
	commonBottom = "</p>";
	commonBottom += "<br />";
	commonBottom += "<div align='right'>";
	commonBottom += "&nbsp;";
	commonBottom += "</div>";
	commonBottom += "</div>";
	
	switch(type){
		case 0:
			htmlTop = "<div class='titleH2'>Results:</div>";
			htmlTop += "<span class='fontSize10px'>Choose your destination and click on it.</span>";
			htmlTop += "<br /><br /><hr size='1' color='#FFD37F' class='margin5px' />";
			htmlTop += "<p class='fontSize10px padding5px'>";
			text = commonTop + htmlTop;
			break;
		case 1:
			htmlTop = "<div class='titleH2'>No Results:</div>";
			htmlTop += "<br />";
			htmlTop += "<p class='fontSize10px padding5px'>";
			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 += "class='countrylinkGrey pointerCursor padding5px' ";
							htmlDoc += "onclick='javascript:changeInputTypeDestination(this.innerHTML);'>";
						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.to_list) == "object")
  	oldObject = document.MainForm.to_list;
  else
  	oldObject = document.getElementById("to_list");
  
  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 = oldObject.className;
		newObject.readOnly = true
		newObject.style.marginTop="-5px";
  
		oldObject.parentNode.replaceChild(newObject,oldObject);
  }else{
	    newObject.value = value;
  }
  
  document.getElementById("other").value = value;  
  HideDestinationsPopup();
}


function selectDestinationCity(textCity){
	document.getElementById("to_list").Type="text";
	document.getElementById("to_list").value=textCity;	
}


/***********************************************/
/***********************************************/
/*			    CSS Functions	   		       */
/***********************************************/
/***********************************************/

function getHeightWidth(){
	
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}

}

function ShowDestinationsPopup(){
		getHeightWidth();
		isIE6 = /msie|MSIE 6/.test(navigator.userAgent); 
		
		document.getElementById('fade').style.height = yWithScroll + "px";
		if(isIE6){
			document.getElementById('fade').style.width = xWithScroll + "px";
			document.getElementById('popUpDestinations').style.width = document.body.clientWidth / 2;
			document.getElementById('popUpDestinations').style.height = document.body.clientHeight > 220 ? "220px" : "auto";
		}
		document.getElementById('popUpDestinations').style.display='block';
		
		document.getElementById('fade').style.display='block'
}

function HideDestinationsPopup(){
		document.getElementById('popUpDestinations').style.display='none';
		document.getElementById('fade').style.display='none'
}

/**
* returns the absolute left location of an element.
* param: e: element
* return: an integer representing the offset from left.
*/
function getElementLeftPosition(e){
	var x=0;
	while(e){
		x+=e.offsetLeft+e.clientLeft;
		e=e.offsetParent;
	}
	return x;
}

/**
* returns the absolute top location of an element.
* param: e: element
* return: an integer representing the offset from top.
*/
function getElementTopPosition(e){
	var y=0;
	while(e){
		y+=e.offsetTop+e.clientTop;
		e=e.offsetParent;
	}
	return y;
}
