﻿// JScript File

	function ParseTerm(ttest){
	
		var resultTerm =""
		termAr = ttest.split(" ")
		
		for (x=0;x<termAr.length;x++){
		
			first = termAr[x].substring(0,1).toUpperCase()
			if (x>0){
				resultTerm += " " + first + termAr[x].substring(1,termAr[x].length).toLowerCase()
			}
			else{
				resultTerm += first + termAr[x].substring(1,termAr[x].length).toLowerCase()
			}
			
		
		}

		return resultTerm
	
	}
	
	function GetLatLon(strin,swch){
	
		llAr = strin.value.split("|")
		return parseFloat(llAr[swch]) 
	
	
	}
	


 // call calendar popup and set the date
	function SetDate(obin,xin,yin){
	
			obin.className="seem"
			
			if (obin.id=="depCal"){

			    cal = new CalendarPopup("depCalDiv", xin, yin); 
				
				cal.select(document.getElementById('depDate'),'depCal','MM/dd/yyyy');
				//document.getElementById('arrCity').focus() 
			}
			if (obin.id=="arrCal"){

			    cal = new CalendarPopup("arrCalDiv", xin, yin); 
				cal.select(document.getElementById('arrDate'),'arrCal','MM/dd/yyyy');
			}
			if (obin.id=="stCal"){

			    cal = new CalendarPopup("stCalDiv", xin, yin); 
				
				cal.select(document.getElementById('stDate'),'stCal','MM/dd/yyyy');
				//document.getElementById('arrCity').focus() 
			}
			if (obin.id=="finCal"){



			    cal = new CalendarPopup("fnCalDiv", xin, yin); 
				cal.select(document.getElementById('fnDate'),'finCal','MM/dd/yyyy');
			}
			return false;
		
		}
		
	
		
	
    // called from test drop
    function CityDrop(ttest){

		
		var allElements, thisElement, drop;
		// process the input
		
		
		
		//searchTerm = GetSearchTerm(ttest.value);
		searchTerm = ParseTerm(ttest.value);
		
		
		//searchTerm = "Dal"
		
		//document.getElementById("pos").value = searchTerm
		
		if (ttest.id=="depCity"){
			curCity = "depChoices"
		}
		if (ttest.id=="arrCity"){
			curCity = "arrChoices"
		}
		if (ttest.id=="stCity"){
			curCity = "stChoices"
		}
		//document.getElementById("help").value = curCity
	
		if (document.getElementById("Trip1_xCity")!=null){
			xmldoc = document.getElementById("Trip1_xCity")
			
		}
		else{
			xmldoc = document.getElementById("ctl00_ContentPlaceHolder1_xCity")
			
		}

		xpex = "//newdataset/data[starts-with(ci_city,'" + searchTerm + "')]"
		//xpex = "//NewDataSet/data[starts-with(ci_city,'" + searchTerm + "')]"
			
	
		theselect = document.getElementById(curCity)
		
		if (IE){

		    
		    
		    
			xmlCities = new ActiveXObject("Microsoft.XMLDOM"); 
			xmlCities.setProperty("SelectionLanguage", "XPath")
			btest = xmlCities.loadXML(xmldoc.innerHTML);
			wnodes = xmlCities.selectNodes(xpex);
			theselect.options.length = 0;
			
			if (wnodes.length>0)
			{
				
				drop = true;
				for (i=0;i<wnodes.length;i++){
				
					try 
					{
						theselect.options[i] = new Option(wnodes(i).childNodes[1].text,wnodes(i).childNodes[0].text);
					}
					catch (exception) 
					{
						result = reportRuntimeError(exception);
						alert("fill select box error : " + result);
					}
				}
			}
				
		}
		else{ // mozilla

		   
			
			allElements = document.evaluate(xpex,xmldoc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
			thisElement = allElements.snapshotItem(0);
			theselect.options.length = 0;
			
			//alert(allElements.snapshotLength)
			
			if (allElements.snapshotLength>0){
				drop = true;
				for (i = 0;i < allElements.snapshotLength;i++){
				
					sNode = allElements.snapshotItem(i)
					theselect.options[i] = new Option(sNode.childNodes[1].textContent,sNode.childNodes[0].textContent);
				}
			}
		}
        
        
		if (drop ) {
			document.getElementById(curCity).className = "seem";
		}
	}
	
	

	// called on keyup from city boxes
	function TestDrop(evt,obin){

		
		
		if (obin.id=="depCity"){
			choiceBox = document.getElementById("depChoices")
		}
		if (obin.id=="arrCity"){
			choiceBox = document.getElementById("arrChoices")
		}
		
		if (obin.id=="depAirport"){
			choiceBox = document.getElementById("depAirChoices")
		}
		if (obin.id=="arrAirport"){
			choiceBox = document.getElementById("arrAirChoices")
		}
		if (obin.id=="stCity"){
			choiceBox = document.getElementById("stChoices")
		}
		if (obin.id=="stAirport"){
			choiceBox = document.getElementById("stAirChoices")
		}
	
	    if (evt==null){
	        if (obin.value.length > 1 && (obin.id=="arrCity" || obin.id=="depCity" || obin.id=="stCity" )){
    			
				CityDrop(obin)
			 }
	    }
	    else{
		    // if key down
		    if (evt.keyCode==40){
    			
			    if(choiceBox.className=="seem"){
    			
				    choiceBox.focus();
				    choiceBox.options.selectedIndex = 0
    				
				    setChoice(evt);
			    }
		    }
		    else{
    		
			    if (obin.value.length > 1 && (obin.id=="arrCity" || obin.id=="depCity" || obin.id=="stCity" )){
    			
				    CityDrop(obin)
			    }
		    }
		}
	}

	// sets the city choice 
	function setChoice(evt,swch){

		// curCity is set in city drop or FillXXXAirports
		if (curCity =="depChoices"){
			
			obin = document.getElementById("depChoices")
			if (obin.selectedIndex!=-1){
				document.getElementById("depCity").value = obin[obin.selectedIndex].text
				document.getElementById("depId").value = obin[obin.selectedIndex].value
			}
			//document.getElementById("pos").value = evt.keyCode
			
			
			// 13 = enter
			if (evt.keyCode==13){
		
				GetDepInfo()
				obin.className = "noseem";
				// now decite where to send focus
				document.getElementById("depDate").focus()
				
				// don't need this as the ajax call will come back thru as curcity = depair
//				if (document.getElementById("depAirport")!=null){
//					
//					document.getElementById("depAirport").focus()
//				}
//				else{
//					document.getElementById("depDate").focus()
//				}
			}
		}
		if (curCity =="arrChoices"){
			obin = document.getElementById("arrChoices")
			if (obin.selectedIndex!=-1){
				document.getElementById("arrCity").value = obin[obin.selectedIndex].text
				document.getElementById("arrId").value = obin[obin.selectedIndex].value
			}
			if (evt.keyCode==13){
			
				//GetArrInfo()
				obin.className = "noseem";
				document.getElementById("arrDate").focus()
			}
		
		}
		if (curCity == "depAir") { // set in FillDepAirports
			obin = document.getElementById("depAirChoices")
			if (obin.selectedIndex!=-1){
				document.getElementById("depAirName").value = obin[obin.selectedIndex].text
				document.getElementById("depAirIata").value = obin[obin.selectedIndex].value
				document.getElementById("depAirport").value = obin[obin.selectedIndex].value
			}
			if (evt.keyCode==13){
			
				//GetArrInfo()
			    obin.className = "noseem";

			    if (document.getElementById("depDate").value.length > 0) {

			        document.getElementById("arrAirport").focus()
			    } 
			    else {
			        document.getElementById("depDate").focus()
			    }
				
				
			}
		
		}
		if (curCity =="arrAir"){

			
			
			obin = document.getElementById("arrAirChoices")
			etype = evt.type
			test = evt.keyCode
			test1 = obin.selectedIndex
			

			if (obin.selectedIndex != -1) {
			    document.getElementById("arrAirName").value = obin[obin.selectedIndex].text
			    document.getElementById("arrAirIata").value = obin[obin.selectedIndex].value
			    document.getElementById("arrAirport").value = obin[obin.selectedIndex].value
			}
			else {
			    swch = 0;

			}
			if (evt.keyCode==13){
			
				//GetArrInfo()
				obin.className = "noseem";
				document.getElementById("passengers").focus()
			}
		
		}
		
		// this is the city
		if (curCity =="stChoices"){
			
			obin = document.getElementById("stChoices")
			if (obin.selectedIndex!=-1){
				
				document.getElementById("stCity").value = obin[obin.selectedIndex].text
				document.getElementById("stId").value = obin[obin.selectedIndex].value
			}
			//document.getElementById("pos").value = evt.keyCode
			
			
			// 13 = enter
			if (evt.keyCode==13){
		
				obin.className = "noseem";
				// now decide where to send focus
					document.getElementById("stAirport").focus()
			}
		}
		
		// this is the city
		if (curCity =="stAirChoices"){
			
			
			
			
			obin = document.getElementById("stAirChoices")
			if (obin.selectedIndex!=-1){
				document.getElementById("stAirport").value = obin[obin.selectedIndex].value
				document.getElementById("stAirName").value = obin[obin.selectedIndex].text
				document.getElementById("stAirIata").value = obin[obin.selectedIndex].value
			}
			//document.getElementById("pos").value = evt.keyCode
			
			
			// 13 = enter
			if (evt.keyCode==13){
		
				//GetStInfo()
				obin.className = "noseem";
				// now decide where to send focus

					document.getElementById("stDate").focus()

			}
		}
		
		
		// this get set in the click - dblclick and onkeyup events from drop down selects
		// if it is a key up it is a swch = 0 otherwise the swch is set to move focus
		if (swch==1){
			CloseDrop(document.getElementById("depChoices"))
		
		}
		if (swch==2){
			CloseDrop(document.getElementById("arrChoices"))
		
		}
		if (swch==3){
			CloseDrop(document.getElementById("depAirChoices"))
		
		}
		if (swch==4){
			CloseDrop(document.getElementById("arrAirChoices"))
		
		}
		
		if (swch==5){
			CloseDrop(document.getElementById("stChoices"))
		
		}
	
		if (swch==6){
			CloseDrop(document.getElementById("stAirChoices"))
		
		}
		//inCity==false;
		
		
		
	}

	
	
	// close city drop down
	function CloseDrop(obin){
	
		//document.getElementById("help").value = "Close drop : " + obin.id
		
		
		if (obin.id =="depChoices"){
			GetDepInfo()
			
			
			if (document.getElementById("depAirport")!=null){
				
				// go get the airports for this city
					
				document.getElementById("depAirport").focus()
			}
			else{
				document.getElementById("depDate").focus()
			}
			
			
		}
		if (obin.id =="arrChoices"){
		
			GetArrInfo()
			
			if (document.getElementById("arrAirport")!=null){
				
				// go get the airports for this city
					
				document.getElementById("arrAirport").focus()
			}
			else{
			    document.getElementById("arrDate").focus()
				// turned this off as it is only valid for return trips
				//SetDate(document.getElementById('arrCal'))
			}
			
			
			
			
		}
	
		if (obin.id =="depAirChoices"){
		
			//GetArrInfo()
			if (document.getElementById("depDate").value.length == 0){
			    document.getElementById("depDate").focus()
			}
			if (document.getElementById("arrCity").value.length > 0){
			    GetArrInfo()
			}
			
		}
		
		if (obin.id =="arrAirChoices"){
		
			//GetArrInfo()
			document.getElementById("passengers").focus()
		}
		
		
		if (obin.id =="stChoices"){
		
			GetStInfo()
			document.getElementById("stAirport").focus()

		}
		
		
		if (obin.id =="stAirChoices"){
		
			//GetArrInfo()
			document.getElementById("stDate").focus()
		}
		
		
		obin.className="noseem";
	
	}
	
	
	
	
	
	
	// set arrival info
	function SetArrInfo(result){

		document.getElementById("arrLatLon").value = result
		//GenRoute()
		//GetAirports()
		// if there is an airport list then fill it
		if (document.getElementById("arrAirport")!=null){
			GetArrAirports()
		}
	
	}

	// get arrival info
	function GetArrInfo(){
	
		qString = "&citId=" + document.getElementById("arrId").value
		callService(SetArrInfo,"GetCityLatLon",qString)
	}
	
	// set departure info
	function SetDepInfo(result){
	
		document.getElementById("depLatLon").value = result
		
		// if there is an airport list then fill it
	     if (document.getElementById("depAirport")!=null){
		GetDepAirports()        }
       
		
		
	}
	
	// get departure info
	function GetDepInfo(){
	
		
          qString = "&citId=" + document.getElementById("depId").value
          callService(SetDepInfo,"GetCityLatLon",qString)
	
	}
	
	
	
	// get stationary info
	function GetStInfo(){
	
		qString = "&citId=" + document.getElementById("stId").value
		callService(SetStInfo,"GetCityLatLon",qString)
	
	}
	
	// set stationary info
	function SetStInfo(result){
	
		document.getElementById("stLatLon").value = result
		GetStAirports()
		// set the flag to say this has been done
		setDepId = 1;
		
		
	}
	
	
	var cycle = 0
	
	function SetNotifyType(obin){
		
			
			if (obin.id=="email"){
				document.getElementById("demail").className="show"
				document.getElementById("dsms").className="hide"
				document.getElementById("bemail").focus()
			
			}
			if (obin.id=="sms"){
				document.getElementById("dsms").className="show"
				document.getElementById("demail").className="hide"
				document.getElementById("dsms").focus()
			
			}
		
		
		}