﻿var currEntry;
var numEntries = 0;
var currSelect = -1;

function validateLocation(evt) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (evt) keycode = evt.which;
    var o = getEventTarget(evt);

    if (keycode == 38 && numEntries > 0) //up
    {
        currSelect--;
        setSelected();
        return;
    }
    else if (keycode == 40 && numEntries > 0)//down
    {
        currSelect++;
        setSelected();
        return;
    }
    else if (keycode == 13 && numEntries > 0 && currSelect >=0)//enter
    {
        if (currSelect == -1) return;
        o.value = trim($("id" + currSelect).innerHTML.replace(/\(city center\)/, ''));
        hideMatches();
        return false;
    }

    
    currEntry = o;
    numEntries = 0;
    currSelect = -1
    var t = o.value;
    if (t.length > 3) {
        www.fuelsurchargeindex.com.UIFSI.ValidateLocation(t, handleValidateLocations, handleNoMatches);
    }
    else {
        handleNoMatches();
    }
}

function checkTab(evt) {

    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (evt) keycode = evt.which;
    var o = getEventTarget(evt);
    
    if (keycode == 9 && numEntries > 0 && currSelect >= 0)//enter
    {
        if (currSelect == -1) return;
        o.value = trim($("id" + currSelect).innerHTML.replace(/\(city center\)/, ''));
        hideMatches();
    }
}

function clearMatches() //Entered new field. Clear
{
    numEntries = 0;
    currSelect = -1;
    hideMatches();
    var o = $("Matches");
    removeChildren(o);
}

function setSelected() 
{
    if (currSelect <= 0) {
        currSelect = 0;
        if(numEntries > 1)("id" + (currSelect + 1)).className = "MatchBox";
        $("id" + currSelect).className = "MatchBoxHighlight";
    }
    else if (currSelect >= numEntries - 1) {
        currSelect = numEntries - 1;
        $("id" + (currSelect - 1)).className = "MatchBox";
        $("id" + currSelect).className = "MatchBoxHighlight";
    }
    else {
        $("id" + (currSelect + 1)).className = "MatchBox";
        $("id" + currSelect).className = "MatchBoxHighlight";
        $("id" + (currSelect - 1)).className = "MatchBox";
    }
}

function handleNoMatches() {
    var o = $("Matches");
    o.style.visibility = "hidden";
}

function hideMatches() {
    var o = $("Matches");
    o.style.visibility = "hidden";
}

function handleValidateLocations(s) {
    var t = eval("(" + s + ")");
    var o = $("Matches");
    removeChildren(o);
    numEntries = t.MatchedLocations.length;
    var pos = getObjectPosition(currEntry);
    o.style.left = pos.x + "px";
    o.style.top = (pos.y + currEntry.offsetHeight) + "px";
    o.style.width = currEntry.style.width;
    o.style.visibility = "visible";
    var div;
    var text;

    for (i = 0; i < t.MatchedLocations.length; i++) {
        div = document.createElement("div");
        div.id = "id" + i;
        text = document.createTextNode(t.MatchedLocations[i].LocationText);
        div.appendChild(text);
        div.className = "MatchBox";
        div.onmouseover = function() { this.className = 'MatchBoxHighlight' }
        div.onmouseout = function() { this.className = 'MatchBox' }
        addEvent(div, "mousedown", setLocation); // click won't work. Textbox fires blur first and cancels it.

        o.appendChild(div);
    }
}

function setLocation(evt) {
    var o = getEventTarget(evt);
    currEntry.value = o.innerHTML.replace(/\(city center\)/, '');
}


