﻿
var ActiveSearch = 1;

function SelectLink(NewActiveSearch) {
    var ActiveSearchLink = document.getElementById("navi_search_" + ActiveSearch);
    var ActiveSearchContent = document.getElementById("search_content_" + ActiveSearch);
    var NewActiveSearchLink = document.getElementById("navi_search_" + NewActiveSearch);
    var NewActiveSearchContent = document.getElementById("search_content_" + NewActiveSearch);
    ActiveSearchLink.className = "nav-deselected";
    ActiveSearchContent.style.display = "none";
    NewActiveSearchLink.className = "nav-selected";
    NewActiveSearchContent.style.display = "block";
    ActiveSearch = NewActiveSearch;
}

function GoToHotelReview(pagename, hid) {
    document.aspnetForm.action = pagename + ".aspx?hid=" + hid;
    document.aspnetForm.submit();
}

function GoToPage(pagename) {
    document.aspnetForm.action = pagename;
    document.aspnetForm.submit();
}

function doSubmit() {
    document.aspnetForm.action = 'HotelResults.aspx';
    document.aspnetForm.submit();
}

function HideSearch() {
    document.getElementById("search-form-content").style.display = "none";
}

function ValidateChildrenAge() {
    return true;
}
function UpdateStations(areagroupid, obj1, obj2, obj3, obj4) {
    var objOption1 = document.createElement('option');
    var objOption2 = document.createElement('option');
    objOptionCount = obj1.options.length;

    for (var x = 0; x <= objOptionCount; x++) {
        obj1.remove(obj1.options[x]);
        obj2.remove(obj2.options[x]);
    }

    if (areagroupid != 0) {
        for (var i = 0; i < StationArray.length; i++) {
            objOption1 = document.createElement('option');
            objOption2 = document.createElement('option');

            if (StationArray[i][0] == areagroupid) {
                objOption1.text = StationArray[i][2];
                objOption1.value = StationArray[i][1];
                objOption2.text = StationArray[i][2];
                objOption2.value = StationArray[i][1];

                try {
                    obj1.add(objOption1, null);
                    obj2.add(objOption2, null); // standards compliant
                }
                catch (ex) {
                    obj1.add(objOption1);
                    obj2.add(objOption2); // IE only
                }
            }
        }

        obj1.disabled = false;
        obj2.disabled = false;

        obj3.value = obj1.value;
        obj4.value = obj2.value;
    }
    else {

        objOption1.text = "Please select an area.";
        objOption1.value = "0";
        objOption2.text = "Please select an area.";
        objOption2.value = "0";

        try {
            obj1.add(objOption1, null);
            obj2.add(objOption2, null); // standards compliant
        }
        catch (ex) {
            obj1.add(objOption1);
            obj2.add(objOption2); // IE only
        }

        obj1.disabled = true;
        obj2.disabled = true;
    }
}
function UpdateLocation(provinceid, objLocation, objProvinceID, objLocationID) {
    var objOption = document.createElement('option');

    objOptionCount = objLocation.options.length;

    for (var x = 0; x <= objOptionCount; x++) {
        objLocation.remove(objLocation.options[x]);
    }

    objOption.text = "All";
    objOption.value = "0";

    try {
        objLocation.add(objOption, null); // standards compliant
    }
    catch (ex) {
        objLocation.add(objOption); // IE only
    }

    for (var i = 0; i < CityArray.length; i++) {
        objOption = document.createElement('option');

        if (provinceid == 0 || CityArray[i][0] == provinceid) {
            objOption.text = CityArray[i][2];
            objOption.value = CityArray[i][1];

            try {
                objLocation.add(objOption, null); // standards compliant
            }
            catch (ex) {
                objLocation.add(objOption); // IE only
            }
        }
    }

    objProvinceID.value = provinceid;
    objLocationID.value = '0'

    var objDestination = document.getElementById(strDestinationID);

    if (provinceid != 0)
        objDestination.value = '';
}

function SetLocation(locationid, objLocationID) {
    objLocationID.value = locationid;

    var objDestination = document.getElementById(strDestinationID);

    if (locationid != 0)
        objDestination.value = '';
}

function GetList(obj, value, id) //id: 0 = Destinations, 1 = Hotel Names
{
    ActiveSuggest = id;
    popup[id] = document.getElementById("PopupList" + id);
    objOutputHolders[id] = obj;

    if (!isCached[id]) {
        if (request.readyState == 0 || request.readyState == 4) {
            request.open("GET", urls[id], true);
            request.onreadystatechange = loadSuggestData;
            request.send(null);
        }
    }
    else {
        if (value.length > 2) {
            var listDataTitles

            var SearchResult = "";
            var a = 0;
            var b;
            var i = 0;

            popup[id].innerHTML = "";

            try {
                if (id == 0) {
                    listDataTitles = objDataHolders[id].Destinations.Destination;

                    b = listDataTitles.length

                    while (a < b) {
                        SearchResult = listDataTitles[a].Name.match(new RegExp(value, "i"));
                        if (SearchResult != null) {
                            if (listDataTitles[a].Type == "City")
                                popup[id].innerHTML += "<a href=\"javascript:;\" onclick=\"SetHolder('" + listDataTitles[a].Name + "',0,'City'," + listDataTitles[a].ID + ");\">" + listDataTitles[a].Name.replace(SearchResult, "<strong>" + SearchResult + "</strong>") + "</a>";
                            else
                                popup[id].innerHTML += "<a href=\"javascript:;\" onclick=\"SetHolder('" + listDataTitles[a].Name + "',0,'Province'," + listDataTitles[a].ID + ");\">" + listDataTitles[a].Name.replace(SearchResult, "<strong>" + SearchResult + "</strong>") + " <em>(Province)</em></a>";
                            i++;
                        }
                        if (i == 15)
                            a = b;
                        else
                            a++;
                    }
                }
                else if (id == 1) {
                    listDataTitles = objDataHolders[id].Hotels.Hotel;

                    b = listDataTitles.length

                    while (a < b) {
                        SearchResult = listDataTitles[a].Name.match(new RegExp(value, "i"));
                        if (SearchResult != null) {
                            popup[id].innerHTML += "<a href=\"javascript:;\" onclick=\"SetHolder('" + listDataTitles[a].Name + "',1);\">" + listDataTitles[a].Name.replace(SearchResult, "<strong>" + SearchResult + "</strong>") + "</a>";
                            i++;
                        }
                        if (i == 15)
                            a = b;
                        else
                            a++;
                    }
                }
            }
            catch (Error) {
                isCached[id] = false;
            }


            if (i != 0)
                popup[id].style.display = 'block';
            else {
                popup[id].innerHTML = "<img src='/images/loader.gif' alt='loading...' title='loading...' />";
                popup[id].style.display = 'none';
            }
        }
        else {
            popup[id].innerHTML = "<img src='/images/loader.gif' alt='loading...' title='loading...' />";
            popup[id].style.display = 'none';
        }
    }
}

function loadSuggestData() {
    if (request.readyState == 4) {
        if (request.status == 200) {
            var response = request.responseText;
            objDataHolders[ActiveSuggest] = JSON.parse(response);
            isCached[ActiveSuggest] = true;
            GetList(objOutputHolders[ActiveSuggest], objOutputHolders[ActiveSuggest].value, ActiveSuggest);
        }
        else if (request.status == 404) {
            alert("Page Does Not Exist!");
        }
        else {
            alert("Error: " + request.status);
        }
    }
}

function SetHolder(value, id, type, d_id) {
    objOutputHolders[id].value = value;
    popup[id].innerHTML = "<img src='/images/loader.gif' alt='loading...' title='loading...' />";
    popup[id].style.display = 'none';
    isPopupClicked[id] = false;

    var id_holder;
    var id_reset;

    if (id == 0) {
        document.getElementById(strProvinceDDL)[0].selected = true;
        document.getElementById(strCityDDL)[0].selected = true;
        UpdateLocation(0, document.getElementById(strCityDDL), document.getElementById(strProvinceID), document.getElementById(strCityID));
        if (type == "City") {
            id_holder = document.getElementById(strCityID);
            id_reset = document.getElementById(strProvinceID);
            id_holder.value = d_id;
            id_reset.value = 0;
        }
        else {
            id_holder = document.getElementById(strProvinceID);
            id_reset = document.getElementById(strCityID);
            id_holder.value = d_id;
            id_reset.value = 0;
        }
        document.getElementById(strIsValidID).value = "true";
    }
    else {
        document.getElementById(strProvinceID).value = "0";
        document.getElementById(strCityID).value = "0";
        document.getElementById(strDestinationID).value = "";
        document.getElementById(strIsValidID).value = "true";
    }
}

function SubmitSearch(url) {
    var objIsValid = document.getElementById(strIsValidID);
    var objProvince = document.getElementById(strProvinceID);
    var objCity = document.getElementById(strCityID);
    var objDestination = document.getElementById(strDestinationID);
    var objHotelName = document.getElementById(strHotelNameID);

    if (objHotelName.value.length > 4) {
        if (objProvince.value == "0" && objCity.value == "0")
            objDestination.value = "";
        objIsValid.value = "true";
    }
    else if (objProvince.value != "0" || objCity.value != "0")
        objIsValid.value = "true";
    else {
        if (objDestination.value.length > 2) {
            objIsValid.value = "false";
            alert("Please select a destination.");
            objDestination.focus();
        }
        else {
            objIsValid.value = "false";
            alert("Please select a destination or a hotel name.");
        }
    }

    if (objIsValid.value == "true") {
        FadeLoadingBar();
        document.aspnetForm.action = url;
        document.aspnetForm.submit();
    }
}

// Travelling Party functions

function OpenEditParty(id) {
    document.getElementById(id).style.display = "block";
}

function BuildRoom(NumberOfRooms, Prefix) {
    var Rooms = new Array(4);

    Rooms[1] = document.getElementById(Prefix + "Room1");
    Rooms[2] = document.getElementById(Prefix + "Room2");
    Rooms[3] = document.getElementById(Prefix + "Room3");
    Rooms[4] = document.getElementById(Prefix + "Room4");

    Rooms[1].style.display = 'none';
    Rooms[2].style.display = 'none';
    Rooms[3].style.display = 'none';
    Rooms[4].style.display = 'none';

    for (var x = 1; x <= NumberOfRooms; x++)
        Rooms[x].style.display = '';
}

function BuildChildAges(Suffix, NumberOfChildren, Prefix) {
    var AgeRow = document.getElementById(Prefix + "Ages" + Suffix);
    var SelectHolder = document.getElementById(Prefix + "ChildrenAges" + Suffix);
    var MaxAge = 15;

    if (NumberOfChildren == 0)
        AgeRow.style.display = 'none';
    else
        AgeRow.style.display = '';

    SelectHolder.innerHTML = "";

    var objSelectChildAge;
    var objAgeOption;

    for (var x = 1; x <= NumberOfChildren; x++) {

        objSelectChildAge = document.createElement('select');
        objSelectChildAge.style.width = "50px";
        objSelectChildAge.id = Prefix + "Age" + Suffix + "Child" + x;
        objAgeOption = document.createElement('option');
        objAgeOption.text = "-";
        objAgeOption.value = "-";
        try {
            objSelectChildAge.add(objAgeOption, null); // standards compliant
        }
        catch (ex) {
            objSelectChildAge.add(objAgeOption); // IE only
        }

        for (var y = 0; y <= MaxAge; y++) {
            objAgeOption = document.createElement('option');
            objAgeOption.text = y;
            objAgeOption.value = y;
            try {
                objSelectChildAge.add(objAgeOption, null); // standards compliant
            }
            catch (ex) {
                objSelectChildAge.add(objAgeOption); // IE only
            }
        }

        SelectHolder.appendChild(objSelectChildAge);
    }
}

function SaveParty(Prefix) {
    var objRooms = document.getElementById(Prefix + "Rooms");
    var strPartyCode = "";
    var strPartyDetails = "";

    var Adults;
    var Children;
    var ChildAge;

    for (var x = 1; x <= objRooms.value; x++) {
        Adults = document.getElementById(Prefix + "AdultsRoom" + x).value;
        Children = document.getElementById(Prefix + "ChildrenRoom" + x).value;

        if (x != 1) {
            strPartyCode += "|";
            strPartyDetails += "<br />";
        }
        strPartyCode += "R:" + x + "$";
        strPartyCode += "A:" + Adults + "$";
        strPartyCode += "C:" + Children + "$";
        strPartyCode += "CA:";
        strPartyDetails += "Room " + x + ": ";
        strPartyDetails += Adults + " Adults, ";

        var strChildAges = '';

        if (Children > 0) {
            strChildAges += '(Ages: ';
            for (var y = 1; y <= Children; y++) {
                ChildAge = document.getElementById(Prefix + "AgeRoom" + x + "Child" + y).value;
                if (ChildAge == "-") {
                    alert("Please enter the ages of all children.");
                    return false;
                }
                if (y != 1) {
                    strPartyCode += ",";
                    strChildAges += ", ";
                }
                strPartyCode += ChildAge;
                strChildAges += ChildAge;
            }
            strChildAges += ")";

            strPartyDetails += '<a href="#" class="with-tooltip" title="' + strChildAges + '">' + Children + " Children</a>";
        }
        else {
            strPartyDetails += '<a href="#" class="with-tooltip" title="No Children">' + Children + " Children</a>";
        }
    }



    document.getElementById(Prefix + "TravellingParty").innerHTML = strPartyDetails;
    if (document.getElementById(Prefix + "EditParty") != null) {
        document.getElementById(Prefix + "EditParty").style.display = "none";
    }

    if (Prefix != '') {
        DecodeParty(strPartyCode, '');
    } else {
        //alert(strPartyCode);
        document.getElementById(strPartyCodeID).value = strPartyCode;
        return true;
    }
}

function DecodeParty(Code, Prefix) {
    var arrRooms = Code.split("|");
    var arrRoomItems = new Array();
    var Rooms;
    var Adults;
    var Children;
    var arrChildrenAges = new Array();

    document.getElementById(Prefix + "Rooms").value = arrRooms.length;
    BuildRoom(arrRooms.length, Prefix);

    for (var x = 0; x < arrRooms.length; x++) {
        arrRoomItems = arrRooms[x].split("$");
        Rooms = arrRoomItems[0].split(":")[1];
        Adults = arrRoomItems[1].split(":")[1];
        Children = arrRoomItems[2].split(":")[1];

        document.getElementById(Prefix + "AdultsRoom" + (x + 1)).value = Adults;
        document.getElementById(Prefix + "ChildrenRoom" + (x + 1)).value = Children;

        BuildChildAges('Room'+Rooms, Children, Prefix);
        arrChildrenAges = arrRoomItems[3].split(":")[1].split(",");
        if (Children > 0) {
            for (var y = 0; y < Children; y++) {
                document.getElementById(Prefix + "AgeRoom" + (x + 1) + "Child" + (y + 1)).value = arrChildrenAges[y];
            }
        }
    }

    SaveParty(Prefix);
}

function SearchExcursions(url) {
    var Adults = parseInt(document.getElementById("AdultsExcursion").value);
    var Children = parseInt(document.getElementById("ChildrenExcursion").value);
    var ChildrenAges = "";
    var PartyCode = "";

    for (x = 1; x <= Children; x++) {
        if (document.getElementById("AgeExcursionChild" + x).value != "-") {
            ChildrenAges += document.getElementById("AgeExcursionChild" + x).value;
            if (x != Children)
                ChildrenAges += ",";
        }
        else {
            alert("Please enter the ages of all children.");
            return false;
        }
    }

    PartyCode = "E:1$A:" + Adults + "$C:" + Children + "$CA:" + ChildrenAges;

    document.getElementById(strExcursionPartyID).value = PartyCode;
    document.aspnetForm.action = url;
    document.aspnetForm.submit();
}

function DecodeExcursionParty(Code) {
    var arrElements = Code.split("$");

    var Adults = arrElements[1].split(":")[1];
    var Children = arrElements[2].split(":")[1];
    var arrChildrenAges = arrElements[3].split(":")[1].split(",");

    document.getElementById("AdultsExcursion").value = Adults;
    document.getElementById("ChildrenExcursion").value = Children;

    BuildChildAges('Excursion', Children, '');

    if (Children > 0) {
        for (var x = 0; x < Children; x++) {
            document.getElementById("AgeExcursionChild" + (x+1)).value = arrChildrenAges[x];
        }
    }
}

function ClosePopup(id) {
    if (!isPopupClicked[id]) {
        popup[id].innerHTML = "<img src='/images/loader.gif' alt='loading...' title='loading...' />";
        popup[id].style.display = 'none';
    }
}
