/* 

SearchField 
written by Alen Grakalic, provided by Css Globe (cssglobe.com)
please visit http://cssglobe.com/post/1202/style-your-websites-search-field-with-jscss/ for more info
	
*/

var generalpath = '';
var AutoComplete_Box_Id = '';

var AdultRow1 = '';
var AdultRow2 = '';
var AdultRow3 = '';
var AdultRow4 = '';
var AdultRow5 = '';
var ChildRow1 = '';
var ChildRow2 = '';
var ChildRow3 = '';
var ChildRow4 = '';
var ChildRow5 = '';
var MaxAdultCount = '';
var MaxChildCount = '';
var ddlRoomsClientId = '';
var HFMaxAdultCountClientId = '';
var HFMaxChildCountClientId = '';
var adultChildCount;

var sDestList = '';
var suggestionText;
var agt = navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var cityList = new Array();
//var searchButtonID = 'ctl00_MasterPage_Main_CPH_CMS_DestinationGuideSearch_UC_CMS_HolidayDestinationGuide_DG_Div1_submit'

this.searchfield = function() {
    // CONFIG 
    // this is id of the search field you want to add this script to.
    // You can use your own id just make sure that it matches the search field in your html file.
    //"ctl00_ctl00_CPH_MainContainer_CTL_HotelSearchControl_UC_AutoComplete_TB_AC_CitySearch"
    //var id = "ctl00$CPH_MainContainer$CTL_HomeBody$CTL_HomeSearchControl$UC_AutoComplete$TB_AC_CitySearch";

    // Text you want to set as a default value of your search field.
    var defaultText = "Enter City Name...";

    // set to either true or false
    // when set to true it will generate search suggestions list for search field based on content of variable below
    var suggestion = true;

    // static list of suggestion options, separated by comma
    // replace with your own
    suggestionText = getDestinationListFromDB();

    // END CONFIG (do not edit below this line, well unless you really, really want to change something :) )

    // Peace,
    // Alen

    var field = document.getElementById(AutoComplete_Box_Id);
    var classInactive = "sf_inactive";
    var classActive = "sf_active";
    var classText = "sf_text";
    var classSuggestion = "sf_suggestion";
    this.safari = ((parseInt(navigator.productSub) >= 20020000) && (navigator.vendor.indexOf("Apple Computer") != -1));
    if (field && !safari) {
        field.value = defaultText;
        field.c = field.className;
        field.className = field.c + " " + classInactive;
        field.onfocus = function() {
            this.className = this.c + " " + classActive;
            this.value = (this.value == "" || this.value == defaultText) ? "" : this.value;
        };
        field.onblur = function() {
            this.className = (this.value != "" && this.value != defaultText) ? this.c + " " + classText : this.c + " " + classInactive;
            this.value = (this.value != "" && this.value != defaultText) ? this.value : defaultText;
            clearList();
        };
        if (suggestion) {

            var selectedIndex = 0;

            field.setAttribute("autocomplete", "off");
            var div = document.createElement("div");
            var list = document.createElement("ul");
            list.style.display = "none";
            div.className = classSuggestion;
            list.style.width = field.offsetWidth + 40 + "px";
            div.appendChild(list);
            field.parentNode.appendChild(div);

            field.onkeypress = function(e) {
                var key = getKeyCode(e);

                if (key == 13) { // enter
                    selectList();
                    selectedIndex = 0;
                    return clickButton(key);

                }
            };

            field.onkeyup = function(e) {
                var key = getKeyCode(e);

                switch (key) {
                    case 13:
                        var returnv = clickButton(key);
                        return returnv;
                        break;
                    case 27:  // esc
                        field.value = "";
                        selectedIndex = 0;
                        clearList();
                        break;
                    case 38: // up
                        navList("up");
                        break;
                    case 40: // down
                        navList("down");
                        break;
                    default:
                        startList();
                        break;
                };
            };

            this.startList = function() {
                var arr = getListItems(field.value);
                if (field.value.length > 0) {
                    createList(arr);
                } else {
                    clearList();
                };
            };

            this.getListItems = function(value) {
                var arr = new Array();
                var src = suggestionText;
                var src = src.replace(/, /g, ",");
                var arrSrc = src.split(",");
                for (i = 0; i < arrSrc.length; i++) {
                    if (arrSrc[i].substring(0, value.length).toLowerCase() == value.toLowerCase()) {
                        arr.push(arrSrc[i]);
                    };
                };
                cityList = arr;
                return arr;
            };

            this.createList = function(arr) {
                resetList();
                if (arr.length > 0) {
                    for (i = 0; i < arr.length; i++) {
                        li = document.createElement("li");
                        a = document.createElement("a");
                        a.href = "javascript:void(0);";
                        a.i = i + 1;
                        a.innerHTML = arr[i];
                        li.i = i + 1;
                        li.onmouseover = function() {
                            navListItem(this.i);
                        };
                        a.onmousedown = function() {
                            selectedIndex = this.i;
                            selectList(this.i);
                            return false;
                        };
                        li.appendChild(a);
                        list.setAttribute("tabindex", "-1");
                        list.appendChild(li);
                    };
                    list.style.display = "block";
                } else {
                    clearList();
                };
            };

            this.resetList = function() {
                var li = list.getElementsByTagName("li");
                var len = li.length;
                for (var i = 0; i < len; i++) {
                    list.removeChild(li[0]);
                };
            };

            this.navList = function(dir) {
                selectedIndex += (dir == "down") ? 1 : -1;
                li = list.getElementsByTagName("li");
                if (selectedIndex < 1) selectedIndex = li.length;
                if (selectedIndex > li.length) selectedIndex = 1;
                navListItem(selectedIndex);
            };

            this.navListItem = function(index) {
                selectedIndex = index;
                li = list.getElementsByTagName("li");
                for (var i = 0; i < li.length; i++) {
                    li[i].className = (i == (selectedIndex - 1)) ? "selected" : "";
                };
            };

            this.selectList = function() {
                li = list.getElementsByTagName("li");
                if (selectedIndex > 0) {
                    if (li.length > (selectedIndex - 1)) {
                        a = li[selectedIndex - 1].getElementsByTagName("a")[0];
                        field.value = a.innerHTML;
                    }
                }
                clearList();
                // aniruddha start
                var adultChildCount = getMaxAdultAndChildrenCount(field.value); //new
                if (adultChildCount) {
                    var adultChildCount = adultChildCount.replace(/, /g, ",");
                    var arrSrc1 = adultChildCount.split(",");

                    MaxAdultCount = Math.abs(arrSrc1[0]);
                    MaxChildCount = Math.abs(arrSrc1[1]);

                    if (document.getElementById(HFMaxAdultCountClientId)) {
                        document.getElementById(HFMaxAdultCountClientId).value = arrSrc1[0];
                    }
                    if (document.getElementById(HFMaxChildCountClientId)) {
                        document.getElementById(HFMaxChildCountClientId).value = arrSrc1[1];
                    }

                    PopulateAdultChildDropdowns();
                }

                // aniruddha end
            };
        };
    };

    this.clearList = function() {
        if (list) {
            list.style.display = "none";
            selectedIndex = 0;
        };
    };
    this.getKeyCode = function(e) {
        var code;
        if (!e) var e = window.event;
        if (e.keyCode) code = e.keyCode;
        return code;
    };

};

// script initiates on page load.

//this.addEvent = function(obj, type, fn) {
//    if (obj.attachEvent) {
//        obj['e' + type + fn] = fn;
//        obj[type + fn] = function() { obj['e' + type + fn](window.event); }
//        obj.attachEvent('on' + type, obj[type + fn]);
//    } else {
//        obj.addEventListener(type, fn, false);
//    };
//};

this.addEvent = function(obj, type, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(type, fn, false);
    }
    else if (obj.attachEvent) {
        obj['e' + type + fn] = fn;
        obj[type + fn] = function() { obj['e' + type + fn](window.event); }
        obj.attachEvent('on' + type, obj[type + fn]);
    } else {
        obj.addEventListener(type, fn, false);
    };
};

addEvent(window, "load", searchfield);

function getDestinationListFromDB() {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {  // Internet Explorer  
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            suggestionText = xmlHttp.responseText;
        }
    }

    //var url = generalpath + "AutoCompleteList.aspx";
    var url = "http://localhost/Ginger.Website/Hotels/AutoCompleteList.aspx";
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return suggestionText;
}

function getMaxAdultAndChildrenCount(CityName) {   
    var xmlHttp;   

    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {  // Internet Explorer  
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            adultChildCount = xmlHttp.responseText;
        }
    }

    //var url = generalpath + "AutoCompleteList.aspx";
    var url = "http://localhost/Ginger.Website/Hotels/HotelAdultChildCount.aspx?cityname=" + CityName;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return adultChildCount;
}

function clickButton(e) {
    //    var bt = document.getElementById(searchButtonID);
    //    if (bt) {
    //        window.location.href = bt;
    //        return false;
    //    }
    return false;
}


function ValidateSearchCity() {

    var fieldvalue = document.getElementById(AutoComplete_Box_Id).value;

    var arr = new Array();
    var src = getDestinationListFromDB();
    var src = src.replace(/, /g, ",");
    var arrSrc = src.split(",");
    for (i = 0; i < arrSrc.length; i++) {
        arr.push(arrSrc[i]);
    };

    if (fieldvalue == '' || fieldvalue == 'Enter City Name...') {
        alert("Please select a city from the city list.");
        document.getElementById(AutoComplete_Box_Id).focus();
        return false;
    }
    if (fieldvalue != '') {
        var match = false;
        for (i = 0; i < arr.length; i++) {
            if (arr[i].toLowerCase() == fieldvalue.toLowerCase()) {
                match = true;
                break;
            };
        };

        if (match == false) {
            alert("Please select a city from the provided city list only.");
            document.getElementById(AutoComplete_Box_Id).focus();
            return false;
        }

    }
    else return true;
}

function PopulateAdultChildDropdowns() {

    if (document.getElementById(ddlRoomsClientId)) {

        //Fill ddlAdult dropdown list.    
        if (document.getElementById(AdultRow1)) {
            SetAdultDropDownList(AdultRow1, MaxAdultCount);
        }

        if (document.getElementById(AdultRow2)) {
            SetAdultDropDownList(AdultRow2, MaxAdultCount);
        }

        if (document.getElementById(AdultRow3)) {
            SetAdultDropDownList(AdultRow3, MaxAdultCount);
        }

        if (document.getElementById(AdultRow4)) {
            SetAdultDropDownList(AdultRow4, MaxAdultCount);
        }

        if (document.getElementById(AdultRow5)) {
            SetAdultDropDownList(AdultRow5, MaxAdultCount);
        }

        //Fill ddlChild dropdown list.
        if (document.getElementById(ChildRow1)) {
            SetChildDropDownList(ChildRow1, MaxChildCount);
        }

        if (document.getElementById(ChildRow2)) {
            SetChildDropDownList(ChildRow2, MaxChildCount);
        }

        if (document.getElementById(ChildRow3)) {
            SetChildDropDownList(ChildRow3, MaxChildCount);
        }

        if (document.getElementById(ChildRow4)) {
            SetChildDropDownList(ChildRow4, MaxChildCount);
        }

        if (document.getElementById(ChildRow5)) {
            SetChildDropDownList(ChildRow5, MaxChildCount);
        }

        return true;
    }

    return false;
}


function SetAdultDropDownList(ID, Count) {
    document.getElementById(ID).options.length = 0;

    for (var x = 1; x <= Count; x++) {
        var optn = document.createElement("OPTION");
        optn.text = "" + x;
        optn.value = "" + x;
        document.getElementById(ID).options.add(optn);
    }
}

function SetChildDropDownList(ID, Count) {
    document.getElementById(ID).options.length = 0;

    for (var x = 0; x <= Count; x++) {
        var optn = document.createElement("OPTION");
        optn.text = "" + x;
        optn.value = "" + x;
        document.getElementById(ID).options.add(optn);
    }
}

