/**
 * listfield: field object
 * listId: id of list
 * value: value to be selected (ignored if null)
 */
function doUpdateOptionsSmileCom(fieldObj,value,oncomplete)
{
	if(fieldObj==null){return;}
	if(value==null || value==""){value=fieldObj.value;}
	if (value == null || value=="" && window[fieldObj.id+'_default']) value = window[fieldObj.id+'_default'];

	//create url
	var url = "getlistoptions.php?path="+fieldObj.name;
	if(fieldObj.alias!=null){url=url+"&alias="+fieldObj.alias;}
	if(fieldObj.getAttribute("parentpath")!=null)
	{
		var parentPaths = fieldObj.getAttribute("parentpath").split(",");
		for(var i=0;i<parentPaths.length;i++)
		{
			var parentFieldObj = document.getElementsByName(parentPaths[i])[0];
			
			if(parentFieldObj.getAttribute("ffid")!=null){url += "&thisHandlerValue="+parentFieldObj.getAttribute("ffid")+"=" + parentFieldObj.value;}
			if(parentFieldObj!=null){url=url+"&parentvalue"+i+"="+parentFieldObj.value;}
		}
	}
	//alert(url);
	var xmlRequestObj = getAjaxRequestObject();
	xmlRequestObj.onreadystatechange = 	function(){doUpdateOptionsReadySmileCom(xmlRequestObj,fieldObj,value,oncomplete);}
	xmlRequestObj.open("GET",url,true);
	xmlRequestObj.send(null);		
}

function doUpdateOptionsReadySmileCom(xmlRequestObj,fieldObj,value,oncomplete) 
{
	if(xmlRequestObj.readyState == 4 && (xmlRequestObj.status == 200 || xmlRequestObj.status == 304)) 
	{
		//alert("doUpdateOptionsReadySmileCom("+fieldObj.name+","+value+") -> "+xmlRequestObj.responseText);
		//clear current options
		while(fieldObj.length>0){fieldObj.remove(0);}
		var xml = parseXml(xmlRequestObj.responseText).documentElement;
		if(xml==null)
		{
			//alert('empty xml for '+fieldObj.name);
			return;
		}
		
		var selectedIndex = 0;
		for(var i=0;i<xml.childNodes.length;i++)
		{
			var optionNode = xml.childNodes[i];
			if (!optionNode || !optionNode.childNodes || optionNode.childNodes.length == 0)
				continue;
			var id = optionNode.getAttribute("id");
			var name = optionNode.childNodes[0].nodeValue	
			var alias = optionNode.getAttribute("alias");
			if(id==value){selectedIndex=i;}
			fieldObj.options[i] = new Option(name,id);	
			fieldObj.options[i].title = name;		
			fieldObj.options[i].alias = alias;		
		}
		fieldObj.selectedIndex = selectedIndex;
		if(fieldObj.selectedIndex==0 && fieldObj.options.length==2)
		{
			if(!isDefined(fieldObj,"autoselectonlyoption") || fieldObj.autoselectonlyoption=="true")
			{
				fieldObj.selectedIndex=1;
			}
		}
		doUpdateOptionsChildFieldSmileCom(fieldObj);
		if (oncomplete) oncomplete();
	}
}

function doUpdateOptionsChildFieldSmileCom(fieldObj, oncomplete)
{
	if(fieldObj.getAttribute("childpath")==null){return;}
	var childPaths = fieldObj.getAttribute("childpath").split(",");

	for(var i=0;i<childPaths.length;i++)
	{
		var childField = document.getElementsByName(childPaths[i])[0];
		if(childField==null){continue;}	
		var initvalue='';
		if(isDefined(childField,"initvalue")){
			initvalue = childField.getAttribute("initvalue"); //read the initvalue
		}
		doUpdateOptionsSmileCom(childField,initvalue, oncomplete);
	}
}

function doUpdateOptionsChildLinkFields(fieldObj, oncomplete)
{
	if(fieldObj.getAttribute("childpath")==null){return;}
	var childPaths = fieldObj.getAttribute("childpath").split(",");

	for(var i=0;i<childPaths.length;i++)
	{
		var childField = document.getElementsByName(childPaths[i])[0];
		if(childField==null){continue;}	
		var initvalue='';
		if(isDefined(childField,"initvalue")){
			initvalue = childField.getAttribute("initvalue"); //read the initvalue
		}
		doUpdateOptionsSmileCom(childField,initvalue, oncomplete);
	}
}

function getAjaxRequestObject()
{  
	// Firefox, Opera 8.0+, Safari 
	try{return new XMLHttpRequest();}catch(e){}   
	// Internet Explorer    
	try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}      
	try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}      
	alert("Your browser does not support AJAX!");        
   	return false;  
}

function parseXml(xml)
{
	if(window.ActiveXObject)
  	{
	  	var doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
 		doc.loadXML(xml);
 		return doc;
  	}
	return new DOMParser().parseFromString(xml,"text/xml");
}
