// DcomboBox.js
// Copywrite 2004 2005 Martin Krolik (martin@krolik.net)
// All rights reserved.
var arrAllComboBoxes = new Array();
var arrAllNewComboBoxes = new Array();
var pOldDComboBoxOnloadHandler = null;
var pOldDComboBoxResizeHandler = null;
pOldDComboBoxOnloadHandler = window.onload;
pOldDComboBoxResizeHandler = window.onresize;
onload =  DComboBoxOnloadHandler;
onresize = DComboBoxResizeHandler;
var magicNumber = 4;
function ComboInit(BaseName)
{
	var objA, objB, offset;
	offset = 0;
	if (navigator.appName.indexOf("Mozilla")>=0) offset = -1;
	objA = xGetElementById(BaseName);
	objB = xGetElementById(BaseName + "DcboBox");
	if (!arrAllNewComboBoxes || !arrAllNewComboBoxes[BaseName])
		counter = 0;
	else
		counter = magicNumber;
	xMoveTo(objB, xPageX(objA, counter) + offset, xPageY(objA, counter));
	xClip(objB , 0, xWidth(objB), xHeight(objB), xWidth(objB) - 23);
	xShow(objB);
	UnSelectAnyOptions(objB);
}
function AllComboInit()
{
  for (var i = 0; i < arrAllComboBoxes.length; i++)
    {
      ComboInit(arrAllComboBoxes[i]);
    }
}
function DComboBoxOnloadHandler()
{
  if (pOldDComboBoxOnloadHandler) pOldDComboBoxOnloadHandler();
  AllComboInit();
}
function DComboBoxResizeHandler()
{
  if (pOldDComboBoxResizeHandler) pOldDComboBoxResizeHandler();
  AllComboInit();

}
function UpdateDCBoxGeneric(evt)
{
	var objA, objB, strName;
	var objEvent = new xEvent(evt);
	objB = objEvent.target;
	strName = objB.name;
	strName = strName.split("DcboBox")[0];
	objA = xGetElementById(strName);
	objA.value = objB.options[objB.selectedIndex].text;
	UnSelectAnyOptions(objB);
	//if ((! document.all) && objA && (objA.onchange)) objA.onchange();
	if (objA && (objA.onchange)) objA.onchange();
}
function UnSelectAnyOptions(selectElement)
{
	// this unselects any records
	for (var n = 0; n < selectElement.options.length; n++)
	{
		selectElement.options[n].selected = false;
	}
	if (selectElement.options.length > 0)  // this is for NS4 on X-Windows
	{
		if (document.layers)
		{
			// this is for NS4 on X-Windows
			selectElement.options[0].selected = true;
		}
		selectElement.options[0].selected = false;
	}
	selectElement.selectedIndex = -1;
	selectElement.value = null;
}
function BuildDCBox(BaseName, StartValue, Options, PixelWidth, Other, AdditionalStyle, ClassName)
{
	var DCOptions
	if (!(PixelWidth)) PixelWidth = 200;
	if (!(StartValue)) StartValue = "";
	if ((typeof Options) == "string")  { DCOptions = Options.split(",") } else { DCOptions = Options }
	var strBuild = "";
	if (document.layers)
		strBuild += "<table border=0><tr><td align=center>";
	strBuild += "<input type='text' class='"+ ClassName +"' value='" + StartValue;
	strBuild += "' name='" + BaseName + "' id='" + BaseName + "' "
	if (!(document.layers))
		strBuild += "style='width: " + (PixelWidth - 19) + "px; position: relative; " + AdditionalStyle + "' ";
	strBuild += Other + " tabIndex='2' />"
	if (document.layers)
		strBuild += "</td></tr><tr><td align=center>- Or Select -</td></tr><tr><td align=center>";
//	else
//		strBuild += "<img border='0' height='1' width='23' />";
	strBuild += "<select name='" + BaseName + "DcboBox' class='"+ ClassName +"'  id='" + BaseName ;
	strBuild += "DcboBox' "
	//alert("ComboBox name2 = " + BaseName + "DcboBox");
	if (!(document.layers))
		strBuild += "style='width: " + PixelWidth + "px; position: absolute; visibility: hidden; " + AdditionalStyle + "' ";
	strBuild += "readonly='true'  onchange='UpdateDCBoxGeneric(event); ' tabIndex='400'>" ;
	if ((DCOptions) && (DCOptions.length))
	{
		for (var cnt = 0; cnt < DCOptions.length; cnt++ )
		{
			strBuild += "<option>" + DCOptions[cnt] + "</option>";
		}
	}
	strBuild += "</select>";
	if (document.layers)
		strBuild += "</td></tr></table>";
	return (strBuild);

}
function WriteDCBox(BaseName, StartValue, Options, PixelWidth, Other, AdditionalStyle, ClassName)
{
	if (!(BaseName)) BaseName = "DefaultDComboBoxName";
	str = BuildDCBox(BaseName,StartValue,Options,PixelWidth,Other,AdditionalStyle, ClassName);
	//window.alert(str);
	document.write(str);
	setTimeout("ComboInit('" + BaseName + "');", 5);
	arrAllComboBoxes.push(BaseName);
}
function JS_in_array(objArray, elementNeedle)
{
	for(objIndex in objArray)
	{
		if(objArray[objIndex] == elementNeedle)
		{
			return true;
		}
	}
	return false;
}
function BuildDCBox2(BaseName, StartValue, Options, PixelWidth, Other, AdditionalStyle, ClassName)
{
	var DCOptions;
	var OptDCOptions;
	var TempDCOptions;
	var Group;
	var Element;
	var strBuild = "";
	var SubtDCOptions = new Array();
	var GroupArray = new Array();

	if (!(PixelWidth))
	{
		PixelWidth = 200;
	}
	if (!(StartValue))
	{
		StartValue = "";
	}
	if ((typeof Options) == "string")
	{
		DCOptions = Options.split(",");
		if ((DCOptions) && (DCOptions.length))
		{
			for (var cnt = 0; cnt < DCOptions.length; cnt++ )
			{
				subStr = DCOptions[cnt];
				if ((typeof subStr) == "string")
				{
					TempDCOptions = subStr.split(":");
					if ((TempDCOptions) && (TempDCOptions.length == 2))
					{
						Group = TempDCOptions[0];
						Element = TempDCOptions[1];
						if(!JS_in_array(GroupArray, Group))
						{
							GroupArray.push(Group);
							SubtDCOptions.push(new Array());
						}
						SubtDCOptions[GroupArray.length-1].push(Element);
					}
				}
			}
		}
	}
	else
	{
		DCOptions = Options;
	}
	if (document.layers)
	{
		strBuild += "<table border=0><tr><td align=center>";
	}
	strBuild += "<input type='text' class='"+ClassName+"' value='" + StartValue;
	strBuild += "' name='" + BaseName + "' id='" + BaseName + "' ";
	if (!(document.layers))
	{
		strBuild += "style='width: " + (PixelWidth - 19) + "px; " + AdditionalStyle + "' ";
	}
	strBuild += Other + " tabIndex='2' />";
	if (document.layers)
	{
		strBuild += "</td></tr><tr><td align=center>- Or Select -</td></tr><tr><td align=center>";
	}
	strBuild += "<select  class='"+ClassName+"' name='" + BaseName + "DcboBox' id='" + BaseName +"DcboBox' tabIndex='400' ";
	if (!(document.layers))
	{
		strBuild += "style='width: "+ PixelWidth +"px; position: absolute; visibility: hidden; " + AdditionalStyle + "' ";
	}
	strBuild += "readonly='true'  onchange='UpdateDCBoxGeneric(event);'>" ;
	if ((SubtDCOptions) && (SubtDCOptions.length))
	{
		for (var cnt = 0; cnt < SubtDCOptions.length; cnt++ )
		{
			OptDCOptions = SubtDCOptions[cnt];
			strBuild += '<OPTGROUP NAME="'+ GroupArray[cnt] +'" LABEL="'+ GroupArray[cnt] +'" CLASS="input">';
			for (var cnt2 = 0; cnt2 < OptDCOptions.length; cnt2++ )
			{
				strBuild += "<option class='input'>" + OptDCOptions[cnt2] + "</option>";
			}
			strBuild += '</OPTGROUP>';
		}
	}
	strBuild += "</select>";
	if (document.layers)
	{
		strBuild += "</td></tr></table>";
	}
	return (strBuild);

}
function WriteDCBox2(BaseName, StartValue, Options, PixelWidth, Other, AdditionalStyle, ClassName)
{
	var str;
	if (!(BaseName))
	{
		BaseName = "DefaultDComboBoxName";
	}
	str = BuildDCBox2(BaseName,StartValue,Options,PixelWidth,Other,AdditionalStyle, ClassName);
//	window.alert(str);
	document.write(str);
	setTimeout("ComboInit('" + BaseName + "');", 5);
	arrAllComboBoxes.push(BaseName);
	if (arrAllNewComboBoxes) arrAllNewComboBoxes[BaseName] = 1;
}