﻿function createCookie(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 readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function initialiseFontSizeSwitcher()
{
    var i;
    var element;
    var links;
    var selected;

    /*
    * This line ensures that the code is only executed if the browser in which the site is being loaded supports the Javascript
    * Document Object Model.
    */
    if (document.getElementById)
    {
        /*
        * This code assigns to a variable a reference to the 'switcher' ul and then grabs any anchors inside it to initialise the
        * font size switcher controls.
        */
        element = document.getElementById('switcher');
        links = element.getElementsByTagName('a');
        
        /*
        * This code checks to see if the text size cookie has been set. If not then it sets the default value.
        * If the cookie has been set then it changes the page to the set value
        */
        var textSize = readCookie("Homecare.PageTextSize");
        if (textSize == null) {
            createCookie("Homecare.PageTextSize", "small", 90);
            // Set selected to the first of the buttons. This tells the site to use the default font size.
            selected = links[0];
        } else {
            document.body.className = textSize;
            switch (textSize) {
                case "small":
                    selected = links[0];
                    break;
                case "medium":
                    selected = links[1];
                    break;
                case "large":
                    selected = links[2];
                    break;
            }
        }

        // Iterate through the links and set them up.
        for(i = 0; i < links.length; i++)
        {
            // Assign to the onclick attribute of each link a function to do the actual switching
            links[i].onclick = function() {
                // Flip the background of the currently selected button so that it appears deselected.
                selected.style.backgroundPosition = 'left top';

                /*
                * Set the class of the body element to the class of the clicked button. This is the bit that actually
                * causes the change in font size.
                */
                //alert(this.className)
                document.body.className = this.className;

                // set cookie
                createCookie("Homecare.PageTextSize", this.className, 90);
                
                // Flip the background of the clicked button so that it appears selected.
                this.style.backgroundPosition = 'left bottom';

                /*
                * Set selected to the button which was clicked so that next time this script runs, the whole process still
                * works.
                */
                selected = this;
            }
        }
        // Set the background position of the default control so that it appears selected
        selected.style.backgroundPosition = 'left bottom';

    }
}
/*
* Function:	initialiseSite()
* Description:	This function triggers the initialisation of the Javascript code which is common to all pages.
*/
function initialiseSite() {
    initialiseFontSizeSwitcher();
}

// This line tells the browser to run initialiseSite once the page has been loaded.
window.onload = initialiseSite;

// Sends through Google Tracking code and brings up print window
function printOrderComplete() {
    pageTracker._trackPageview('/ordercomplete/print');
    window.print()
}
