//#################### AJAX ###########################
//function $(id) {
//	if(document.getElementById(id)) {
//		return document.getElementById(id);
//	}
//	return false;
//}

function check(e, regexp) {
	if(navigator.userAgent.indexOf('Gecko') != -1) {
		charCode = e.which;
	} else {
		charCode = e.keyCode;
	}
	if(charCode > 31) {
		znak = String.fromCharCode(charCode);
		return regexp.test(znak);
	}
}

function updateLiveBox(lastUpdate) {
	//alert('lastUpdate: '+lastUpdate);
	var ob = document.getElementById('livebox_content');
        //ob.innerHTML = "Start";
	advAJAX.post({
	    url: "modules/livebox.php",
	    parameters : {
	      "lastUpdate" : lastUpdate,
	      "type" : "simple"
	    },
	    mimeType: 'text/html',
	    timeout : 30000,
	    //onTimeout : function() { ob.innerHTML = "Przekroczenie limitu czasu połączenia."; },
	    retry: 20,
	    retryDelay: 10000,
	    //onRetry : function() { ob.innerHTML = "Ponawiam próbę połączenia..."; },
	    //onRetryDelay : function() { ob.innerHTML = "Oczekuję na połączenie..."; },
	    onSuccess : function(obj) {
		var ret = obj.responseText;
	        ob.innerHTML = ret;
	    },
	    onError : function(obj) { ob.innerHTML = "Wystąpił błąd: " + obj.status; }
	});
}

function updateLiveBox2(lastUpdate2,idl,ile)  {
	ile = 30000;
	//alert(lastUpdate2);
        //ob.innerHTML = "Start";

	advAJAX.post({
	    url: "modules/livebox_podstrona.php",
	    parameters : {
	      "lastUpdate" : lastUpdate2,
	      "idl" : idl,
	      "type" : "check"
	    },
	    mimeType: 'text/html',
	    timeout : 30000,
	    //onTimeout : function() { ob.innerHTML = "Przekroczenie limitu czasu połączenia."; },
	    retry: 20,
	    retryDelay: 10000,
	    //onRetry : function() { ob.innerHTML = "Ponawiam próbę połączenia..."; },
	    //onRetryDelay : function() { ob.innerHTML = "Oczekuję na połączenie..."; },
	    onSuccess : function(obj) {
		var ret = obj.responseText;
		var tmp = ret.split("|");
		//alert(tmp);
	        if(tmp[0] != 'now') {
	                //alert(ret);
         		//alert('nie update');
		}
		else {
	        	//alert('lastUpdate - a: '+lastUpdate);
		        sendUpdateLive(lastUpdate2, idl, ile);
		        lastUpdate = tmp[1];
		}
	    }//,
	    //onError : function(obj) { ob2.innerHTML = "Wystąpił błąd: " + obj.status; }
	});
        sec_left = 30;
}

function sendUpdateLive(lastUpdate, idl, ile) {
        //alert('rozpoczynam update: '+lastUpdate);
	var ob2 = document.getElementById('livebox_content2');
	advAJAX.post({
	    url: "modules/livebox_podstrona.php",
	    parameters : {
	      "lastUpdate" : lastUpdate,
		  "idl" : idl,
		  "ile" : ile,
	      "type" : "simple"
	    },
	    mimeType: 'text/html',
	    timeout : 30000,
	    //onTimeout : function() { ob.innerHTML = "Przekroczenie limitu czasu połączenia."; },
	    retry: 20,
	    retryDelay: 10000,
	    //onRetry : function() { ob.innerHTML = "Ponawiam próbę połączenia..."; },
	    //onRetryDelay : function() { ob.innerHTML = "Oczekuję na połączenie..."; },
	    onSuccess : function(obj) {
		var ret = obj.responseText;
	        ob2.innerHTML = ret;
	    },
	    onError : function(obj) { ob2.innerHTML = "Wystąpił błąd: " + obj.status; }
	});
}


function xml2array(xml) {
 var result = new Array();
 var key = null;
 var value = null;

 for (var i = 0; i < xml.childNodes.length; i++) {
   //key = xml.getElementsByTagName("items")[0].getAttribute("content");
   value = xml.getElementsByTagName("items")["content"].firstChild.nodeValue;
   result[i] = value;
 }

 return result;
}

