var downArrow = false;

function intertravelLoad() {
    //alert($("#intertravel").attr("src"));
    //alert(parent.frames[0].location.href);
    //alert("a");
}

function airportSelect(e, elem)
{
    downArrow = false;
    var query = elem.value;
    var inputBox = elem;
    var selectBoxId = elem.id + '_airports';
    var selectBoxSelector = "#" + selectBoxId;
    var prev = document.getElementById(elem.id + "_prev").value;
    
    
    if (e.keyCode == 40)
    {
        downArrow = true;
        $(selectBoxSelector).focus();
        inputBox.value = document.getElementById(selectBoxId).value;
    }

    if (e.keyCode == 13)
    {
        inputBox.value = document.getElementById(selectBoxId).value; 
        document.getElementById(elem.id + "_prev").value = inputBox.value;
        if (elem.id == 'from')
        {
            $("#to").focus();    
        }
        else
        {
            $("#date_from").focus();    
        }
        
        $("#" + selectBoxId).fadeOut();
        return;
    }
    
    
/*    
    if (e.keyCode == 27)    
    {
        $(selectBoxSelector).fadeOut();    
    }
    */
    
    if (prev != inputBox.value)
    {
        $(selectBoxSelector).fadeIn();
        var id = inputBox.id;
        $.get("/avio-karte/airports.php", {q: query, id: id}, function(html) {parseAirports(html, id)}, "html");
    }
}


function airportValid(elem)
{
    var inputBox = elem;
    var selectBoxId = elem.id + '_airports';
    var selectBoxSelector = "#" + selectBoxId;
    if (document.getElementById(selectBoxId).selectedIndex != -1)
    {
        inputBox.value = document.getElementById(selectBoxId).options[document.getElementById(selectBoxId).selectedIndex].text;    
        document.getElementById(elem.id + "_prev").value = inputBox.value;
    }
    downArrow = false;
}


function parseAirports(html, id)
{
    var selectBoxId = id + '_airports';
    var selectBoxSelector = "#" + selectBoxId;
        
    $(selectBoxSelector).html(html);
    $(selectBoxSelector + " option").click(function(){ $(selectBoxSelector).fadeOut();});
    document.getElementById(selectBoxId).selectedIndex = 0;
}        


function enter(event, targetId, focusId)
{
    if (event.keyCode == 13)
    {
        $("#" + targetId).fadeOut();
        $("#" + focusId).focus();   
        return false; 
    } 
}

 
