//-----------------------------------------
var aCreativeShowcaseSearchIDs = new Array();

//-----------------------------------------
function CreativeShowcaseSearch_AttachLoadEvent(sectionguid)
{
	aCreativeShowcaseSearchIDs[aCreativeShowcaseSearchIDs.length] = sectionguid;
	CrossBrowser_AttachLoadEvent(CreativeShowcaseSearch_OnLoad);
}

//-----------------------------------------
function CreativeShowcaseSearch_OnLoad()
{
	for (var i=0; i < aCreativeShowcaseSearchIDs.length; i++)
	{
		CreativeShowcaseSearch_init(aCreativeShowcaseSearchIDs[i])
	}
}

//-----------------------------------------
function CreativeShowcaseSearch_init(sectionguid)
{
	CreativeShowcaseSearch_GetSearchControl(sectionguid,1)
}

//-----------------------------------------
function CreativeShowcaseSearch_GetSearchControl(sectionguid,position)
{
	position=parseInt(position);
	var nextposition=position+1;
	var SearchControlDiv=document.getElementById("CS_SearchControl_"+sectionguid+"_"+position);

	if(SearchControlDiv)
	{
		var AttributeID=SearchControlDiv.getAttribute("Adv_CS_AttributeID");
		var OptionTypeCode=SearchControlDiv.getAttribute("OptionTypeCode");
		var url = MarketBaseURL + "/pages/CreativeShowcaseSearch_GetSearchControl.aspx?CS_SectionGUID=" + sectionguid + "&AttributeID=" + AttributeID + "&OptionTypeCode=" + OptionTypeCode + "&nextposition=" + nextposition + "&position=" + position;
		var postxml=CreativeShowcaseSearch_GetSearchControlPostXML(sectionguid,position);
		var html=CrossBrowser_GetHTML(url,postxml);
		var select=document.getElementById("CS_SearchOption_"+sectionguid+"_"+position);

	    var prevValue=SearchControlDiv.getAttribute("CS_SearchOption"+position);
	    SearchControlDiv.setAttribute("CS_SearchOption"+position,"");
		//retain the value they had selected for this select
		if(select&&prevValue=="")
		{
			prevValue=select.options[select.selectedIndex].getAttribute("optionvalue");
		}
		SearchControlDiv.innerHTML=html;
		
		//select the value they had selected if it's still in the list
		select=document.getElementById("CS_SearchOption_"+sectionguid+"_"+position);
		for(var i=0;i<select.options.length;i++)
		{
			if(select.options[i].getAttribute("optionvalue")==prevValue)
			{
				select.options[i].selected=true;
				break;
			}	
		}
		CreativeShowcaseSearch_GetSearchControl(sectionguid,position+1)
	}
} 

//-----------------------------------------
function CreativeShowcaseSearch_GetSearchControlPostXML(sectionguid,position)
{
	//netscape always makes your elements lowercase so just make them all lowercase so it works in ie and netscape
	//careful not to create an element that is a predefined html element - you will have problems. so make all your element names custom names
	var rootxml=document.createElement("rootxml");
	var optionsxml=document.createElement("optionsxml");
	for(var i=position-1;i>0;i--)
	{
		var SearchControl=document.getElementById("CS_SearchControl_"+sectionguid+"_"+i);
		if(SearchControl)
		{
			var AttributeID=SearchControl.getAttribute("Adv_CS_AttributeID");
			var select=document.getElementById("CS_SearchOption_"+sectionguid+"_"+i);
			var optionxml=document.createElement("optionxml");
			optionxml.setAttribute("adv_attributeid",select.options[select.selectedIndex].getAttribute("Adv_AttributeID"));
			optionxml.setAttribute("optiontypecode",select.options[select.selectedIndex].getAttribute("OptionTypeCode"));
			optionxml.setAttribute("value",select.options[select.selectedIndex].getAttribute("optionvalue"));
			optionxml.setAttribute("position",select.getAttribute("position"));
			optionsxml.appendChild(optionxml);
		}
	}
	
	rootxml.appendChild(optionsxml);
	//mac safari forces the node names to upper case, windows safari forces them lower case!!
	var html=rootxml.innerHTML.replace(/rootxml/gi, "rootxml").replace(/optionsxml/gi, "optionsxml").replace(/optionxml/gi, "optionxml");
	
	return html;
} 
