﻿var util =
{
    trim: function() {
        a = this.replace(/^\s+/, '');
        return a.replace(/\s+$/, '');
    },

    replace: function(value) {
        return value.replace(/[^a-zA-Z 0-9]+/g, '').replace("'", "`");
    },

    setCookie: function(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) +
                    ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    },

    getCookie: function(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 "";
    },

    replaceSubstring: function(inputString, fromString, toString) {
        // Goes through the inputString and replaces every occurrence of fromString with toString
        var temp = inputString;
        if (fromString == "") {
            return inputString;
        }
        if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
            while (temp.indexOf(fromString) != -1) {
                var toTheLeft = temp.substring(0, temp.indexOf(fromString));
                var toTheRight = temp.substring(temp.indexOf(fromString) + fromString.length, temp.length);
                temp = toTheLeft + toString + toTheRight;
            }
        } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
            var midStrings = new Array("~", "`", "_", "^", "#");
            var midStringLen = 1;
            var midString = "";
            // Find a string that doesn't exist in the inputString to be used
            // as an "inbetween" string
            while (midString == "") {
                for (var i = 0; i < midStrings.length; i++) {
                    var tempMidString = "";
                    for (var j = 0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
                    if (fromString.indexOf(tempMidString) == -1) {
                        midString = tempMidString;
                        i = midStrings.length + 1;
                    }
                }
            } // Keep on going until we build an "inbetween" string that doesn't exist
            // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
            while (temp.indexOf(fromString) != -1) {
                var toTheLeft = temp.substring(0, temp.indexOf(fromString));
                var toTheRight = temp.substring(temp.indexOf(fromString) + fromString.length, temp.length);
                temp = toTheLeft + midString + toTheRight;
            }
            // Next, replace the "inbetween" string with the "toString"
            while (temp.indexOf(midString) != -1) {
                var toTheLeft = temp.substring(0, temp.indexOf(midString));
                var toTheRight = temp.substring(temp.indexOf(midString) + midString.length, temp.length);
                temp = toTheLeft + toString + toTheRight;
            }
        } // Ends the check to see if the string being replaced is part of the replacement string or not
        return temp; // Send the updated string back to the user
    }
}



function newImage(arg) {
    if (document.images) {
        rslt = new Image();
        rslt.src = arg;
        return rslt;
    }
}

function changeImages() {
    if (document.images && (preloadFlag == true)) {
        for (var i = 0; i < changeImages.arguments.length; i += 2) {
            document[changeImages.arguments[i]].src = changeImages.arguments[i + 1];
        }
    }
}

var preloadFlag = false;
function preloadImages() {
    if (document.images) {
        btnHome_over = newImage("/images/MasterPage/btnHome-over.jpg");
        btnAboutUs_over = newImage("/images/MasterPage/btnAboutUs-over.jpg");
        btnExpertise_over = newImage("/images/MasterPage/btnExpertise-over.jpg");
        btnDesignRequest_over = newImage("/images/MasterPage/btnDesignRequest-over.jpg");
        btnContactUs_over = newImage("/images/MasterPage/btnContactUs-over.jpg"); 
        preloadFlag = true;
    }
}