
//minimale zoeklengte
var minLength = 3;   
var xmlhttp;
var boxID = 0;
var IE = ( navigator.appVersion.indexOf("MSIE") != -1 )


function ajax() 
{ 
  try 
    { 
    // Firefox, Opera 8.0+, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
  catch (e) 
    { 
    // Internet Explorer 
    try 
      { 
      xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
      } 
    catch (e) 
      { 
      try 
        { 
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
      catch (e) 
        { 
        alert("Your browser does not support AJAX!"); 
        return false; 
        } 
      } 
    }  
}


function trySearch(id)
{
    ajax();
    boxID = id;
    document.getElementById(boxID).value = '';
    var q = document.getElementById(boxID+'_SEARCH') != null ?  document.getElementById(boxID+'_SEARCH').value : "";
    if(q.length>=minLength){
        search(q);
    }
    else{
        document.getElementById(boxID+'_List').innerHTML = '';
        document.getElementById(boxID+'_List').style.display = 'none';
    }    
}

function search(q)
{
    xmlhttp.open("GET", "searchforms/searchlocations.aspx?q=" + q + "&l=EN", true);  
    
    xmlhttp.onreadystatechange = function() {   
    if(xmlhttp.readyState == 4)  
        processResult();
    };  
    xmlhttp.send(''); 
    
}

function processResult()
{
    // create an instance of XSLTProcessor   
    var processor = new XSLTProcessor();  
    var xslDoc = Sarissa.getDomDocument(); 
    xslDoc.async = false;   
    xslDoc.load("xslt/airportListENG.xslt"); 
   if(xslDoc.parseError != null &&
            xslDoc.parseError.errorCode != 0)   
      alert("not well formed or other error!");   
   else {
        processor.importStylesheet(xslDoc);
        var newDocument = processor.transformToDocument(xmlhttp.responseXML); 
        // show transformation results   
        document.getElementById(boxID+'_List').innerHTML = new XMLSerializer().serializeToString(newDocument);
        document.getElementById(boxID+'_List').style.display = 'block';
    }
}

function setAirport(iata,name)
{   
    document.getElementById(boxID).value = iata;
    document.getElementById(boxID+'_SEARCH').value = name;
    document.getElementById(boxID+'_List').innerHTML = '';
    document.getElementById(boxID+'_List').style.display = 'none';
    
    if(CopyDestinations)
    {      
        CopyDestinations(boxID);
    }
    else
    {       
        CopyDepartures();
    }
    
    if(document.getElementById("frDest"))
    {
        HideMenu3();
    }
}

function endSearch(wait)
{    
    if(wait==null)
    {
        setTimeout('endSearch(1)',100);
    }
    else
    {
        var arrLists = ['departureAirport', 'destinationAirport'];
        for(var i=0;i<arrLists.length;i++)
        {
            if(document.getElementById(arrLists[i] + '_List'))
            {
                document.getElementById(arrLists[i] + '_List').innerHTML = '';
                document.getElementById(arrLists[i] + '_List').style.display = 'none';
            }
        }                
    }  
}

//-----------------------------------------------------------------------------

function goToDestinations(e){
    var key;
    
    if (e.which){
        key = e.which;
    }else if (e.keyCode){
        key = e.keyCode;
    }
    
    if(key==40){
        if (IE){
            document.getElementById('destination_1').style.visibility = 'visible';
            document.getElementById('destination_1').focus();
            document.getElementById('totalResult').scrollTop = 0;
        }
        else
        {
            document.getElementById('destination_1').focus();
            document.getElementById('totalResult').scrollTop = 0;
        } 
    
        document.getElementById('row_1').className = 'ListItemHover';  
        return false;
    }
}

function toggleDestinations(pos, e, total, val){

    var DaIndexId = pos;
    var key;

    if (e.which){
        key = e.which;
    }else if (e.keyCode){
        key = e.keyCode;
    }
    document.getElementById('row_' + parseInt(DaIndexId)).className = 'ListItem';
    if(key==38 && DaIndexId>0) { //keyUp

        if (DaIndexId==1){
            if (IE){
                document.getElementById('destination_' + total).style.visibility = 'visible';
                document.getElementById('destination_' + total).focus();
            }
            else
            {
                document.getElementById('destination_' + total).focus();
            }
            document.getElementById('totalResult').scrollTop = 9999999;
            document.getElementById('row_' + total).className = 'ListItemHover';
            return false;

        }else{

            if (IE){

                document.getElementById('destination_' + parseInt(DaIndexId-1)).style.visibility = 'visible';
                document.getElementById('destination_' + parseInt(DaIndexId-1)).focus();
                document.getElementById('totalResult').scrollTop = parseInt(document.getElementById('totalResult').scrollTop) - 10;
            }
            else
            {
                document.getElementById('destination_' + parseInt(DaIndexId-1)).focus();
            }
            document.getElementById('row_' + parseInt(DaIndexId-1)).className = 'ListItemHover';
            return false;
        }
    }
    else if(key==40) { //keyDown

        if (DaIndexId==total){
            if (IE) {
                document.getElementById('destination_1').style.visibility = 'visible';
                document.getElementById('destination_1').focus();
            }
            else
            {
                document.getElementById('destination_1').focus();
            }
            document.getElementById('totalResult').scrollTop = 0;
            document.getElementById('row_1').className = 'ListItemHover';
            return false;

        }else{
            if (IE) {
                document.getElementById('destination_' + parseInt(DaIndexId+1)).style.visibility = 'visible';
                document.getElementById('destination_' + parseInt(DaIndexId+1)).focus();
                document.getElementById('totalResult').scrollTop = parseInt(document.getElementById('totalResult').scrollTop) + 10 ;
            }
            else
            {
                document.getElementById('destination_' + parseInt(DaIndexId+1)).focus();
            } 

            document.getElementById('row_' + parseInt(DaIndexId+1)).className = 'ListItemHover';
            return false;
        }
    }
    else if(key == 9) //tab
    {
        arrVal = val.split('|');
        setAirport(arrVal[0],arrVal[1]);
    }
    else if(key == 13) //enter
    {
        arrVal = val.split('|');
        setAirport(arrVal[0],arrVal[1]);
    }
}

function checkIfEmpty(thisid, targetid)
{
    if(document.getElementById(thisid).value == "")
    {
       document.getElementById(targetid).value = "";
       if(document.getElementById(targetid + "_SEARCH"))
       {
        document.getElementById(targetid + "_SEARCH").value = "";     
       }       
    }
}

function CopyDestinations() 
{
    var f = document.formulierQuickSearch;
    if (f.returnDepartureAirport.value == '') 
    {
      f.returnDepartureAirport.value = f.destinationAirport.value;
      //f.returnDepartureAirport_SEARCH.value = f.destinationAirport_SEARCH.value;
    }
      f.returnDestinationAirport.value = f.departureAirport.value;
     //f.ReturnDestinationAirport_SEARCH.value = f.departureAirport_SEARCH.value;
}

function validateIATA(IATA)
{
    //eerst check of er wel een mogelijke iata is ingevuld (is altijd 3 letters)
    if(IATA.length!=3){
        return false;
     }
    
    xmlhttp.open("GET", "searchforms/searchlocations.aspx?q=" + IATA, false);  
    xmlhttp.send(''); 
    
    var xmlDoc = xmlhttp.responseXML;
    xmlDoc.setProperty("SelectionLanguage", "XPath");   
    var oElem = xmlDoc.selectSingleNode('//IsIata/text()');
    
    var result = new XMLSerializer().serializeToString(oElem) ;

    return result=="true";
}

