﻿
//-------------------------------------------------------------------------------------------------------------------------------------
//
//Barry Fogarty 9 Jan 2009
//
//The following functions are required site-wide to power the PDF Basket popup and to pass the required information (Page Url and Title)
//
//-------------------------------------------------------------------------------------------------------------------------------------


//Add Page - gets a reference to the document url and title to pass to the popup
function addPageToBasket() {
    var PageTitle = formatPageTitle(document.title);
    var PageUrl = formatUrl(document.URL);
    amendCookie(PageUrl, PageTitle);
    toggleDocToolBar('PageAdded');
    
    //debug:
    //alert("Page added to your report.");
    //alert(getCookie('pBasket'));
}

//Add Pages - accepts 2 arrays containing the doc titles and urls to add to PDF basket.
function addPagesToBasket(arrUrls, arrTitles) {
    var pBasket = getCookie('pBasket');
    //loop pages and add to cookie
    for (var i = 0; i < arrUrls.length; i++) {
        var tmpUrl = formatUrl(arrUrls[i]);
        var tmpTitle = formatPageTitle(arrTitles[i]);
        amendCookie(tmpUrl, tmpTitle);
    }
    toggleDocToolBar('PageAdded');

    //debug:
    //alert("Page added to your report.");
    //alert(getCookie('pBasket'));
}

function amendCookie(PageUrl, PageTitle) {
    
    //Add using a JS Cookie if it does not already exist in cookie
    var pBasket = getCookie('pBasket');
    if (pBasket.indexOf(PageUrl) < 0) {
        pBasket += PageUrl + '|' + PageTitle + '*';
        setCookie('pBasket', pBasket, '');
        //Recalculate pdf basket page counter
        updatePageCounter();
    }
    //debug:
    //alert("Page added to your report.");
    //alert(getCookie('pBasket'));
}

//increments pdf basket page counter if a valid page has been added
function updatePageCounter() {
    var pBasketPageCount = getCookie('pBasketPageCount');
    pBasketPageCount = parseInt(pBasketPageCount);
    if (isNaN(pBasketPageCount)) {
        pBasketPageCount = 1;
    }
    else {
        pBasketPageCount++;
    }
    setCookie('pBasketPageCount', pBasketPageCount, '');
    displayPrintBasketPageCount(pBasketPageCount);
    
    //debug:
    //alert(getCookie('pBasketPageCount'));
}

//Checks to see if current page is in print basket
//Returns value of pBasketPageCount and enables the counter view if > 0
function checkPrintBasket() {
    var PageUrl = formatUrl(document.URL);
    var pBasket = getCookie('pBasket');
    var pBasketPageCount = getCookie('pBasketPageCount');
    if (pBasket.indexOf(PageUrl) != -1) {
        toggleDocToolBar('PageAdded');
    }
    if (parseInt(pBasketPageCount) > 0) {
        displayPrintBasketPageCount(pBasketPageCount);
    }
}

//Switch display mode of the doc toolbar if user has added current page to basket
function toggleDocToolBar(mode) {
    if (document.getElementById('doc-tools') != null) {
        if (mode == 'PageAdded') {
            document.getElementById('liAddToPrintBasket').style.display = 'none';
            document.getElementById('liPageAdded').style.display = '';
        }
    }
}

//Show PrintBasketPageCount element with the correct number of pages displayed
function displayPrintBasketPageCount(pBasketPageCount) {
    if (document.getElementById('divPrintBasketPageCount') != null) {
        var pageWord = (pBasketPageCount == 1) ? ' page' : ' pages';
        document.getElementById('divPrintBasketPageCount').innerHTML = pBasketPageCount + pageWord + " in My Report";
        document.getElementById('divPrintBasketPageCount').style.display = '';
    }
}


//Check if page is requested in PDF Basket Mode (sets the print stylesheet as the screen stylesheet if pdfMode querystring element = true)
//Allows PDF Converter to apply the print stylesheet so it can style the page correctly for its output (hide nav elements etc)
function checkMode() {
    var qs = new Querystring();
    if (qs.contains("pdfMode")) {
        //enable the print stylesheet
        var pdfStyleSheet = document.getElementById('pdfStyleSheet');
        pdfStyleSheet.media = 'screen';
    }
    else {
        checkPrintBasket(); //avoid adding another body onload call to all documents - check print basket contents here instead.
    }
}

//-------------------------------------------------------------------------------------------------------------------------------------
//
//Barry Fogarty 20 Feb 2009
//
//Helper functions for the above scripts : String formatting
//
//-------------------------------------------------------------------------------------------------------------------------------------

function formatUrl(theURL) {
    //Replace the ServerAddress value below with the IP/Port address of the gsk.com server:
    var ServerAddress = 'http://10.5.22.33:31102/';
    
    if (theURL.indexOf('http://www.gsk.com/') != -1)
        theURL = theURL.replace('http://www.gsk.com/', ServerAddress);
    if (theURL.indexOf('http://gsk.com/') != -1)
        theURL = theURL.replace('http://gsk.com/', ServerAddress);
    if (theURL.indexOf('#') != -1)
        theURL = theURL.replace('#', '');
    return theURL;
}

function formatPageTitle(theString) {
    var stringCommonTitle = '- Responsibility - GlaxoSmithKline';
    if (theString.indexOf(stringCommonTitle) != -1)
        theString = theString.replace(stringCommonTitle, '');
    if (theString.indexOf(';') != -1)
        theString = theString.replace(';', '');
    return theString;
}


//-------------------------------------------------------------------------------------------------------------------------------------
//
//Barry Fogarty 9 Jan 2009
//
//Helper functions for the above scripts : Querystring methods
//
//-------------------------------------------------------------------------------------------------------------------------------------


/* Client-side access to querystring name=value pairs
Version 1.3
28 May 2008
	
License (Simplified BSD):
http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
//
//Barry Fogarty 12 Feb 2009
//
//Cookie management functions - could be modularised into its own JS or common?
//
//-------------------------------------------------------------------------------------------------------------------------------------


function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function eraseCookie(name) {
    setCookie(name, "", -1);
}

