// <!--
//  wp.js   wp.js  7/17/00  1.1
// this function removes a cookie

var Title;
var LastName;
var FirstName;
var MiddleInitial;
var WPNum;
// serverName variable can be found in common.js

function remove_cookie (name)
{
   document.cookie = name + "=; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain=.nwa.com";
}

//  function to retrieve a cookie by name

function get_cookie(name)
{
   var cname = name + "=";
   var allcookies = document.cookie;
   var pos = allcookies.indexOf(cname);
   if (pos != -1) {
      var start = pos + cname.length;
      var end = allcookies.indexOf(";", start);
      if (end == -1) end = allcookies.length;
      return(unescape(allcookies.substring(start,end)));
   }
   return("");
}

//  UTF-8 function to logout of skymiles by deleting all cookies

function logout_skymiles ()
{
   remove_cookie("WorldPerks");
   remove_cookie("WP_PIN");
   remove_cookie("NWASSOToken");
   remove_cookie("form0");
   remove_cookie("orbot");
     var edcoregCookie = get_cookie("edcoreg");
    if ( edcoregCookie != "") {
       var ifrm = document.createElement("IFRAME");
       ifrm.setAttribute("src", "http://auctions.nwa.com/cgi-bin/ncommerce3/Logoff?merchant_rn=78954854&url=http://www.nwa.com");
       ifrm.style.width = 1+"px";
       ifrm.style.height = 1+"px";
       ifrm.style.visibility = "hidden";
       document.body.appendChild(ifrm);
       remove_cookie("edcoreg");
    }

   if (visitordata.lang_code == null || visitordata.lang_code == 'en') {
        alert("You have successfully logged out.");
   } else if (visitordata.lang_code == 'jp') {
        alert("ワールドパークスからのログアウトが完了しました。");
   } else if (visitordata.lang_code == 'tc') {
        alert("您已經成功登出「環宇哩程優惠計劃」。");
   } else if (visitordata.lang_code == 'sc') {
        //alert('您已经成功登出"环宇里程优惠计划"。');
   } else if (visitordata.lang_code == 'kr') {
	    alert('회원님께서는 월드퍽스에서 성공적으로 로그아웃하셨습니다.');
   }
   
 
   if (visitordata.lang_code != null) {
       if (visitordata.country_code == "jp" && visitordata.lang_code == "jp"){
           location = serverName + "/asia/jp/skymiles/index.html";
       }
       else
            location = serverName + "/asia/" + visitordata.lang_code + "/skymiles/index.html";
   } else {
       location = serverName + "/asia/en/skymiles/index.html";
   }
}



//  LOCAL ENCODING function to logout of skymiles by deleting all cookies

function logout_skymiles_ll ()
{
   remove_cookie("WorldPerks");
   remove_cookie("WP_PIN");
   remove_cookie("form0");
   remove_cookie("orbot");
   remove_cookie("NWASSOToken");
     var edcoregCookie = get_cookie("edcoreg");
    if ( edcoregCookie != "") {
       var ifrm = document.createElement("IFRAME");
       ifrm.setAttribute("src", "http://auctions.nwa.com/cgi-bin/ncommerce3/Logoff?merchant_rn=78954854&url=http://www.nwa.com");
       ifrm.style.width = 1+"px";
       ifrm.style.height = 1+"px";
       ifrm.style.visibility = "hidden";
       document.body.appendChild(ifrm);
       remove_cookie("edcoreg");
    }

   if (visitordata.lang_code == null || visitordata.lang_code == 'en') {
        alert("You have successfully logged out.");
   } else if (visitordata.lang_code == 'jp') {
        alert("You have successfully logged out.");
   } else if (visitordata.lang_code == 'kr') {
        alert("You have successfully logged out.");
   } else if (visitordata.lang_code == 'sc') {
        alert("You have successfully logged out.");
   } else if (visitordata.lang_code == 'tc') {
        alert("You have successfully logged out.");
   }

   if (visitordata.lang_code != null) {
       if (visitordata.country_code == "jp" && visitordata.lang_code == "jp"){
           location = serverName + "/asia/jp/skymiles/index.html";
       }
       else
            location = serverName + "/asia/" + visitordata.lang_code + "/skymiles/index.html";
   } else {
       location = serverName + "/asia/en/skymiles/index.html";
   }
}

function logout_skymiles_home ()
{
   remove_cookie("WorldPerks");
   remove_cookie("WP_PIN");
   remove_cookie("form0");
   remove_cookie("orbot");
   remove_cookie("NWASSOToken");
     var edcoregCookie = get_cookie("edcoreg");
    if ( edcoregCookie != "") {
       var ifrm = document.createElement("IFRAME");
       ifrm.setAttribute("src", "http://auctions.nwa.com/cgi-bin/ncommerce3/Logoff?merchant_rn=78954854&url=http://www.nwa.com");
       ifrm.style.width = 1+"px";
       ifrm.style.height = 1+"px";
       ifrm.style.visibility = "hidden";
       document.body.appendChild(ifrm);
       remove_cookie("edcoreg");
    }

   alert("You have successfully logged out.");
   if (visitordata.country_code != null) {
       if (visitordata.lang_code != null) {
           location = serverName + "/" + visitordata.country_code + "/" + visitordata.lang_code;
       } else {
           location = serverName + "/" + visitordata.country_code + "/" + en;
       }
   } else {
       location = serverName + "/hk/en";
   }
}


// JR - 06/26/00 - another function to logout of skymiles by deleting all cookies

function logout_reload ()
{
   remove_cookie("WorldPerks");
   remove_cookie("WP_PIN");
   remove_cookie("form0");
   remove_cookie("orbot");
   remove_cookie("NWASSOToken");
     var edcoregCookie = get_cookie("edcoreg");
    if ( edcoregCookie != "") {
       var ifrm = document.createElement("IFRAME");
       ifrm.setAttribute("src", "http://auctions.nwa.com/cgi-bin/ncommerce3/Logoff?merchant_rn=78954854&url=http://www.nwa.com");
       ifrm.style.width = 1+"px";
       ifrm.style.height = 1+"px";
       ifrm.style.visibility = "hidden";
       document.body.appendChild(ifrm);
       remove_cookie("edcoreg");
    }

   window.location.reload (); 
}

//  function to automagically post SkyMiles account balance form
//  on /skymiles/direct/ hidden form

// GM - -01/30/02 - logout of skymiles elite main page.
// GM -             and, show popup, and reload
function logout_elite_reload()
{
   remove_cookie("WorldPerks");
   remove_cookie("WP_PIN");
   remove_cookie("form0");
   remove_cookie("orbot");
   remove_cookie("NWASSOToken");
     var edcoregCookie = get_cookie("edcoreg");
    if ( edcoregCookie != "") {
       var ifrm = document.createElement("IFRAME");
       ifrm.setAttribute("src", "http://auctions.nwa.com/cgi-bin/ncommerce3/Logoff?merchant_rn=78954854&url=http://www.nwa.com");
       ifrm.style.width = 1+"px";
       ifrm.style.height = 1+"px";
       ifrm.style.visibility = "hidden";
       document.body.appendChild(ifrm);
       remove_cookie("edcoreg");
    }

   alert("You have successfully logged out of SkyMiles");
   window.location.reload ();
}


// KJ - -02/07/02 - logout of skymiles and return to elite main page.
function logout_elite()
{
   remove_cookie("WorldPerks");
   remove_cookie("WP_PIN");
   remove_cookie("form0");
   remove_cookie("orbot");
   remove_cookie("NWASSOToken");
     var edcoregCookie = get_cookie("edcoreg");
    if ( edcoregCookie != "") {
       var ifrm = document.createElement("IFRAME");
       ifrm.setAttribute("src", "http://auctions.nwa.com/cgi-bin/ncommerce3/Logoff?merchant_rn=78954854&url=http://www.nwa.com");
       ifrm.style.width = 1+"px";
       ifrm.style.height = 1+"px";
       ifrm.style.visibility = "hidden";
       document.body.appendChild(ifrm);
       remove_cookie("edcoreg");
    }

   alert("You have successfully logged out of SkyMiles");
   location = serverName;
}

function postWP()
{
    var WP_num = get_WP_member_num();
    var WP_PIN = get_WP_pin();
    if (WP_PIN != "" && WP_num != "")
    {
        document.wpbalance.account.value=WP_num;
        document.wpbalance.pin.value=WP_PIN;
        document.wpbalance.save_pin.value="Y";
        document.wpbalance.save_pin.checked="TRUE";
        document.wpbalance.submit();
    } else {
    location = serverName + "/skymiles/milea/mileage.shtml";
    }
}

//  function to automagically post SkyMiles account history form
//  on /skymiles/direct/ hidden form

function postWP_hst()
{
    var WP_num = get_WP_member_num();
    var WP_PIN = get_WP_pin();
    if (WP_PIN != "" && WP_num != "")
    {
        document.wphistory.account.value=WP_num;
        document.wphistory.pin.value=WP_PIN;
        document.wphistory.save_pin.value="Y";
        document.wphistory.save_pin.checked="TRUE";
        document.wphistory.submit();
    } else {
    location = "https://www.nwa.com/skymiles/accthst/accthst.shtml";
    }
}

//  function to retrieve WP member name from SkyMiles Cookie

function get_WP_member_name()
{
    var SkyMiles = get_cookie("WorldPerks");
    if (SkyMiles != "")
    {
        var cookie_flds = SkyMiles.split('&');
        return(cookie_flds[2] + " " + cookie_flds[1] + ",");
    }
    return("");
}

function get_WP_member_last_name()
{
    var SkyMiles = get_cookie("WorldPerks");
    if (SkyMiles != "")
    {
      var cookie_flds = SkyMiles.split('&');
      return(cookie_flds[1]);
    }
    return("");
}

//  function to retrieve WP member balance from SkyMiles Cookie
//      JR - 05/25/00 - added

function get_WP_member_balance()
{
    var SkyMiles = get_cookie("WorldPerks");
    if (SkyMiles != "")
    {
        var cookie_flds = SkyMiles.split('&');
        var WPBalance = cookie_flds[5];
    return (WPBalance);

    }
    return("");
}
//  function to retrieve WP member title & last name from SkyMiles Cookie
//      JR - 05/24/00 - added to output title + last (i.e. Mr Smith)

function get_WP_member_title_name()
{
    var SkyMiles = get_cookie("WorldPerks");
    if (SkyMiles != "")
    {
        var cookie_flds = SkyMiles.split('&');
        return(cookie_flds[3] + cookie_flds[1]);
    }
    return("");
}

//  function to retrieve WP member title from SkyMiles Cookie

function get_WP_member_title()
{
    var SkyMiles = get_cookie("WorldPerks");
    if (SkyMiles != "")
    {
        var cookie_flds = SkyMiles.split('&');
        return(cookie_flds[3]);
    }
    return(",");
}

//  function to retrieve WP member number from SkyMiles Cookie

function get_WP_member_num()
{
    var SkyMiles = get_cookie("WorldPerks");
    if (SkyMiles != "")
    {
        var cookie_flds = SkyMiles.split('&');
        return(cookie_flds[0]);
    }
    return("");
}

//  function to retrieve WP member PIN from WP_PIN cookie

function get_WP_pin()
{
    var SkyMiles = get_cookie("WP_PIN");
    if (SkyMiles != "")
    {
        var cookie_flds = SkyMiles.split('&');
        return(cookie_flds[0]);
    }
    return("");
}

//  function to build SkyMiles Logout request in sidebar

function sidebar_logout()
{
    var cookie_str = get_cookie("WorldPerks");
    if (cookie_str != "")
    {
        document.write("<TR><TD BGCOLOR=FFFFCC><IMG SRC=https://www.nwa.com/images/red_hr.gif ");
        document.write("Height=1 Width=130 Valign=top><BR> ");
        document.write("<IMG SRC=https://www.nwa.com/images/clear.gif Height=3 Width=1><BR> ");
        document.write("<A HREF=javascript:logout_skymiles();><b>Logout</b></A> ");
        document.write("of SkyMiles");
        document.write("<BR><IMG SRC=https://www.nwa.com/images/clear.gif Height=3 Width=1><BR> ");
        document.write("<IMG SRC=https://www.nwa.com/images/red_hr.gif Height=1 Width=130 ");
        document.write("Valign=bottom></TD></TR>");
    }
}

//  function to retrieve WP member's elite tier standing
//  JR - 04/18/00

// KJ - 02/07/02 - build SkyMiles Logout request in elite site's sidebar
function sidebar_logout_elite ()
{
    var cookie_str = get_cookie("WorldPerks");
    if (cookie_str != "")
    {
        document.write("<TR><TD BGCOLOR=FFFFCC>");
        document.write("<B><A HREF=javascript:logout_elite();>Logout</A></B>");
        document.write(" of SkyMiles<BR>");
        document.write("<BR><IMG SRC=/images/red_hr.gif WIDTH=130 HEIGHT=1 ");
        document.write("VALIGN=BOTTOM></TD></TR>");
    }
}

function get_WP_tier()
{
    var SkyMiles = get_cookie("WorldPerks");
    if (SkyMiles != "")
    {
        var cookie_flds = SkyMiles.split('&');
        return(cookie_flds[4]);
    }
    return ("");
}

//  function to retrieve names, etc.
//  JR - 06/22/00

function get_WP_info()
{
    var SkyMiles = get_cookie("WorldPerks");
    if (SkyMiles != "")
    {
        var cookie_flds = SkyMiles.split('&');

    WPNum = cookie_flds[0];
    LastName = cookie_flds[1];
    FirstName = cookie_flds[2];
    Balance = cookie_flds[5];
    Title = cookie_flds[3];

        return(1);
    }
    return (0);
}

function isNumber(wpNum,wpField) {
   try{
      wpField.value = numberFilter(wpNum);
   }catch(e){}
}

//-->

function readPrefCookie() {

    /* Checks if user is logged in */
    if (!(get_cookie("WorldPerks"))) {
        return;
    }

    var cookPairs= new Array;
    var ca = document.cookie.split(';');
    var checkCheckInMMR = 0;
    var mDepart= 0;
    var mReturn= 0;
    var dDepart= 0;
    var dReturn= 0;
    var today = new Date();
    var departFlag = false;
    var currentDay = today.getDate();
    var currentMonth = today.getMonth();
    var currentYear = today.getFullYear();
    currentMonth += 1;

    //loop through all forms on page
    for (var h=0;h < document.forms.length; h++) {
        // loop only if it is check-in or MMR box
        if (checkCheckInMMR == 2) {
            break;
        }
        var fName =  document.forms[h].name;
        if (fName == "form0" || fName == "orbot") {
            checkCheckInMMR++;
            // loop through cookies if this cookie name is the same as the form name
            for (var i=0 ; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length)
                {
                    if (c.indexOf(fName) == 0)
                    {
                        cookPairs =  c.substring(fName.length,c.length).split('&');
                    }
                }
                // loop through cookie Param/Value pairs
                for (var j=0;j < cookPairs.length; j++) {
                    cookParams = new Array;
                    cookParams = cookPairs[j].split('||');
                    // compare the Form element name, to the Param Name in our cookie
                    // and set the Form element Value = Cookie Param Value
                    for (var k=0; k<document.forms[h].elements.length; k++) {
                        if (cookParams[0] ==  document.forms[h].elements[k].name) {
                            if (cookParams[1] != "") {
                                switch (document.forms[h].elements[k].type) {
                                    case "checkbox":
                                        if (cookParams[1]=="true")
                                        document.forms[h].elements[k].checked =  cookParams[1];
                                        break;
                                    case "radio":
                                        if (cookParams[1]==document.forms[h].elements[k].value) {
                                            document.forms[h].elements[k].checked =  true;
                                        }
                                        break;
                                    case "select-one":
                    switch (cookParams[0]) {
                    case "departMonth":
                        mDepart = cookParams[1];
                        break;
                    case "departDay":
                        dDepart = cookParams[1];
                        break;
                    case "returnMonth":
                        mReturn = cookParams[1];
                        break;
                    case "returnDay":
                        dReturn = cookParams[1];
                        break;
                    default :
                        if (cookParams[0] == "checkInType") {
                        document.forms[h].elements[k].selectedIndex = cookParams[1];
                        }
                        else {
                        document.forms[h].elements[k].value =  cookParams[1];
                        }
                        break;
                                        }
                    default :
                    if (cookParams[0] == "departMonth" || cookParams[0] == "departDay" ||cookParams[0] == "returnMonth" || cookParams[0] == "returnDay")
                    {
                                            break;
                                        }
                                        else if (cookParams[0] == "checkInType") {
                                            break;
                                        }
                        else if (!cookParams[1]) {
                                            break;
                                        }
                                        else {
                                            document.forms[h].elements[k].value = cookParams[1];
                                        }
                                        break;
                                }//switch
                            }//if
                        }//if
                    }//for
                }//for
            }//for
        }//if
    }//for
    var dMonth = document.orbot.departMonth;
    var dDay = document.orbot.departDay;
    var rMonth = document.orbot.returnMonth;
    var rDay = document.orbot.returnDay;
        if (dMonth && dDay && rMonth && rDay) {
    /* If conditions not satisfied, show default dates */
    if (mDepart == currentMonth && dDepart >= currentDay) {
        dMonth.value = mDepart;
        dDay.value = dDepart;
    }
    else if (mDepart > currentMonth) {
        dMonth.value = mDepart;
        dDay.value = dDepart;
    }
    else {
        departFlag = true;
    }
    /* If condition not satisfied, calculate correct additional days */
    if (mReturn > currentMonth && mReturn > mDepart && departFlag == false) {
        rMonth.value = mReturn;
        rDay.value = dReturn;
        departFlag = true;
    }
    /* Calculate correct Return month and day */
    if (departFlag == false) {
        var tempDayRet = dDay.selectedIndex + 7;
        if ((mDepart == 2) && ((currentYear%4) == 0)) { //Feb leap year
            if (tempDayRet > 28) {
                rDay.selectedIndex = tempDayRet - 29;
                departFlag=true;
            }
        }
        else if ((mDepart == 2) && ((currentYear%4) != 0)) { //Feb not leap year
            if (tempDayRet > 27) {
                rDay.selectedIndex = tempDayRet - 28;
                departFlag=true;
            }
        }
        else if ((mDepart == 1) || (mDepart == 3) ||(mDepart == 5) ||(mDepart == 7) ||
            (mDepart == 8) ||(mDepart == 10) || (mDepart == 12)) //Months with 31 days
        {
            if (tempDayRet > 30) {
                rDay.selectedIndex = tempDayRet - 31;
                departFlag=true;
            }
        }
        else { //Months with 30 days
            if (tempDayRet > 29) {
                rDay.selectedIndex = tempDayRet - 30;
                departFlag=true;
            }
        }
        if (departFlag == true) {
            var tempMonthRet = dMonth.selectedIndex + 1;
            if (tempMonthRet > 11) {
                rMonth.selectedIndex = 0;
            }
            else {
                rMonth.selectedIndex = tempMonthRet;
            }
        }
        else {
            rDay.selectedIndex = tempDayRet;
            rMonth.selectedIndex = dMonth.selectedIndex;
        }
    }
        }
    /* If WP # is not selected, reset MMR and check-in */
    setCheckInType();
    checkReturn();
    /* When this is an award, disable return date and class of service */
    if ((document.orbot.SEARCHON[2])&&(document.orbot.SEARCHON[2].checked)) {
        enableButton(false,document.orbot)
    }
}//function

function setPrefCookie(theForm) {

    /* Checks if user is logged in */
    if (!(get_cookie("WorldPerks"))) {
        return;
    }

    /* Refresh cookie first */
    remove_cookie(theForm.name);

    var cookVal =  theForm.name +"=";
    var exdate=new Date()
    exdate.setTime(exdate.getTime()+(150*24*60*60*1000));
    var expires = "; expires="+exdate.toGMTString();
    expires += "; path=/; domain=.nwa.com;";
    for(i=0; i<theForm.elements.length; i++){
        switch (theForm.elements[i].type)
        {
            case "hidden":
                break;
            case "text" :
                if (theForm.elements[i].name != "WP" && theForm.elements[i].name != "RL" && theForm.elements[i].name != "ET") {
                    cookVal +=  '&' + theForm.elements[i].name + '||' +  theForm.elements[i].value;
                }
                break;
            case "textarea":
                cookVal += '&' + theForm.elements[i].name + '||' +  theForm.elements[i].value;
                break;
            case "checkbox":
                cookVal += '&' + theForm.elements[i].name + '||' +  theForm.elements[i].checked;
                break;
            case "select-one":
                if (theForm.elements[i].name == "checkInType") {
                    cookVal += '&' + theForm.elements[i].name + '||' + theForm.elements[i].selectedIndex;
                }
                else {
                    cookVal += '&' + theForm.elements[i].name + '||' + theForm.elements[i].options[theForm.elements[i].selectedIndex].value ;
                }
                break;
            case "radio":
                if (theForm.elements[i].checked == true)
                {
                    cookVal += '&' + theForm.elements[i].name + '||' + theForm.elements[i].value;
                }
                else{
                    //nothing
                    break;
                }
                break;
            default:
                break;
        }//switch
    }//for
    document.cookie= cookVal +  expires;
}//setPrefCookie

function checkReturn(){
    if ((document.orbot) && (document.orbot.TRIP_TYPE)) {
        if (document.orbot.TRIP_TYPE[0].checked)
        {
            document.orbot.returnMonth.disabled=false;
            document.orbot.returnDay.disabled=false;
        }
        else{
            document.orbot.returnMonth.disabled=true;
            document.orbot.returnDay.disabled=true;
        }
    }
}
