/*
 * This function updates page using given XML data.
 * Parses XML structure:
 * <data>
 *   <variable>
 *     <name>name1</name>
 *     <value>val1</value>
 *   </variable>
 *   <variable>
 *     <name>name2</name>
 *     <value>val2</value>
 *   </variable>
 * </data>
 *
 * This function updates innerHTML for field_id = <name> with the content of <value>
 */
function xml_update_page( xml )
{
	var variables = xml.getElementsByTagName( 'variable' );
	
	for( i=0; i<variables.length; i++ ) // loop through the <variable>'s
	{
		var var_name = '';
		var var_value = '';
		for( j=0; j<variables[i].childNodes.length; j++ ) // loop through <variable>'s childs
		{
			if( variables[i].childNodes[j].nodeType != 1 ) continue; // skip text
			if( variables[i].childNodes[j].nodeName == 'name' )
				var_name = variables[i].childNodes[j].firstChild.nodeValue;
			if( variables[i].childNodes[j].nodeName == 'value' )
				var_value = variables[i].childNodes[j].firstChild.nodeValue;
		}
		if( var_name == '' ) continue; // no proper data
		// sprawdzmy, czy jest to referencja - znaczy trzeba przemapowac var_name, na zmienna o nazwie var_name
		if( document.getElementById( var_name ) )
			document.getElementById( var_name ).innerHTML = var_value;
		//alert( 'eval: (' + 'document.getElementById( ' + var_name + ' ).innerHTML = var_value )' );
		if( eval( 'document.getElementById( ' + var_name + ' )' ) )
			eval( 'document.getElementById( ' + var_name + ' ).innerHTML = var_value' );
	}
}

/*
 * This function fetches url?params file via XMLHttpRequest and calls "procesor to handle the result
 */
function ajax_get_xml( url, handler_function, info_field )
{
	var xmlObj = null;
	if(window.XMLHttpRequest)
	{
		xmlObj = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return false;
	}

	xmlObj.onreadystatechange = function()
	{
		if( xmlObj.readyState == 4 ) // ready
		{
			handler_function( xmlObj.responseXML );
		}
		// else
			//document.getElementById( info_field ).innerHTML = "&nbsp;&nbsp;Operacja w toku ... &nbsp;&nbsp;";
	}
	xmlObj.open( 'GET', url + '&rand=' + Math.random(), true );
	xmlObj.send( '' );
}

/*
 * This function fetches XMLHttpRequest and updates the page using received XML data.
 * The function does not know the type and meaning of data - logics is maintained on the server.
 */
function ajax_update_page( url, info_field )
{
	ajax_get_xml( url, xml_update_page, info_field );
}

/*
 * This function takes XML element and returns text value of given child element.
 * xml - XML element
 * name - child element name
 */

function xml_get_element_text( xml, name )
{
	if( xml.getElementsByTagName(name)[0] )
		if( xml.getElementsByTagName(name)[0].childNodes ) 
			if( xml.getElementsByTagName(name)[0].childNodes[0] ) 
				if( xml.getElementsByTagName(name)[0].childNodes[0].nodeValue )
					return xml.getElementsByTagName(name)[0].childNodes[0].nodeValue;
	return '';
}

