var mocksBasePath = 'http://www.mocks.ie';

function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
  {
    window.onload = func;
  }
  else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function urlencode (str) {
    var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };

    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();

    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

function initMocksBasePath(basePath)
{
    mocksBasePath = basePath;
}

function ajaxAddToCart(formId)
{
    if (typeof formId == "undefined")
    {
        formId = "addtocart";
    }

    var targetNode = dojo.byId("floating_shopping_cart_content");
    targetNode.innerHTML = 'Adding to cart ... please wait<br/><img src="' + mocksBasePath + '/images/loading.gif" />';
    targetNode.style.height = "500px";
    dijit.byId('floating_shopping_cart').show();
    targetNode.style.height = "auto";
    var xhrArgs = {
        form: dojo.byId(formId),
        handleAs: "text",
        load: function(data) {
            targetNode.innerHTML = data;
        },
        error: function(error) {
            //We'll 404 in the demo, but that's okay.  We don't have a 'postIt' service on the
            //docs server.
            targetNode.innerHTML = "Error Occured!";
        }
    }
    //Call the asynchronous xhrPost
    var deferred = dojo.xhrPost(xhrArgs);
}

function ajaxViewCart(viewUrl)
{
    var targetNode = dojo.byId("floating_shopping_cart_content");
    //The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
    targetNode.innerHTML = 'Loading to cart ... please wait<br/><img src="' + mocksBasePath + '/images/loading.gif" />';
    targetNode.style.height = "500px";
    dijit.byId('floating_shopping_cart').show();
    targetNode.style.height = "auto";
    var xhrArgs = {
        url: viewUrl,
        handleAs: "text",
        preventCache: true,
        load: function(data) {
            targetNode.innerHTML = data;
        },
        error: function(error) {
            targetNode.innerHTML = "An unexpected error occurred: " + error;
        }
    }

    //Call the asynchronous xhrGet
    var deferred = dojo.xhrGet(xhrArgs);
}

function ajaxDeleteItemFromCart(deleteUrl)
{
    var targetNode = dojo.byId("floating_shopping_cart_content");
    //The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
    targetNode.innerHTML = 'Updating to cart ... please wait<br/><img src="' + mocksBasePath + '/images/loading.gif" />';
    dijit.byId('floating_shopping_cart').show();
    var xhrArgs = {
        url: deleteUrl,
        handleAs: "text",
        preventCache: true,
        load: function(data) {
            targetNode.innerHTML = data;
        },
        error: function(error) {
            targetNode.innerHTML = "An unexpected error occurred: " + error;
        }
    }

    //Call the asynchronous xhrGet
    var deferred = dojo.xhrGet(xhrArgs);
}

function ajaxViewBreakDown(viewUrl)
{
    var targetNode = dojo.byId("floating_breakdown_content");
    //The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
    targetNode.innerHTML = 'Loading exam breakdown ... please wait<br/><img src="' + mocksBasePath + '/images/loading.gif" />';
    targetNode.style.height = "400px";
    targetNode.style.width = "680px";
    dijit.byId('floating_breakdown').show();
    var xhrArgs = {
        url: viewUrl,
        handleAs: "text",
        preventCache: true,
        load: function(data) {
            targetNode.innerHTML = data;
            //location.href='#top'
        },
        error: function(error) {
            targetNode.innerHTML = "An unexpected error occurred: " + error;
        }
    }

    //Call the asynchronous xhrGet
    var deferred = dojo.xhrGet(xhrArgs);
}


/*
Author: Robert Hashemian
http://www.hashemian.com/

You can use this code in any manner so long as the author's
name, Web address and this disclaimer is kept intact.
********************************************************
*/

function calcage(secs, num1, num2) {
    s = ((Math.floor(secs/num1))%num2).toString();
    if (LeadingZero && s.length < 2)
        s = "0" + s;
    return "<b>" + s + "</b>";
}

function CountBack(secs) {
    if (secs < 0) {
        document.getElementById("countdown_timer").innerHTML = FinishMessage;
        return;
    }
    DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
    DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
    DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
    DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

    document.getElementById("countdown_timer").innerHTML = DisplayStr;
    if (CountActive)
        setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}

function putspan() {
    document.write("<span id='countdown_timer'></span>");
}

