// this script requires: 
// "http://www.ansakimg.com/assets/js/functions.common.js"
// to function 

// declare a global  XMLHTTP Request object
var xmlHttpObj = Array;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function createXMLHttpObj(u)
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		xmlHttpObj[u] = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlHttpObj[u] = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			xmlHttpObj[u] = '';
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!xmlHttpObj[u] && typeof XMLHttpRequest != "undefined") 
	{
		xmlHttpObj[u] = new XMLHttpRequest();
	}
}

// main function to be called to populate dimensions drop down
function populateDimensionDropDown(u,eleID,category,dimensionName,selectedValue,searchRefs)
{
	// show loading state
	xmlDropDownLoadingState(eleID);
	
	// append '&' at the beginning of 'searchRefs' if not found
	if(searchRefs != null)
	{
		if(searchRefs.charAt(0) != '&')
		{
			searchRefs = "&" + searchRefs;
		}
	}
	else
	{
		searchRefs = '';
	}
	
	// url of page that will send xml data back to client browser
	var requestUrl = "/ansakimg.com/assets/scripts/dddp/search_dimensions/data_provider.php" + "?e=" + encodeURIComponent(eleID) + "&c=" + encodeURIComponent(category) + "&d=" + encodeURIComponent(dimensionName) + "&s=" + encodeURIComponent(selectedValue) + searchRefs;
	
	createXMLHttpObj(u);
	
	// verify xmlHttpObj[u] variable was successfully initialized
	if(xmlHttpObj[u])
	{
		// this function called when state of xmlHttpObj[u] changes; indicates data has been received from the server
		var stateChangeFunction = 'function stateChangeHandler' + u + '(){if(xmlHttpObj[' + u + '].readyState == 4){if(xmlHttpObj[' + u + '].status == 200){xmlPopulateDropDown(xmlHttpObj[' + u + '].responseXML.documentElement);}else{}}}';
		eval(stateChangeFunction);
		
		// when the state of the xmlHttpObj[u] changes - e.g.receiving data back from the server
		xmlHttpObj[u].onreadystatechange = eval("stateChangeHandler" + u);
		// define the iteraction with the server -- true for as asynchronous.
		xmlHttpObj[u].open("GET", requestUrl,  true);
		// send request to server, null arg  when using "GET"
		xmlHttpObj[u].send(null);		
	}
}

// populate dimension drop down
function xmlPopulateDropDown(selectMenuNode)
{
	var eleID = selectMenuNode.getAttribute("id");
	var ele = document.getElementById(eleID);
	
	// clear drop down menu
	clearDropDown(ele,1);
	
	var selectMenuChildNodes = selectMenuNode.getElementsByTagName('option');
	var optionItem, text, value, selectedValue, selected;
	
	// populate the dropdown list with xml data
	for (var i = 0; i < selectMenuChildNodes.length; i++)
	{
   		text = getInnerText(selectMenuChildNodes[i]);// option text
		value = selectMenuChildNodes[i].getAttribute("value");// option value
		selectedValue = selectMenuChildNodes[i].getAttribute("selected");// selected value
		// select option
		selected = false;
		if(selectedValue == "selected")
			selected = true;
		// declare & create option
		optionItem = new Option(text, value, false, selected);
		ele.options[ele.length] = optionItem;
	}
}

// show select menu loading state
function xmlDropDownLoadingState(e)
{
	var e = document.getElementById(e);
	
	// clear drop down menu; keep first option
	clearDropDown(e,1);
	
	// add selected loading option
	var optionItem = new Option("Loading...", "", false, true);// 4th arg is for selected
	e.options[e.length] = optionItem;
	e.selectedIndex = e.length-1;// redundant
	
	// previous version:
	// change/select first option text
	// e.options[0].text = "Loading...";
	// e.selectedIndex = 0;
}

// returns the node text value 
function getInnerText(node)
{
	 return (node.textContent || node.innerText || node.text) ;
}