
MultiOptionControl.InputTypes = 
{
	Checkbox : "CHECKBOX",
	Radio: "RADIO"
}

// OBJECT

function MultiOptionControl(oCtlOrConstructorArguments) {
	GarbageHeap.Add(this);
	Object.Inherits(this, WebControl);
   
	if (oCtlOrConstructorArguments == null || oCtlOrConstructorArguments == undefined || typeof (oCtlOrConstructorArguments) != 'object')
		throw new Error('Control element or constructor arguments object not passed to MultiOptionControl()');

	var oCtl = null;
	if (oCtlOrConstructorArguments.tagName != undefined) {
		// Old style - Pre rendered in ASP
		oCtl = oCtlOrConstructorArguments;

		if (oCtl.ObjectType != "MULTIOPTIONCONTROL") {
			throw new Error('invalid control element passed to new MultiOptionControl()');
		}

		this.Control = oCtl;
	}
	else {
		var ConstructorArguments = oCtlOrConstructorArguments;

		var sName = ConstructorArguments.Name;
		var oParent = ConstructorArguments.Parent;

		if (sName == undefined)
			throw new Error('No name defined for MultiOptionControl');

		if (oParent == undefined) oParent = document.body;

		if (sName.substr(0, 3) != "ctl") sName = 'ctl' + sName;

		oCtl = document.createElement("div");
		oCtl.setAttribute("id", sName);
		oCtl.setAttribute("name", sName);
		oCtl.style.overflow = "visible";

		oCtl.unselectable = "on";
		oCtl.ObjectType = "MULTIOPTIONCONTROL";
		oCtl.className = "MultiOption";


		if (oParent != null) oParent.appendChild(oCtl);
		this.Control = oCtl;

		this.Render(ConstructorArguments);
	}


	this.Control.JSControl = this;

	this.Count = MOC_CountInputRows(oCtl);
	
	this.Show = function() { SetControlInvisible(oCtl, false); }
		
	this.Hide = function() { SetControlInvisible(oCtl, true); }
	

	this.Disable = function() { 
		
	
		switch(this.Disable.arguments.length)
		{
			case 0: 
				SetControlDisable(oCtl); 
				break;
			
			case 1:
				SetControlDisable(oCtl, this.Disable.arguments[0]); 
				break;
			
			default:
				alert('unsupported amount of arguments passed to control[' + oCtl.name + '] :: Disable()');
				break;
		}
	}
	
	this.Enable = function() {
		SetControlDisable(oCtl, false);
	}
	
	
	this.Captions = function () {
		var targRow = this.Rows(this.Captions.arguments[0]);
	
		if (this.Captions.arguments.length > 1) {
			// set caption
			if (targRow == null || targRow == undefined) return false;
			
			targRow.children[1].innerText = this.Captions.arguments[1];
			
			return true;
			
		} else {
			// get caption
			if (targRow == null || targRow == undefined) return "";
			
			return targRow.children[1].innerText;
		}
	
	} 	
	
	this.Rows = function(Index) { return MOC_GetRow(oCtl, this.Rows.arguments[0]); }
	
	this.Inputs = function (Index) { return MOC_GetInput(oCtl, this.Inputs.arguments[0]); }
	
	this.SelectedIndex = function() { 
		
		if (this.SelectedIndex.arguments.length) {
			// Set selected index
			return MOC_SetSelectedIndex(oCtl, this.SelectedIndex.arguments[0]);
		} else {
			// get selected index
			return MOC_SelectedIndex(oCtl); 
		}
	}
	
	
	this.SelectedValue = function() {
		
		if (this.SelectedValue.arguments.length) {
			// set selected value
			return MOC_SetSelectedValue(oCtl, this.SelectedValue.arguments);
		} else {
			// get selected values
			return MOC_GetSelectedValue(oCtl);
		}
	}
	
	
	this.SetInput = function (Index, Val) { return MOC_SetInput(oCtl, this.SetInput.arguments[0], this.SetInput.arguments[1]) }
	
	this.AddRowEvent = function(eventName, eventPointer) { return MOC_AddRowEvent(oCtl, this.AddRowEvent.arguments[0], this.AddRowEvent.arguments[1]); }
	
	this.AddInputEvent = function(eventName, eventPointer) { return MOC_AddInputEvent(oCtl, this.AddInputEvent.arguments[0], this.AddInputEvent.arguments[1]); }
}
MultiOptionControl.prototype.Render = MOC_Render;


function MOC_Render(ConstructorArguments) {
	var Width = ConstructorArguments.Width;
	var Height = ConstructorArguments.Height;
	var Left = ConstructorArguments.Left;
	var Top = ConstructorArguments.Top;
	var zIndex = ConstructorArguments.zIndex;
	var Position = ConstructorArguments.Position;
	var Options = ConstructorArguments.Options;

	var ApplyScaling = ConstructorArguments.ApplyScaling;
	var HideOnNoCaption = ConstructorArguments.HideOnNoCaption;
	var HideRowOnNoCaption = ConstructorArguments.HideRowOnNoCaption;
	var RowCaption = ConstructorArguments.RowCaption;

	var TabNumber = ConstructorArguments.TabNumber;
	var Style = ConstructorArguments.Style;
	if (Style == undefined) Style = WebControl.Styles.Standard;

	var Name = ConstructorArguments.Name;

	var InputType = ConstructorArguments.InputType;
	if (InputType == undefined)
		InputType = MultiOptionControl.InputTypes.Checkbox;

	var Caption = ConstructorArguments.Caption;
	if (Caption == undefined) Caption = '';
	if (typeof (Caption) != 'string')
		throw new Error('Caption must be a string value');

	var TabIndex = ConstructorArguments.TabIndex;

	var Disabled = ConstructorArguments.Disabled;
	if (Disabled == undefined) Disabled = false;
	if (typeof (Disabled) != 'boolean')
		throw new Error('Disabled must be a boolean value');

	var Visible = ConstructorArguments.Visible;
	if (Visible == undefined) Visible = true;
	if (typeof (Visible) != 'boolean')
		throw new Error('Visible must be a boolean value');



	if (Top == undefined) Top = 0;
	if (isNaN(Top))
		throw new Error("Top must be a number");

	if (Left == undefined) Left = 0;
	if (isNaN(Left))
		throw new Error("Left must be a number");

	if (Position == undefined) Position = "relative";

	if (Width == undefined) Width = 200;
 
	this.Control.style.width = Width;
	this.Control.style.top = Top;
	this.Control.style.left = Left;
	this.Control.style.position = Position;
	this.Control.VisualStyle = Style.toString();
	

	if (zIndex == undefined) zIndex = 1;
	if (Position == undefined) Position = "absolute";
	if (InputType == undefined) InputType = TextControl.InputTypes.Text;
	if (TabNumber == undefined) TabNumber = "";

	this.Control.ControlType = InputType;


	if (HideOnNoCaption == undefined) HideOnNoCaption = false;
	if (typeof (HideOnNoCaption) != "boolean")
		throw new Error("HideOnNoCaption must be a boolean");

	if (HideRowOnNoCaption == undefined) HideRowOnNoCaption = false;
	if (typeof (HideRowOnNoCaption) != "boolean")
		throw new Error("HideRowOnNoCaption must be a boolean");

	RowCaption = RowCaption == undefined || RowCaption ? "block" : "none";

	if (HideOnNoCaption && Caption.length == 0) {
		Visible = false;
	}

	var sStyleOption = '';
	var sStyleCaption = '';
	switch (Style) {
		case WebControl.Styles.Glass:
		case "4":
		case "Vista":
			sStyleCaption = "ctlMultiOptionCaptionNormalVista";
			sStyleOption = "ctlMultiOptionNormalVista";
			break;

		case WebControl.Styles.XP:
		case "2":
		case "XP":
			sStyleCaption = "ctlMultiOptionCaptionNormalXP";
			sStyleOption = "ctlMultiOptionNormalXP";
			break;

		case WebControl.Styles.Standard:
		default:
			sStyleCaption = "ctlMultiOptionCaptionNormal";
			sStyleOption = "ctlMultiOptionNormal";
	}

	if (Options == null) {
		throw new Error("Options argument has not been specified");
		// Expect array of { Caption (text), Value (text), Checked (bool), Disabled (bool)} 
	}

	this.Control.OptionCount = Options.length;

	this.Control.onmouseover = function () { MOC_CtlMouseOver(this); };
	this.Control.onmouseout = function () { MOC_CtlMouseOut(this); };

	var ctlHasCaption = Caption == undefined || Caption.toString().length == 0 ? "none" : "block";
	var rowStyle = '';
 
	var a = new Array();
	var idx = 0;
	a[idx++] = '<table class="' + sStyleOption + '" cellpadding=0 cellspacing=1 width=100%>';
	a[idx++] = '    <tr style="display: ' + ctlHasCaption + '">';
	a[idx++] = '        <td class="' + sStyleCaption + '" width=100% unselectable="on" colspan=2 ondblclick="javascript: MOC_CtlCaptionDoubleClick(this);">' + escape(Caption) + '</td>';
	a[idx++] = '    </tr>';

	for (var i = 0; i < Options.length; i++) {
		if (Options[i].Caption.toString().length == 0 && HideRowOnNoCaption) {
			rowStyle = "none";
		}
		else {
			rowStyle = "block";
		}
   
		a[idx++] = '<tr style="display: ' + rowStyle + ';" onmouseover="javascript: MOC_InputRowMouseOver(this);" onmouseout="javascript: MOC_InputRowMouseOut(this);" onmouseup="javascript: MOC_InputRowMouseUp(this); EventCollector(this, \'ROW_MOUSEUP\', \'MULTIOPTIONCONTROL\');" onmousedown="javascript: EventCollector(this, \'ROW_MOUSEDOWN\', \'MULTIOPTIONCONTROL\');">';
		a[idx++] = '    <td width="0"><input type="' + InputType + '" value="' + escape(Options[i].Value) + '" class="' + sStyleOption + '" ' + (Options[i].Checked ? 'checked' : '') + ' name="' + Name + '" id="' + Name + '"  ' + (Options[i].Disabled ? 'disabled' : '') + '  onmouseup="javascript: MOC_InputMouseup();" onclick="MOC_InputChanged();" onchange="MOC_InputChanged();" onfocus="javascript: MOC_InputRowMouseOver(this,\'focus\');" onblur="javascript: MOC_InputRowMouseOut(this,\'unfocus\');" onpropertychange="EventCollector(this, \'INPUT_PROPERTYCHANGE\', \'MULTIOPTIONCONTROL\');" /></td>';
		a[idx++] = '    <td width="100%" class="' + sStyleOption + '" unselectable="on" style="display : ' + RowCaption + '">' + Options[i].Caption + '</td>';
		a[idx++] = '</tr>';
	}
	 
	a[idx++] = '</table>';

	var s = a.join("");
	
	this.Control.innerHTML = s;
}


function MOC_SetSelectedIndex(oCtl, Index) {
	var oRows = oCtl.children[0].children[0];
	
	if (Index > oRows.children.length + 1) return false;
	
	oRows.children[Index + 1].children[0].children[0].checked = true;
	oRows.children[Index + 1].children[0].fireEvent('onclick');			// fire event for the row
	return true;
}



function MOC_SetSelectedValue(oCtl, Values) {
	//Values = array of values to test against
	
	var oRows = oCtl.children[0].children[0];
	var cType = oCtl.ControlType;
	var cReturn = "";
	
	if (Values.length == 1) Values = Values[0].split(",");

	for (var i = 1; i < oRows.children.length; i++) { oRows.children[i].children[0].children[0].checked = false; } 
		
	for (var i = 1; i < oRows.children.length; i++)
	{
		for (var j = 0; j < Values.length; j++) {
			//alert(oRows.children[i].children[0].children[0].value + " : " + Values[j]);
		
			if (oRows.children[i].children[0].children[0].value == Values[j]) 
			{
				if (cType == "RADIO") {
					oRows.children[i].children[0].children[0].checked = true;				// return first selected input
					return;
				} else {
					oRows.children[i].children[0].children[0].checked = true;
				}
			}
		}
	}
	
	return;
}


function MOC_GetSelectedValue(oCtl) {

	var oRows = oCtl.children[0].children[0];
	var cType = oCtl.ControlType;
	var cReturn = "";
	
	for (var i = 1; i < oRows.children.length; i++)
	{
		if (oRows.children[i].children[0].children[0].checked) 
		{
			if (cType == "RADIO") {
				return oRows.children[i].children[0].children[0].value;							// return first selected input
			} else {
				cReturn = cReturn + "," + oRows.children[i].children[0].children[0].value;		// Gav changed i to (i-1)
			}
			
		}
	}
	
	if (cReturn.length) {
		return cReturn.substr(1);
	} else {
		return "";													// no input selected, return -1
	}
	
}


function MOC_SelectedIndex(oCtl) {
	var oRows = oCtl.children[0].children[0];
	var cType = oCtl.ControlType;
	var cReturn = "";
	
	for (var i = 1; i < oRows.children.length; i++)
	{
		if (oRows.children[i].children[0].children[0].checked) 
		{
			if (cType == "RADIO") {
				return i-1;												// return first selected input
			} else {
				cReturn = cReturn + "," + (i-1);			// Gav changed i to (i-1)
			}
			
		}
	}
	
	if (cReturn.length) {
		return cReturn.substr(1);
	} else {
		return -1;													// no input selected, return -1
	}
}


function MOC_SetInput(oCtl, Index, val) {
	var oRows = oCtl.children[0].children[0];
	
	if ((Index + 1) < oRows.length) return false;
	
	oRows.children[Index + 1].children[0].children[0].checked = val;
	 
	return true;
}


function MOC_GetInput(oCtl, Index) {
	var oRows = oCtl.children[0].children[0];
	
	if ((Index + 1) < oRows.length) return "";
	
	return oRows.children[Index + 1].children[0].children[0];
}


function MOC_GetRow(oCtl, Index) {
	var oRows = oCtl.children[0].children[0];
	
	if ((Index + 1) < oRows.length) return "";
	
	return oRows.children[(Index + 1)];
}


function MOC_CountInputRows(oCtl) {
	var oRows = oCtl.children[0].children[0];

	return oRows.length - 1;	// input count is the amount of rows minus the caption.	
}



function MOC_AddRowEvent(oCtl, evtName, evtPointer) {
	var oRows = oCtl.children[0].children[0];

	for (var i = 1; i < oRows.children.length; i++) 
	{
		oRows.children[i].attachEvent(evtName, evtPointer);
	}
	
}


function MOC_AddInputEvent(oCtl, evtName, evtPointer) {
	var oRows = oCtl.children[0].children[0];

	for (var i = 1; i < oRows.children.length; i++) 
	{
		oRows.children[i].children[0].children[0].attachEvent(evtName, evtPointer);
	}
}


function MOC_CtlMouseOver(oCtl) {
	var oCaption;
	var oInput;
	var oInputCol;
	var oFuncBtn;
	
	var InputColClass;

	oCaption = oCtl.children[0].children[0].children[0].children[0];
	oInput = oCtl.children[0].children[0].children[1].children[0].children[0];		

	var sStyle = '';
	switch (oCtl.VisualStyle)
	{
		case "4":
			sStyle = "VISTA";
			break;
		case "3":
			sStyle = "XP";
			break; 
		case "1":
		default:
			sStyle = "";
			break;
	}

	oCtl.children[0].className = "ctlMultiOptionOver" + sStyle;
}


function MOC_CtlMouseOut(oCtl) {
	var oCaption;
	var oInput;
	var oInputCol;
	var oFuncBtn;
	var InputColClass;

	oCaption = oCtl.children[0].children[0].children[0].children[0];
	oInput = oCtl.children[0].children[0].children[1].children[0].children[0];
	
	try {
		if (document.activeElement == oInput) {
			return;
		} else {
			// nothing
		}
	} catch (e) {
		return;
	}
	
	var sStyle = '';
	switch (oCtl.VisualStyle)
	{
		case "4":
			sStyle = "VISTA";
			break;
		case "3":
			sStyle = "XP";
			break; 
		case "1":
		default:
			sStyle = "";
			break;
	}
	
	oCtl.children[0].className = "ctlMultiOptionNormal" + sStyle;
}




function MOC_InputRowMouseOver(oInputRow) {
	var oCtl; 
	var oInput;
	var oInputCaption;

	if (MOC_InputRowMouseOver.arguments.length > 1)
	{
		oCtl = oInputRow.offsetParent.offsetParent.offsetParent;
		oInputRow = oInputRow.parentElement.parentElement;
	} else {
		oCtl = oInputRow.offsetParent.offsetParent;
	}

	oInput = oInputRow.children[0].children[0];
	oInputCaption = oInputRow.children[1];

	MOC_CtlMouseOver(oCtl);

	var sStyle = '';
	switch (oCtl.VisualStyle)
	{
		case "4":
			sStyle = "VISTA";
			break;
		case "3":
			sStyle = "XP";
			break; 
		case "1":
		default:
			sStyle = "";
			break;
	}
	
	oInput.className = "ctlMultiOptionOver" + sStyle;
	oInputCaption.className = "ctlMultiOptionOver" + sStyle;
}
	
	
	
function MOC_InputRowMouseOut(oInputRow) {
	var oCtl;
	var oInput;
	var oInputCaption;
	
	if (MOC_InputRowMouseOut.arguments.length > 1)
	{
		oCtl = oInputRow.offsetParent.offsetParent.offsetParent;
		oInputRow = oInputRow.parentElement.parentElement;
	} else {
		oCtl = oInputRow.offsetParent.offsetParent;
	}
	
	oInput = oInputRow.children[0].children[0];
	oInputCaption = oInputRow.children[1];
	
	MOC_CtlMouseOut(oCtl);
	
	var sStyle = '';
	switch (oCtl.VisualStyle)
	{
		case "4":
			sStyle = "VISTA";
			break;
		case "3":
			sStyle = "XP";
			break; 
		case "1":
		default:
			sStyle = "";
			break;
	}
	
	oInput.className = "ctlMultiOptionNormal" + sStyle;
	oInputCaption.className = "ctlMultiOptionNormal" + sStyle;
}



function MOC_InputRowMouseUp(oInputRow) {
	var oCtl = oInputRow.offsetParent.offsetParent;

	if (oCtl.disabled) return;
	
	var oInput = oInputRow.children[0].children[0];

	if (oInput.disabled) return;

	switch (oCtl.ControlType.toUpperCase())
	{
		case "CHECKBOX":
			if (oInput.checked == true) { oInput.checked = false } else { oInput.checked = true } 
			break; 
			
		case "RADIO":
			oInput.checked = true;
			break;
	}

	oInput.fireEvent('onchange');	
}



function MOC_CtlCaptionDoubleClick(oCaption) {
	return;
}

function MOC_InputChanged()
{
	var srcElement = window.event.srcElement;
	var oJSCtl = GetParentJSControl(srcElement);
	if (oJSCtl == null) return;

	oJSCtl.Events.RaiseEvent("INPUT_CHANGE", srcElement);
}

function MOC_InputMouseup()
{
	var srcElement = window.event.srcElement;
	window.event.cancelBubble = true;     
	return true;
}

