﻿var requestTimer = 300; // time in ms before sending request -> original value 800
var minLength = 2; //length of prefix before querying db

var resultsLinkColor = "#003399";
var captionColor = "#000000";
var searchForColor = "#000000";
var suggestionBoxBg = "#f4fbff";
var suggestionBoxTextColor = "#000000";

var highLightRowColor = "#fff799"; 
var highLightTextColor = "#003366";

var searchForm = new Array(); //container
var evtKeyCode = 0;
var cursorDirection = "down";

autoCompleteObj = function(name, textBoxID)
{
    this.id = name;
    this.suggestionBox = null;
    this.highlightedRow = - 1;
    this.textBox = document.getElementById(textBoxID);
    this.prefixText = this.textBox.value;
    this.selections = new Array();
    this.selection = "";
    this.searchResults = null;
    this.recordCount = 0;
}

function initAutoComplete(prefixText, formIndex)
{
    var autoComplete = document.getElementById(searchForm[formIndex].id);
    var highlightedRow = searchForm[formIndex].highlightedRow;
    var list = autoComplete.getElementsByTagName("li");
    autoComplete.style.backgroundColor = suggestionBoxBg;
    searchForm[formIndex].recordCount = list.length;
    searchForm[formIndex].searchResults = list;
    searchForm[formIndex].prefixText = prefixText;
    searchForm[formIndex].selection = "";
    if (searchForm[formIndex].textBox.value == "" || list.length == 1 || evtKeyCode == 27 || (evtKeyCode == 35 || evtKeyCode == 36))
    {
        hideResults(formIndex);
    }
    searchForm[formIndex].textBox.value = prefixText; 
}

function highlightRow(row, formIndex)
{
    if (!searchForm[formIndex].searchResults)
    {
        return;
    }
    showResults(formIndex);
    if (row >= searchForm[formIndex].recordCount)
    {
        if (cursorDirection == "down")
        {
            row = searchForm[formIndex].recordCount - 1;
            searchForm[formIndex].suggestionBox = searchForm[formIndex].searchResults.item(row);
        }
        else
        {
            row = searchForm[formIndex].recordCount; 
        }
    }
    if (row < 0)
    {
        if (cursorDirection == "down")
        {
            row = -1;
            searchForm[formIndex].highlightedRow = row;
            searchForm[formIndex].suggestionBox = null;
        }
        else
        {
            row = 0;
            searchForm[formIndex].suggestionBox = searchForm[formIndex].searchResults.item(row);
        }
    }
    if (searchForm[formIndex].highlightedRow != -1 && row != searchForm[formIndex].highlightedRow)
    {
        resetStyle(formIndex);
        searchForm[formIndex].highlightedRow = -1;
    }
    if (row >= 0)
    {
        searchForm[formIndex].selection = searchForm[formIndex].selections[row];
        searchForm[formIndex].suggestionBox = searchForm[formIndex].searchResults.item(row);        
        highLight(row,formIndex);	            
    }
    else
    {
        searchForm[formIndex].selection = "";
    }        
}
function resetStyle(formIndex)
{
    searchForm[formIndex].suggestionBox.style.backgroundColor = suggestionBoxBg;
    searchForm[formIndex].suggestionBox.style.color = suggestionBoxTextColor;
    if (searchForm[formIndex].suggestionBox.displaySpan != null)
    {
        var results = searchForm[formIndex].suggestionBox.displaySpan.getElementsByTagName("span");
        if (results.item(0) != null)
        {
            results.item(0).style.color = resultsLinkColor;
        }
        if (results.item(1) != null)
        {
            results.item(1).style.color = captionColor;
        }
        var searchFor = searchForm[formIndex].suggestionBox.displaySpan.getElementsByTagName("a")[0];
        if (searchFor != null)
        {
            searchFor.style.color = searchForColor;        
        }
    } 
}

function showHideSelect(vis)
{
    if (navigator.appName.indexOf("MSIE"))
    {
        for (var i = 0;i < document.forms.length; i++)
        {
            for (var j = 0; j < document.forms[i].length; j++) 
            {
                if (document.forms[i].elements[j].options) 
                {
                    document.forms[i].elements[j].style.visibility = vis;
                }
            }
        } 
    }
}

function highLight(i, formIndex)
{
    searchForm[formIndex].highlightedRow = i;
    searchForm[formIndex].suggestionBox.style.backgroundColor = highLightRowColor;
    searchForm[formIndex].suggestionBox.style.cursor = "pointer";
    if (searchForm[formIndex].suggestionBox.displaySpan != null)
    {
        var results = searchForm[formIndex].suggestionBox.displaySpan.getElementsByTagName("span");

        if (results.item(0) != null)
        {
            results.item(0).style.color = highLightTextColor;
        }
        if (results.item(1) != null)
        {
            results.item(1).style.color = highLightTextColor;
        }
        var searchFor = searchForm[formIndex].suggestionBox.displaySpan.getElementsByTagName("a")[0];
        if (searchFor != null) 
        {
            searchFor.style.color = highLightTextColor;
        }
    } 
}

function getSubmit(formIndex)
{
    if (document.getElementById(searchForm[formIndex].id).style.visibility == "visible")
    {
        var highlightedRow = searchForm[formIndex].highlightedRow;
        if (highlightedRow >= 0)
        { 
            hideResults(formIndex);            
            var newKey = searchForm[formIndex].selection;
            if (newKey != null && newKey != "")
            {
                if (newKey.charAt(newKey.length - 1) == ' ')
                {
                    searchForm[formIndex].textBox.value = newKey.substring(0, newKey.length - 1);
                }
                else
                {
                    searchForm[formIndex].textBox.value = newKey;
                }
            }
        }
        else
        {
           document.getElementById('aspnetForm').submit();
        }
        return false; 
    }
}

var request = null;
var lastQuery = "";
function postBack(formIndex)
{
    try
    {
        var query = searchForm[formIndex].textBox.value;
        if (query.length >= minLength && lastQuery != query)
        {
            var url;
            
            if (document.getElementById('ctl00_body_destinationCity') != null || (document.getElementById('Product_1') != null && document.getElementById('Product_1').checked == true))
            {
                url = "../_ajax/webservices/autoComplete.asmx/intelDestinations?prefixText=" + encodeURIComponent(query) + "&count=5";
            }
            else
            {
                url = "../_ajax/webservices/autoComplete.asmx/intelAirports?prefixText=" + encodeURIComponent(query) + "&count=5";
            }
            
            
            abortRequest();
    	    if (request == null)
    	    { 
                request = (!window.XMLHttpRequest) ? (ActiveXObject ? (new ActiveXObject("Microsoft.XMLHTTP")) : "") : (new XMLHttpRequest());
                request.open("GET", url, true);
                request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                request.onreadystatechange = new Function("getResponse(" + formIndex + ")");
                request.send(null);
                lastQuery = query;
                }
        }
        else if (lastQuery != query)
        {
            hideResults(formIndex);
        }
    }
   catch (e)
   {
        hideResults(formIndex);
   } 
}

var responding = false;
function getResponse(formIndex)
{
    if (request != null && request.readyState == 4 && responding == false)
    {
        responding = true;
        var responseStatus = 0
        var response = "";
        try
        {
            response = request.responseText;
            responseStatus = request.status
            response = response.replace('<?xml version="1.0" encoding="utf-8"?>', '');
            response = response.replace('<string xmlns="http://tripres.com/">', '');
            response = response.replace('</string>', '');
        }
        catch(e)
        {
        }
        if (responseStatus != 200 || response == "")
        {
	        hideResults(formIndex);
	    }
        else
        { 
            try
            { 
	            eval('renderResults(' + response + ''+formIndex + ');');
	        }
	        catch (e)
	        {
	            hideResults(formIndex);
	        }
	    }
        abortRequest(); 
        responding = false;
    }
}

function renderResults(prefixText, response, airport,formIndex)
{
    searchForm[formIndex].selections = response;
    searchForm[formIndex].prefixText = prefixText;
    if (response.length == 0)
    {
        searchForm[formIndex].recordCount = 0;
        searchForm[formIndex].searchResults = null;
    }
    clearResults(formIndex);
    showResults(formIndex);
    if (cursorDirection == "down")
    {
        searchForm[formIndex].highlightedRow = - 1;
    }
    else
    {
        searchForm[formIndex].highlightedRow = response.length + 1;
    }
    var autoComplete = document.getElementById(searchForm[formIndex].id);
    var query = searchForm[formIndex].textBox.value;
    var span = document.createElement("span");
    autoComplete.appendChild(span);
    var div = document.createElement("div");
    var ul = document.createElement("ul");
    div.appendChild(ul);
    autoComplete.appendChild(div);
    for (var i = 0; i < response.length + 1; ++i)
    {
        var li = document.createElement("li");
        li.className = "plane";
        var liText = document.createElement("span");
        li.displaySpan = liText;
        if (i != response.length)
        {
            if(airport == null)
                liText.innerHTML = "<span style='color:" + resultsLinkColor + ";'>" + response[i] +"</span>";
            else
            liText.innerHTML = "<span style='color:" + resultsLinkColor + ";'>" + response[i] +"</span> " + "<span style='color:" + captionColor + ";'>" + airport[i] + "</span>";
        }
        else
        {
            liText.innerHTML="...continue searching for <font style='font-weight:bold;font-size:11px;'>" + query + "</font>";
        }
        li.appendChild(liText);
        li.onmouseover = new Function("mouseOver("+ i +", " + formIndex + ")");
        li.onmousedown = new Function("getSubmit(" + formIndex + ")"); 
        ul.appendChild(li);
    }
    initAutoComplete(prefixText,formIndex);
}

function KeyDown(evt, formIndex)
{
	evtKeyCode = evt.keyCode;
	var validKey = true;
    switch (evtKeyCode)
    {
        case 9: case 10: case 11: case 12: case 13: 
        case 16: case 17: case 18: case 27: case 33: 
        case 34: case 35: case 36: case 37: case 38: 
        case 39: case 40: case 45:
	        validKey = false;
	        break;
        default:
	        break;
    }
    var query = searchForm[formIndex].textBox.value;
    if (validKey && evtKeyCode != 0 && query.length >= minLength - 1)
    { 
        abortRequest(); 
        setTimeout("postBack("+ formIndex +")", requestTimer);
    }
    else if (validKey && evtKeyCode == 0 && searchForm[formIndex].textBox.value != "" && query.length >= minLength - 1) //mouseclick
    { 
        abortRequest(); 
        setTimeout("postBack("+ formIndex +")", 400);
    }
    else
    {
        var autoComplete = document.getElementById(searchForm[formIndex].id);
        switch (evtKeyCode)
        {
            case 13: //enter
                return getSubmit(formIndex);
            break;
            case 38: //up
                if (autoComplete.style.visibility == "visible")
                {
	                highlightRow(searchForm[formIndex].highlightedRow - 1, formIndex);
	            }
            break;
            case 40: //down
                if (autoComplete.style.visibility == "visible")
                {
	                highlightRow(searchForm[formIndex].highlightedRow + 1,formIndex);
	            }
            break;
            case 9:   //tab
            case 16: //shift
            case 17: //cntrl
            case 18: //alt
            case 33: //pup
            case 34: //pdown
            case 35: //end
            case 36: //home
            case 37: //left
            case 39: //right
                abortRequest(); //and do nothing
            break;            
            default:
                if (evtKeyCode != 0)
                {
                    initAutoComplete(searchForm[formIndex].textBox.value,formIndex);
                }
            break; 
        }
    }
    return true;
}

var mouseOver = function(i,formIndex)
{
    if(searchForm[formIndex].highlightedRow != -1 && i != searchForm[formIndex].highlightedRow)
    {
        resetStyle(formIndex);
        searchForm[formIndex].highlightedRow = -1
    }
    searchForm[formIndex].searchResults = document.getElementById(searchForm[formIndex].id).getElementsByTagName("li");
    searchForm[formIndex].suggestionBox = searchForm[formIndex].searchResults.item(i);
    searchForm[formIndex].selection = searchForm[formIndex].selections[i];
    highLight(i,formIndex);
};

function abortRequest()
{
    if (request != null) //is there an active request?
    {
        try
        { 
            request.abort(); //abort it
        }
        catch (e)
        {
        }
        request = null;
    }         
}

function disableEnter(event)
{
    if (event.keyCode == 13)
    {
        return false;
    }
}
function clearResults(formIndex)
{
    document.getElementById(searchForm[formIndex].id).innerHTML = "";
}
        
function showResults(formIndex)
{
    showHideSelect('hidden');
    document.getElementById(searchForm[formIndex].id).style.visibility = "visible";
}

function hideResults(formIndex)
{
    showHideSelect('visible');
    document.getElementById(searchForm[formIndex].id).style.visibility = "hidden";
}
