function doSearch() {
    StripHTMLFromSearchText();

    var searchText = document.getElementById('ctl00_txtSearch').value;
    top.location = 'http://www.blueairweb.com/Cauta/' + searchText;
    return false;

}

function setSearchButtonFocus() {
    document.getElementById('ctl00_btnSearch').focus();
}

function GetEnter(e) {
    var keycode;

    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;

    if (keycode == 13) //enter
    {
        StripHTMLFromSearchText();
        doSearch();
        return false;
    }
}


function StripHTMLFromSearchText() {
    var txtSearch = document.getElementById('ctl00_txtSearch');
    txtSearch.value = txtSearch.value.stripHTML();

}

String.prototype.stripHTML = function() {
    // What a tag looks like
    var matchTag = /<(?:.|\s)*?>/g;
    // Replace the tag
    return this.replace(matchTag, "");

}


function changeLang(langCode) {
    //langCode = 'en-US';
    //return;
    if ((langCode == 'en-US') || (langCode == 'ro-RO') || (langCode == 'it-IT') || (langCode == 'es-ES'))
        window.location.href = location.pathname + "?culture=" + langCode;



}
function ShowFeesDetails(id) {
    id = 'divFeesDetails_' + id
    var el = document.getElementById(id);
    var disp;

    // workaround for IE (because IE does'nt know 'table-row')
    if (navigator.appName.indexOf("Internet Explorer") > 0) {
        disp = 'block';
    }
    else {
        disp = 'table-row';
    }

    disp = 'inline';
    if (el != null) {
        if (el.style.display == 'none') {
            el.style.display = disp;
        }
        else {
            el.style.display = 'none';
        }
    }
}

function SetSecondFlexible1(value, id) {
    var elementID = id.substr(0, id.length - 1);
    elementID = elementID + '2';
    var objTo = document.getElementById(elementID);
    for (var i = 0; i < objTo.options.length; i++) {
        if (objTo.options[i].value == value) {
            objTo.options[i].selected = true;
            break;
        }
    }
}

function SetSecondFlexible2(value, id) {
    var elementID = id.substr(0, id.length - 1);
    elementID = elementID + '2';
    var objTo = document.getElementById(elementID);

    for (var i = 0; i < objTo.options.length; i++) {
        if (objTo.options[i].value == value) {
            objTo.options[i].selected = true;
            break;
        }
    }
}

function SetTicketInsurance(value) {
    //    alert('test: '+ ssrControlName);
    var ckbSSRPurchase = document.getElementById(ssrControlName + '_SSRPurchase_PASSENGERCOUNT_0_SSRCOUNT_0');
    var ddlSSRCode = document.getElementById(ssrControlName + '_SSRCode_PASSENGERCOUNT_0_SSRCOUNT_0');

    ckbSSRPurchase.checked = value;
    if (ckbSSRPurchase.checked) {
        var blHasTKIN = false;

        for (var i = 0; i < ddlSSRCode.options.length; i++) {
            if (ddlSSRCode.options[i].value == 'TKIN') {
                ddlSSRCode.options[i].selected = true;
                blHasTKIN = true;
                break;
            }
        }

        if (!blHasTKIN) {
            ddlSSRCode.options.add(new Option('Ticket Insurance', 'TKIN'));
            ddlSSRCode.options[ddlSSRCode.options.length - 1].selected = true;
        }

        SetInsuranceQuantity();

        //SetSSRControlsDisabled(true);

    }
    else {
        //SetSSRControlsDisabled(false);
    }
}

function SetInsuranceQuantity() {
    var ckbSSRPurchase = document.getElementById(ssrControlName + '_SSRPurchase_PASSENGERCOUNT_0_SSRCOUNT_0');
    if (ckbSSRPurchase.checked) {
        var dropDownNames = applicationPassengerArrayValues.split(",");
        var ds = document['SkySales'];
        var paxDropdownRendered = false;

        if (document.getElementById && document.createTextNode) {
            var paxCount = 0;
            var undefined;

            for (var i = 0; i < dropDownNames.length; i++) {
                if (ds[dropDownNames[i]] != undefined) {
                    paxDropdownRendered = true;
                    paxCount = paxCount + parseInt(ds[dropDownNames[i]].value);
                }
            }
            var intPassangerNumber = paxCount;

            var ddlSSRQuantity = document.getElementById(ssrControlName + '_SSRQuantity_PASSENGERCOUNT_0_SSRCOUNT_0');
            var blHasThisQuantity = false;

            for (var i = 0; i < ddlSSRQuantity.options.length; i++) {
                if (ddlSSRQuantity.options[i].value == intPassangerNumber) {
                    ddlSSRQuantity.options[i].selected = true;
                    blHasThisQuantity = true;
                    break;
                }
            }

            if (!blHasThisQuantity) {
                ddlSSRQuantity.options.add(new Option(intPassangerNumber, intPassangerNumber));
                ddlSSRQuantity.options[ddlSSRQuantity.options.length - 1].selected = true;
                //ddlSSRQuantity.disabled = true;
            }
        }
    }

}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload =
            function() {
                if (oldonload) {
                    oldonload();
                }
                func();
            }
    }
}
function AvailabilitySearchValues_Validate() {
    return true;
}
function AvailabilitySearchInputOnPageLoad() {
    SetMarketDateRange('1|1', applicationJavaScriptHtmlId);
    ProcessQueryString(applicationJavaScriptHtmlId);
    CorectAdult(applicationJavaScriptHtmlId);
}
function AvailabilitySearchInput1OnPageLoad() {
    SetMarketDateRange('1|1', qbJavaScriptHtmlId);
    ProcessQueryString(qbJavaScriptHtmlId);
    CorectAdult(applicationJavaScriptHtmlId);
}

function AvailabilitySearchInput3OnPageLoad() {
    SetMarketDateRange('1|1');
    //CorectAdult();
}

function SetMarketDateRange(mdrValue, hmlID) {
    var ddlMarketDateRange1 = document.getElementById(hmlID + '_DropDownListMarketDateRange1');
    var ddlMarketDateRange2 = document.getElementById(hmlID + '_DropDownListMarketDateRange2');

    for (var i = 0; i < ddlMarketDateRange1.options.length; i++) {
        if (ddlMarketDateRange1.options[i].value == mdrValue) {
            ddlMarketDateRange1.options[i].selected = true;
            break;
        }
    }

    for (var i = 0; i < ddlMarketDateRange2.options.length; i++) {
        if (ddlMarketDateRange2.options[i].value == mdrValue) {
            ddlMarketDateRange2.options[i].selected = true;
            break;
        }
    }

    ddlMarketDateRange2.style.display = "inline";
}

function SetSearchBy() {

    var ddlDropDownListSearchBy = document.getElementById(applicationJavaScriptHtmlId + '_DropDownListSearchBy');
    ddlDropDownListSearchBy.style.display = 'none'
    ddlDropDownListSearchBy.options[0].selected = true;

}
function CorectAdult(controlPrefix) {

    var ddlAdult = document.getElementById(controlPrefix + '_DropDownListPassengerType_ADT');
    ddlAdult.options[0].value = "";
}


function SSRPassengerInputOnPageLoad() {

}

function searchNextPreviousDay(mktIx, way) {
    try {
        //alert("applicationJavaScriptHtmlId " + applicationJavaScriptHtmlId);
        // get selected date
        var ds = document['SkySales'];
        var oneday = 86400000;
        var mktDay = ds[applicationJavaScriptHtmlId + '_DropDownListMarketDay' + mktIx].options[ds[applicationJavaScriptHtmlId + '_DropDownListMarketDay' + mktIx].selectedIndex].value;
        var mktMonth = ds[applicationJavaScriptHtmlId + '_DropDownListMarketMonth' + mktIx].options[ds[applicationJavaScriptHtmlId + '_DropDownListMarketMonth' + mktIx].selectedIndex].value.substring(5, 7);
        var mktYear = ds[applicationJavaScriptHtmlId + '_DropDownListMarketMonth' + mktIx].options[ds[applicationJavaScriptHtmlId + '_DropDownListMarketMonth' + mktIx].selectedIndex].value.substring(0, 4);
        var msec = Date.UTC(mktYear, mktMonth - 1, mktDay);

        var newmsec = msec;

        // add one day (86400000 ms = 1 day)
        if (way == 0) {
            newmsec -= oneday;
        }
        else {
            newmsec += oneday;
        }

        // create new date
        var newDate = new Date(newmsec);

        // search index for day
        for (var i = 0; i < ds[applicationJavaScriptHtmlId + '_DropDownListMarketDay' + mktIx].options.length; i++) {
            var dval = parseInt(ds[applicationJavaScriptHtmlId + '_DropDownListMarketDay' + mktIx].options[i].value, 10);

            // set new date (day)
            if (dval == newDate.getDate()) {
                ds[applicationJavaScriptHtmlId + '_DropDownListMarketDay' + mktIx].options[i].selected = true;
                break;
            }
        }

        // search index for month and year
        for (var i = 0; i < ds[applicationJavaScriptHtmlId + '_DropDownListMarketMonth' + mktIx].options.length; i++) {
            var mval = ds[applicationJavaScriptHtmlId + '_DropDownListMarketMonth' + mktIx].options[i].value;
            var morg = newDate.getFullYear() + "-";
            var mm = newDate.getMonth() + 1;

            if (mm < 10) {
                morg += "0" + mm;
            }
            else {
                morg += mm;
            }

            // set new date (month and year)
            if (mval == morg) {
                ds[applicationJavaScriptHtmlId + '_DropDownListMarketMonth' + mktIx].options[i].selected = true;
                break;
            }
        }

        // send request
        //javascript: __doPostBack('AvailabilitySearchNextPreviousDayView$LinkButtonNewSearch', '')
        javascript: __doPostBack('QuickBookingSelectView$AvailabilitySearchInputSelectView$LinkButtonNewSearch', '')
        //CompactSearchValidation(true);
    }
    catch (e) {
        alert(e);
        // do nothing
    }
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
    return '';
}

function ProcessQueryString(hmlID) {
    var fromID = querySt("fromID");
    var toID = querySt("toID");

    if (fromID != '' && toID != '') {
        flights(fromID, toID);
    }
}

function flights(fromID, toID) {
    var objFrom = document.getElementById(hmlID + 'originStation1'); //fromAirport
    var objTo = document.getElementById(hmlID + 'destinationStation1'); //toAirport
    var objFrom1 = document.getElementById(hmlID + '_TextBoxMarketOrigin1'); //fromAirport
    var objTo1 = document.getElementById(hmlID + '_TextBoxMarketDestination1'); //toAirport

    objFrom1.value = fromID;
    objTo1.value = toID;
    for (var i = 1; i < objFrom.options.length; i++) {
        if (objFrom.options[i].value == fromID) {
            objFrom.options[i].selected = true;
            break;
        }
    }

    //OriginMac(objFrom);
    //setOptions();

    for (var i = 1; i < objTo.options.length; i++) {
        if (objTo.options[i].value == toID) {
            objTo.options[i].selected = true;
            break;
        }
    }
    //OriginMac(objTo);

}

function checkBookingStartDate() {

    var currentDate = new Date();
    var date_picker_id_1 = document.getElementById("date_picker_id_1").value.split('/');
    var bookingDate = new Date(date_picker_id_1[2], date_picker_id_1[0] - 1, date_picker_id_1[1]);

    var checkBobx = document.getElementById("ControlGroupSearchChangeView_AvailabilitySearchInputSearchChangeView_CheckBoxChangeMarket_1");
    if (bookingDate < currentDate)
        checkBobx.disabled = true;
    else
        checkBobx.disabled = false;

}
function opc_validate_contact() {
    var isOk = true;

    var email1 = document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxEmailAddress");
    var email2 = document.getElementById("TextBoxEmailAddress_second");

    if ((email1.value != email2.value) || (email1.value == '') || (email2.value == '')) {
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxEmailAddressLabel1").style.color = 'red';
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxEmailAddressLabel1").style.fontWeight = 'bold';
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxEmailAddressLabel2").style.color = 'red';
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxEmailAddressLabel2").style.fontWeight = 'bold';

        email1.focus();
        isOk = false;
    }
    else {
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxEmailAddressLabel1").style.color = '';
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxEmailAddressLabel1").style.fontWeight = 'normal';
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxEmailAddressLabel2").style.color = '';
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxEmailAddressLabel2").style.fontWeight = 'normal';
    }

    var cellPhone1 = document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhone");
    var cellPhone2 = document.getElementById("TextBoxOtherPhone_Second");
    if ((cellPhone1.value != cellPhone2.value) || (cellPhone1.value == '') || (cellPhone2.value == '')) {
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel1").style.color = 'red';
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel1").style.fontWeight = 'bold';
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel2").style.color = 'red';
        document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel2").style.fontWeight = 'bold';

        cellPhone1.focus();
        isOk = false;
    }
    else {
        var numericExpression = /^\+\([0-9]+\)[0-9]+$/; //        /^[0-9]+$/
        if (!cellPhone1.value.match(numericExpression)) {
            document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel1").style.color = 'red';
            document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel1").style.fontWeight = 'bold';

            cellPhone1.focus();
            isOk = false;
        }
        else {
            if (!IsPrefixOK()) {
                document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel1").style.color = 'red';
                document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel1").style.fontWeight = 'bold';

                cellPhone1.focus();
                isOk = false;
            }
            else {
                document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel1").style.color = '';
                document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel1").style.fontWeight = 'normal';
            }
        }

        if (!cellPhone2.value.match(numericExpression)) {
            document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel2").style.color = 'red';
            document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel2").style.fontWeight = 'bold';

            cellPhone1.focus();
            isOk = false;
        }
        else {
            document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel2").style.color = '';
            document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhoneLabel2").style.fontWeight = 'normal';
        }
    }

    return isOk;
}

function showAdditionalDocuments(checkBoxID, divID) {
    document.getElementById(checkBoxID).checked = true;
    document.getElementById(checkBoxID).disabled = true;

    document.getElementById(divID).style.display = 'block';

}

function UpdatePhonePrefix(controlID) {

    var dropdownIndex = document.getElementById(controlID).selectedIndex;
    var dropdownValue = document.getElementById(controlID)[dropdownIndex].value;

    var cellPhone1 = document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhone");
    var cellPhone2 = document.getElementById("TextBoxOtherPhone_Second");
    cellPhone1.value = hashCountry[dropdownValue];
    cellPhone2.value = hashCountry[dropdownValue];

}

String.prototype.startsWith = function(str) {
    return (this.substr(0, str.length) == str)
}

String.prototype.trim = function() {
    return
    (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))
}
String.prototype.endsWith = function(str) {
    return (this.substr((this.length - str.length), str.length) == str)
}

function IsPrefixOK() {
    controlID = contactinput_JavaScriptHtmlId + '_DropDownListCountry';
    var dropdownIndex = document.getElementById(controlID).selectedIndex;
    var dropdownValue = document.getElementById(controlID)[dropdownIndex].value;
    var cellPhone1Value = '' + document.getElementById(contactinput_JavaScriptHtmlId + "_TextBoxOtherPhone").value;

    if (cellPhone1Value.startsWith('' + hashCountry[dropdownValue])) return true;
    else return false;
}

function PaymentInputOnPageLoad() {
    SetCreditCard('ExternalAccount:VI');
}

function SetCreditCard(typeCreditCard) {
    controlID = PaymentInput_JavaScriptHtmlId + '_DropDownListPaymentMethodCode';
    var dropdownIndex = document.getElementById(controlID).selectedIndex;
    var dropdownValue = document.getElementById(controlID)[dropdownIndex].value;

    if (dropdownValue != typeCreditCard) {
        var ddlPaymentMethodCode = document.getElementById(controlID);
        for (var i = 1; i < ddlPaymentMethodCode.options.length; i++) {
            if (ddlPaymentMethodCode.options[i].value == typeCreditCard) {
                ddlPaymentMethodCode.options[i].selected = true;
                var ddlPaymentMethodCode = document.getElementById(controlID);
                ddlPaymentMethodCode.style.display = 'none';
                setTimeout('__doPostBack(\'' + controlID.replace(/_/gi, "$") + '\',\'\')', 0)
                break;
            }
        }
    }
    else {
        var ddlPaymentMethodCode = document.getElementById(controlID);
        ddlPaymentMethodCode.style.display = 'none';
    }

}

////////////////////////////////////
//  SSRMarket
/////////////////////////////
function SSRMarcketOnPageLoad() {
    if (document.getElementById('ControlGroupSelectView_SsrMarketInputSelectView_CheckBoxSSRs').checked == false) {
        document.getElementById('ControlGroupSelectView_SsrMarketInputSelectView_CheckBoxSSRs').click();
    }
    document.getElementById('ControlGroupSelectView_SsrMarketInputSelectView_CheckBoxSSRs').style.display = "none";

    setBagCount(document.getElementById('ControlGroupSelectView_SsrMarketInputSelectView_TextBoxSsrMkt1SSR2'));

    if (opc_SsrRequestsExists()) {
        ssrStatus = 'saved';

    }
    else {
        ssrStatus = 'not_present';
        //        document.getElementById('btnSaveSSR').style.display = "";
        //        document.getElementById('btnNext').style.display = "none";
        //
    }
    document.getElementById('btnSaveSSR').style.display = "none";
    document.getElementById('btnNext').style.display = "";

}

var bagCount = 0;
function setBagCount(textBox) {
    checkInt(textBox);
    bagCount = textBox.value;
    if (bagCount > 0) {
        document.getElementById('bagCheckBoxLabel').style.color = '';
        document.getElementById('bagCheckBoxLabel').style.display = "none";
        document.getElementById('bagCheckBox').style.display = "none";
    }
    else {
        document.getElementById('bagCheckBoxLabel').style.display = "inline";
        document.getElementById('bagCheckBox').style.display = "inline";
    }
}

function opc_SsrRequestsExists() {
    var undefined;
    var ds = document['SkySales'];
    var prefix = 'ControlGroupSelectView_SsrMarketInputSelectView_TextBoxSsrMkt';

    for (var i = 1; i <= mktCount; i++) {
        for (var j = 1; j <= ssrCount; j++) {
            if ((document.getElementById(prefix + i + 'SSR' + j) != undefined) && (document.getElementById(prefix + i + 'SSR' + j).value > 0)) {
                return true;
            }
        }
    }
    return false;
}

function opc_validate_bag() {
    if (bagCount == 0) {
        var ckb = document.getElementById('bagCheckBox');
        if (!ckb.checked) {
            document.getElementById('bagCheckBoxLabel').style.color = 'red';
            alert(opc_validate_bag_error_msg);
            ckb.focus();
            return false;
        }
    }
    else {
        document.getElementById('bagCheckBoxLabel').style.color = '';
    }

    if (!IsSsrStatusOK()) return false;

    return true;
}

function IsSsrStatusOK() {
    if (ssrStatus == 'added') {
        alert(ssr_not_saved_err_msg);
        return false;
    }
    else
        return true;
}

function RemoveTargetTop() {
    document.getElementById('SkySales').target = '';
}

function startChromeMenu() {
    cssdropdown.startchrome("chromemenu");

}

function timeoutCheck() {
    elapsedTime = elapsedTime + 30;
    if (elapsedTime > maxTime) {
        //alert('Your session is expired.');
        alert(alertText);
        parent.location.href = "search.aspx";
    }
    else {
        oneSecond = window.setTimeout("timeoutCheck();", 30000);
    }
}