// --- [start /site/flightsScripts.js] ---
flightsPredictiveText = {
	url: "/site/voyages/billets-avion-vols/predictive-text-backend.html?skin=frfr.lastminute.com&LOCALE=fr_FR&TYPE=A%2CC&LOCATION=",
	init: function(s_inputIds){
	   if(typeof(s_inputIds) === "undefined"){
      return false;
     }
	  //s_inputIds format (|-separated string): "inputId0|inputId1|inputId2|...|inputId{n}"
    var a_inputIds = s_inputIds.split("|");
    for(var i=0;i < a_inputIds.length;i++){
      //Load input id
      var s_inputId = a_inputIds[i];
      if(lm.e(s_inputId) && lm.e(s_inputId).type == 'text'){
        var s_autocompleteDivId = s_inputId + '_autocomplete_div';
        var s_autocompleteSelectId = s_inputId + '_autocomplete_select';
        
  			var destinationContainer = lm.e(s_inputId).parentNode
  			new lm.element.block({p:destinationContainer,id:s_autocompleteDivId, className:'autoComplete'})
  			new lm.element.block({p:s_autocompleteDivId,id:s_autocompleteSelectId,tagName:'select'})

  			placeTextBox = lm.e(s_inputId)
  			placeDivContainer = lm.e(s_autocompleteDivId)
  			placeSelectBox = lm.e(s_autocompleteSelectId)
  			placeTextBox.setAttribute('autocomplete', 'off')
  			placeTextBox.onkeyup = flightsPredictiveText.keyUpHandler
  			placeTextBox.onkeydown = flightsPredictiveText.keyDownHandler
  			placeTextBox.onkeypress = flightsPredictiveText.keyPressHandler
  			placeTextBox.onfocus = flightsPredictiveText.focusHandler
  			//placeTextBox.onblur = flightsPredictiveText.hideSuggestDiv
  			placeSelectBox.onclick = flightsPredictiveText.selectClickHandler
  			placeSelectBox.addOption = flightsPredictiveText.addOption
  			
        var a_link = lm.getElementsBy({parentNode:destinationContainer,tagName:'a'});
  			if(a_link.length !== 0){
  			 lm.style(lm.getElementsBy({parentNode:destinationContainer,tagName:'a'})[0],'display','none')
  			}

  			http = flightsPredictiveText.getHTTPObject();
  			
  			savedXml="";
  			reqTimeDelay="";
  			
  			priorTextboxValue = "";
  			selectBoxShowing = false;
  			infoMsgShowing = false;
		  }
    }
		
	},
	httpRequest: function (inpStr){
		if(http){
			http.open("GET", this.url + escape(inpStr.toUpperCase()), true);
			http.onreadystatechange = flightsPredictiveText.handleHttpResponse;
			http.send(null);
		}
	},
	handleHttpResponse: function(){
		if(http.readyState == 4 && http.status == 200){
			if(http.responseText.indexOf('invalid') == -1 && http.responseXML.documentElement){
				savedXml= http.responseXML.documentElement.cloneNode(true);
				flightsPredictiveText.compileOptions(http.responseXML.documentElement);
			}
		}
	},
	compileOptions: function(locationsNode){
	  
	  //"Clean" the Suggestions' container 
		flightsPredictiveText.hideSuggestDiv();
	
		for(var n=locationsNode.firstChild; n!=null; n=n.nextSibling){
	
			var placeName = "";
			var cityCode = "";
	
			if(n.nodeName == "CODEMATCH"){
				placeName = n.childNodes[0].attributes.getNamedItem("N").value;
				cityCode = n.childNodes[0].attributes.getNamedItem("C").value;
				placeSelectBox.addOption(placeName,cityCode,"");
			}
	
			if(n.nodeName == "LOC"){
				placeName = n.attributes.getNamedItem("N").value;
				cityCode = n.attributes.getNamedItem("C").value;
				placeSelectBox.addOption(placeName,cityCode,"");
	
				if(n.childNodes){
					for(var i=0; i < n.childNodes.length; i++){
						placeName = n.childNodes[i].attributes.getNamedItem("N").value;
						cityCode = n.childNodes[i].attributes.getNamedItem("C").value;
						placeSelectBox.addOption(placeName,cityCode,"---");
					}
				}
			}
		}
		//if there are no results found, display info message
		if(placeSelectBox.options.length == 0){
      flightsPredictiveText.showInfo("noresults");
    }
    //else, display results...
    else{      
      flightsPredictiveText.showSuggestDiv();
    }
	},
	filterOptions: function(inpStr){//after press 3 filter on client
		if(!savedXml)return;
		var filteredXml = savedXml.cloneNode(true);
		var noMatchNodes = new Array();
	
		for(var n=filteredXml.firstChild; n!=null; n=n.nextSibling){
			if(n.nodeName == "CODEMATCH"){
				noMatchNodes[noMatchNodes.length] = n;
			}
			if(n.nodeName == "LOC"){
				var placeName = n.attributes.getNamedItem("N").value;
				if(placeName.substr(0,inpStr.length).toUpperCase()!=inpStr.toUpperCase()){
					noMatchNodes[noMatchNodes.length] = n;
				}
			}
		}
		//remove non matches
		for(var i=0;i < noMatchNodes.length;i++){
			filteredXml.removeChild(noMatchNodes[i]);
		}
		flightsPredictiveText.compileOptions(filteredXml);
	},
	getHTTPObject: function(){
		var xmlhttp;
		/*@cc_on
		@if(@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(E){
				xmlhttp = false;
			}
		}
		@else
		xmlhttp = false;
		@end @*/
		if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
			try{
				xmlhttp = new XMLHttpRequest();
				xmlhttp.overrideMimeType("text/xml"); 
			} 

			catch(e){
				xmlhttp = false;
			}
		}
		return xmlhttp;
	}, 
	keyUpHandler: function(ev){//Run query
    
    ev=ev||event||null;
		if(ev){
			var cc=ev.charCode||ev.keyCode||ev.which;
			//up arrow,down arrow,up arrow,down arrow,enter,escape, left arrow, right arrow - was caught on keyDown & keyPress so cancel event
			if (cc==38||cc==40||cc==57385||cc==57386||cc==13||cc==27||cc==37||cc==39) return false;
			
			var qLength = placeTextBox.value.length;
			if(qLength<2)flightsPredictiveText.hideSuggestDiv();
			else if(qLength >=2 && qLength <= 3){//second and third keypress
			  flightsPredictiveText.showInfo("loading");
				clearTimeout(flightsPredictiveText.reqTimeDelay);
				flightsPredictiveText.reqTimeDelay = setTimeout("flightsPredictiveText.httpRequest('"+placeTextBox.value+"')",300);
			}
			else if(qLength>3){//subsequent keypresses
				flightsPredictiveText.filterOptions(placeTextBox.value);
			}
		}
		return true;	
	}, 
	keyDownHandler: function (ev){//Paging action through select box
		if(!selectBoxShowing)return;
		if(infoMsgShowing)return;
		ev=ev||event||null;
		if(ev){
			var cc=ev.charCode||ev.keyCode||ev.which;	
	
			if(cc==38||cc==57385){ //up
				if(placeSelectBox.selectedIndex > 0)placeSelectBox.selectedIndex--;
				else placeSelectBox.selectedIndex = placeSelectBox.options.length - 1;			
				flightsPredictiveText.updateTextBox(placeSelectBox.options[placeSelectBox.selectedIndex].value);
				return false;
			}
	
			else if(cc==40||cc==57386){ //down
				placeSelectBox.selectedIndex = (placeSelectBox.selectedIndex+1)%placeSelectBox.options.length;
				flightsPredictiveText.updateTextBox(placeSelectBox.options[placeSelectBox.selectedIndex].value);
				return false;
			}
		}
		return true;
	}, 
	keyPressHandler: function (ev){//Intercept return or escape key to hide select box
		ev=ev||event||null;
		if(ev){
			var cc=ev.charCode||ev.keyCode||ev.which;			
			if(cc==13 || cc==27){ //enter || esc
				if(cc==13 && !infoMsgShowing)
          flightsPredictiveText.selectClickHandler();
				flightsPredictiveText.hideSuggestDiv();
				placeTextBox.focus();
				return false;
			}
		}
		return true;
	}, 
	focusHandler: function(ev){
	  //Hide the old concerned SuggestDiv
	  flightsPredictiveText.hideSuggestDiv();
	  //Determine the concerned input
	  flightsPredictiveText.determineInput(ev);
	  //Hide the new concerned SuggestDiv
		flightsPredictiveText.hideSuggestDiv();
		
		this.select();
	}, 
	selectClickHandler: function(ev){
	  if(infoMsgShowing)return;
		if(placeSelectBox.selectedIndex > -1){
			flightsPredictiveText.updateTextBox(placeSelectBox.options[placeSelectBox.selectedIndex].value);
		}
	},
	hideSuggestDiv: function(){
		document.onclick = null;
		placeDivContainer.style.display = "none";
		//Clear booleans
    infoMsgShowing = false;
    selectBoxShowing = false;
    //Clear info message styles
    placeTextBox.setAttribute((document.all ? 'className' : 'class'), '');
    placeSelectBox.setAttribute((document.all ? 'className' : 'class'), '');
    //Clear the select box
		placeSelectBox.length = 0;
		
		lm.forEach(lm.getElementsBy({tagName: 'select'}),function(item){
				if(item.id){
				  var _id = item.id + "";
          if(_id.indexOf("_autocomplete_select") != -1){
  					item.removeAttribute('style')
  				}
				}
			})
		lm.forEach(lm.getElementsBy({tagName: 'input'}),function(item){
				if(item.id){
				  var _id = item.id + "";
          if(_id.indexOf("_autocomplete_select") != -1){
  					item.removeAttribute('style')
  				}
				}
		})
	},
	showSuggestDiv: function(){
		if(placeSelectBox.options.length > 0){
		  //select box size
		  placeSelectBox.size = 10;
      if(placeSelectBox.options.length < 10)placeSelectBox.size = placeSelectBox.options.length+1;
      
		  //If info message do not select the first entry
      if(infoMsgShowing)
        placeSelectBox.options[0].selected = false;
      else
        placeSelectBox.options[0].selected = true;
      
			document.onclick = flightsPredictiveText.hideSuggestDiv;
			selectBoxShowing = true;
			lm.forEach(lm.getElementsBy({tagName: 'select'}),function(item){
				if(item.id){
				  var _id = item.id + "";
          if(_id.indexOf("_autocomplete_select") != -1){
  					lm.style(item, 'position','static')
  				}
				}
			})
			
			lm.forEach(lm.getElementsBy({tagName: 'input'}),function(item){
				if(item.id){
				  var _id = item.id + "";
          if(_id.indexOf("_autocomplete_select") != -1){
  					lm.style(item, 'position','static')
  				}
				}
			})
			placeDivContainer.style.display = "block";
		}
	},
	updateTextBox: function(value){
		placeTextBox.value = flightsPredictiveText.sentenceCase(value);
	},
	addOption: function(placeName,cityCode,indent){
		//cityCode isn't used at the moment
		var placeString;
    if(indent==="info_msg")
      placeString = placeName;
    else
      placeString = indent + flightsPredictiveText.sentenceCase(placeName);
      
		this.options[this.options.length] = new Option(placeString,placeName,0,0);
	},
	sentenceCase: function(str){
		var stringArray = str.split(" ");
	
		for(i=0;i < stringArray.length;i++){
			if(stringArray[i].charAt(0) == "(")break;
			if(stringArray[i] == "NY")break;
			var first = stringArray[i].substr(0,1);
			var rest = stringArray[i].substring(1,stringArray[i].length);
			stringArray[i] = first.toUpperCase() + rest.toLowerCase();
		}
	
		return stringArray.join(" ");
	},
	determineInput: function(e){
	  //cross-browser event handling
    var targ;
  	if (!e) var e = window.event;
  	if (e.target) targ = e.target;
  	else if (e.srcElement) targ = e.srcElement;
  	if (targ.nodeType == 3) // defeat Safari bug
  		targ = targ.parentNode;
    
    var s_inputId = targ.id;
    var s_autocompleteDivId = s_inputId + '_autocomplete_div';
    var s_autocompleteSelectId = s_inputId + '_autocomplete_select';
    placeTextBox      = lm.e(s_inputId);
  	placeDivContainer = lm.e(s_autocompleteDivId);
  	placeSelectBox    = lm.e(s_autocompleteSelectId);
  },
  showInfo: function(key_info){
    //"Clean"
    flightsPredictiveText.hideSuggestDiv();

    //Info message to display
    var info_msg="";
    
    if(key_info === "loading"){
      info_msg='search_js_predictiveText_loading';
    }
    else if(key_info === "noresults"){
      info_msg='search_js_predictiveText_noResults';
    }
        
    
    //Check if the loaded message is well translated/internationalized
    if(info_msg.indexOf('search_js_predictiveText_') !== -1)
       info_msg='';
	   //return false;
    
    //Add the message in the select box
    placeSelectBox.addOption(info_msg,'null','info_msg');
    //Style the select box
    placeSelectBox.setAttribute((document.all ? 'className' : 'class'), 'info');
	
	//Style the input text
    if(key_info === "loading")
    placeTextBox.setAttribute((document.all ? 'className' : 'class'), 'ajaxLoading');

    //Show the autocomplete box
    infoMsgShowing = true;
    flightsPredictiveText.showSuggestDiv();
  }
}

// update the day dropdowns on load to show the correct number of days
lm.addEvent({fn:function(){ 
	if(lm.e('departureDateMonth'))setDates(1);
	if(lm.e('returnDateMonth'))setDates(2);
}})
// --- [end prdpcthpb0615:4004 - /site/flightsScripts.js - Nov 20, 2009 11:49 pm GMT - pct2,pct0410 - H-2GiQqoTFMAABFVX0wAACg7 - cache 1800 ] ---
