﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  
// File    : Common.js
// Created : Dec 07 
// Purpose : This Javascript File is loaded by the master page and will be available at all times to all pages.
//           Use it to add global JS.
//
// ToDo: This file should be compressed before launch!
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


if (typeof Salem == "undefined" || !Salem) {
    /**
     * The Salem global namespace object.  If Salem is already defined, the
     * existing Salem object will not be overwritten so that defined
     * namespaces are preserved.
     * @class Salem
     * @static
     */
    var Salem = {};
}

/**
 * Returns the namespace specified and creates it if it doesn't exist
 * <pre>
 * Salem.namespace("property.package");
 * Salem.namespace("Salem.property.package");
 * </pre>
 * Either of the above would create Salem.property, then
 * Salem.property.package
 *
 * Be careful when naming packages. Reserved words may work in some browsers
 * and not others. For instance, the following will fail in Safari:
 * <pre>
 * Salem.namespace("really.long.nested.namespace");
 * </pre>
 * This fails because "long" is a future reserved word in ECMAScript
 *
 * @method namespace
 * @static
 * @param  {String*} arguments 1-n namespaces to create 
 * @return {Object}  A reference to the last namespace object created
 */
Salem.RegisterNamespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=Salem;

        // Salem is implied, so it is ignored if it is included
        for (j=(d[0] == "Salem") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

//declare the Salem.Js.JobSites Namespace
Salem.RegisterNamespace("Salem.Js.JobSites");


//The Following Namespace and Functions Handle CSS Class adding and removing
Salem.RegisterNamespace("Salem.Css");
Salem.Css = {

    AddClassName: function(_obj, _class) {
        if (_obj != null) {
            if (_obj.className) // if there is a class
            {
                //the classes are just a space separated list, so first get the list
                var arrList = _obj.className.split(' ');

                // if the new class name may already exist in list
                Salem.Css.RemoveClassName(_obj, _class);

                // add the new class to end of list
                arrList[arrList.length] = _class;

                // add the new class to beginning of list
                //arrList.splice(0, 0, class);

                // assign modified class name attribute
                _obj.className = arrList.join(' ');

            }
            // if there was no class
            else {
                // assign modified class name attribute      
                _obj.className = _class;
            }
        }
    },

    SetClassName: function(_obj, _class) {
        if (_obj != null) {
            _obj.className = _class;
        }
    },

    RemoveClassName: function(_obj, _class) {
        var exists = false;
        if (_obj != null) {
            // if there is a class
            if (_obj.className) {
                // the classes are just a space separated list, so first get the list
                var arrList = _obj.className.split(' ');

                // get uppercase class for comparison purposes
                var classUpper = _class.toUpperCase();

                // find all instances and remove them
                for (var i = 0; i < arrList.length; i++) {
                    // if class found
                    if (arrList[i].toUpperCase() == classUpper) {
                        // remove array item
                        arrList.splice(i, 1);
                        // decrement loop counter as we have adjusted the array's contents
                        i--;
                        exists = true;
                    }
                }
                // assign modified class name attribute
                _obj.className = arrList.join(' ');
            }
        }
        return exists;
    },

    HasClassName: function(_obj, _class) {
        var ret = false;
        if (_obj != null) {
            // if there is a class
            if (_obj.className) {
                // the classes are just a space separated list, so first get the list
                var arrList = _obj.className.split(' ');

                // get uppercase class for comparison purposes
                var classUpper = _class.toUpperCase();

                // find all instances and check them
                for (var i = 0; i < arrList.length; i++) {
                    // if class found
                    if (arrList[i].toUpperCase() == classUpper) {
                        ret = true;
                    }
                }
                // assign modified class name attribute
                _obj.className = arrList.join(' ');
            }
        }
        return ret;
    },

    SwapClassName: function(_addObj, _removeObj, _class) {
        Salem.Css.AddClassName(_addObj, _class);
        Salem.Css.RemoveClassName(_removeObj, _class);
    },

    ReplaceClassName: function(_obj, _oldClass, _newClass) {
        Salem.Css.RemoveClassName(_obj, _oldClass);
        Salem.Css.AddClassName(_obj, _newClass);
    }
};

/* Salem Sound Namespace is a helper function that plays a background sound of your choice */
Salem.RegisterNamespace("Salem.Sounds");
Salem.Sounds = {
    Play: function(sound, debug) {
        var embed = document.getElementById('__SendMailSoundFrame');
        if (embed != null) {
            embed.parentNode.removeChild(embed);
        }

        embed = document.createElement('embed');
        embed.setAttribute('id', '__SendMailSoundFrame');
        embed.setAttribute('src', sound);
        embed.setAttribute('loop', 'false');
        embed.setAttribute('volume', '200');
        embed.setAttribute('autostart', 'true');
        if (debug == null || debug == false) {
            embed.setAttribute('hidden', 'true');
        }
        document.body.appendChild(embed);
    }
};

Salem.RegisterNamespace("Salem.String");
Salem.String = {

    IsNullOrEmpty: function() {
        var ret = false;
        for (var i = 0; i < arguments.length; i++) {
            if (ret == false && (arguments[i] == null || Salem.String.Trim(arguments[i].toString()).length == 0)) { ret = true; }
        }
        return ret;
    },

    Format: function(str) {
        for (var i = 1; i < arguments.length; i++) {
            var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
            var val = (Salem.String.IsNullOrEmpty(arguments[i])) ? '' : arguments[i];
            str = str.replace(re, val);
        }
        return str;
    },

    Trim: function(str) {
        return str.replace(/^\s+|\s+$/g,"");
    },
    
    LeftTrim: function(str) {
        return str.replace(/^\s+/,"");
    },
    
    RightTrim: function(str) {
	    return str.replace(/\s+$/,"");
    }

};
 
Salem.RegisterNamespace("Salem.Utils");
Salem.Utils = {

    IsValidEmail: function(email) {
        var reg = new RegExp('^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$');
        return reg.test(email);
    }

};

Salem.RegisterNamespace("Salem.Browser");
Salem.Browser = {

    GetWidth: function() {
        var w;
        if (document.innerWidth) {
            w = document.innerWidth;
        } else if (document.documentElement.clientWidth) {
            w = document.documentElement.clientWidth;
        } else if (document.body) { w = document.body.clientWidth; }
        return w;
    },

    GetHeight: function() {
        var h;
        if (document.innerHeight) {
            h = document.innerHeight;
        } else if (document.documentElement.clientHeight) {
            h = document.documentElement.clientHeight;
        } else if (document.body) { h = document.body.clientHeight; }
        return h;
    },
    
    GetElementWidth: function(_objName) {
        var w = 0;
        var _obj = document.getElementById(_objName);
        if (_obj != null && _obj.clientWidth)
        {
            w = _obj.clientWidth;
        }
        return w;
    },

    GetElementHeight: function(_objName) {
        var h = 0;
        var _obj = document.getElementById(_objName);
        if (_obj != null && _obj.clientHeight)
        {
            h = _obj.clientHeight;
        }
        return h;
    }
    
};

Salem.RegisterNamespace("Salem.Url");
Salem.Url = {

    SetHash: function(value)
    {
        //var url = document.location.href.split("#");
        document.location.hash = value;
    },

    ClearHash: function()
    {
        document.location.hash = '';
    },

    RemoveHash: function()
    {
        var url = document.location.href.split("#");
        document.location.href = url[0];
    },

    GetHashString: function()
    {
        var ret = null;
        if (document.location.href.indexOf("#") > -1)
        {
            var url = document.location.href.split("#");
            ret = url[1];
        }
        return ret;
    },

    GetHashValue: function(_key)
    {
        var ret = "";
        var hash = Salem.Url.GetHashString();
        if (hash != null)
        {
            var kvp = hash.split("&");
            for (var i = 0; i < kvp.length; i++)
            {
                var key = ''; var value = '';
                if (kvp[i].indexOf("=") > -1)
                {
                    var kv = kvp[i].split("=");
                    key = kv[0];
                    value = kv[1];
                }
                if (_key.toLowerCase() == key.toLowerCase())
                {
                    ret = value;
                    break;
                }
            }
        }
        return ret;
    }

};