Cathay Award Search Fixer 2022

Un-Elevate Your Cathay Award Search 2022

As of 01/09/2022. See the latest version.

// ==UserScript==
// @name                Cathay Award Search Fixer 2022
// @name:zh-TW          國泰獎勵機票搜尋引擎修復神器 2022
// @namespace           jayliutw
// @version             2.3.2
// @description         Un-Elevate Your Cathay Award Search 2022
// @description:zh-TW   國泰航空 2022 里程獎勵機票搜尋引擎「反升級」套件
// @author              jayliutw
// @require             https://greasyfork.org/scripts/431490-greasyforkscriptupdate/code/GreasyForkScriptUpdate.js?version=1080054
// @connect             greasyfork.org
// @match               https://*.cathaypacific.com/cx/*/book-a-trip/redeem-flights/redeem-flight-awards.html*
// @match               https://book.cathaypacific.com/*
// @grant               GM_setValue
// @grant               GM_getValue
// @grant               GM_xmlhttpRequest
// @grant               unsafeWindow
// @license             GPL
// ==/UserScript==

(function() {
    'use strict';

    function addCss(cssString) {
        var head = document.getElementsByTagName('head')[0];
        var newCss = document.createElement('style');
        newCss.type = "text/css";
        newCss.innerHTML = cssString;
        head.appendChild(newCss);
    }
//GM_setValue("last_update", "2022/08/08");
function show_update(version) {
    document.querySelector(".unelevated_update").classList.add("update_show");
    document.querySelector("#upd_version").innerText = version;
    console.log(version)
}

    var uef_from = GM_getValue("uef_from", "HKG") || localStorage.getItem("uef_from") || "HKG";
    var uef_to = GM_getValue("uef_to", "TYO") || localStorage.getItem("uef_to") || "TYO";
    var uef_date = GM_getValue("uef_date", "20230801") || localStorage.getItem("uef_date") || "20230801";
    var uef_adult = GM_getValue("uef_adult", "1") || localStorage.getItem("uef_adult") || "1";
    var uef_child = GM_getValue("uef_child", "0") || localStorage.getItem("uef_child") || "0";

    var route_changed = false;

    addCss (
        '.unelevated_form { position:relative;transition: margin-left 0.7s ease-out;z-index: 10; font-family: "GT Walsheim","Cathay Sans EN", CathaySans_Rg, sans-serif; border: 1px solid #bcbec0; margin:10px 0; background: #f7f6f0; padding: 8px 0px 8px 8px; border-top: 5px solid #367778; box-shadow: 0px 0px 7px rgb(0 0 0 / 20%);} ' +
        '.unelevated_form.uef_collapsed { margin-left:-90%;} ' +
        '.unelevated_title {font-weight: 400; font-size: 17px; font-family: "GT Walsheim","Cathay Sans EN", CathaySans_Lt, sans-serif; color: #2d2d2d; margin: 5px;} '+
        '.unelevated_form label { display: inline-block; position: relative; width:50%; padding: 0px 8px 0px 0px; } ' +
        '.unelevated_form label span { position: absolute; top: 0px; left: 5px; color: #66686a; font-family: Cathay Sans EN, CathaySans_Rg, sans-serif; line-height: 25px; font-size: 10px;} ' +
        '.unelevated_form input { font-family: Cathay Sans EN, CathaySans_Lt, sans-serif; padding: 19px 5px 5px 5px; border-radius: 0px; border: 1px solid #bcbec0; display: inline-block; margin: 0px 8px 8px 0px; height: 45px; width: 100%; font-size:16px} ' +
        '.unelevated_form button.uef_search { background-color: #367778; border: none; color: white; display: inline-block;vertical-align: top; margin: 0px; height: 45px; width: calc(50% - 8px);} ' +
        '.unelevated_sub { line-height:25px; vertical-align:top;} .coffee_emoji {display:inline-block; line-height:25px; font-size: 25px; margin-left: 6px; vertical-align: top;}' +
        '.unelevated_sub a { line-height:25px; vertical-align:top; font-family: Cathay Sans EN, CathaySans_Bd, sans-serif; font-size: 15px !important; text-decoration:underline dotted !important; margin: 0px; color: #ae4b4b !important; font-weight: bold;}' +
        '.unelevated_sub a:after { content:none !important; }' +
        'a.uef_toggle, a.uef_toggle:hover { background: #367778; display: block; position: absolute; right: -1px; top: -5px; padding-top:5px; width: 30px; text-align: center; text-decoration: none; color: white !important; padding-bottom: 5px; }' +
        'a.uef_toggle:after {content:\'«\'} .uef_collapsed a.uef_toggle:after {content : \'»\'} ' +
        '.bulk_hidden {display:none;}' +
        '.bulk_box {min-height: 60px; transition: margin-top 0.7s ease-out;background: #f7f6f0; padding: 8px; border: 1px solid #bcbec0; box-shadow: 0px 0px 7px rgb(0 0 0 / 20%); margin-top: -11px; margin-bottom: 20px; z-index: 9; position: relative;}' +
        '.bulk_box_hidden {position:relative; margin-top:-80px;}' +
        '.bulk_table { width:100%; border: 1px solid #c6c2c1; margin: 10px 0; font-size: 12px;}' +
        '.bulk_table th { text-align:center; font-weight:bold; background: #ebedec; line-height:17px; }' +
        '.bulk_table td { background:white; }' +
        '.bulk_table tr:nth-child(even) td { background:#f9f9f9; }' +
        '.bulk_table th, .bulk_table td { border: 1px solid #c6c2c1; padding: 5px; }' +
        '.bulk_table .bulk_date { width:80px; text-align:center; }' +
        '.bulk_table .bulk_date a { text-decoration:underline !important; }' +
        '.bulk_table td.bulk_flights { padding:5px 5px 0 5px }' +
        '.bulk_table .bulk_flights .bulk_no_flights { display:block;padding-bottom:5px; }' +
        '.bulk_table .flight_item { display: inline-block; background: #e0e0e0; line-height:15px; padding: 6px; border-radius: 5px; margin-right: 6px; margin-bottom: 5px; white-space: nowrap; }' +
        '.bulk_table .flight_item img { line-height: 15px; max-height: 15px; vertical-align: middle; margin-right: 5px; max-width: 20px;}' +
        '.bulk_table .flight_item.direct { background: #cbe0cf; }' +
        '.bulk_table .flight_item span { padding: 2px 5px; color: white; border-radius: 5px; margin-left: 5px; }' +
        'span.bulk_j { background: #002e6c;}' +
        'span.bulk_f { background: #832c40;}' +
        'span.bulk_p { background: #487c93;}' +
        'span.bulk_y { background: #016564;}' +
        '.flight_item span.stopover { color: #909090 !important; display: inline-block; background: white; font-size: 11px; margin: 0px 6px !important; }' +
        '.bulk_submit {background-color: #367778; border: none; color: white; vertical-align: middle; margin: 0px auto; height: 45px; line-height: 35px; padding: 5px 0; width: 100%; display: block;}' +
        '.bulk_submit img, button.uef_search img {line-height: 35px; height: 25px; display: inline-block; margin-right: 10px; vertical-align: -7px;}' +
        '.bulk_searching, .uef_search.searching  {background-color: #b9cdc9 !important;}' +
        '.col-select-departure-flight > .row:last-of-type { padding-bottom: 140px; }' +
        'span.info-x { border-radius: 5px; padding: 2px 5px; margin-left: 5px; color:white; font-size:10px; font-family: CathaySans_Md; }' +
        'span.info-f { background: #832c40;}' +
        'span.info-j { background: #002e6c;}' +
        'span.info-p { background: #487c93;}' +
        'span.info-y { background: #016564;}' +
        '.unelevated_update { border-radius: 5px; background: #f27878; padding: 5px 10px; margin: 10px 8px 0 0; text-align: center; display:none; }'+
        '.unelevated_update a { color:white !important; } .unelevated_update a:after { content:none !important; }' +
        '.unelevated_update a span { font-weight:bold; font-family: "GT Walsheim","Cathay Sans EN", CathaySans_Md, sans-serif; }'+
        '.unelevated_update.update_show { display:block; }'
    );

    var lang = (navigator.language != "zh-TW") ? {
            "ec" : "HK",
            "el": "en",
            "search" : "Search",
            "coffee" : "Did this tool help you? Buy me a coffee! ",
            "searching" : "<img src='https://book.cathaypacific.com/CathayPacificAwardV3/AML_S65.12/common/skin/img/icons/cx/icon-loading.gif'> Searching...",
            "searching_w_cancel" : "<img src='https://book.cathaypacific.com/CathayPacificAwardV3/AML_S65.12/common/skin/img/icons/cx/icon-loading.gif'> Searching... (Click to Stop)",
            "next_batch" : "Load More...",
            "search_10" : "Batch Availability for 20 Days",
            "flights" : "Available Flights",
            "first" : "First",
            "business" : "Bus",
            "premium" : "Prem",
            "economy" : "Econ",
            "date" : "Date",
            "no_flights" : "No Flights",
            "expired" : "Search Next 20 (Requires Refresh)",
            "searching_cont" : "<img src='https://book.cathaypacific.com/CathayPacificAwardV3/AML_S65.12/common/skin/img/icons/cx/icon-loading.gif'> Please wait... (Page will refresh)",
            "super" : "SuperCharged Award Search",
            "error" : "Unknown Error... Try Again",
            "bulk_batch" : "Batch Search",
            "bulk_flights" : "Flights",
            "new_version" : "有新版本可更新:"
        } : {
            "ec" : "TW",
            "el": "zh",
            "search" : "搜尋",
            "coffee" : "這工具有幫到你嗎?歡迎請我喝杯咖啡呀!",
            "searching" : "<img src='https://book.cathaypacific.com/CathayPacificAwardV3/AML_S65.12/common/skin/img/icons/cx/icon-loading.gif'> 請稍後...",
            "searching_w_cancel" : "<img src='https://book.cathaypacific.com/CathayPacificAwardV3/AML_S65.12/common/skin/img/icons/cx/icon-loading.gif'> 請稍後... (點我暫停)",
            "next_batch" : "載人更多...",
            "search_10" : "批次搜尋 20 天可兌換航班",
            "flights" : "可兌換航班",
            "first" : "頭等",
            "business" : "商務",
            "premium" : "豪經",
            "economy" : "經濟",
            "date" : "日期",
            "no_flights" : "無航班",
            "expired" : "再搜尋 20 天 (畫面需重整)",
            "searching_cont" : "<img src='https://book.cathaypacific.com/CathayPacificAwardV3/AML_S65.12/common/skin/img/icons/cx/icon-loading.gif'> 請稍後... (視窗將會刷新)",
            "super" : "SUPERCharged Award Search",
            "error" : "不明錯誤... 再試一次",
            "bulk_batch" : "批次查詢",
            "bulk_flights" : "航班",
            "new_version" : "New Version Available:"
        };

    var cx_json = {
        "awardType": "Standard",
        "brand": "CX",
        "cabinClass": "Y",
        "entryCountry": lang.ec,
        "entryLanguage": lang.el,
        "entryPoint": "https://www.cathaypacific.com/cx/" + lang.el + "_" + lang.ec + "/book-a-trip/redeem-flights/redeem-flight-awards.html",
        "errorUrl": "https://www.cathaypacific.com/cx/" + lang.el + "_" + lang.ec + "/book-a-trip/redeem-flights/redeem-flight-awards.html?recent_search=ow",
        "isFlexibleDate": false,
        "numAdult": 1,
        "numChild": 0,
        "promotionCode": "",
        "returnUrl": "https://www.cathaypacific.com/cx/" + lang.el + "_" + lang.ec + "/book-a-trip/redeem-flights/redeem-flight-awards.html?recent_search=ow",
        "segments": [
            {
                "departureDate": "20230801",
                "origin": "TPE",
                "destination": "TYO"
            }
        ]
    };

    var bulk_continue = false;
    var limit_reached = false;

function isValidDate(dateString)
{
    // First check for the pattern
    if(!/^\d{8}$/.test(dateString)) return false;

    // Parse the date parts to integers
    var day = dateString.substring(6, 8);
    var month = dateString.substring(4, 6);
    var year = dateString.substring(0, 4);

    // Check the ranges of month and year
    if(year < 1000 || year > 3000 || month == 0 || month > 12) return false;

    var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    // Adjust for leap years
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) monthLength[1] = 29;

    // Check the range of the day
    return day > 0 && day <= monthLength[month - 1];
};

function waitForElm(selector) {
    return new Promise(resolve => {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector));
        }

        const observer = new MutationObserver(mutations => {
            if (document.querySelector(selector)) {
                resolve(document.querySelector(selector));
                observer.disconnect();
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    });
}


    var cxhttp = new XMLHttpRequest();
    cxhttp.onload = function() {
        var response = JSON.parse(this.responseText);
        cxform(response.parameters);
    }

    function cxsearch(string){
        cxhttp.withCredentials = true;
        cxhttp.open("POST", "https://api.cathaypacific.com/redibe/standardAward/create");
        cxhttp.setRequestHeader("Content-type", "application/json");
        cxhttp.send(string);
    }

    function cxform(parameters) {

        // Create a form dynamically
        var form = document.createElement("form");
        var action = "https://book.cathaypacific.com/CathayPacificAwardV3/dyn/air/booking/availability";
        if (bulk_continue) action += "?bulk_continue=1"
        form.setAttribute("name", "cxform");
        form.setAttribute("method", "post");
        form.setAttribute("action", action);

        for(var item in parameters) {
            var input = document.createElement("input");
            input.setAttribute("type", "hidden");
            input.setAttribute("name", item);
            input.setAttribute("value", parameters[item]);
            form.appendChild(input);
        }

        document.getElementsByTagName("body")[0].appendChild(form);
        document.forms.cxform.submit();
    }

    function addtoBulk(html){
        document.querySelector(".bulk_table tbody").insertAdjacentHTML("beforeend", html);
    }

var bulk_http = new XMLHttpRequest();
var bulk_url = (typeof formSubmitUrl !== 'undefined') ? formSubmitUrl : "";
var bulk_date = "";
var bulk_limit = 21;
    bulk_http.onreadystatechange = function() {//Call a function when the state changes.
        if(bulk_http.readyState == 4 && bulk_http.status == 200) {
            console.log(bulk_http);
            bulk_date = bulk_date ? bulk_date : document.querySelector("input[name=uef_date]").value;
            var search_year = +bulk_date.substring(0, 4);
            var search_month = +bulk_date.substring(4, 6);
            var search_day = +bulk_date.substring(6, 8);
            var search_date = new Date(search_year, search_month - 1, search_day);
            var responseText = JSON.parse(bulk_http.responseText);
            var response = JSON.parse(responseText.pageBom);
            var flights = response.modelObject?.availabilities?.upsell?.bounds[0].flights || [];
            var flightHTML = "";
            var formatted_date = search_date.getFullYear() +"-"+ (search_date.getMonth() +1).toString().padStart(2, '0') +"-"+ search_date.getDate().toString().padStart(2, '0');
            flightHTML += "<tr><td class='bulk_date'>";
            flightHTML += "<a href='javascript:void();' data-date='" + formatted_date.replaceAll("-","") + "'>" + formatted_date + "</a>";
            flightHTML += "</td><td class='bulk_flights'>";
            var noflights = true;
            flights.forEach((flight) => {
                var available = "";
                var f1 = flight.segments[0].cabins?.F?.status || 0;
                var j1 = flight.segments[0].cabins?.B?.status || 0;
                var p1 = flight.segments[0].cabins?.N?.status || 0;
                var y1 = (+flight.segments[0].cabins?.E?.status || 0) + (+flight.segments[0].cabins?.R?.status || 0);
                if(flight.segments.length == 1) {
                    if (f1 >= 1) available = available + " <span class='bulk_f'>F <b>" + f1 + "</b></span>";
                    if (j1 >= 1) available = available + " <span class='bulk_j'>J <b>" + j1 + "</b></span>";
                    if (p1 >= 1) available = available + " <span class='bulk_p'>PY <b>" + p1 + "</b></span>";
                    if (y1 >= 1) available = available + " <span class='bulk_y'>Y <b>" + y1 + "</b></span>";
                    if (available != "") {
                        flightHTML += "<span class='flight_item direct'><img src='https://book.cathaypacific.com/CathayPacificAwardV3/AML_S65.15/common/skin/img/airlines/logo-" + flight.segments[0].flightIdentifier.marketingAirline.toLowerCase() + ".png'>" + flight.segments[0].flightIdentifier.marketingAirline + flight.segments[0].flightIdentifier.flightNumber + available + "</span>";
                        noflights = false;
                    }
                } else {
                    var f2 = flight.segments[1].cabins?.F?.status || 0;
                    var j2 = flight.segments[1].cabins?.B?.status || 0;
                    var p2 = flight.segments[1].cabins?.N?.status || 0;
                    var y2 = (+flight.segments[1].cabins?.E?.status || 0) + (+flight.segments[1].cabins?.R?.status || 0);
                    if (f1 >= 1 && f2 >= 1) available = available + " <span class='bulk_f'>F <b>" + Math.max(f1, f2) + "</b></span>";
                    if (j1 >= 1 && j2 >= 1) available = available + " <span class='bulk_j'>J <b>" + Math.max(j1, j2) + "</b></span>";
                    if (p1 >= 1 && p2 >= 1) available = available + " <span class='bulk_p'>PY <b>" + Math.max(p1, p2) + "</b></span>";
                    if (y1 >= 1 && y2 >= 1) available = available + " <span class='bulk_y'>Y <b>" + Math.max(y1, y2) + "</b></span>";
                    if (available != "") {
                        flightHTML += "<span class='flight_item'><img src='https://book.cathaypacific.com/CathayPacificAwardV3/AML_S65.15/common/skin/img/airlines/logo-" + flight.segments[0].flightIdentifier.marketingAirline.toLowerCase() + ".png'>" + flight.segments[0].flightIdentifier.marketingAirline + flight.segments[0].flightIdentifier.flightNumber + "<span class='stopover'>" + /^[A-Z]{3}:([A-Z:]{3,7}):[A-Z]{3}_/g.exec(flight.flightIdString)[1].replace(":"," / ") + "</span>" + flight.segments[1].flightIdentifier.marketingAirline + flight.segments[1].flightIdentifier.flightNumber + available + "</span>";
                        noflights = false;
                    }
                }
            });
            if (noflights) flightHTML += "<span class='bulk_no_flights'>"+lang.no_flights+"</span>";
            flightHTML += "</td></tr>";
            addtoBulk(flightHTML);
            var next_date = new Date(search_year, search_month - 1, search_day + 1);
            bulk_date = next_date.getFullYear() +""+ (next_date.getMonth() +1).toString().padStart(2, '0') +""+ next_date.getDate().toString().padStart(2, '0');
            bulk_search();
        } else if (bulk_http.readyState == 4 && bulk_http.status >= 400) {
            console.log(bulk_http);
            document.querySelector(".bulk_submit").innerHTML = lang.expired;
            limit_reached = true;
            bulk_limit = 21;
            document.querySelector(".bulk_submit").classList.remove("bulk_searching");
        } else if (bulk_http.readyState == 4 && bulk_http.status >= 300) {
            document.querySelector(".bulk_submit").classList.remove("bulk_searching");
            document.querySelector(".bulk_submit").innerHTML = lang.error;
        }

            console.log(bulk_http);
    }

function single_date(date){
        document.querySelector("#uef_date").value = date;
        document.querySelector(".bulk_submit").innerHTML = lang.searching_w_cancel;
        document.querySelector(".uef_search").click();
}


function bulk_search() {
    if(limit_reached || route_changed) {
        bulk_continue = true;
        document.querySelector("#uef_date").value = bulk_date;
        document.querySelector(".bulk_submit").innerHTML = lang.searching_w_cancel;
        document.querySelector(".uef_search").click();
        return;
    } else if (--bulk_limit <= 0) {
        document.querySelector(".bulk_submit").innerText = lang.next_batch;
        document.querySelector(".bulk_submit").classList.remove("bulk_searching");
        return;
    }
    bulk_date = bulk_date ? bulk_date : document.querySelector("input[name=uef_date]").value;
    var search_year = +bulk_date.substring(0, 4);
    var search_month = +bulk_date.substring(4, 6);
    var search_day = +bulk_date.substring(6, 8);
    var search_date = new Date(search_year, search_month - 1, search_day);
    var new_date = search_date.getFullYear() +""+ (search_date.getMonth() + 1).toString().padStart(2, '0') +""+ search_date.getDate().toString().padStart(2, '0') + "0000";
    var params = document.querySelector("form[name=MNMB_FACADE_FORM] input[name=ENTRY_REQUEST]").value;
    params = params.replace('&ENCT=2', '').replace('&SERVICE_ID=1', '').replace('&DIRECT_LOGIN=YES', '').replace('&B_DATE_1', '&OLD_DATE').replace(/(&ENC=[^&]+)/g,"");
    params = params + "WDS_PRE_STEP=SCHEDULE_DRIVEN&WDS_CURRENT_STEP=SCHEDULE_DRIVEN&WDS_TIER_SELECTED=STD&WDS_SELECTED_TIER=STD&PAGE_TICKET=0&&WDS_DATE=" + new_date + "&DDS_PROMO_SELECTED=FALSE&B_DATE_1=" + new_date ;
    bulk_http.open('POST', bulk_url, true);

    //Send the proper header information along with the request
    bulk_http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    bulk_http.setRequestHeader('Accept', 'application/json, text/plain, */*');

    bulk_http.send(params);
}

    function insertBulk(elm){
        elm.insertAdjacentHTML("beforeend", "<div class='bulk_box'><div>" +
                               "<table class='bulk_table bulk_hidden'><thead><th class='bulk_date'>" + lang.date + "</th><th class='bulk_flights'>" + lang.flights + " <span class='info-x info-f'>" + lang.first + "</span><span class='info-x info-j'>" + lang.business + "</span><span class='info-x info-p'>" + lang.premium + "</span><span class='info-x info-y'>" + lang.economy + "</span></th></thead><tbody></tbody></table>" +
                               "</div>" +
                               "<button class='bulk_submit'>" + lang.search_10 + "</button>" +
                               "</div>");

        document.querySelector(".bulk_submit").addEventListener("click",function(e){
            if (route_changed) {
                bulk_continue = true;
                document.querySelector(".bulk_submit").innerHTML = lang.searching_w_cancel;
                document.querySelector(".uef_search").click();
            } else {
                document.querySelector(".bulk_table").classList.remove("bulk_hidden");
                console.log(bulk_limit);
                if (bulk_limit != 21 && bulk_limit != 0 && bulk_limit != -1){
                    bulk_limit = 0;
                    document.querySelector(".bulk_submit").innerText = lang.next_batch;
                    document.querySelector(".bulk_submit").classList.remove("bulk_searching");
                } else {
                    bulk_limit = 21;
                    document.querySelector(".bulk_submit").innerHTML = lang.searching_w_cancel;
                    document.querySelector(".bulk_submit").classList.add("bulk_searching");
                    bulk_search();
                }
            }
        });

        document.querySelector(".bulk_table").addEventListener("click",function(e){
            if(e.target.dataset.date) {
                bulk_limit = 0;
                single_date(e.target.dataset.date)
            }
        });
/*
        if(GM_getValue("bulk_show") == true || localStorage.getItem("bulk_show") == true) {
                document.querySelector(".unelevated_title").innerText = lang.super;
                document.querySelector(".bulk_box").classList.remove("bulk_box_hidden");
        }
*/
        const queryString = window.location.search;
        const urlParams = new URLSearchParams(queryString);
        const bcont_query = urlParams.get('bulk_continue');
        if (bcont_query) {
            document.querySelector(".bulk_submit").innerHTML = lang.searching_w_cancel;
            document.querySelector(".bulk_submit").classList.add("bulk_searching");
            waitForElm('form[name=MNMB_FACADE_FORM] input[name=ENTRY_REQUEST]').then((elm) => {
                setTimeout(() => {
                    document.querySelector(".bulk_submit").click();
                }, "1000")
            });
        }
    }

    function insertForm(elm){
        elm.insertAdjacentHTML("beforeend", "<div class='unelevated_form'>" +
                               "<div class='unelevated_title'>Unelevated Award Search</div>" +
                               "<label><span>From</span><input type='text' onClick='this.select();' id='uef_from' name='uef_from' placeholder='TPE' value='" + uef_from + "'></label>" +
                               "<label><span>To</span><input type='text' onClick='this.select();' id='uef_to' name='uef_to' placeholder='TYO' value='" + uef_to + "'></label>" +
                               "<label><span>Adults</span><input type='number' inputmode='decimal' onClick='this.select();' id='uef_adult' name='uef_adult' placeholder='Adults' value='" + uef_adult + "'></label>" +
                               "<label><span>Children</span><input type='number' inputmode='decimal' onClick='this.select();' id='uef_child' name='uef_child' placeholder='Children' value='" + uef_child + "'></label>" +
                               "<label><span>Date</span><input class='uef_date' onClick='this.setSelectionRange(6, 8);' id='uef_date' type='number' inputmode='decimal' name='uef_date' placeholder='20221001' value='" + uef_date + "'></label>" +
                               "<button class='uef_search'>" + lang.search + "</button>" +
                               "<div class='unelevated_sub'><a href='https://jayliu.net/buymeacoffee' target='_blank'>" + lang.coffee + "</a><span class='coffee_emoji'>☕</span></div>" +
                               "<div class='unelevated_update'><a href='https://greasyfork.org/en/scripts/449998-cathay-award-search-fixer-2022' target='_blank'>"+ lang.new_version +" <span id='upd_version'>2.1.3</span> &raquo;</a></div>" +
                               //"<a href='javascript:void();' class='uef_toggle'></a>" +
                               "</div>");

        /*document.querySelector(".uef_toggle").addEventListener("click",function(e){
        document.querySelector(".unelevated_form").classList.toggle("uef_collapsed")
    });*/

        document.querySelector(".unelevated_form input[name=uef_from]").addEventListener("change",function(e){
            this.value = this.value.toUpperCase().split(' ').join('').substring(0, 3);
            route_changed = true;
            if(document.querySelector(".bulk_submit")) {
                document.querySelector(".bulk_submit").innerHTML = lang.bulk_batch + " " + document.querySelector(".unelevated_form input[name=uef_from]").value + " - " + document.querySelector(".unelevated_form input[name=uef_to]").value + " " + lang.bulk_flights;
            }
        });
        document.querySelector(".unelevated_form input[name=uef_to]").addEventListener("change",function(e){
            this.value = this.value.toUpperCase().split(' ').join('').substring(0, 3);
            route_changed = true;
            if(document.querySelector(".bulk_submit")) {
                document.querySelector(".bulk_submit").innerHTML = lang.bulk_batch + " " + document.querySelector(".unelevated_form input[name=uef_from]").value + " - " + document.querySelector(".unelevated_form input[name=uef_to]").value + " " + lang.bulk_flights;
            }
        });
        document.querySelector(".unelevated_form input[name=uef_date]").addEventListener("change",function(e){
            if (!isValidDate(this.value)) {
                var default_date = new Date(new Date().getTime()+(5*24*60*60*1000));
                alert("Invalid Date");
                this.value = default_date.getFullYear() + (default_date.getMonth() +1).toString().padStart(2, '0') + default_date.getDate().toString().padStart(2, '0');
            }
        });
        /*
        var secret_mode = 0;
        document.querySelector(".unelevated_title").addEventListener("click",function(e){
            if(++secret_mode == 9) {
                document.querySelector(".unelevated_title").innerText = lang.super;
                document.querySelector(".bulk_box").classList.remove("bulk_box_hidden");
                localStorage.setItem("bulk_show",true);
                GM_setValue("bulk_show",true);
            }
        });*/
/*

        var last_update = new Date(GM_getValue("last_update")) || false;
        var current_update = new Date();
        current_update = new Date(current_update.getFullYear(),current_update.getMonth() +1,current_update.getDate() );

        if (!last_update || current_update.getTime() - last_update.getTime() >= 86400000) {
            var GFU = new GreasyForkUpdater();
            GFU.checkUpdate(449998, GM_info.script.version, function(update, updateurl, metaData) {
                if (update != false) {
                    show_update(metaData.version);
                    console.log(updateurl);
                    console.log(metaData.version);
                    //https://greasyfork.org/en/scripts/449998-cathay-award-search-fixer-2022
                    GM_setValue("latest_version",metaData.version);
                } else {
                    GM_setValue("latest_version",GM_info.script.version);
                }
                GM_setValue("last_update", current_update.getFullYear() +"/"+ (current_update.getMonth() +1).toString().padStart(2, '0') +"/"+ current_update.getDate().toString().padStart(2, '0'));
            });
        } else if (GM_getValue("latest_version") != GM_info.script.version) {
            show_update(GM_getValue("latest_version"));
        }
*/

        document.querySelector(".uef_search").addEventListener("click",function(e){
            uef_from = document.querySelector("#uef_from").value;
            uef_to = document.querySelector("#uef_to").value;
            uef_date = document.querySelector("#uef_date").value;
            uef_adult = document.querySelector("#uef_adult").value;
            uef_child = document.querySelector("#uef_child").value;
            localStorage.setItem("uef_from",uef_from);
            localStorage.setItem("uef_to",uef_to);
            localStorage.setItem("uef_date",uef_date);
            localStorage.setItem("uef_adult",uef_adult);
            localStorage.setItem("uef_child",uef_child);
            GM_setValue("uef_from",uef_from);
            GM_setValue("uef_to",uef_to);
            GM_setValue("uef_date",uef_date);
            GM_setValue("uef_adult",uef_adult);
            GM_setValue("uef_child",uef_child);
            cx_json.numAdult = uef_adult;
            cx_json.numChild = uef_child;
            cx_json.segments[0].departureDate = uef_date;
            cx_json.segments[0].origin = uef_from.toUpperCase();
            cx_json.segments[0].destination = uef_to.toUpperCase();
            var cx_string = JSON.stringify(cx_json);
            document.querySelector(".uef_search").innerHTML = lang.searching;
            document.querySelector(".uef_search").classList.add("searching");
            cxsearch(cx_string);
        });
    }


    if(document.querySelector('.redibe-v3-flightsearch-description')) {
        insertForm(document.querySelector('.redibe-v3-flightsearch-description'));
    } else {
        waitForElm('.redibe-v3-flightsearch-description').then((elm) => {
            insertForm(elm);
        });
    }
    if(document.querySelector('.bound-route')) {
        insertForm(document.querySelector('.bound-route'));
        insertBulk(document.querySelector('.bound-route'));
    } else {
        waitForElm('.bound-route').then((elm) => {
            insertForm(elm);
            insertBulk(elm);
        });
    }



})();