﻿/****************************************
*        RCAttributeCollection
*****************************************/

/// <summary>
/// Richer Components - Attribute Collection Class
/// </summmary>
function RCAttributeCollection()
{
    this._keys = new Array();
    this._values = new Array();
}; // RCStyle

/// <summary>
/// Richer Components - Attribute Collection - Prototype
/// </summmary>
RCAttributeCollection.prototype = 
{
    /// <summary>
    /// Add a new attribute
    /// </summmary>
    /// <param name="key">Key</param>
    /// <param name="value">Value</param>
    Add : function( key, value )
    {
        this._keys[ this._keys.length ] = key;
        this._values[ this._values.length ] = value;
    }, // Add
    
    /// <summary>
    /// Returns the attribute
    /// </summmary>
    /// <param name="key">Key</param>
    Get : function( key )
    {
        var value = null;
        
        for (var i = 0; this._keys.length && value == null; i++)
            if ( this._keys[i] == key )
                value = this._values[i];
        
        return value;
    }, // Get
    
    /// <summary>
    /// Returns this instance HTML markup value
    /// </summmary>
    ToHTML : function()
    {
        var toHtml = '';
    
        for (var i = 0; i < this._keys.length; i++)
        {
            if ( i > 0 )
                toHtml += ' ';
        
            toHtml += this._keys[i];
            toHtml += '="';
            toHtml += this._values[i];
            toHtml += '"';
        }
        
        return toHtml;
    } // ToHTML
}

/****************************************
*               RCStyle
*****************************************/

// <summary>
/// Richer Components - Style Class
/// </summmary>
function RCStyle()
{
    this._keys = new Array();
    this._values = new Array();
} // RCStyle

/// <summary>
/// Richer Components - Style - Prototype
/// </summmary>
RCStyle.prototype = 
{
    /// <summary>
    /// Add a new attribute
    /// </summmary>
    /// <param name="key">Key</param>
    /// <param name="value">Value</param>
    Add : function( key, value )
    {
        this._keys[ this._keys.length ] = key;
        this._values[ this._values.length ] = value;
    }, // AddAttribute
    
    /// <summary>
    /// Returns this instance HTML markup value
    /// </summmary>
    ToHTML : function()
    {
        var toHtml = '';
    
        if ( this._keys.length > 0 )
        {
            toHtml += 'style="';
            
            for (var i = 0; i < this._keys.length; i++)
            {
                toHtml += this._keys[i];
                toHtml += ':';
                toHtml += this._values[i];
                toHtml += ';';
            }
            
            toHtml += '"';
        }
        
        return toHtml;
    } // ToHTML
}

/****************************************
*               RCHTMLObject
*****************************************/

/// <summary>
/// Richer Components - HTML Class
/// </summmary>
function RCHTMLObject( tag )
{
    this.Tag = tag;
    this.Style = new RCStyle();
    this.Attributes = new RCAttributeCollection();
    
    this.Controls = new Array();
} // RCHTMLObject

/// <summary>
/// Add a new HTML markup control to this instance
/// </summmary>
RCHTMLObject.prototype.AddControl = function( htmlControl )
{
    this.Controls[ this.Controls.length ] = htmlControl;
} // AddControl

/// <summary>
/// Returns the HTML markup for this instance
/// </summmary>
RCHTMLObject.prototype.ToHTML = function()
{
    var html = '';
    
    html += '<';
    html += this.Tag;
    html += ' ';
    html += this.Attributes.ToHTML();
    html += ' ';
    html += this.Style.ToHTML();
    html += '>';
    
    //Get the controls
    for (var i = 0; i < this.Controls.length; i++)
    {
        if ( eval( this.Controls[i].ToHTML ) )
            html += this.Controls[i].ToHTML();
        else
            html += this.Controls[i];
    }
    
    html += '</';
    html += this.Tag;
    html += '>';
    
    return html;
} // ToHTML

/****************************************
*          Global functions
*****************************************/

/// <summary>
/// CreateRadio
/// </summmary>
function RCHTMLObject_CreateRadio(name, text, value, selected)
{
    var radio = null;
    
    radio = new RCHTMLObject( 'input' );
    radio.Attributes.Add( 'type', 'radio' );
    radio.Attributes.Add( 'name', name );
    radio.Attributes.Add( 'value', value );
    
    if ( selected )
        radio.Attributes.Add( 'checked', true ); 
        
    radio.AddControl( text );
        
    return radio;
} // RCHTMLObject_CreateRadio

/// <summary>
/// CreateTextBox
/// </summmary>
function RCHTMLObject_CreateTextBox(id, text)
{
    var textBox = null;
    
    textBox = new RCHTMLObject( 'input' );
    textBox.Attributes.Add( 'type', 'textbox' );
    
    if ( text != null )
    {
        //replace double quotes by escaping it
        text = text.replace( /"/g, '&quot;');    
        textBox.Attributes.Add( 'value', text );
    }
    
    if ( id != null )
        textBox.Attributes.Add( 'id', id );
        
    return textBox;
} // RCHTMLObject_CreateTextBox

/// <summary>
/// CreateDiv
/// </summmary>
function RCHTMLObject_CreateDiv(id, className)
{
    var div = null;
    
    div = new RCHTMLObject( 'div' );
    
    if ( id ) 
        div.Attributes.Add( 'id', div );
        
    if ( className )
        div.Attributes.Add( 'class', className );
        
    return div;
} // RCHTMLObject_CreateDiv

/// <summary>
/// CreateAnchor
/// </summmary>
function RCHTMLObject_CreateAnchor(id, className, href, text)
{
    var anchor = null;
    
    anchor = new RCHTMLObject( 'a' );
    
    if ( id ) 
        anchor.Attributes.Add( 'id', id );
        
    if ( className )
        anchor.Attributes.Add( 'class', className );
        
    if ( href )
        anchor.Attributes.Add( 'href', href );
        
    if ( text )
        anchor.AddControl( text );
        
    return anchor;
} // RCHTMLObject_CreateAnchor

/// <summary>
/// CreateTable
/// </summmary>
function RCHTMLObject_CreateTable( id, cellspacing, cellpadding )
{
    var table = null;
    
    table = new RCHTMLObject( 'table' );
    table.Attributes.Add( 'border', 0 );
    
    if ( ! cellspacing )
        cellspacing = 0;
        
    if ( ! cellpadding )
        cellpadding = 0;
    
    table.Attributes.Add( 'cellspacing', cellspacing );
    table.Attributes.Add( 'cellpadding', cellpadding );
    
    if ( id )
        table.Attributes.Add( 'id', id ); 
        
    return table;
} // RCHTMLObject_CreateTable

/// <summary>
/// AddRowToTable
/// </summmary>
function RCHTMLObject_AddRowToTable( table )
{
    var row = new RCHTMLObject( 'tr' ); 
    table.AddControl( row );
    return row;
} // RCHTMLObject_AddRowToTable

/// <summary>
/// AddCellToRow
/// </summmary>
function RCHTMLObject_AddCellToRow( row )
{
    var cell = new RCHTMLObject( 'td' ); 
    row.AddControl( cell );
    return cell;
} // RCHTMLObject_AddCellToRow