var req; 
var div_id;
var button_to_disable;

function loadXMLDoc(url, param_div_id, disable_button) {

//	alert(url);

	// we need to check here the cookie
	var found_username = 0;
	var found_password = 0;
	var cookies_array = document.cookie.split(";");
	for (var i = 0; i < cookies_array.length; i++) {
//		alert('|' + trim(cookies_array[i]) + '|');
		var parts_array = trim(cookies_array[i]).split("=");
		if (parts_array[0] == 'username') {
			found_username = 1;
			username = parts_array[1];
		}
		if (parts_array[0] == 'password_crypt') {
			found_password = 1;
			password_crypt = parts_array[1];
		}
	}

	if (found_username && found_password) {
	} else {
		if (url.indexOf('ajax') == -1 && url.indexOf('main_articles') == -1) {
			window.location.href = '/?logout=1';
			return false;
		}
	}

	div_id = param_div_id;
	button_to_disable = disable_button;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        alert("Your browser does not support XMLHttpRequest");
		return false;
	}
	if (req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	} else {
		alert("Error");
		return false;
	}
	return req;
}

function processReqChange() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var response = req.responseText;
			if (response != '') {
				document.getElementById(div_id).innerHTML = response;
				if (document.getElementById(div_id).style.display == 'none'){
					document.getElementById(div_id).style.display = 'block';
				}
				if (document.getElementById('ajax_loading')) {
					document.getElementById('ajax_loading').style.visibility = 'hidden';
				}
			} else {
				document.getElementById(div_id).innerHTML = '';
				document.getElementById(div_id).style.display = 'none';
			}
			if (button_to_disable) {
				eval("document.forms[0]." + button_to_disable + ".disabled = true");
			}
		} else {
			if (req.status == '404') {
				problem = '404 / file not found';
			}
			alert("There was a problem retrieving the data [" + problem + "]");
		}
	}

	if (! document.getElementById('main_footer_loaded')) {
//		alert('pippo');
	} else {
//		document.getElementById('ajax_loading').style.visibility = 'hidden';
	}

}


function open_main_ajax(url, li_id, num_li) {

	document.getElementById('ajax_loading').style.visibility = 'visible';

	var random = rand(1000);
	var question_mark = '';
	if (url.indexOf('?') == -1) {
		question_mark = '?';
	} else {
		question_mark = '';
	}
	rand_url = url + question_mark + '&' + random;
//	alert(rand_url);
	loadXMLDoc('' + rand_url, 'main');
	loadXMLDoc('' + rand_url, 'main');
	if (num_li > 0) {
		for (var i = 1; i <= num_li; i++) {
			eval("document.getElementById('li" + i + "').className = 'buttons_li';");
		}
	}
	if (li_id != '' && li_id != undefined) {
//		alert("LI_ID: " + li_id);
		document.getElementById(li_id).className = 'button_clicked';
	}

}


function submit_form_main_ajax(form, url) {
	var random = rand(1000);
	if (check_form(form) == false) {
		return false;
	}
	div_id = 'main';
	var query_string = '';
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].name != false) {
			if ((form.elements[i].type == 'checkbox' || form.elements[i].type == 'radio') && form.elements[i].checked == false) {
				continue;
			}
//			alert(form.elements[i].name + ' => ' + form.elements[i].type);
			query_string = query_string + '&' + form.elements[i].name + '=' + escape(form.elements[i].value);
		}
	}
	url = url + '?' + random + '&' + query_string;
	loadXMLDoc(url, div_id);
}



function loadXMLDoc2(url, param_div_id, disable_button) {

	// we need to check here the cookie
	var found_username = 0;
	var found_password = 0;
	var cookies_array = document.cookie.split(";");
	for (var i = 0; i < cookies_array.length; i++) {
//		alert('|' + trim(cookies_array[i]) + '|');
		var parts_array = trim(cookies_array[i]).split("=");
		if (parts_array[0] == 'username') {
			found_username = 1;
			username = parts_array[1];
		}
		if (parts_array[0] == 'password_crypt') {
			found_password = 1;
			password_crypt = parts_array[1];
		}
	}

	if (found_username && found_password) {
	} else {
		if (url.indexOf('ajax') == -1) {
			window.location.href = '/?logout=1';
			return false;
		}
	}

	div_id = param_div_id;
	button_to_disable = disable_button;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        alert("Your browser does not support XMLHttpRequest");
		return false;
	}
	if (req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	} else {
		alert("Error");
		return false;
	}
	return req;
}