





var ASCENDING = "ascending";
var DESCENDING = "descending";





function Select(bSorted, bAscend)
{


this.innerHTML		= "";
this.outerHTML		= "";
this.Attributes		= "";
this.Selected;
this.OnChange;
this.Disabled		= false;
this.ID				= "";
this.TabIndex		= "";
this.Sorted			= bSorted;
this.Ascend			= bAscend;



this.AddEmptyOption		= _addEmptyOption;
this.AddOption			= _addOption;
this.AddOptionHtml		= _addOptionHtml;
this.AddOptionGroup		= _addOptionGroup;
this.AddAttribute		= _addAttribute;
this.Render				= _render;



var _optionGroup = new OptionGroup("", bSorted, bAscend, false);
var OptionGroupArray  = new Array();
OptionGroupArray.push(_optionGroup);



function _addEmptyOption()
{
_optionGroup.AddEmptyOption();
}



function _addOptionHtml(sOptionHtml)
{
_optionGroup.AddOptionHtml(sOptionHtml);
}



function _addOption(sOptionText, sOptionValue, sExpandos)
{
_optionGroup.AddOption(sOptionText, sOptionValue, sExpandos);
}



function _addOptionGroup(oOptionGroup)
{
OptionGroupArray.push(oOptionGroup);
}



function _addAttribute(sAttributeName, sAttributeValue)
{
this.Attributes	   += " " + sAttributeName + "=\"" + CrmEncodeDecode.CrmHtmlAttributeEncode(sAttributeValue) + "\" ";
}



function _render()
{


if (this.TabIndex != "")
{
this.AddAttribute("TabIndex", this.TabIndex);
}
if (this.Disabled)
{
this.AddAttribute("disabled", "true");
}
if (this.Sorted)
{
if (this.Ascend)
{
this.AddAttribute("Sort", ASCENDING);
}
else
{
this.AddAttribute("Sort", DESCENDING);
}
}
this.AddAttribute("id", this.ID);
this.AddAttribute("name", this.ID);
this.AddAttribute("class", "esms-SelectBox");

if (!IsNull(this.OnChange) && this.OnChange != "")
{
this.AddAttribute("onchange", this.OnChange);
}

if (!IsNull(this.Selected))
{
this.AddAttribute("defaultSelected", this.Selected);
}



this.outerHTML = "<select ";
this.outerHTML += this.Attributes;
this.outerHTML += ">";



for (index in OptionGroupArray)
{
var oGroup = OptionGroupArray[index];



oGroup.Selected = this.Selected;
this.innerHTML += oGroup.Render();
}

this.outerHTML += this.innerHTML;



this.outerHTML += "</select>";

return this.outerHTML;
}
}











function OptionGroup(sLabel, bSorted, bAscend, bDisplayGrouping)
{



this.innerHTML		= "";
this.outerHTML		= "";
this.Attributes		= "";
this.Selected;
this.Label			= sLabel;
this.Sorted			= IsNull(bSorted) ? false : bSorted;
this.Ascend			= IsNull(bAscend) ? false : bAscend;
this.ID				= "";



this.DisplayGrouping = IsNull(bDisplayGrouping) ? true : bDisplayGrouping;



this.AddEmptyOption		= _addEmptyOption;
this.AddOption			= _addOption;
this.AddOptionHtml		= _addOptionHtml;
this.AddAttribute		= _addAttribute;
this.Render				= _render;



var arrayOptions		= new Array();



function _addEmptyOption(sExpandos)
{
_addOption("","", sExpandos);
}




function _addOption(sOptionText, sOptionValue, sExpandos)
{
var oOption			= new Option();
oOption.Text		= sOptionText;
oOption.Value		= sOptionValue;
oOption.Expandos	= sExpandos;
arrayOptions.push(oOption);
}



function _addAttribute(sAttributeName, sAttributeValue)
{
this.Attributes	   += " " + sAttributeName + "=\"" + CrmEncodeDecode.CrmHtmlAttributeEncode(sAttributeValue) + "\" ";
}





function _addOptionHtml(sOptionHtml)
{
this.innerHTML += sOptionHtml;
}

function _render()
{


if (this.DisplayGrouping)
{

this.AddAttribute("id", this.ID);
this.AddAttribute("label", this.Label);


if (this.Sorted)
{
if (this.Ascend)
{
this.AddAttribute("Sort", ASCENDING);
}
else
{
this.AddAttribute("Sort", DESCENDING);
}
}

this.outerHTML = "<optgroup "
this.outerHTML += this.Attributes;
this.outerHTML +=">";
}



if (this.Sorted)
{
arrayOptions.sort(this.Ascend ? ascendingComparator : descendingComparator);
}



for (index in arrayOptions)
{
var oOption = arrayOptions[index];



if (!IsNull(this.Selected) && oOption.Value == this.Selected)
{
oOption.Selected = true;
}

this.innerHTML += oOption.Render();
}

this.outerHTML += this.innerHTML;

if (this.DisplayGrouping)
{
this.outerHTML += "</optgroup>";
}

return this.outerHTML;
}



function ascendingComparator(oLHS, oRHS)
{


return oLHS.Text.localeCompare(oRHS.Text);
}



function descendingComparator(oLHS, oRHS)
{
return oRHS.Text.localeCompare(oLHS.Text);
}
}



function Option()
{
this.Selected		= false;
this.Text			= "";
this.Value			= "";
this.outerHTML		= "";
this.Title          = "";
this.Expandos		= "";

this.Render			= _render;

function _render()
{
this.outerHTML = "<option value=\"";
this.outerHTML += CrmEncodeDecode.CrmHtmlAttributeEncode(this.Value);
this.outerHTML += "\" ";

this.outerHTML += "title=\"";



this.outerHTML += ( this.Title.length == 0 ?  CrmEncodeDecode.CrmHtmlAttributeEncode( this.Text) :  CrmEncodeDecode.CrmHtmlAttributeEncode( this.Title ));
this.outerHTML += "\" ";

if (this.Selected)
{
this.outerHTML += "SELECTED ";
}
if (!IsNull(this.Expandos))
{
this.outerHTML += this.Expandos;
}
this.outerHTML += ">";
this.outerHTML += CrmEncodeDecode.CrmHtmlEncode(this.Text);
this.outerHTML += "</option>";

return this.outerHTML;
}
}