﻿//Globals:
var theHost = 'http://www.capita.co.uk/';
//var theHost = 'http://localhost:1795/';

/*window.onload = function() {
    checkMode();
}*/

/* SharePoint will call this method on page load */
_spBodyOnLoadFunctionNames.push("checkMode");

//function addPageToBasket: 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);
    //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, '');
    }
    alert("Page added to your report.");
    //debug:
    //alert(getCookie('pBasket'));
}

//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 26 Feb 2009
//
//Helper functions for the above scripts : String formatting
//
//-------------------------------------------------------------------------------------------------------------------------------------

//function formatUrl: remove unwanted chars from the URL
function formatUrl(theURL) {
    var theURL = theURL.replace(theHost, '');
    if (theURL.indexOf('#') != -1)
        theURL = theURL.replace('#', '');
    return theURL;
}
//function formatPageTitle: remove common string in page title
function formatPageTitle(theString) {
    var stringCommonTitle = '';
    if (theString.indexOf(stringCommonTitle) != -1)
        theString = theString.replace(stringCommonTitle, '');        
    return escape(theString);
}
//function formatPageTitleWithPipe: if title contains a pipe char |, only use to the left
function formatPageTitleWithPipe(theString) {
    if (PageTitle.indexOf('|') != -1) {
        var arrPageTitle = PageTitle.split('|');
        PageTitle = escape(arrPageTitle[0]);
    }
}


//-------------------------------------------------------------------------------------------------------------------------------------
//
//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 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);
}

function initPerformanceTable(){
    var qs = new Querystring();
    if (!qs.contains("pdfMode")) {
        showSpecificContent('table',1,7);
        showSpecificContent('tabMenu',2,8);
    }   
}