/*
***********************************************************************************************
This functions were translated from VBscript, And supprt only xml data, and no multiple chioice
***********************************************************************************************
*/

/*
=============================================================
	function: isOptionSelected

	description: TODO - write description

	input parameters:
		TODO - input parameters
=============================================================
*/
function isOptionSelected(currentValue,selectedValues) {
	if (String(selectedValues) == String(currentValue)) {
		return " Selected=\"true\" ";
	}
	else {
		return "";
	}
}

/*
=============================================================
	Function: optionTagWriter

	description: TODO - write description

	input parameters:
		TODO - input parameters
=============================================================
*/
function optionTagWriter(val, txt, selectedValues) 
{
	return 	"	<option value=\"" + val + "\"" +
			isOptionSelected(val, selectedValues)+ ">" +
			txt + "</option>";
}

/* 
=============================================================
	Function: createComboStr()

	description:
		this function can recive an XML node collection object or a Recordset object
		or a 2 dimansional array and return an HTML select box string
		if i_oOptions is an XML object i_sValElIdentifier and i_sTxtElIdentifier must be node or attribute names
		if i_oOptions is a RECORDSET object i_sValElIdentifier and i_sTxtElIdentifier can be numeric or field name
		if i_oOptions is an ARRAY object i_sValElIdentifier and i_sTxtElIdentifier must be numeric

	input parameters:
		i_oOptions				- object. an options object. either xml, array or record set
		i_sValElIdentifier		- string or integer. value element identifier
		i_sTxtElIdentifier		- string or integer. text element identifier
		i_vSelectedValues		- string or array of selected values (for multiple select)
		i_sComboName			- string. select tag's name attribute
		i_bIsMultimple			- boolean. indicates whether it is a multiple select
		i_sDefaultOptionValue	- string. default option value
		i_sDefaultOptionText	- string. default option text
		i_sComboAttributes		- string. optional select tag's attributes

	return value:
		string. HTML combo string
=============================================================
*/
function createComboStr(
		oOptions,
		sValElIdentifier,
		sTxtElIdentifier,
		vSelectedValues,
		sComboName,
		bIsMultimple,
		sDefaultOptionValue,
		sDefaultOptionText,
		sComboAttributes)
{				
	var sOptionTagsStr;
	var sDefaultOptionTag;
	var sMultipleStr = "";
	var oNode;
	
	sOptionTagsStr = "";
	
	// split i_vSelectedValues variant in to an array if this COMBO is a MULTIPLE choice
	// and the i_vSelectedValues variant contains commas ","
	if (bIsMultimple && (typeof(vSelectedValues) == "string") && (vSelectedValues.indexOf(",") != -1))
	{
		vSelectedValues = vSelectedValues.split(",");
	}
	
	// set the default option
	if (sDefaultOptionValue != "" && sDefaultOptionText != "")
	{
		sDefaultOptionTag =	optionTagWriter(sDefaultOptionValue, sDefaultOptionText, vSelectedValues);
	}
	
	// handling xml document or xml collection
	
	// loop through collection nodes
	for (var i=0;i<oOptions.length;i++) {
		// check if data in this XML node collection is in
		// attributes or child nodes
		
		// this is attribute base node collection
		
		oNode = oOptions[i];
		
		if ((oNode.attributes.length > 0) &&
			oNode.getAttribute(sValElIdentifier) &&
			oNode.getAttribute(sTxtElIdentifier))
		{
			sOptionTagsStr = sOptionTagsStr +
				optionTagWriter(
					oNode.getAttribute(sValElIdentifier),
					oNode.getAttribute(sTxtElIdentifier),
					vSelectedValues
					);
		}
		//this is childe node base node collection
		else if (oNode.childNodes.length > 0)
		{
			sOptionTagsStr = sOptionTagsStr +
				optionTagWriter(
					oNode.selectSingleNode(i_sValElIdentifier).text,
					oNode.selectSingleNode(i_sTxtElIdentifier).text,
					i_vSelectedValues);
					
		}
	}
	
	return ("\n" +
		"<select " +
			"name=\"" + sComboName + "\" " +
			"id=\"" + sComboName + "\" " +
			sMultipleStr +
			sComboAttributes + ">" + "\n" +
			sDefaultOptionTag +
			sOptionTagsStr +
		"</select>" + "\n");
}
// exapmple
/*
var oDataXML = new ActiveXObject("Microsoft.XMLDOM");
	oDataXML.async=false;
	oDataXML.loadXML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<AV_VENDORS>" +
	"<Vendor id=\"0\" name=\"AVG\" />" +
	"<Vendor id=\"1\" name=\"Symantec\" />" +
"</AV_VENDORS>");
var oOptions1 = oDataXML.getElementsByTagName("Vendor");
*/
