/**
 * @author atomekk@gmail.com
 * based on -> http://developer.mozilla.org/en/docs/AJAX:Getting_Started
 */

if (document.images)
{
  pic1= new Image(24,24);
  pic1.src="/site_media/poland_dot_images/ajax-loader.gif";
}
function makeRequestMenu(url, id, dom_id, lang) {
	document.getElementById('loading_'+dom_id).style.display = "block";
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		// See note below about this line
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { alertContentsMenu(http_request, id, dom_id, lang); };
	http_request.open('GET', url, true);
	http_request.send(null);
	}

function alertContentsMenu(http_request, id, dom_id ,lang) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200 || window.location.href.indexOf("http")==-1)  {
			//Handle response data here------------------



			document.getElementById('loading_'+dom_id).style.display = "none";

			var json_data_pure = http_request.responseText;
			json_data=json_data_pure.split('-*STOP*-');

      		eval("var the_object = ("+json_data[0]+")");
      		//eval("var the_description = ("+json_data[1]+")");
      		the_description = eval(json_data[1]);
      		//alert(the_description);
      		eval("var the_root = ("+json_data[2]+")");

			for (counter=0;counter<=the_object.length-1;counter++){
				//alert(the_object[counter]['fields']['obj_slug']);
				if (the_object[counter]['fields']['obj_pid'] == id){
					newLI=document.createElement('li');
					newLI.setAttribute('class','tree_lvl_2');
					newA=document.createElement('a');
					newA.setAttribute('href','/'+lang+'/'+the_root[0]['fields']['obj_slug']+'/'+the_object[counter]['fields']['obj_slug']);
					for (counter1=0;counter1<=the_description.length-1;counter1++){
						if (the_description[counter1]['desc_pid'] == the_object[counter]['pk']){
							newText=document.createTextNode(' '+Utf8.decode(the_description[counter1]['desc_title']));
							newA.appendChild(newText);
						}
					}
					newLI.appendChild(newA);
					document.getElementById('ul_'+dom_id).appendChild(newLI);

				for (counter2=0;counter2<=the_object.length-1;counter2++){
					if (the_object[counter2]['fields']['obj_pid'] == the_object[counter]['pk']){
						newLI=document.createElement('li');
						newLI.setAttribute('class','tree_lvl_3');
						newA=document.createElement('a');
						newA.setAttribute('href','/'+lang+'/'+the_root[0]['fields']['obj_slug']+'/'+the_object[counter]['fields']['obj_slug']+'/'+the_object[counter2]['fields']['obj_slug']);
						for (counter3=0;counter3<=the_description.length-1;counter3++){
							if (the_description[counter3]['desc_pid'] == the_object[counter2]['pk']){
								newText=document.createTextNode(' '+Utf8.decode(the_description[counter3]['desc_title']));
								newA.appendChild(newText);
							}
						}
						newLI.appendChild(newA);
						document.getElementById('ul_'+dom_id).appendChild(newLI);
					}
				}
				}
			}

		} else {
			document.getElementById('loading_'+dom_id).style.display = "none";
			alert('There was a problem with the request.');
		}
	}
}




function makeRequest(url, containerid) {
	var http_request = false;
	//var containerid_temp = containerid;
	//alert(containerid_temp);

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		// See note below about this line
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { alertContents(http_request, containerid); };
	http_request.open('GET', url, true);
	http_request.send(null);

	//alert('response:'+http_request.responseText);
	}

function alertContents(http_request, containerid) {

	document.getElementById('loading-dot').style.display = "block";
	if (http_request.readyState == 4) {
		if (http_request.status == 200 || window.location.href.indexOf("http")==-1)  {
			//|| window.location.href.indexOf("http")==-1)
			//alert(http_request.responseText);
			document.getElementById('loading-dot').style.display = "none";
			replaceContent(http_request.responseText,containerid);
			new Effect.Appear(containerid,{ duration: 2.0 })

		} else {
			alert('There was a problem with the request.');
		}
	}
}

function replaceContent (what, containerid){
	//alert('replace:'+what+'-'+containerid);
	document.getElementById(containerid).innerHTML=what;
}