﻿///<reference path="CliPropConfig6.js" />

//--------------------------------------------------------------------------------
function CliAttachOnclickEventHandler( element, callback )
{
    ///<summary>Adds an onclick event handler to an element.  The callback
    ///method is called with the element clicked.</summary>
    ///<param name="element">element to attach element to</param>
    ///<param name="callback">function to call. called with element that was
    ///actually clicked</param>
    
    var clickHandler = function( evt )
    {
        try
        {
            evt.cancelBubble = true;
            return callback( evt.srcElement );
        }
        catch( e )
        {
            CliDisplayError();// errorAction, errorCode );
            //CliDebug( "CliAttachOnclickEventHandler-->eventhandler " + e.message );
        }
    }
    
    element.attachEvent( "onclick", clickHandler );
}

//--------------------------------------------------------------------------------
function CliBasicTrim( value, length )
{
    ///<summary>If the string is longer than length, return a shorter version
    ///terminated with ellipsis, else return null</summary>
    ///<param name="value">string to trim</param>
    ///<param name="length">maximum number of characters</param>

    if( value.length > length )
    {
        return value.substring( 0, length - 1 ) + "...";
    }
    else
    {
        return null;
    }
}

//--------------------------------------------------------------------------------
function CliUrlEncode( string )
{
    ///<summary>converts a string to a URL encoded string </summary>
    ///<param name="url">The string to encode</param>
    
    var notEncoded = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";
    var HexChars = "0123456789ABCDEF";

    var encodedString = "";    
    for (var i = 0; i < string.length; i++ ) 
    {
        var character = string.charAt(i);
        if (notEncoded.indexOf(character) != -1) 
        {
            encodedString += character;
        }
        else 
        {
            var characterCode = character.charCodeAt(0);
            if (characterCode > 255) 
            {
                throw ("String out of 8 bit range to encode into URL" );
            }
            else 
            {
	            encodedString += "%";
	            encodedString += HexChars.charAt((characterCode >> 4) & 0xF);
	            encodedString += HexChars.charAt(characterCode & 0xF);
            }
        }
    } 
    return encodedString
}


// ----------------------------------------------------------------------------
function CliHasClassName( objElement, strClass )
{
    ///<summary>returns boolean indicating whether the object has the class name
    ///built with the understanding that there may be multiple classes</summary>
    ///<param name="objElement">element to manipulate</param>
    ///<param name="strClass">class name to remove</param>

    if ( objElement.className )
    {
        // the classes are just a space separated list, so first get the list
        var arrList = objElement.className.split( ' ' );

        // get uppercase class for comparison purposes
        var strClassUpper = strClass.toUpperCase();

        // find all instances and remove them
        for( var i = 0; i < arrList.length; i++ )
        {
            // if class found
            if ( arrList[i].toUpperCase() == strClassUpper )
            {
                // we found it
                return true;
            }
        }
    }

    // if we got here then the class name is not there
    return false;
}


// ----------------------------------------------------------------------------
function CliAddClassName( objElement, strClass )
{
    ///<summary>add a class from the class attribute of a DOM element
    ///built with the understanding that there may be multiple classes</summary>
    ///<param name="objElement">element to manipulate</param>
    ///<param name="strClass">class name to remove</param>

    // if there is a class
    if ( objElement.className )
    {
        // the classes are just a space separated list, so first get the list
        var arrList = objElement.className.split( ' ' );

        // get uppercase class for comparison purposes
        var strClassUpper = strClass.toUpperCase();

        // find all instances and remove them
        for( var i = 0; i < arrList.length; i++ )
        {
            // if class found
            if( arrList[i].toUpperCase() == strClassUpper )
            {
                // remove array item
                arrList.splice( i, 1 );

                // decrement loop counter as we have adjusted the array's contents
                i--;
            }
        }

        // add the new class to end of list
        arrList[arrList.length] = strClass;

        // assign modified class name attribute
        objElement.className = arrList.join( ' ' );
    }
    else // if there was no class
    {
        // assign modified class name attribute      
        objElement.className = strClass;
    }
}//


// ----------------------------------------------------------------------------
function CliRemoveClassName( objElement, strClass )
{
    ///<summary>removes a class from the class attribute of a DOM element
    ///built with the understanding that there may be multiple classes</summary>
    ///<param name="objElement">element to manipulate</param>
    ///<param name="strClass">class name to remove</param>

    // if there is a class
    if ( objElement.className )
    {
        // the classes are just a space separated list, so first get the list
        var arrList = objElement.className.split( ' ' );

        // get uppercase class for comparison purposes
        var strClassUpper = strClass.toUpperCase();

        // find all instances and remove them
        for ( var i = 0; i < arrList.length; i++ )
        {
            // if class found
            if ( arrList[i].toUpperCase() == strClassUpper )
            {
                // remove array item
                arrList.splice( i, 1 );

                // decrement loop counter as we have adjusted the array's contents
                i--;
            }
        }

        // assign modified class name attribute
        objElement.className = arrList.join(' ');
    }
}
