var timer;
try {
    $(".collapseExpand").hover(
  function () {
      $(this).removeClass('collapseText').addClass('expandText');
  },
  function () {
      $(this).removeClass('expandText').addClass('collapseText');
  });
} catch (err) { } 

window.focus();
window.status = "Mayfair travel";

if (window.name == "") {
    window.name = "Corporate";
}

var Explorer = (document.all);

function WindowClicked(e) {
    var sender = (e && e.target) || (window.event && window.event.srcElement);
    if (sender.className != "dtSel") {
        if (document.getElementById('divDepartureTime'))
            document.getElementById('divDepartureTime').style.visibility = 'hidden';
        if (document.getElementById('divReturnTime'))
            document.getElementById('divReturnTime').style.visibility = 'hidden';
        if (document.getElementById('divHotelRating'))
            document.getElementById('divHotelRating').style.visibility = 'hidden';
        if (document.getElementById('divHotelClassification'))
            document.getElementById('divHotelClassification').style.visibility = 'hidden';
        if (document.getElementById('divMenu'))
            document.getElementById('divMenu').style.visibility = 'hidden';
    }
}

window.document.onclick = WindowClicked;

function OpenPopup(popurl, target, width, height, resizable) {
    var vAttributes = "";
    if (width != "") { vAttributes += "width=" + width + ", " }
    if (height != "") { vAttributes += "height=" + height + ", " }
    if (resizable == "true") { vAttributes += "status=yes,resizable=yes,scrollbars=yes,location=yes,menubar=yes,toolbar=yes" }
    else if (resizable == "popup") { vAttributes += "status=yes,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no" }
    else { vAttributes += "status=no,resizable=no,scrollbars=no,location=no,menubar=no,toolbar=no" }

    var winpops = window.open(popurl, target, vAttributes);
}

function ToggleDiv(divid) {
    var div = document.getElementById(divid);

    if (div.style.display == 'none') {
        div.style.filter = "alpha(opacity=5)";
        div.style.display = "block";
        div.style.zIndex = 1000;

        if (Explorer)
            fadeIn(divid, 10);
        return true;
    }
    else {
        if (Explorer)
            fadeOut(divid, 100);
        else
            div.style.display = "none";
        return false;
    }
}

/*************** MENU ************************/
function ToggleTopMenu(link, divid) {
    var div = document.getElementById(divid);

    if (div.style.display == 'none' || div.style.display == "")
        div.style.display = "block";

    link.onmouseout = function () {
        t = setTimeout("closeMenu('" + divid + "')", 100);
    }

    div.onmouseover = function () {
        clearTimeout(t);
    }

    div.onmouseout = function (e) {
        if (Explorer) {
            if (!event.fromElement.contains(event.toElement) && !div.contains(event.toElement))
                t = setTimeout("closeMenu('" + divid + "')", 100);
        }
        else {
            var targetAncestor = e.relatedTarget;
            while (targetAncestor.parentNode) {
                if (targetAncestor === e.currentTarget) {
                    return; // ITS MOVING OUT OF US, BUT INTO OUR PROGENY, SO NOT REALLY OUT!
                }
                targetAncestor = targetAncestor.parentNode;
            }

            t = setTimeout("closeMenu('" + divid + "')", 200);
        }
    }
}

function closeMenu(divid) {
    var div = document.getElementById(divid);
    div.style.display = "none";
}
/*************** MENU END ********************/

/*************** BOOKING BOX *****************/
function ShowRooms(ddl) {
    divId = "divRoom";

    var maxRooms = 3;
    for (var i = 2; i <= maxRooms; i++) {
        if (i <= ddl.value) document.getElementById(divId + i).style.display = "block";
        else document.getElementById(divId + i).style.display = "none";
    }
}

function ShowChildren(ddl, item) {
    divId = "divChild";
    if (item == 1) divId += "1";
    if (item == 2) divId += "2";
    if (item == 3) divId += "3";

    var maxChildren = 3;
    for (var i = 1; i <= maxChildren; i++) {
        if (i <= ddl.value) document.getElementById(divId + i).style.display = "block";
        else document.getElementById(divId + i).style.display = "none";
    }
}

function ToggleTime(divid) {
    var div = document.getElementById(divid);

    if (div.style.visibility == 'hidden' || div.style.visibility == '') {
        div.style.visibility = 'visible';
    }
    else {
        div.style.visibility = 'hidden'
    }
}

function SelectFromList(link) {
    val = link.innerHTML;
    id = link.parentNode.id.replace("div", "");
    document.getElementById("href" + id).innerHTML = val;
    document.getElementById("hdn" + id).value = val;
}

function BBHotelFlightShowSubmit(frmIsValid) {
    if (frmIsValid) {
        document.getElementById('divSelectTrip').style.display = 'block';
        document.getElementById('divSelectTripDis').style.display = 'none';
        document.getElementById('divSelectTripWorking').style.display = 'none';
    }
    else {
        document.getElementById('divSelectTrip').style.display = 'none';
        document.getElementById('divSelectTripDis').style.display = 'block';
        document.getElementById('divSelectTripWorking').style.display = 'none';
    }
}

function BBHotelFlightShowWorking(mode) {
    if (mode == 1) {
        document.getElementById('divSelectTrip').style.display = 'none';
        document.getElementById('divSelectTripDis').style.display = 'none';
        //        document.getElementById('divSelectTripWorking').style.display = 'block';
        WorkingBg(mode)
    }
    else {
        document.getElementById('divSelectTrip').style.display = 'block';
        document.getElementById('divSelectTripDis').style.display = 'none';
        //        document.getElementById('divSelectTripWorking').style.display = 'none';
        WorkingBg(mode)
    }
}

function WorkingBg(mode) {
    if (mode == 1) {
        document.getElementById('waitBg').style.display = 'block';
       timer = setTimeout("WorkingBg(0);alert('Plase try again');", 16000);
    }
    else {
        clearTimeout(timer);
        document.getElementById('waitBg').style.display = 'none';
    }
    
}

function SetbookingBox() {
    if (document.getElementById("txtDepartuteDate").value == "") SetTxtDate("", "txtDepartuteDate", 1, 0);
    document.getElementById('iFrameToggleDepartuteDate').contentWindow.ResetCalendar();

    if (document.getElementById("txtReturnDate").value == "") SetTxtDate("txtDepartuteDate", "txtReturnDate", 0, 7);
    document.getElementById('iFrameToggleReturnDate').contentWindow.ResetCalendar();
}

function SetTxtDate(compareDate, objtxt, monthsToAdd, daysToAdd) {
    var vTodayDate = new Date();
    if (compareDate != "")
        vTodayDate = new Date(document.getElementById(compareDate).value.split('/')[2], parseInt(document.getElementById(compareDate).value.split('/')[1]) - 1, parseInt(document.getElementById(compareDate).value.split('/')[0]), 0, 0, 0);
    var vDateToShow = new Date((vTodayDate.getYear() < 1000 ? (parseInt(vTodayDate.getYear()) + 1900) : vTodayDate.getYear()), (parseInt(vTodayDate.getMonth()) + monthsToAdd), (parseInt(vTodayDate.getDate()) + daysToAdd), 0, 0, 0);
    var vTxtValue = vDateToShow.getDate() + "/" + (parseInt(vDateToShow.getMonth()) + 1) + "/" + (vDateToShow.getYear() < 1000 ? (parseInt(vDateToShow.getYear()) + 1900) : vDateToShow.getYear());

    document.getElementById(objtxt).value = vTxtValue;
}

function CompareDate(dtFrom, dtTo) {
    if (!document.getElementById(dtFrom)) return;
    if (!document.getElementById(dtTo)) return;

    dtFromValue = document.getElementById(dtFrom).value;
    vFromDate = new Date(dtFromValue.split('/')[2], parseInt(dtFromValue.split('/')[1]) - 1, parseInt(dtFromValue.split('/')[0]), 0, 0, 0);

    dtToValue = document.getElementById(dtTo).value;
    vToDate = new Date(dtToValue.split('/')[2], parseInt(dtToValue.split('/')[1]) - 1, parseInt(dtToValue.split('/')[0]), 0, 0, 0);

    if (vToDate < vFromDate) {
        SetTxtDate(dtFrom, dtTo, 0, 7);
        if (document.getElementById('iFrameToggle' + dtTo.replace("txt", "")))
            document.getElementById('iFrameToggle' + dtTo.replace("txt", "")).contentWindow.ResetCalendar();
    }
}
/*************** BOOKING BOX END *************/

/*************** CALENDAR ********************/
function CloseMenus(divToExclude) {
    obj = window.parent.document.getElementsByTagName("iframe");
    for (var i = 0; i < obj.length; i++) {
        if (obj[i].id.indexOf("iFrameToggle") >= 0) {
            if (obj[i].id != divToExclude) {
                if (obj[i] != null && obj[i].style.display == "block") {
                    obj[i].style.display = "none";
                }
            }
        }
    }
}

function ShowCalendar(iFrameId) {
    ToggleDiv(iFrameId);
}

function fadeIn(divid, opacity) {
    try {
        if (opacity < 100) {
            div = document.getElementById(divid);
            div.style.filter = "alpha(opacity=" + opacity + ")";
            var tempopacity = opacity + 7
            var tempFunct = "fadeIn('" + divid + "'," + tempopacity + ");";
            setTimeout(tempFunct, 5);
        }
    }
    catch (e) {
    }
}

function fadeOut(divid, opacity) {
    try {
        if (opacity > 10) {
            div = document.getElementById(divid);
            div.style.filter = "alpha(opacity=" + opacity + ")";
            var tempopacity = opacity - 10
            var tempFunct = "fadeOut('" + divid + "'," + tempopacity + ");";
            setTimeout(tempFunct, 5);
        }
        else
            div.style.display = 'none';
    }
    catch (e) {
        div.style.display = 'none';
    }
}
/*************** CALENDAR END ****************/

/*************** FLASH ***********************/
function ConvertAllFlash() {
    if (isIE()) {
        E = document.getElementsByTagName("object");
        for (i = 0; i <= E.length - 1; i++) {
            E[i].outerHTML = E[i].outerHTML;
            E[i].play();
        }
    }
}

function isIE() {
    var strBrwsr = navigator.userAgent.toLowerCase();
    if (strBrwsr.indexOf("msie") > -1 && strBrwsr.indexOf("mac") < 0)
        return true;
    else
        return false;
}

setTimeout("ConvertAllFlash();", 1);

window.onunload = function () {
    if (isIE()) {
        E = document.getElementsByTagName("object");
        for (i = 0; i <= E.length - 1; i++) {
            E[i].outerHTML = "";
        }
    }
}
/*************** FLASH END *******************/

/*************** AUTOCOMPLETE ****************/
function StartAutoComplete(lan, fillDepartureDates, showAirports, isFixed) {
    $(function () {
        $("#txtDestination").autocomplete("Ajax.aspx?act=arriving&lanId=" + lan, {
            minChars: 2,
            max: 30,
            autoFill: false,
            matchContains: true,
            scrollHeight: 182,
            width: 200,
            keypress: false
        }).bind('focus', function () {
            $.get("Ajax.aspx?act=airports", {
                destination: "" + $('#txtDestination').val() + "",
                lanId: lan,
                showAirportsList: showAirports
            },
            function (data) {
                $('#divAirports').html(data);
                if (fillDepartureDates && document.getElementById("divAirports").innerHTML != "") {
                    if (document.getElementById("ddlDestinationAirport"))
                        document.getElementById("ddlDestinationAirport").onchange = "AsyncCallFill('departureDates', 'divDepartureDate', '&lanId=' + lan + '&ifx=' + isFixed, true);";
                    AsyncCallFill("departureDates", "divDepartureDate", '&lanId=' + lan + '&ifx=' + isFixed, true);
                }
            });
        }).bind('keydown', function (e) {
            var intKey = (window.Event) ? e.which : e.keyCode;            
            if (intKey == '13') {
                $.get("Ajax.aspx?act=airports", {
                    destination: "" + $('#txtDestination').val() + "",
                    lanId: lan,
                    showAirportsList: showAirports
                },
            function (data) {
                $('#divAirports').html(data);
                if (fillDepartureDates && document.getElementById("divAirports").innerHTML != "") {
                    if (document.getElementById("ddlDestinationAirport"))
                        document.getElementById("ddlDestinationAirport").onchange = "AsyncCallFill('departureDates', 'divDepartureDate', '&lanId=' + lan + '&ifx=' + isFixed, true);";
                    AsyncCallFill("departureDates", "divDepartureDate", '&lanId=' + lan + '&ifx=' + isFixed, true);
                }
            });
            }
        }).bind('blur', function () {
            if (document.getElementById("txtDestination").value == "") {
                $.get("Ajax.aspx?act=airports", {
                    destination: "" + $('#txtDestination').val() + "",
                    lanId: lan,
                    showAirportsList: showAirports
                },
                function (data) {
                    $('#divAirports').html(data);
                    if (fillDepartureDates && document.getElementById("divAirports").innerHTML != "") {
                        if (document.getElementById("ddlDestinationAirport"))
                            document.getElementById("ddlDestinationAirport").onchange = "AsyncCallFill('departureDates', 'divDepartureDate', '&lanId=' + lan + '&ifx=' + isFixed, true);";
                        AsyncCallFill("departureDates", "divDepartureDate", '&lanId=' + lan + '&ifx=' + isFixed, true);
                    }
                });
            }
        });
        $(".ac_over").select(function () {
        });


    });
}
/*************** AUTOCOMPLETE END ************/

/*************** AJAX ************************/
function AsyncCallExec(action, attributes, passForm) {
    $.ajax({
        url: "Ajax.aspx?act=" + action + attributes + (passForm ? GatherFormInputs() : ''),
        type: "GET",
        cache: false,
        timeout: (15000),
        success: function (data) {
            eval(data);
        }
    });
    //    $.get("Ajax.aspx?act=" + action + attributes + (passForm ? GatherFormInputs() : ''),
    //        function (data) {
    //            eval(data);
    //        });
}

function AsyncCallFill(action, divId, attributes, passForm) {
    $.ajax({
        url: "Ajax.aspx?act=" + action + attributes + (passForm ? GatherFormInputs() : ''),
        type: "GET",
        cache: false,
        timeout: (15000),
        success: function (data) {
            $('#' + divId).html(data);
        }
    });
    //    $.get("Ajax.aspx?act=" + action + attributes + (passForm ? GatherFormInputs() : ''),
    //        function (data) {
    //            $('#' + divId).html(data);
    //        });
}

function GatherFormInputs() {
    var fields = new Array();

    //loop through form elements and retrieve field NAMEs and Values
    for (var x = 1; x < eval("document.forms[0].elements.length"); x++) {
        // join them into a string.
        if (document.forms[0].elements[x].id == "__VIEWSTATE") {
            //avoid
        }
        else if (document.forms[0].elements[x].type == "select-multiple") {
            var tempSelectedValues = "";
            for (var k = 0; k <= document.forms[0].elements[x].options.length - 1; k++) {
                if (document.forms[0].elements[x].options[k].selected)
                    tempSelectedValues += document.forms[0].elements[x].options[k].value + ",";
            }
            eval("fields.push(document.forms[0].elements[x].id+'='+tempSelectedValues)");
        }
        else if (document.forms[0].elements[x].type == "checkbox") {
            if (document.forms[0].elements[x].checked)
                eval("fields.push(document.forms[0].elements[x].id+'=1')");
            else
                eval("fields.push(document.forms[0].elements[x].id+'=0')");
        }
        else if (document.forms[0].elements[x].type == "radio") {
            if (document.forms[0].elements[x].checked)
                eval("fields.push(document.forms[0].elements[x].id+'=1')");
            else
                eval("fields.push(document.forms[0].elements[x].id+'=0')");
        }
        else {
            if (document.forms[0].elements[x].id.indexOf("txtDepartuteDate") >= 0 && document.forms[0].elements[x].id != "txtDepartuteDate") {
                //skip txtDepartuteDate for list of Hotels by Country and Destination
                var elValue = "";
            }
            else if (document.forms[0].elements[x].id.indexOf("txtReturnDate") >= 0 && document.forms[0].elements[x].id != "txtReturnDate") {
                //skip txtReturnDate for list of Hotels by Country and Destination
                var elValue = "";
            }
            else
            if (document.forms[0].elements[x].type != "button") {
                var elValue = "";
                if (document.forms[0].elements[x].value != null) {
                    elValue = escape(document.forms[0].elements[x].value);
                    //elValue = document.forms[0].elements[x].value;
                    //elValue = replaceAll(elValue, '&', '%26');
                    //elValue = replaceAll(elValue, '+', '%2b');
                }
                eval("fields.push(document.forms[0].elements[x].id + '=' + elValue )");
            }
        }
    }

    return "&" + fields.join('&');
}

function replaceAll(str, from, to) {
    var idx = str.indexOf(from);

    while (idx > -1) {
        str = str.replace(from, to);
        idx = str.indexOf(from);
    }

    return str;
}
/*************** AJAX END ********************/

var bskDirection = "left";
var step = 40, time = 10;
var sliding = false;
function slideBasketInit(mode) {
    window.focus();
    if (sliding) return;
    var divBtnImg = document.getElementById("basketPreviewImg");
    if (divBtnImg.style.right == "")
        divBtnImg.style.right = "-110px";
    var divBasket = document.getElementById("basketPreview");
    if (divBasket.style.right == "")
        divBasket.style.right = "-201px";

    if (bskDirection == "left")
        setTimeout("slideBasketLeft();", time);
    else if (bskDirection == "right" && mode == "2")
        setTimeout("slideBasketRight();", time);
}

function slideBasketLeft() {
    sliding = true;
    var divBtnImg = document.getElementById("basketPreviewImg");
    var divBasket = document.getElementById("basketPreview");

    divBtnImg.style.right = (parseInt(divBtnImg.style.right.replace("px", "")) + step) + "px";
    divBasket.style.right = (parseInt(divBasket.style.right.replace("px", "")) + step) + "px";

    if (divBtnImg.style.right.replace("px", "") < 0 || divBasket.style.right.replace("px", "") < 0) {
        if (divBtnImg.style.right.replace("px", "") >= 0)
            divBtnImg.style.right = "0";
        setTimeout("slideBasketLeft();", time);
    }
    else {
        divBtnImg.style.right = "0";
        divBasket.style.right = "0";
        bskDirection = "right";
        sliding = false;
    }
}

function slideBasketRight() {
    sliding = true;
    var divBtnImg = document.getElementById("basketPreviewImg");
    var divBasket = document.getElementById("basketPreview");

    divBtnImg.style.right = (parseInt(divBtnImg.style.right.replace("px", "")) - step) + "px";
    divBasket.style.right = (parseInt(divBasket.style.right.replace("px", "")) - step) + "px";

    if (divBtnImg.style.right.replace("px", "") > -110 || divBasket.style.right.replace("px", "") > -201) {
        if (divBtnImg.style.right.replace("px", "") <= -110)
            divBtnImg.style.right = "-110px";
        setTimeout("slideBasketRight();", time);
    }
    else {
        divBtnImg.style.right = "-110px";
        divBasket.style.right = "-201px";
        bskDirection = "left";
        sliding = false;
    }
}
