//funzione invio iscrizione con ajax
function InviaIscrizione(){
var imput = document.getElementById('Comune')
var element = (imput.type == "select-one") ? imput.options[imput.selectedIndex].text
: escape(imput.value)
document.getElementById('image-attesa').style.display='block';
document.getElementById('InviaForm').style.display='none';
/*chiamata ajax in modalita post cosi imposta
-1 percoroso pagina sul server
-2 id relativo alla risposta
-3 metodo d'invio
-4 dati inviati*/
RequestAjax('VerificaIscrizione.asp','ErroreImput','POST','js=attivo&Nome='+escape(document.getElementById('Nome').value)+'&Cognome='+escape(document.getElementById('Cognome').value)+'&Indirizzo='+escape(document.getElementById('Indirizzo').value)+'&Comune='+element+'&Citta='+escape(document.getElementById('Citta').value)+'&Cap='+escape(document.getElementById('Cap').value)+'&Telefono='+document.getElementById('Telefono').value+'&Cellulare='+escape(document.getElementById('Cellulare').value)+'&Email='+document.getElementById('Email').value+'&C-F='+escape(document.getElementById('C-F').value)+'&Psw='+escape(document.getElementById('Psw').value)+'&Iva='+escape(document.getElementById('Iva').value));
}
//funzione login del iscritto in molità post
function login(){
document.getElementById('image-attesa').style.display='block';
document.getElementById('InviaForm').style.display='none';
RequestAjax('log.asp','ErroreLogin','POST','js=attivo&E-mail='+document.getElementById('E-mail').value+'&Psw='+escape(document.getElementById('Psw').value));
}
//funzione login admin in molità post
function login_admin(){
document.getElementById('image-attesa').style.display='block';
document.getElementById('admin_submit').style.display='none';
RequestAjax('/cms-document/admin/log_admin.asp','ErroreLoginAdmin','POST','js=attivo&admin_user='+document.getElementById('admin_user').value+'&admin_psw='+escape(document.getElementById('admin_psw').value));
}
//imposto l'oggetto per la chiamata ajax
function objAjx(){
      var ajax = false
      if (window.XMLHttpRequest) // if Mozilla, Safari etc
      ajax = new XMLHttpRequest()      
      else if (window.ActiveXObject){ // if IE
         try {
            ajax = new ActiveXObject("Msxml2.XMLHTTP")            
         } 
         catch (e){
            try{
               ajax = new ActiveXObject("Microsoft.XMLHTTP")
            }
            catch (e){}
         }
      }  
      else{         
         return false   
      }
      return ajax     
}
	//funzione chiamata ajax
	function RequestAjax(pagina,div,metodo,dati){
			//alert(pagina+' '+div+' '+metodo+' '+dati)
	ajx = objAjx();
    if(ajx){
	//identifico il metodo d'invio e imposto la spedizione dei dati
	if(metodo=='GET'){
    ajx.open('GET', pagina+"?"+dati+"&noCache="+ Math.floor(Math.random() * (9999)), true)         
	ajx.send(null)
	}else{
	ajx.open('POST', pagina, true)         
	ajx.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajx.send(dati)
	}
	//verifico il cambio distato
		ajx.onreadystatechange=function(){
		//alert(ajx.responseText)
		//verifico quando lo stato e 4 (risposta del server)
        if (ajx.readyState == 4 && ajx.status==200){
		//imbase al metodo d'invio e la pagina richiamata setto le risposte e le azioni		
			if(metodo=='POST'){
				if(pagina=="/cms-document/admin/log_admin.asp"){
					document.getElementById('image-attesa').style.display='none';
					document.getElementById('admin_submit').style.display='block';
					if(ajx.responseText.indexOf("Utente autorizzato")!=-1){
					location.href='/cms-document/admin/';}
					else{
					document.getElementById(div).innerHTML=ajx.responseText;
					}
				}
				if(pagina=="VerificaIscrizione.asp"){
					document.getElementById('image-attesa').style.display='none';
					document.getElementById('InviaForm').style.display='block';
					if(ajx.responseText.indexOf("Inserimento avvenuto")!=-1){
					location.href='Inserimento.asp';}
					else{
					document.getElementById(div).innerHTML=ajx.responseText;
					}
				}
				if(pagina=="FileDelete.asp"){
					var risposta = parseInt(ajx.responseText)
					if(risposta!=""){
					alert('eliminazione avvenuta correttamente')
					return RequestAjax('/cms-document/admin/scheda.asp','scheda-'+risposta,'GET','id='+risposta);
					}
			}
				if(pagina=="log.asp"){
					document.getElementById('image-attesa').style.display='none';
					document.getElementById('InviaForm').style.display='block';
					if(ajx.responseText.indexOf("Utente autorizzato")!=-1){
					location.href='utenti/';}
					else{
					document.getElementById(div).innerHTML=ajx.responseText;
					}
				}
				
			}
			if(metodo=='GET'){
					if(pagina=="Email.asp"){
					document.getElementById(div).style.display='block'
					document.getElementById(div).innerHTML=ajx.responseText;
					}

				if(pagina=="mail.asp"){
					document.getElementById(div).style.display='block'
					document.getElementById(div).innerHTML=ajx.responseText;
					}
				if(pagina=="scheda.asp"){
					document.getElementById(div).innerHTML=ajx.responseText;
					}
			if(pagina=="progress.asp"){
			  document.getElementById(div).innerHTML = ajx.responseText
		  }
			if(pagina=="Comuni.asp"){
					document.getElementById(div).innerHTML=ajx.responseText;
					var dati = document.getElementById("Comune").options[document.getElementById("Comune").selectedIndex].value;
					document.getElementById("Citta").value=dati.split('|')[0]
					document.getElementById("Cap").value=dati.split('|')[1]
					document.getElementById("Telefono").value=dati.split('|')[2]
					document.getElementById('Citta').readOnly='readonly';
					document.getElementById('Cap').readOnly='readonly';
				}
				else if(pagina=="Iscrizione.asp"){
				document.getElementById(div).innerHTML=ajx.responseText;
				var input = document.getElementsByTagName('input');
				for(var i=0; i<input.length; i++){
				if(input[i].id=='Citta' || input[i].id=='Cap'){
				input[i].value='Campo disabilitato';
				input[i].className='ImputDisabled';
				input[i].readOnly='readonly';
				input[i].readOnly='readonly';}
				else if(input[i].type=='text'){
				input[i].value='Dato obbligatorio';}
				if(i==3)input[i].setAttribute('autocomplete','off');
				if(i==7)input[i].value='Dato facoltativo';
				if(i==9)input[i].value='Dato obbligatorio per aziende';
				if(i==10)input[i].value='Dato obbligatorio per utenti privati';
				}
			}
	}
				document.getElementById(div).innerHTML=ajx.responseText;
		}
	  }
	}
}
//funzione che smista i dati (cap/prefisso/citta)
function Inserisci(sel){
if(sel.options[sel.selectedIndex].value!=0){
	document.getElementById('Telefono').value=sel.options[sel.selectedIndex].value.split('|')[2];
	document.getElementById('Citta').value=sel.options[sel.selectedIndex].value.split('|')[0];
	document.getElementById('Cap').value=sel.options[sel.selectedIndex].value.split('|')[1];}
	else{
		document.getElementById('Telefono').value='Dato obbligatorio';
		document.getElementById('Cap').value='Campo disabilitato';
		document.getElementById('Citta').value='Campo disabilitato';
		var div = document.getElementById("DivComune");
		div.innerHTML='';
				var element = document.createElement("input")
				element.id="Comune";
				element.name="Comune";
				element.setAttribute('autocomplete','off')
				element.className="ImputRichiesto"
				element.onkeyup=function(){if(this.value.length>3)RequestAjax('Comuni.asp','DivComune','GET','Comune='+this.value);}
				div.appendChild(element)
				element.focus();	
		
	}
}
//funzione select stato clienti 
function ModificaCliente(riga,attiva,dato,id){
	var tr = document.getElementsByTagName("table")[0].getElementsByTagName("tr")
	if(attiva == 1){
	var sel = "<select name=\"stato\" id=\"stato\" onchange=\"location.href='modificacliente.asp?Id="+id+"&stato='+this.options[this.selectedIndex].value\">"
	if(dato == 0) sel+="<option value=\"0\" selected=\"selected\">Attesa</option><option value=\"2\">Bloccato</option><option value=\"1\">Attivo</option></select>"
	if(dato == 2) sel+="<option value=\"0\">Attesa</option><option value=\"2\" selected=\"selected\">Bloccato</option><option value=\"1\">Attivo</option></select>"
	if(dato == 1) sel+="<option value=\"0\">Attesa</option><option value=\"2\">Bloccato</option><option value=\"1\" selected=\"selected\">Attivo</option></select>"
	tr[riga].getElementsByTagName("td")[3].innerHTML=sel		
	tr[riga].getElementsByTagName("td")[4].innerHTML='<a href="javascript:ModificaCliente('+riga+',0,'+dato+','+id+');"><img src="../images/018.png" alt="modifica" /></a>';
	}else{
		tr[riga].getElementsByTagName("td")[3].innerHTML=dato;
	  	tr[riga].getElementsByTagName("td")[4].innerHTML='<a href="javascript:ModificaCliente('+riga+',1,'+dato+','+id+');"><img src="../images/018.png" alt="modifica" /></a>';
	}
}
//finzione conferma cancellazione cliente
function EliminaCliente(cliente,id){
	if(confirm("sicuro di voler eliminare "+cliente))
	location.href='eliminacliente.asp?Id='+id+'&cliente='+cliente
	else return false;
}


