var __dropdownbox_version=1;

///<Documentation>
///<Class name="DropDownBox">
function DropDownBox(id,caption,options,required)
{
	this.Constructor(id,caption,options,required);
}

///<Constructor>
///<summary>DropDownBox constructor</summary>
///<param name="id" type="string" required="false">
///     The id to be assigned to the HTML control generated from the Render()
///     function. If this is not provided, than a GUID is automatically assigned.
///</param>
///<param name="caption" type="string" required="false">
///     The name to be displayed as a caption for the DropDownBox
///</param>
///<param name="text" type="string" required="false">
///     The value to be entered into the DropDownBox
///</param>
DropDownBox.prototype.Constructor = function(id, caption, options, required)
{
    if (!__base_version || __base_version < 1)
        new Exception("DropDownBox", "Constructor", "The DropDownBox object requires the inclusion of the base.js file.").Throw();
    if (!__string_version || __string_version < 1)
        new Exception("DropDownBox", "Constructor", "The DropDownBox object requires the inclusion of the string.js file.").Throw();

    var ddb = $(id + '_obj');
    if (ddb)
        ddb = eval(ddb.value);

    this.ID = id ? id : __getGUID();
    this.Control = this.ID + '_box';
    this.Caption = caption ? caption : ddb ? ddb.Caption : '';
    this.Disabled = ddb ? ddb.Disabled : false;
    this.IsRequired = required ? required : ddb ? ddb.IsRequired : false;
    this.Type = 'DropDownBox';
    this.Sort = ddb ? ddb.Sort : true;
    this.Options = options ? options : ddb ? ddb.Options : new Array();
    this.OnChange = ddb ? ddb.OnChange : null;
}
///</Constructor>

///<Methods>
///<Methods>
///<Method name="getSelectedValue">
///<summary>Returns the value of the currently selected option</summary>
///<returns>String</returns>
DropDownBox.prototype.Clear = function()
{
    var dd = $(this.Control);
    if (!dd) return;

    var parent = dd.parentNode;
    var clone = dd.cloneNode(false);
    parent.replaceChild(clone, dd);
    this.Control = $(this.Control);
}
///</Method>

///<Method name="getSelectedValue">
///<summary>Returns the value of the currently selected option</summary>
///<returns>String</returns>
DropDownBox.prototype.GetSelectedValue = function()
{
    var dd = $(this.Control);
    if (dd && dd.options && dd.options.length > 0)
        return dd.options[dd.selectedIndex].value;
    return '';
}
///</Method>

///<Method name="Render">
///<summary>Renders the textbox</summary>
///<returns>HTML object</returns>
DropDownBox.prototype.Render = function()
{
    var dd = document.createElement("table"),
		row = dd.insertRow(),
		caption = row.insertCell(),
		drop = row.insertCell(),
		box = document.createElement("select"),
		obj = document.createElement("<INPUT Type='hidden'>");

    dd.id = this.ID;

    obj.id = this.ID + '_obj';
    obj.value = '(' + JSONstring.make(this) + ')';

    box.id = this.ID + '_box';
    box.className = "dropdownbox_box";

    if (this.IsValid)
        box.onblur = 'new ' + this.Type + '(\'' + this.ID + '\').IsValid();';
    if (this.OnChange)
        box.onchange = this.OnChange;

    if (this.Sort)
        this.Options.sort(dropdownbox_sortText);

    var lastGroup = null
    optGroup = null;
    for (var i = 0; i < this.Options.length; i++)
    {
        var group = this.Options[i].group;
        if (group && group != lastGroup)
        {
            if (lastGroup && optGroup)
                box.appendChild(optGroup);

            optGroup = document.createElement("OPTGROUP");
            optGroup.label = group;
            lastGroup = group;
        }

        var opt = document.createElement("OPTION");
        opt.value = this.Options[i].value;
        opt.innerHTML = this.Options[i].text;
        opt.title = this.Options[i].text;
        opt.selected = this.Options[i].selected;

        if (optGroup)
            optGroup.appendChild(opt);
        else
            box.appendChild(opt);
    }

    caption.className = 'dropdownbox_caption';
    caption.innerHTML = this.Caption;

    drop.appendChild(box);
    drop.appendChild(obj);
    dd.disabled = this.Disabled;

    return dd;
} 
///</Method>

///<Method name="AddOption">
///<summary>Adds an option to the listbox</summary>
///<param name="value" type="string" required="false">
///     Used to fill in the value of the listbox. This will be submitted on
///     a form post if the option is selected.
///</param>
///<param name="text" type="string" required="false">
///     This value will be shown to the user in the dropdown box but not 
///     submitted on a form post.
///</param>
///<param name="selected" type="boolean" required="false">
///     This value determines if this option will be the default selected item.
///     If more that one has a value of "true" than the last value will be selected.
///</param>
///<param name="group" type="string" required="false">
///     This value allows the use of option groups. If assigned a value, the text will
///     be displayed under a given group.
///</param>
///<returns>Null</returns>
DropDownBox.prototype.AddOption = function(value, text, selected, group)
{
    this.Options.push({ "value": value, "text": text, "selected": (selected ? selected : false), "group": group });

    var dd = $(this.Control)
    if (dd)
    {
        var opt = document.createElement("OPTION");
        opt.value = value;
        opt.innerHTML = text;
        opt.title = text;

        if (selected) opt.selected = true;
        dd.appendChild(opt);
    }
} 
///</Method>

///<EventMethod name="textbox_IsMasked">
dropdownbox_sortText=function(a,b)
{
	if(a.group!=b.group) return 0;
	if(!a.text && !b.text) return 0;
	if(!a.text) return -1;
	if(!b.text) return 1;
	
	var aText=a.text.toLowerCase(),
		bText=b.text.toLowerCase();
	if(aText<bText) return -1;
	if(aText>bText) return 1;
	return 0;
}
///</EventMethod>

///<EventMethod name="textbox_IsMasked">
dropdownbox_sortGroup=function(a,b)
{
	if(!a.group && !b.group) return 0;
	if(!a.group) return -1;
	if(!b.group) return 1;
	
	var aGroup=a.group.toLowerCase(),
		bGroup=b.group.toLowerCase();
	if(aGroup<bGroup) return -1;
	if(aGroup>bGroup) return 1;
	return 0;
}
///</EventMethod>
///</Methods>
///</Class>


///<UnitTests>
try
{
	if(__JSUnit_version>0)
	{
		
		__jsUnit.AddTest('DropDownBox','Render','Tests the Render function of the dropdown object.','positive',
			function()
			{
				var	dd= new DropDownBox();
				dd.Caption='Test Drop Down';
				dd.AddOption(1,'Yes',true);
				dd.AddOption(0,'No');
				
				return new JSUnitTestResult(dd.Render().outerHTML.replace(/[\r\n]/g,''),'',true,dd.ID);
			});
		__jsUnit.AddTest('DropDownBox','Render','Tests the Render function of the dropdown constructor.','positive',
			function()
			{
				var	dd= new DropDownBox('test','Test Drop Down',[{"value":"Yes","text":"Yes","selected":false,"group":null},{"value":"No","text":"No","selected":true,"group":null}]);
				
				return new JSUnitTestResult(dd.Render().outerHTML.replace(/[\r\n]/g,''),'',true,dd.ID);
			});
	}
}
catch(err){}
///</UnitTests>
///</Documentation>
