
// OBJECT 

function MultiOptionControl(oCtl) {
	this.Inherits(WebControl);
	
	if (oCtl == undefined || oCtl.ObjectType != "MULTIOPTIONCONTROL") 
	{
		alert("undefined control (oCtl) object passed to MultiOptionControl() constructor");
		return;
	}


	this.Control = oCtl;
	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]); }
}



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];
	
	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;
}