﻿function Ace_FillDropDown (name, text, value) {
    var cntrl = document.getElementById (name);
    while (cntrl.firstChild != null) cntrl.removeChild (cntrl.firstChild);
    for (x=0;x<text.length;x++) {
        var opt = document.createElement("OPTION");
        cntrl.add (opt);
        opt.innerHTML = text[x];
        if (value != null) {
            opt.value = value[x];
        } else {
            opt.Value = text[x];
        }
    }
}

function Ace_Locator() {
    // Globals
    this.count = 0;
    this.selected = -1;
    this.active = false;
    this.cntrl = document.getElementById('Ratebox_txtLocator');
    this.hidden = document.getElementById('Ratebox_txtHidden');
    
    // Shim IFRAME
    this.shim = document.createElement ("IFRAME");
    this.shim.src = "javascript:'';";
    this.shim.scrolling = 'no';
    this.shim.frameBorder = 'no';
    this.shim.style.display = 'none';
    this.shim.style.position = 'absolute';
    this.shim.style.zIndex = 10000;
    
    // Top Level Table Element
    this.table = document.createElement ("TABLE");
    this.table.border = 0;
    this.table.cellPadding = 0;
    this.table.cellSpacing = 0;
    this.table.className = 'locatortable';
    this.table.style.width = (this.cntrl.offsetWidth + 40) + 'px';
    this.table.style.display = 'none';
    this.table.style.position = 'absolute';
    this.table.style.zIndex = this.shim.style.zIndex + 1;
    
    // Attach To Document
    var body = document.getElementsByTagName ('body').item (0);
    body.appendChild(this.shim);
    body.appendChild(this.table);
    
    // Start Location Load
    this.Load = function(session, success)
    {
        AceWebService.LocationLookup(session, this.cntrl.value, success);
        return false;
    }
    
    // Retrieve Location Load Results
    this.LookupResults = function (value) {
        var Airports = value.Airport;
        var Locs = value.Value;
        var Descs = value.Text;
        if (Locs.length < 1) {
            this.Hide ();
            alert ('No Locations Found');
            return;
        }
        var Body;
        if (this.table.firstChild == null) {
            Body = document.createElement ("TBODY");
            this.table.appendChild(Body);
        } else {
            Body = this.table.firstChild;
        }
        while (Body.firstChild) Body.removeChild (Body.firstChild);
        this.selected = -1;
        this.count = 0;
        for (x=0;x<Locs.length;x++) {
            var tr = document.createElement ("TR");
            tr.className = 'locatordefault';
            tr.vAlign = 'middle';
            tr.onmouseover = this.MouseOver;
            tr.onmousedown = this.MouseDown;

            // First Column = Index Value
            var index = document.createElement("TD");
            index.style.display = 'none';
            index.innerHTML = x;
            tr.appendChild (index);

            // Second Column = Location Code
            var loc = document.createElement ("TD");
            loc.style.display = 'none';
            loc.innerHTML = Locs[x];
            tr.appendChild (loc);

            // Third Column = Airplane And Description            
            var desc = document.createElement ("TD");
            if (Airports[x]) {
                var pic = document.createElement ("IMG");
                pic.src = '/images/airplane.gif';
                desc.appendChild (pic);
            }
            desc.appendChild (document.createTextNode (Descs[x]));
            //desc.onmousedown = this.MouseDown;
            tr.appendChild (desc);
            
            Body.appendChild (tr);
            this.count++;
        }
        this.active = false;
        if (this.count) {
            var top = 0;
            var left = 0;
            var temp = this.cntrl;
            while (temp) {
                top += temp.offsetTop;
                left += temp.offsetLeft;
                temp = temp.offsetParent;
            }
            this.table.style.top = (top + this.cntrl.offsetHeight) + "px";
            this.table.style.left = left + "px";
            this.shim.style.display = '';
            this.table.style.display = '';
            this.active = true;
            this.SetupShim();
            this.Select(0);
        }
    }
    
    this.Accept = function (index) {
        if (this.active) {
            var body = this.table.firstChild;
            if (body && index >= 0) {
                var tr = body.childNodes.item(index);
                var desc = tr.lastChild.lastChild.data;
                var loc = tr.childNodes.item(1).innerHTML;
                this.cntrl.value = desc;
                this.hidden.value = loc;
            }
            this.Hide ();
            __doPostBack('Ratebox$txtLocator','Locator');
        }
    }
    
    // Setup Shim Size
    this.SetupShim = function () {
        this.shim.style.top = this.table.offsetTop + "px";
        this.shim.style.left = this.table.offsetLeft + "px";
        this.shim.style.width = this.table.offsetWidth + "px";
        this.shim.style.height = this.table.offsetHeight + "px";
    } 

    this.Select = function (index) {
        if (this.active) {
            var Body;
            this.selected = index;
            if (this.table.firstChild == null) {
                Body = document.createElement ("TBODY");
                this.table.appendChild(Body);
            } else {
                Body = this.table.firstChild;
            }
            if (Body) {
                for (x=0;x<Body.childNodes.length;x++) {
                    var tr = Body.childNodes.item(x);
                    if (x == this.selected) {
                        tr.className = 'locatorselected';
                    } else {
                        tr.className = 'locatordefault';
                    }
                }
            }
            this.SetupShim();
        }
    }

    this.MoveUp = function () {
        if (this.count) {
            var temp = this.selected + 1;
            if (temp < this.count) this.Select (temp);
        }
    }
    this.MoveDown = function () {
        if (this.count) {
            var temp = this.selected - 1;
            if (temp >= 0) this.Select (temp);
        }
    }

    this.Hide = function () {
        this.active = false;
        this.shim.style.display = 'none';
        this.table.style.display = 'none';
    }

    // MouseOver Popup Entries (Direct Event Handler)
    this.MouseOver = function (e) {
        if (Locator) {
            if (Locator.active) {
                Locator.Select (parseInt (this.firstChild.innerHTML, 10));
            }
        }
    }
    
    // Mouse Down Entries (Direct Event Handler)
    this.MouseDown = function (e) {
        if (Locator) {
            if (Locator.active) {
                Locator.Accept (parseInt (this.firstChild.innerHTML, 10));
            }
        }
    }
    
    // Got Focus On Locator
    this.Focus = function (e) {
        if (this.cntrl.value == '[Enter location name - 3 letters or more]') {
            this.cntrl.value = '';
        }
    }
    
    // Lost Focus On Locator
    this.Blur = function (e) {
        if (this.active) {
            if (this.selected != -1) {
                this.Accept (this.selected);
            } else {
                this.Hide ();
            }
        }
    }
    
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


