﻿var querystring = getQueryString();

if (querystring['Print'] || querystring['print'] == "True") {
    // Disable all styleSheets (media=none)
    if (document.styleSheets.length > 0) {
        for (i = 0; i < document.styleSheets.length; i++) {
            var oSheet = document.styleSheets[i];
            if (typeof (oSheet.media) == 'string') {
                try {
                    oSheet.media = 'none';
                }
                catch (e) {
                }
            }
            else {
                oSheet.media.mediaText = 'none';
            }
        }
    }

    if (document.createStyleSheet) {
        // IE - add print stylesheet (media=screen + print)
        document.createStyleSheet('/Style Library/SPW/Print.css');
    }
    else {
        // FireFox - add print stylesheet (media=screen + print)
        var newSS = document.createElement('link');
        newSS.rel = 'stylesheet';
        newSS.type = "text/css";
        newSS.href = "/Style Library/SPW/Print.css";
        document.getElementsByTagName("head")[0].appendChild(newSS);
    }

    print();
}

function openPrintPreview() {
    var url = document.location;

    if (!querystring) {
        url += "?Print=True";
    }
    else {
        url += "&Print=True";
    }
    window.open(url);
}

function getQueryString() {
    var querystring = new Array;

    // parse current url into an array with the keys/values
    var q = String(document.location).split('?')[1];
    if (!q) return false;
    q = q.split('&');

    for (var i = 0; i < q.length; i++) {
        // for each key/value, split them at the '='
        // and add them to the qerystring array
        var o = q[i].split('=');
        querystring[o[0]] = o[1];
    }

    // return the querystring
    return querystring;
}