﻿var __RC__theFormPostCollection = null;
var __RC__theFormPostData = null;

/// <summary>
/// Returns the correct element using the given ID
/// </summary>
/// <param name="objectId">Object ID</param>
function $( objectId )
{
    return document.getElementById( objectId );
} // function $
/*
/// <summary>
/// Forces the ASP.NET Web Page to Initialize the
/// Call Back even if has already been initialized.
/// It sets the form post data and collection to 
/// empty values first.
/// </summary>
function ASPNET_InitCallback()
{
    
    IMPORTANT NOTE (ODD BEHAVIOR): By some unknown reason (so far), when we call the WebForm_InitCallback method
                                   and then we try to call the server back by using the ICallbackHandler way it stops it 
                                   execution without any javascript error or any other information (we have tryied by
                                   checking the debug console of the browser and nothing happens). So basically we get
                                   stock after the WebForm_InitCallback method call.
                                   What happens is that when we call our CallBackServer method it just does nothing and
                                   keeps waiting for a response, but it never arrives.
                                   The solution was to store temporally the current __theFormPostCollection and __theFormPostData
                                   values, callback the server and finally re-set the __theFormPostCollection and __theFormPostData 
                                   with the old values.
    
    if ( window.__theFormPostCollection && window.__theFormPostData && window.WebForm_InitCallback )
    {
        __theFormPostCollection.length = 0;
        __theFormPostData = '';

        WebForm_InitCallback();
    }
    

    if ( window.__theFormPostCollection && window.__theFormPostData && window.WebForm_InitCallback ) 
    {
        var nameValueObj = null;

        //Save current values
        __RC__theFormPostCollection = __theFormPostCollection;
        __RC__theFormPostData = __theFormPostData;

        //get the current viewstate
        nameValueObj = new Object();
        nameValueObj.name = '__VIEWSTATE';
        nameValueObj.value = $('__VIEWSTATE').value;

        //set only the viewstate
        __theFormPostData = WebForm_EncodeCallback(nameValueObj.name) + '=' + WebForm_EncodeCallback(nameValueObj.value) + '&';
        __theFormPostCollection[0] = nameValueObj;
    }
} // ASPNET_InitCallback

/// <summary>
/// Post Call Back event
/// </summary>
function ASPNET_PostCallback()
{
    if ( __RC__theFormPostCollection != null && __RC__theFormPostData != null )
    {
        //set values
        __theFormPostCollection = __RC__theFormPostCollection;
        __theFormPostData = __RC__theFormPostData;
        
        //clear saved values
        __RC__theFormPostCollection = null;
        __RC__theFormPostData = null;
    }
} // ASPNET_PostCallback
*/

/// <summary>
/// Build a new action string with the correct syntax.
/// Parameter Syntax: '!ActionName[ActionValues]'
/// </summary>
/// <param name="actionName">Action Name</param>
/// <param name="actionValues">Action Values</param>
function RicherComponents_BuildActionString( actionName, actionValues )
{
    var actionString = '';
    
    actionString += '!';
    actionString += actionName;
    actionString += '[';
    actionString += actionValues;
    actionString += ']';
    
    return actionString;
} // RicherComponents_BuildActionString

/// <summary>
/// Build a new parameter string with the correct syntax.
/// Parameter Syntax: '@ParamName{ParamValue}'
/// </summary>
/// <param name="paramName">Parameter Name</param>
/// <param name="paramValue">Parameter Value</param>
/// <param name="encode">Encode the string?</param>
function RicherComponents_BuildParameterString( paramName, paramValue, encode )
{
    var parameterString = '';
    
    if ( encode )
        paramValue = RicherComponents_EncodeString( paramValue );
    
    parameterString += '@';
    parameterString += paramName;
    parameterString += '{';
    parameterString += paramValue;
    parameterString += '}';
    
    return parameterString;
} // RicherComponents_BuildParameterString

/// <summary>
/// Encodes the given value
/// </summary>
/// <param name="valueToEncode">Value to encode</param>
function RicherComponents_EncodeString( valueToEncode )
{
    var encodedValue = valueToEncode;

    encodedValue = encodedValue.replace(/\+/g,'+00');
    encodedValue = encodedValue.replace(/\$/g,'+01');
    encodedValue = encodedValue.replace(/\-/g,'+02');
    encodedValue = encodedValue.replace(/\=/g,'+03');
    encodedValue = encodedValue.replace(/\//g,'+04');
    encodedValue = encodedValue.replace(/\\/g,'+05');
    encodedValue = encodedValue.replace(/\?/g,'+06');
    encodedValue = encodedValue.replace(/\</g,'+07');
    encodedValue = encodedValue.replace(/\>/g,'+08');
    encodedValue = encodedValue.replace(/\~/g,'+09');
    encodedValue = encodedValue.replace(/\&/g,'+10');
    encodedValue = encodedValue.replace(/\#/g,'+11');
    encodedValue = encodedValue.replace(/\^/g,'+12');
    encodedValue = encodedValue.replace(/\%/g,'+13');
    encodedValue = encodedValue.replace(/\}/g,'+14');
    encodedValue = encodedValue.replace(/\{/g,'+15');
    encodedValue = encodedValue.replace(/\]/g,'+16');
    encodedValue = encodedValue.replace(/\[/g,'+17');
    encodedValue = encodedValue.replace(/\@/g,'+18');
    encodedValue = encodedValue.replace(/\"/g,'+19');
    encodedValue = encodedValue.replace(/\:/g,'+20');
    encodedValue = encodedValue.replace(/\;/g,'+21');
    encodedValue = encodedValue.replace(/\'/g,'+22');
    encodedValue = encodedValue.replace(/\|/g,'+23');
    
    return encodedValue;
} // RicherComponents_EncodeString

/// <summary>
/// Decode the HTML string
/// </summary>
/// <param name="valueToDecode">Value to decode</param>
function RicherComponents_DecodeHTML( valueToDecode )
{
    var decodedValue = valueToDecode;

    decodedValue = decodedValue.replace(/&amp;/g,'&');
    decodedValue = decodedValue.replace(/&lt;/g,'<');
    decodedValue = decodedValue.replace(/&gt;/g,'>');
    decodedValue = decodedValue.replace(/&quot;/g,'"');
    
    return decodedValue;
} // RicherComponents_DecodeHTML

/// <summary>
/// Returns the height of the given element
/// </summary>
function GetHeight( element )
{
    var height = 0;
    
    height = element.clientHeight;
    if ( ! height || height == 0 )
        height = element.offsetHeight;
        
    return height;
} // GetHeight

/// <summary>
/// Returns the width of the given element
/// </summary>
function GetWidth( element )
{
    var width = 0;
    
    width = element.clientWidth;
    if ( ! width || width == 0 )
        width = element.offsetWidth;
        
    return width;
} // GetWidth

/****************************************
*               Date
*****************************************/

/// <summary>
/// To String
/// </summary>
Date.prototype.ToString = function() 
{
    var toString = '';
    
    toString += this.getDate();
    toString += '/';
    toString += (this.getMonth() + 1);
    toString += '/';
    toString += this.getFullYear();
    
    return toString;
} // ToString