//client_s/funzioni AJAX
// OGGETTO XMLHttpRequest
function ajax(){
	var ajaxRequest;
	try{
		// controllo per i browser diversi da IE
		ajaxRequest = new XMLHttpRequest();
	}catch (e){
		// contorollo per IE
		try{
			ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
		}catch (e){
			try{
				ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
			}catch (e){
				// controllo per i browser che non supportano l'XMLHttpRequest
				alert('Error Ajax');
				return false;
			}
		}
	}
	return ajaxRequest;
}
//LOADING
function stato_loading(){
	setTimeout("loading(1)",100);
	setTimeout("loading(0)",800);
}
function loading(stato){
	if(document.getElementById('loading_com')){
		if(stato==1) document.getElementById('loading_com').style.display='';
		if(stato==0) document.getElementById('loading_com').style.display='none';
	}
	if(document.getElementById('loading_sign')){
		if(stato==1) document.getElementById('loading_sign').style.display='';
		if(stato==0) document.getElementById('loading_sign').style.display='none';
	}
}
// RICEZIONE DA AJAX
function mostra(elemento,id,param) {
	param = param.replace(/&amp;/g,"&");
	htmlRequest = ajax();
	if (htmlRequest==null){
		alert ('Error Ajax');
		return;
	}
	switch(elemento){
		case 'commenti':
		htmlRequest.onreadystatechange = function(){
			// Restituisce lo stato della richiesta
			if(htmlRequest.readyState == 4){
				// Restituice il corpo della risposta come stringa
				document.getElementById(elemento).innerHTML = htmlRequest.responseText;
			}
		}
		// chiamata della pagina PHP che estrae i records
		htmlRequest.open('GET', '/client_s/funzioni/exe.php?modo=commenti&action=mostra&id_contenuto='+id+'&'+param, true);
		htmlRequest.send(null);
		break;
	}
}

//TRASMISSIONE PER AJAX
function inserisci(elemento,param){
	param = param.replace(/&amp;/g,"&");
	htmlRequest = ajax();
	if (htmlRequest==null){
		alert ('Error Ajax');
		return;
	}
	switch(elemento){
		//INSERISCO IN COMMENTI
		case 'commenti':
		var root = document.comments;
		if(root.nome.value == '' || root.messaggio.value == '')
		{
			alert('Nome, e-mail e messaggio sono obbligatori');
			return;
		}
		if (root.email.value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1)
		{
			alert('Email error');
			return;
		}
		// inviamo i parametri al file per l'INSERT nel database
		htmlRequest.open('POST', '/client_s/funzioni/exe.php');
		htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		htmlRequest.send(
		'nome='+root.nome.value+
		'&email='+root.email.value+
		'&url='+root.www.value+
		'&messaggio='+root.messaggio.value+
		'&modo=commenti&action=inserisci'+
		'&id_contenuto='+root.id_contenuto.value+
		'&id_autore='+root.id_autore.value+
		'&mo='+root.mo.value+
		'&code='+root.code.value+
		'&ac='+root.ac.value+
		'&ip_autore='+root.ip_autore.value
		);
		if(root.ricordami.checked == true){
			memo_cookie('nick',root.nome.value,'365');
		}
		// svuotiamo il modulo per il messaggio
		stato_loading();
		root.messaggio.value = '';
		root.messaggio.focus();
		// ripeto 2 volte per forzare la cache (explorer fa capricci.. ovviamente!)
		mostra(elemento,root.id_contenuto.value,param);
		mostra(elemento,root.id_contenuto.value,param);
		break;
	}

}

//AJAX CON PROTOTYPE
function sendRequest(elemento) {
	stato_loading();
	var elemento;
	switch(elemento){
		case 'nuovo_utente':
		new Ajax.Request("/client_s/funzioni/exe.php", {
			method: 'post',
			postBody:
			"password="+$F("password_i")+
			"&ripassword="+$F("ripassword")+
			"&email="+$F("email_i")+
			"&ip_autore="+$F("ip_autore_")+
			"&action="+$F("action")+
			"&modo="+$F("modo")+
			"&id_mod="+$F("id_mod")+
			"&code="+$F("code")+
			"&check="+$F("check"),
			onComplete: showResponse
		});
		break;
		case 'recupero_dati':
		new Ajax.Request("/client_s/funzioni/exe.php", {
			method: 'post',
			postBody:
			"email="+$F("email_r")+
			"&action="+$F("action_r")+
			"&modo="+$F("modo")+
			"&id_mod="+$F("id_mod")+
			"&code="+$F("code"),
			onComplete: showResponse
		});
		break;
		case 'login':
		new Ajax.Request("/client_s/funzioni/exe.php", {
			method: 'post',
			postBody:
			"email="+$F("email_l")+
			"&action="+$F("action_l")+
			"&password="+$F("password_l")+
			"&modo="+$F("modo")+
			"&id_mod="+$F("id_mod")+
			"&code="+$F("code")+
			"&ricordami="+$F("ricordami_l"),
			onComplete: showResponse
		});
		break;
		case 'modifica_utente':
		new Ajax.Request("/client_s/funzioni/exe.php", {
			method: 'post',
			postBody:
			"nome="+$F("nome")+
			"&cognome="+$F("cognome")+
			"&action="+$F("action")+
			"&password_vecchia="+$F("password_vecchia")+
			"&password="+$F("password")+
			"&ripassword="+$F("ripassword")+
			"&modo="+$F("modo")+
			"&id_mod="+$F("id_mod")+
			"&code="+$F("code"),
			onComplete: showResponse2
		});
		break;
	}
}
// RESPONSO OPERAZIONI
function showResponse(req)	{
	var res=/error/;
	var res2=/reload/;
	if(req.responseText.match(res)){
		$('response_error').style.padding= "10px";
		$('response_error').style.display= "block";
		$('response_ok').style.display= "none";
		$('response_error').innerHTML= req.responseText;
	}else{
		if(req.responseText.match(res2)){
			$('btn_close').onclick=function(){ricarica()};
			$('fade').onmouseover=function(){ricarica()};
			if($('cont_login')) $('cont_login').onmouseover=function(){ricarica()};

		}
		if(document.getElementById('ricordami_l').checked == true){
			memo_cookie('e',document.getElementById('email_l').value,'365');
			memo_cookie('p',hex_md5(hex_sha1(document.getElementById('password_l').value)),'365');
		}
		$('response_ok').style.padding= "10px";
		$('response_ok').style.display= "block";
		$('response_error').style.display= "none";
		$('response_ok').innerHTML= req.responseText;
		$('cont_dinamic').style.display= "none";
	}
}
// RESPONSO MODIFICA ACCOUNT (Non ho usato quello che c'è su perchè non funzionava con il modulo di modifica account..boh!!)
function showResponse2(req){
	var res2=/reload/;
	if(req.responseText.match(res2)){
		$('response_ok').style.padding= "10px";
		$('response_ok').style.display= "block";
		$('response_ok').innerHTML= req.responseText;
		$('response_error').style.display= "none";
		$('btn_close').onclick=function(){ricarica()};
		$('fade').onmouseover=function(){ricarica()};
		if($('cont_account')) $('cont_account').onmouseover=function(){ricarica()};
	}else{
		$('response_error').style.padding= "10px";
		$('response_error').style.display= "block";
		$('response_ok').style.display= "none";
		$('response_error').innerHTML= req.responseText;
	}
}
