// Copyright 2006 WildStorms Digital.
// All Rights Reserved
//
// Ajax prototype
//
// Author: Ng Teng Yong
//


var req; 		//xmlhttprequest object
var _selectedRowIndex = -1;
var _previousRowIndex = -1;
var _timerID = 0;
var _inputField = null;
var _currentValue = 'none';
//initialize cross browser xmlhttprequest object
function Initialize() {
    try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (oc) {
            req = null;
        }
    }

    if (!req && typeof XMLHttpRequest != "undefined") {
        req = new XMLHttpRequest();
    }
}

function ShowDiv(divid) {
    if (document.layers)
        document.layers[divid].visibility = "show";
    else {
        if (_inputField.value.length > 0) {
            document.getElementById(divid).style.visibility = "visible";
            document.getElementById(divid).style.borderStyle = "solid";
            document.getElementById(divid).style.borderWidth = "1px";
        }
        else {
            document.getElementById(divid).style.visibility = "hidden";
            document.getElementById(divid).style.borderStyle = "none";
        }
    }
}

function HideDiv(divid) {
    if (document.layers)
        document.layers[divid].visibility = "hide";
    else {
        if (document.getElementById(divid) != null) {
            document.getElementById(divid).style.visibility = "hidden";
            document.getElementById(divid).style.borderStyle = "none";
        }
    }
}

function movein(which, overcolor) {
    which.style.background = overcolor;
}

function moveout(which, outcolor) {
    which.style.background = outcolor;
}

function TypeAheadkeyPress(which) {
    
    var rowElements = document.getElementById('typeAheadResults').getElementsByTagName('td');
    var Key = window.event.keyCode;
    if (Key == 13) {


    }

}
//function movein(which, cssClass) {
//    which.style.css = cssClass;
//}

//function moveout(which, cssClass) {
//    which.style.css = cssClass;
//}

//tbid - Textbox id
function press(tbid, text, dd_press, requirePostBack) {
    if (requirePostBack) {
        el(tbid).value = text;
        Initialize();
        document.getElementById('ctl00_ContentPlaceHolder_hdnCustomerDiscountCode').value = text;
        var baseurl = window.location.href; //get the url of the current web page
        baseurl = baseurl + '?valueText=' + text;

        if (req != null) {
            req.onreadystatechange = function() { HideDiv(dd_press); }; //event handler for each state change
            req.open("GET", baseurl, true);
            req.send(null);
        }
        window.document.forms[0].submit();
    } else {
        //el(tbid).value = text;
        if (_inputField != null) {
            _inputField.value = text;
        }
        HideDiv(dd_press);
    }
}
function MoveUp() {

    if (_selectedRowIndex < 0)
        _selectedRowIndex = 0;
    else
        _selectedRowIndex++;
    var rowElements = document.getElementById('typeAheadResults').getElementsByTagName('td');
    if (rowElements != null && rowElements.length > _selectedRowIndex) {

        _inputField.value = rowElements[_selectedRowIndex].innerHTML;
        if (_inputField.value.length > 0 && _inputField.value != 'none')
            _currentValue = _inputField.value;
        rowElements[_selectedRowIndex].style.background = 'LightBlue';
        if (_previousRowIndex != -1 && (rowElements.length > _previousRowIndex) && (_previousRowIndex != _selectedRowIndex)) {
            rowElements[_previousRowIndex].style.background = 'White'
        }
        _previousRowIndex = _selectedRowIndex;
    }
    else {
        _selectedRowIndex = -1;
    }
}
function MoveDown() {
    _selectedRowIndex--;
    if (_selectedRowIndex < 0) {
        _selectedRowIndex = 0;
    }
    var rowElements = document.getElementById('typeAheadResults').getElementsByTagName('td');
    if (rowElements != null && rowElements.length > _selectedRowIndex) {
        _inputField.value = rowElements[_selectedRowIndex].innerHTML;
        if (_inputField.value.length > 0 && _inputField.value != 'none')
            _currentValue = _inputField.value;
        rowElements[_selectedRowIndex].style.background = 'LightBlue';
        if (_previousRowIndex != -1 && (rowElements.length > _previousRowIndex) && (_previousRowIndex != _selectedRowIndex)) {
            rowElements[_previousRowIndex].style.background = 'White'
        }
        _previousRowIndex = _selectedRowIndex;


    }
}
function onTypeAheadKeyDownEvent(searchButtonID, e, text, requirePostBack) {

    if (requirePostBack) {
        if (e.keyCode == 13 || e.keyCode == 9) {
            if (_currentValue.length > 0 && _currentValue.value != 'none') {
                Initialize();
                document.getElementById('ctl00_ContentPlaceHolder_hdnCustomerDiscountCode').value = text;
                var baseurl = window.location.href; //get the url of the current web page
                baseurl = baseurl + '?valueText=' + text;

                if (req != null) {
                    req.onreadystatechange = function() { HideDiv(text); }; //event handler for each state change
                    req.open("GET", baseurl, true);
                    req.send(null);
                }
                window.document.forms[0].submit();
                return false;
            }
        }
    }
    else {
        if (e.keyCode == 13) {
            document.getElementById(searchButtonID).focus();
            document.getElementById(searchButtonID).click();
            document.getElementById(searchButtonID).focus();
            document.getElementById(searchButtonID).click();
            return false;
        }
    }
}
//SendQuery function sends request to server asyncrhonously.
function SendQuery(source, arg, key, dd_, styshet_, e, searchButtonID, isAllow, requirePostBack) {
    if (requirePostBack) {
        Initialize();
        _inputField = source;
        var baseurl = window.location.href; //get the url of the current web page
        var keyCode = e.keyCode;
        document.getElementById('ctl00_ContentPlaceHolder_hdnCustomerDiscountCode').value = key;
        //check if other HTTP GET argument exist,if yes use "?" else use "&" to concatenate http request string
        if (baseurl.indexOf("?") == -1)
            var url = baseurl + "?" + arg + "=" + key; //the get argument must be unique for each control.arg is a unique variable of a web page
        else
            var url = baseurl + "&" + arg + "=" + key;

        if (keyCode == 40) {
            MoveUp();


        }

        //keyMoveUp
        else if (keyCode == 38) {
            MoveDown();

        }
        if (keyCode != 40 && keyCode != 38 && keyCode != 13) {
            if (req != null) {
                req.onreadystatechange = function() { Process(dd_, styshet_); }; //event handler for each state change
                req.open("GET", url, true);
                req.send(null);
            }
        }
    }
    else {
        if (isAllow == "true") {
            _inputField = source;
            var keyCode = e.keyCode;
            //keyMoveDown
            if (keyCode == 40) {
                MoveUp();


            }

            //keyMoveUp
            else if (keyCode == 38) {
                MoveDown();

            }
            else if (keyCode == 13) {
                document.getElementById(searchButtonID).focus();
                document.getElementById(searchButtonID).click();
                document.getElementById(searchButtonID).focus();
                document.getElementById(searchButtonID).click();
                return false;

            }
            else {
                _previousRowIndex = -1;
                _selectedRowIndex = -1;
                Initialize();
                var baseurl = window.location.href; //get the url of the current web page

                //check if other HTTP GET argument exist,if yes use "?" else use "&" to concatenate http request string
                if (baseurl.indexOf("?") == -1)
                    var url = baseurl + "?" + arg + "=" + key; //the get argument must be unique for each control.arg is a unique variable of a web page
                else
                    var url = baseurl + "&" + arg + "=" + key;

                if (req != null) {
                    req.onreadystatechange = function() { Process(dd_, styshet_); }; //event handler for each state change
                    req.open("GET", url, true);
                    req.send(null);
                }
            }
        }
    }

}

function Process(dd, styshet) {
    if (req.readyState == 4) {
        if (req.status == 200)// only if "OK"
        {
            if (req.responseText == "<result></result>")
                HideDiv(dd); //hide drop down area
            else {
                ShowDiv(dd); //show drop down area
                var xml = xmlParse(req.responseText);
                var xslt = xmlParse(styshet);
                var html = xsltProcess(xml, xslt);
                if (html == "")
                    HideDiv(dd);
                else
                    el(dd).innerHTML = html;

            }
        }
        else {
            el(dd).innerHTML = "There was a problem retrieving data:<br>" + req.statusText;
        }
    }
}


