/**
 * Runs Ajax query for new field data and sets callback function that deals with it
 */

function PrintAjaxField(i_Class, i_Params, i_FieldName, i_OnCompleteFunction)
{
    
    _container = document.getElementById(i_FieldName + '_container');

    var params = 'class=' + i_Class;
    params += '&action=PrintField';
    params += '&params[name]=' + i_FieldName;
    params += i_Params;

	if (!HTTPObj)
		createHTTPObj();

	if (HTTPObj.RPCType == 'iframe2')
		params += "&jscallback=1";
	else
		params += "&jscallback=0";

	SetWait(_container);

	do_rpc('/AjaxService.php?'+params, i_FieldName, '', i_OnCompleteFunction);
}

/**
 * Copies html-options from html-select included in i_XMLHttpRequest.responseText 
 * to html-select i_FieldName
 */

function UpdateOptionField(i_XMLHttpRequest, i_FieldName, i_CallBack)
{	
    _container = document.getElementById(i_FieldName + '_container');
    UnsetWait(_container);

    if (HTTPObj.readyState == 4 || HTTPObj.readyState == "complete") 
	{
        if (HTTPObj.status == 200) 
		{
			eval("var parsedData = " + HTTPObj.responseText);
	        var _dst = document.getElementById(i_FieldName);

	        // remove all existing options
	        for (i=_dst.length-1; i >= 0 ; i--)
	        {
	            _dst.remove(i);
	        }

	        // copy options from parsedData to _dst
			for (var keyValue in parsedData)
			{
	            var _option = document.createElement('option');

	            _option.text = parsedData[keyValue];
	            _option.value = keyValue;

	            try
	            {
	                _dst.add(_option, null); // standards compliant
	            }
	            catch(ex)
	            {
	                _dst.add(_option); // IE only
	            }	
			}	

			// finalize
	        _container.innerHTML = '';

	        if (_dst.length > 1)
	            _dst.disabled = false;
	        else
	            _dst.disabled = true;

			if (i_CallBack)
				eval(i_CallBack);

	        return true;
        } 
		else if (HTTPObj.readyState != "complete")
		{
	        var _dst = document.getElementById(i_FieldName);

	        // remove all existing options
	        for (i=_dst.length-1; i >= 0 ; i--)
	        {
	            _dst.remove(i);
	        }

	        _container.innerHTML = '';

	        _dst.disabled = true;

	        return false;
        }
    }
}

function SetWait(i_Container)
{
    i_Container.style.display = 'inline';
    i_Container.innerHTML = '<img src="images/wait.gif" align="absmiddle" border="0"/>';
}

function UnsetWait(i_Container)
{
    i_Container.innerHTML = '';
    i_Container.style.display = 'none';
}

