﻿// JScript file for PayPerView, aka eCommerce
/* Sets a returnValue of and closes the current window */
function closeWindow(returnValue) {
    if (returnValue) {
        window.returnValue = returnValue;
    }
    else if (gTx) {
        window.returnValue = gTx;
    }
    else {
        window.returnValue = 'error';
    }
    window.close();
}
/* Opens a modal dialog box,
 * and grays out the parent window.
 * */
function openModal(hostedButtonId, ppvTxId, acTitle, acId) {
    grayOut(true);
    var retVal = window.showModalDialog('SpecialReportPayment.aspx?cmd=_s-xclick&hosted_button_id=' + hostedButtonId + '&custom=' + ppvTxId + '&item_name=\'' + acTitle + '\'&item_number=' + acId, 'windowModalTest', getDialogFeatures(800, 800, false));
    grayOut(false);
    if (retVal) {
        if (retVal != 'cancel') {
            window.location = '/SpecialReport/SpecialReportDisplay.aspx?cm=' + ppvTxId + '&tx=' + retVal;
        }
    }
    return false;
}
/* Opens a modal dialog box containing the PayPal Website Payments Standard interface,
* and grays out the parent window.
* The retVal should be set via the Cancel/Complete redirection page provided by us via the closeWindow() method or some derivative of it.
* */
function openPayPalModal(hostedButtonId, ppvTxId, acTitle, acId) {
    grayOut(true);
    var retVal = window.showModalDialog('https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=' + hostedButtonId + '&custom=' + ppvTxId + '&item_name=\'' + acTitle + '\'&item_number=' + acId, 'PayPal', getDialogFeatures(800, 800, false));
    grayOut(false);
    if (retVal) {
        if (retVal != 'cancel') {
            window.location = '/SpecialReport/SpecialReportDisplay.aspx?cm=' + ppvTxId + '&tx=' + retVal;
        }
        window.location = '/SpecialReport/SpecialReportDisplay.aspx?cm=' + ppvTxId + '&tx=' + retVal;
    }
    return false;
}

/* Opens a modal dialog box containing the PayPal Sandbox Website Payments Standard interface,
 * and grays out the parent window.
 * The retVal should be set via the Cancel/Complete redirection page provided by us via the closeWindow() method or some derivative of it.
 * */
function openSandboxModal(hostedButtonId, ppvTxId, acTitle, acId) {
    grayOut(true);
    var retVal = window.showModalDialog('https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=' + hostedButtonId + '&amount=101&custom=' + ppvTxId + '&item_name=\'' + acTitle + '\'&item_number=' + acId, 'PayPal', getDialogFeatures(800, 800, false));
    grayOut(false);
    if (retVal) {
        if (retVal != 'cancel') {
            window.location = '/SpecialReport/SpecialReportDisplay.aspx?cm=' + ppvTxId + '&tx=' + retVal;
        }
        window.location = '/SpecialReport/SpecialReportDisplay.aspx?cm=' + ppvTxId + '&tx=' + retVal;
    }
    return false;
}

/* Takes in a width as int, height as int and boolean indicating
 * the width of the dialog box,
 * the height of the dialog box,
 * and whether the dialog box is resizable.
 * */
function getDialogFeatures(dialogWidth, dialogHeight, resizable) {
    return "dialogWidth: " + dialogWidth + "px;" +
                "dialogHeight: " + dialogHeight + "px;" +
                "status: yes;unadorned: yes;scroll: no;help: no;" + (resizable ? "resizable: yes;" : "");

}

/* Function takes a boolean and an options array.
 * Boolean controls the gray-out div.
 * Options array overrides preset grayout variables.
 * */
function grayOut(vis, options) {
    // Pass true to gray out screen, false to ungray
    // options are optional.  This is a JSON object with the following (optional) properties
    // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
    // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
    // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
    // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
    // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
    // in any order.  Pass only the properties you need to set.
    var options = options || {};
    var zindex = options.zindex || 65536;
    var opacity = options.opacity || 50;
    var opaque = (opacity / 100);
    var bgcolor = options.bgcolor || '#000000';
    var dark = document.getElementById('darkenScreenObject');
    if (!dark) {
        // The dark layer doesn't exist, it's never been created.  So we'll
        // create it here and apply some basic styles.
        // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
        var tbody = document.getElementsByTagName("body")[0];
        var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position = 'absolute';                 // Position absolutely
        tnode.style.top = '0px';                           // In the top
        tnode.style.left = '0px';                          // Left corner of the page
        tnode.style.overflow = 'hidden';                   // Try to avoid making scroll bars            
        tnode.style.display = 'none';                      // Start out Hidden
        tnode.id = 'darkenScreenObject';                   // Name it so we can find it later
        tbody.appendChild(tnode);                            // Add it to the web page
        dark = document.getElementById('darkenScreenObject');  // Get the object.
    }
    if (vis) {
        // Calculate the page width and height 
        if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
            var pageWidth = document.body.scrollWidth + 'px';
            var pageHeight = document.body.scrollHeight + 'px';
        } else if (document.body.offsetWidth) {
            var pageWidth = document.body.offsetWidth + 'px';
            var pageHeight = document.body.offsetHeight + 'px';
        } else {
            var pageWidth = '100%';
            var pageHeight = '100%';
        }
        //set the shader to cover the entire page and make it visible.
        dark.style.opacity = opaque;
        dark.style.MozOpacity = opaque;
        dark.style.filter = 'alpha(opacity=' + opacity + ')';
        dark.style.zIndex = zindex;
        dark.style.backgroundColor = bgcolor;
        dark.style.width = pageWidth;
        dark.style.height = (isBrowserIE() ? pageHeight : '100%');
        dark.style.display = 'block';
    } else {
        dark.style.display = 'none';
    }
}
/* Returns a boolean indicating whether the browser is IE-based or not.
 * */
function isBrowserIE() {
    // Get TargetId depending on browser type
    return navigator.appName.match("Internet Explorer");
}
/* Returns a control by a given Id.
 * */
function getObjById(id) {
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];

    return returnVar;
}
/* Returns a control by a given Name.
 * */
function getObjByName(name) {
    var objFullName = 'document.aspnetForm.' + name;
    var obj = eval(objFullName);
    return obj;
}
