//If the srh namespace object is not defined, create it.
if ( typeof ( srh ) == "undefined" )
{ srh = {}; }

//If the srh.common namespace object is not defined, create it.
if ( typeof ( srh.common ) == "undefined" )
{
    srh.common = {

        getJSON: function ()
        {
            try
            {
                // Test to see if browser has built in JSON support 
                if ( typeof ( window.top.JSON ) == "undefined" )
                {
                    if ( typeof ( window.JSON ) == "undefined" )
                    {
                        // Load the JSON WebResource library
                        var JSONPath = Xrm.Page.context.getServerUrl() + "/WebResources/srh_/businessbanking/json.js";
                        var request = new XMLHttpRequest();
                        request.open( "GET", JSONPath, false );
                        request.send( null );
                        eval( request.responseText );
                        return JSON;
                    }
                    else
                    {
                        return window.JSON;
                    }
                }
                else
                {
                    // Use the built in JSON support
                    return window.top.JSON;
                }
            }
            catch ( ex )
            {
                alert( "srh.common.getJSON() Exception: " + ex.Message );
                return null;
            }
        },

        getJQuery: function ()
        {
            try
            {
                // Test to see if browser has built in JQuery support 
                if ( typeof ( window.top.JQuery ) == "undefined" )
                {
                    if ( typeof ( window.JQuery ) == "undefined" )
                    {
                        // Load the JQuery WebResource library
                        var JQueryPath = Xrm.Page.context.getServerUrl() + "/WebResources/srh_/businessbanking/jquery.js";
                        var request = new XMLHttpRequest();
                        request.open( "GET", JQueryPath, false );
                        request.send( null );
                        eval( request.responseText );
                        return $;
                    }
                    else
                    {
                        return window.JQuery;
                    }
                }
                else
                {
                    // Use the built in JSON support
                    return window.top.JQuery;
                }
            }
            catch ( ex )
            {
                alert( "srh.common.getJQuery() Exception: " + ex.Message );
                return null;
            }
        }

    }
}

//If the srh.common.form namespace object is not defined, create it.
if ( typeof ( srh.common.form ) == "undefined" )
{
    srh.common.form = {

        // Check that this is a valid execution context object by testing that it has a getEventSource() method
        isValidContext: function ( context )
        {
            if ( ( context != "undefined" ) && ( context != null ) && ( context.getEventSource ) )
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

//If the srh.common.form namespace object is not defined, create it.
if ( typeof ( srh.common.form.attribute ) == "undefined" )
{
    srh.common.form.attribute = {

        // Check that this is a valid String attribute
        isValidBoolean: function ( attributeName )
        {
            if ( this.isValidAttribute( attributeName ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( attribute.getAttributeType() == "boolean" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid String attribute
        isValidDateTime: function ( attributeName )
        {
            if ( this.isValidAttribute( attributeName ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( attribute.getAttributeType() == "datetime" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid String attribute
        isValidDecimal: function ( attributeName )
        {
            if ( this.isValidAttribute( attributeName ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( attribute.getAttributeType() == "decimal" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid String attribute
        isValidDouble: function ( attributeName )
        {
            if ( this.isValidAttribute( attributeName ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( attribute.getAttributeType() == "double" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid String attribute
        isValidInteger: function ( attributeName )
        {
            if ( this.isValidAttribute( attributeName ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( attribute.getAttributeType() == "integer" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid String attribute
        isValidLookup: function ( attributeName )
        {
            if ( this.isValidAttribute( attributeName ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( attribute.getAttributeType() == "lookup" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid String attribute
        isValidMemo: function ( attributeName )
        {
            if ( this.isValidAttribute( attributeName ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( attribute.getAttributeType() == "memo" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid String attribute
        isValidMoney: function ( attributeName )
        {
            if ( this.isValidAttribute( attributeName ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( attribute.getAttributeType() == "money" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid String attribute
        isValidOptionSet: function ( attributeName )
        {
            if ( this.isValidAttribute( attributeName ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( attribute.getAttributeType() == "optionset" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid String attribute
        isValidString: function ( attributeName )
        {
            if ( this.isValidAttribute( attributeName ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( attribute.getAttributeType() == "string" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid attribute
        isValidAttribute: function ( attributeName )
        {
            if ( ( attributeName != "undefined" ) && ( attributeName != null ) && ( typeof ( attributeName ) == "string" ) )
            {
                var attribute = Xrm.Page.data.entity.attributes.get( attributeName );
                if ( ( attribute != "undefined" ) && ( attribute != null ) )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}

//If the srh.common.form namespace object is not defined, create it.
if ( typeof ( srh.common.form.control ) == "undefined" )
{
    srh.common.form.control = {

        // Check that this is a valid IFrame control
        isValidIFrame: function ( controlName )
        {
            if ( this.isValidControl( controlName ) )
            {
                var control = Xrm.Page.ui.controls.get( controlName );
                if ( control.getControlType() == "iframe" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        },

        // Check that this is a valid control
        isValidControl: function ( controlName )
        {
            if ( ( controlName != "undefined" ) && ( controlName != null ) && ( typeof ( controlName ) == "string" ) )
            {
                var control = Xrm.Page.ui.controls.get( controlName );
                if ( ( control != "undefined" ) && ( control != null ) )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}