﻿/*
* 2009-02 : DJM : Massive rewrite to use jQuery. Prototype is no longer required.
* Requires jQuery 1.3.1+
*/

// JSON class for generic DOM functions.
// ****************************************************************************
// Developer notes ::
// ** MDC : Initial creation.
//
var headscapeDOM = {
    //
    // Generic read cookie function.
    //
    // Developer notes ::
    // ** MDC : Initial creation.
    //
    getCookie: function(strName) {
        var aryCookies = document.cookie.split(";");
        var nameEQ = strName + "=";
        for (var i = 0; i < aryCookies.length; i++) {
            aryCookies[i] = jQuery.trim(aryCookies[i]);
            if (aryCookies[i].indexOf(nameEQ) == 0) return aryCookies[i].substring(nameEQ.length, aryCookies[i].length);
        }
        return null;
    },
    // ----------------------------------------------------------------------------

    //
    // Reads a sub cookie (URL Encoded) from a cookie.
    //
    // Developer notes ::
    // ** MDC : Initial creation.
    //
    getSubCookie: function(strName, strSubName) {
        var strReturnValue = null;
        var strFullCookie = this.getCookie(strName);
        if (strFullCookie) {
            var arySubCookies = strFullCookie.split("&");
            var nameEQ = strSubName + "=";
            for (var i = 0; i < arySubCookies.length; i++) {
                arySubCookies[i] = jQuery.trim(arySubCookies[i]);
                if (arySubCookies[i].indexOf(nameEQ) == 0) {
                    strReturnValue = arySubCookies[i].substring(nameEQ.length, arySubCookies[i].length);
                    break;
                }
            }
        }
        return strReturnValue;
    },
    // ----------------------------------------------------------------------------
    // Generic write cookie function.
    //
    // Developer notes ::
    // ** MDC : Initial creation.
    //
    setCookie: function(strName, strValue, intExpiryDays) {
        var aryCookies = document.cookie.split(";");
        var nameEQ = strName + "=";
        var strNewCookieFull = "";
        var intThisCookie = -1;
        var strExpires = "";

        if (intExpiryDays) {
            var date = new Date();
            date.setTime(date.getTime() + (intExpiryDays * 24 * 60 * 60 * 1000));
            strExpires = "; expires=" + date.toGMTString();
        }

        strNewCookieFull = nameEQ + strValue + strExpires + "; path=/";

        for (var i = 0; i < aryCookies.length; i++) {
            aryCookies[i] = jQuery.trim(aryCookies[i]);
            if (aryCookies[i].indexOf(nameEQ) == 0) {
                // We have a match
                intThisCookie = i;
                aryCookies[i] = strNewCookieFull;
                break;
            }
        }

        // Check if we need to add the cookie
        if (intThisCookie == -1) {
            aryCookies.push(strNewCookieFull);
            intThisCookie = aryCookies.length - 1;
        }

        // Write this cookie back - not the full cookie list as this is not what JS expects
        document.cookie = aryCookies[intThisCookie];
    },
    // ----------------------------------------------------------------------------

    //
    // Updates a sub cookie.
    //
    // Developer notes ::
    // ** MDC : Initial creation.
    //
    setSubCookie: function(strCookieName, strSubCookieName, strValue) {
        var strFullCookie = this.getCookie(strCookieName);
        if (strFullCookie) {
            var arySubCookies = strFullCookie.split("&");
            var nameEQ = strSubCookieName + "=";
            var strNewCookieFull = nameEQ + strValue;
            var intThisSubCookie = -1;

            for (var i = 0; i < arySubCookies.length; i++) {
                arySubCookies[i] = jQuery.trim(arySubCookies[i]);
                if (arySubCookies[i].indexOf(nameEQ) == 0) {
                    // We have a match
                    intThisSubCookie = i;
                    break;
                }
            }

            // Check if we need to add or update the cookie
            if (intThisSubCookie == -1) {
                arySubCookies.push(strNewCookieFull)
            } else {
                // Update our found cookie
                arySubCookies[intThisSubCookie] = strNewCookieFull;
            }

            // Write the full cookie back
            this.setCookie(strCookieName, arySubCookies.join("&"), 365);
        }
    },
    // ----------------------------------------------------------------------------

    isInteger: function(sInteger) {
        if (sInteger == undefined) return false;
        var deccount = 0;
        var isInt = true;
        var inputStr;
        inputStr = sInteger.toString(); // in case sInteger is not a string
        for (var i = 0; i < inputStr.length; i++) {
            var oneChar = inputStr.charAt(i);
            if (oneChar < "0" || oneChar > "9" || oneChar == ".") {
                isInt = false;
                i = inputStr.length;
            }
        }
        return isInt;
    }
}
// ****************************************************************************
