﻿// JScript File

// check for proper mpdrm components
function doComponentCheck() {
    try {
        mpdrmobj2 = new ActiveXObject("mpDRMNETOBJLib.RMGetLicense");
        r = mpdrmobj2.GetSystemInfo();
        s = mpdrmobj.GetDRMVersion();
        if (s.indexOf("=" + DVDBurnerCoreVersion) > -1) {
            document.getElementById("showInstall").innerHTML = "<hr noshade size=1 color=#666666 width=95%><table border=\"0\" cellspacing=\"0\" cellpadding=\"5\"><tr><td class=\"a12\">The CinemaNow DVD Burner is installed in your system. You are ready to purchase this movie!</td><td><img src=\"img/dvd_burner_installed_icon02.gif\" height=\"63\" width=\"161\" border=\"0\" alt=\"DVD Burner Installed\"></td></tr></table><hr noshade size=1 color=#666666 width=95%>";
            document.aspnetForm.clientinfo.value = mpdrmobj2.GetSystemInfo();
        }
        else {
            testtimer = setTimeout("doComponentCheck()", 2000);
        }
    } catch (e) {
        testtimer = setTimeout("doComponentCheck()", 2000)
    }
}

function installApp() {
    window.open(DVDBurnerDownloadPath, 'installWindow', "width=100,height=100");
    doComponentCheck();
}


function securityCode() {
    window.open("securitycode.aspx", "new", "scrollbars=no,resizable=no,location=no,status=no,toolbar=no,width=400,height=535");
}

function Privacy() {
    window.open("privacy.aspx", "new", "scrollbars=yes,resizable=yes,location=no,status=no,toolbar=no");
}

function Terms() {
    window.open("Terms.aspx", "new", "scrollbars=yes,resizable=yes,location=no,status=no,toolbar=no");
}

function BillingCountryChanged(obj) {
    BillingCountryChangedValue(obj.value);
}

function BillingCountryChangedValue(val) {
    try {
        var dZUS = document.getElementById('dZipUS');
        var dSUS = document.getElementById('dStateUS');
        var dZInt = document.getElementById('dZipInt');
        var dSCan = document.getElementById('dStateCan');

        var dShipZUS = document.getElementById('dShippingZipUS');
        var dShipSUS = document.getElementById('dShippingStateUS');
        var dShipZInt = document.getElementById('dShippingZipInt');
        var dShipSCan = document.getElementById('dShippingStateCan');

        //when in billing mode, it does not show sec code box	
        var secCode = document.getElementById('dSecCode');
        var secCode1 = document.getElementById('dSecCode1');

        var bShipChange = true;

        if (!dShipZUS || !dShipSUS || !dShipZInt || !dShipSCan) {
            bShipChange = false;
        }

        if (val == 'usa') {

            if (bShipChange) {
                dShipZUS.style.display = 'block';
                dShipSUS.style.display = 'block';
                dShipZInt.style.display = 'none';
                dShipSCan.style.display = 'none';
            }
        }

        else if (val == 'can') {
            if (bShipChange) {
                dShipZInt.style.display = 'block';
                dShipSCan.style.display = 'block';
                dShipZUS.style.display = 'none';
                dShipSUS.style.display = 'none';
            }
        }

        else {

            if (bShipChange) {
                dShipZInt.style.display = 'block';
                dShipSCan.style.display = 'none';
                dShipZUS.style.display = 'none';
                dShipSUS.style.display = 'none';
            }
        }

        if (val == 'usa') {
            dZUS.style.display = 'block';
            dSUS.style.display = 'block';

            if (secCode) secCode.style.display = 'block';
            if (secCode1) secCode1.style.display = 'block';

            dZInt.style.display = 'none';
            dSCan.style.display = 'none';
        }

        else if (val == 'can') {
            dSCan.style.display = 'block';
            dZUS.style.display = 'none';
            dZInt.style.display = 'block'; //Fix CNBB-121: just move the order of this statement here, I don't know why, but it works

            if (secCode) secCode.style.display = 'block';
            if (secCode1) secCode1.style.display = 'block';

            dSUS.style.display = 'none';
        }
        else {
            dZInt.style.display = 'block';
            dSCan.style.display = 'none';
            dZUS.style.display = 'none';

            if (secCode) secCode.style.display = 'none';
            if (secCode1) secCode1.style.display = 'none';

            dSUS.style.display = 'none';
        }
    }
    catch (e)
        { }
}

function hideElement(element) {
    if (element) { element.style.display = 'none'; }
}

function showElement(element) {
    if (element) { element.style.display = 'block'; }
}

function getCCElement() {
    return document.getElementById("dBillingInfo");
}

function getRequiredElement() {
    return document.getElementById("requiredMsg");
}

function hideCC() {
    var dC = document.getElementById("dCollapse");
    if (dC) {
        showElement(dC);
        var ccElement = getCCElement();
        if (ccElement) { hideElement(ccElement); }
    }
    var reqElement = getRequiredElement();
    if (reqElement) { hideElement(reqElement); }
}

var currentAddressMode = 'billing';
function showCC(addressMode/*"home"|empty(i.e.'billing') :  default:empty('billing')*/, disableAutoFillAddress /*true|false*/) {
    //variables currentUserHomeAddress, currentUserBillingAddress used below are generated/rendered by logic in .vb file
    addressMode = typeof addressMode === 'undefined' ? 'billing' : addressMode;
    var hfFundStatus = document.getElementById('hfIsFundSufficient');
    if (addressMode === 'home' && hfFundStatus && hfFundStatus.value === '0') {
        //when the funds are not sufficient, require user to input credit card information along with billing address
        addressMode = 'billing';
    }
    
    var dC = document.getElementById("dCollapse");
    if (dC) {
        showElement(dC);
        var ccElement = getCCElement();
        if (ccElement) { showElement(ccElement); }
    }
    var reqElement = getRequiredElement();
    if (reqElement) { showElement(reqElement); }

    if (currentAddressMode == addressMode) {
        return;
    }
    
    var spanAddrTitle = document.getElementById('spanAddressTitle');
    var ccinfoDiv = document.getElementById('ccinfo'); //ccinfo: the pane for input credit card number
    var panePhone = document.getElementById('dBillingPhone'); //phone number pane

    //when using funds of gift certificates and no billing address on file, require home address when: use "home" mode
    if (ccinfoDiv) ccinfoDiv.style.visibility = (addressMode == 'home') ? 'hidden' : 'visible'; //because it's use css style of float = right.so use property visibility here
    if (spanAddrTitle) spanAddrTitle.innerHTML = (addressMode == 'home') ? 'Home Address' : 'Billing Information';
    if (panePhone) panePhone.style.display = (addressMode == 'home') ? 'none' : 'block';

    if (addressMode === 'home' && typeof currentUserHomeAddress != 'undefined' && currentUserHomeAddress != null) {
        try {
            if (!disableAutoFillAddress) {
                with (currentUserHomeAddress) {
                    document.getElementById('tbFirstName').value = firstName || '';
                    document.getElementById('tbMiddleName').value = middleName || '';
                    document.getElementById('tbLastName').value = lastName || '';
                    document.getElementById('tbAddress1').value = address || '';
                    document.getElementById('tbAddress2').value = address2 || '';
                    document.getElementById('tbCity').value = city || '';
                    if (window.location.host.toLowerCase() == 'canada.cinemanow.com') {//BBY CAN
                        document.getElementById('ddlStateCan').value = state || '';
                        document.getElementById('tbZipInt').value = zipCode || '';
                    } else {
                        document.getElementById('ddlStateUS').value = state || '';
                        document.getElementById('tbZipUS').value = zipCode || '';
                    }
                    //SKIP country here, it's handled not by JS
                } 
            }
        } catch (ex) { }
    } else if ((addressMode === '' || addressMode === 'billing')) {
        try {
            if (typeof currentUserHomeAddress != 'undefined') {
                //if currentUserHomeAddress is rendered on page, it means current user has no billing address on file. So clear the inputs and require user to input billing address
                clearBillingAddress();
            }
        } catch (ex) { }
    }
    currentAddressMode = addressMode;
}
function clearBillingAddress() {//clear the inputs on page for billing address
    //only when billing info has not been saved on file, currentUserHomeAddress can be rendered on page. So for billing address, keep it empty as oringinal.
    document.getElementById('tbFirstName').value = '';
    document.getElementById('tbMiddleName').value = '';
    document.getElementById('tbLastName').value = '';
    document.getElementById('tbAddress1').value = '';
    document.getElementById('tbAddress2').value = '';
    document.getElementById('tbCity').value = '';
    if (window.location.host.toLowerCase() == 'canada.cinemanow.com') {//BBY CAN
        document.getElementById('ddlStateCan').value = '';
        document.getElementById('tbZipInt').value = '';
    } else {
        document.getElementById('ddlStateUS').value = '';
        document.getElementById('tbZipUS').value = '';
    }
}
function clearErrorsWhenSwitchingAddressMode() {
    var lblEM = document.getElementById('lblErrorMsg'); if (lblEM) lblEM.style.display = 'none';
    var lbls = document.getElementById('billing-innerpadding').getElementsByTagName('label'); //billing-innerpadding holds address and credit card info
    for (var i = 0; i < lbls.length; i++) {
        var o = lbls[i];
        if (o.getAttribute('for') != '') {
            o.style.color = '';//reset the color to original style
        }
    }
}
function resetFlagsWhenSwitchingPaymentMethod() {
    var hf = document.getElementById('hfIsFundSufficient');
    if (hf) hf.value = '';
}
function ToCollapseOrNot() {
    try {
        var dC = document.getElementById("dCollapse");
        if (dC) {
            if (dC.style.display == 'none') { showCC(); }
            else if (document.getElementById("dBillingInfo").style.display != 'none') { hideCC(); }
            else { showCC(); }
        }
    } catch (e) { alert('error collapse'); }
}

function ToCollapseOrNotDebit() {
    if (document.getElementById("debitComment").style.display != 'none')
        document.getElementById("debitComment").style.display = 'none';
    else
        document.getElementById("debitComment").style.display = 'inline';
}

function updatePrice(obj) {
    if (obj) {
        c = obj.value.split(',');
        ba = myObj('lblCCAmount')
        if (ba) { myObj('lblCCAmount').innerHTML = c[2] }
    }
}

function DisableButton() {
    document.body.style.cursor = 'wait';
    //document.forms[0].submit();
    window.setTimeout("disableButton('" +
			window.event.srcElement.id + "')", 0);
}

function disableButton(buttonID) {
    document.getElementById(buttonID).disabled = true;
}

function isValidCustomer() {
    var d = new Date();
    var h = d.getTimezoneOffset() / 60;
    var ul = UserLanguages;
    if (h >= 4 && h <= 11) {
        return true
        //language check
        //if (ul.toLowerCase()=='en-us'){return true}
    }
    return false
}

function validateCustomer() {
    //obj = document.aspnetForm.rblCash
    //obj = document.aspnetForm.paymentOptionRB
    obj = getElementsByClass("radioBtn");
    doAlert = false
    if (!isValidCustomer()) {
        for (i = 1; i < obj.length; i++) {
            obj[i].disabled = true;
            doAlert = true;
        }
    }
    if (doAlert) {
        validationMsg.innerText += '<br />Sorry, due to certain restrictions we are unable to process this transaction using pre-paid funds. You must enter a credit card to purchase this title.'
    }
}

function openpopup(thisurl, thisname) {
    popup = window.open(thisurl, thisname, "channelmode=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,width=529,height=386,top=10,left=10")
}

function resizePaymentOptionDivs() {
    var maxHeight = 0;
    var controlCount = 0;
    var controlArray = new Array();
    var divs = document.getElementsByTagName('div');

    for (var i = 0; i < divs.length; i++) {
        if (divs[i].className == "PaymentMethodDetails") {
            if (divs[i].offsetHeight > maxHeight) {
                maxHeight = divs[i].offsetHeight;
            }
            controlArray[controlCount] = divs[i];
            controlCount++;
        }
    }

    for (var j = 0; j < controlCount; j++) {
        controlArray[j].style.height = maxHeight + "px";
    }

    //Fix for IE 7
    if (maxHeight > 0 && typeof document.body.style.maxHeight != "undefined") {
        try {
            var v = document.getElementById('PaymentDetailHr');
            v.style.padding = maxHeight + "px 0px 0px 0px";
        } catch (e) { }
    }

}

function getElementsByClass(searchClass, node, tag) {
    var classElements = new Array();
    if (node == null)
    { node = document; }
    if (tag == null)
    { tag = '*'; }
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp('(^|\\s)' + searchClass + '(\\s|$)');
    for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

function getRadioButtonByValue(rvalue) {
    var rbls = getElementsByClass("radioBtn");
    if (rbls.length > 0) {
        for (i = 0; i < rbls.length; i++) {
            if (rbls[i].value == rvalue) {
                return rbls[i];
            }
        }
    }
}
function selectLastPaymentRadioButton(rValue) {
    var rb1 = getRadioButtonByValue(rValue);
    if (rb1) {
        rb1.checked = true;
    }
}
function clickRBLimage(rValue) {
    var rb1 = getRadioButtonByValue(rValue);
    if (rb1) {
        rb1.checked = true;
    }
    clearErrorsWhenSwitchingAddressMode(); //add the function call for fixing CNBB-605
    resetFlagsWhenSwitchingPaymentMethod(); // CNBB-625,638
}

function clickPaypalSubmit() {
    //paypalSubmit
    var ppSub = document.getElementById('paypalSubmit')
    if (ppSub) {
        ppSub.click();
    }
}

function hideTerms() {
    var terms = document.getElementById('termsDiv')
    if (terms) { terms.style.display = 'none'; }
}

function showTerms() {
    var terms = document.getElementById('termsDiv')
    if (terms) { terms.style.display = 'block'; }
}

function hideCheckoutBottom() {
    try {
        hideTerms();
        document.getElementById('submitDiv').style.display = 'none';
    } catch (e) { }
}

function showCheckoutBottom() {
    try {
        showTerms();
        document.getElementById('submitDiv').style.display = 'block';
    } catch (e) { }
}

function clickRadioShowCC(addressMode/*"home"|empty(i.e."billing") :  default:empty('billing')*/, disableAutoFillAddress) {
    showCC(addressMode, disableAutoFillAddress);
    showCheckoutBottom();
}

function clickRadioHideCC() {
    hideCC();
    showCheckoutBottom();
}


function AssociateContent() {

    try {

        if (_mm_init('normal')) {
            document.getElementById("appid").value = _mm_Exec('GetAppID', null, null);
            //AJAXRequest("GET", 'Handler/AssociateDellContentToUser.ashx?appid=' + _mm_Exec( 'GetAppID', null, null ), null, AssociateContentCallBack, false);
        }

    } catch (e) {
        //alert(e);
    }

    return false;

}

function AssociateContentCallBack() {
}

var paymentOptionTmr = null;
function persistPaymentOption() {
    try {
        if (document.getElementById('lblErrorMsg')) {
            if (document.getElementById('lastPaymentOption')) {
                if (document.getElementById('lastPaymentOption').value != '')
                    selectLastPaymentRadioButton(document.getElementById('lastPaymentOption').value);
            }
        }
        clearInterval(paymentOptionTmr);
    }
    catch (e) {
    }
}

// init the timer only when the page is fully loaded.
if (window.addEventListener) // W3C standard
{
    window.addEventListener('load', function() { paymentOptionTmr = setInterval(persistPaymentOption, 300); }, false); // NB **not** 'onload'
}
else if (window.attachEvent) // Microsoft
{
    window.attachEvent('onload', function() { paymentOptionTmr = setInterval(persistPaymentOption, 300); });
}



