function in_array(string, array){
	for (i = 0; i < array.length; i++){
		if(array[i] == string){
			return true;
		}
	}
	return false;
}

function isInteger (s){
      var i;

      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
}

function isEmpty(s){
      return ((s == null) || (s.length == 0))
}

function isDigit (c){
      return ((c >= "0") && (c <= "9"))
}


function ck_string(str,regexp) {
    var patternvalido = regexp;
    var reg = new RegExp(patternvalido);
    return str.match(reg);
}

// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
var debugMode = true;
// hide loading data

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() {
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e){
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                        "MSXML2.XMLHTTP.5.0",
                                        "MSXML2.XMLHTTP.4.0",
                                        "MSXML2.XMLHTTP.3.0",
                                        "MSXML2.XMLHTTP",
                                        "Microsoft.XMLHTTP");
		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
			try { 
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			} 
			catch (e) {
			}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp){
		//	var ld = document.getElementById("loadingdata");
		//	ld.style.display="";
		alert("Error creating the XMLHttpRequest object.");
	}
	else {
		return xmlHttp;
	}
}

// function called when the state of the HTTP request changes
function handleRequestStateChange() {
	// when readyState is 4, we are ready to read the server response
	if (xmlHttp.readyState == 4) {
		// continue only if HTTP status is "OK"
		if (xmlHttp.status == 200) {
			try{
				// do something with the response from the server
				handleServerResponse();
			}
			catch(e){
				// display error message
				alert("Error reading the response: " + e.toString());
				alert("rg58 - Errore nel leggere la risposta! Può causare questo errore: " + e.toString());
			}
		} 
		else{
			// display status message
			//alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
			alert("rg66 - C'è stato un problema nella ricezione dei dati:\n" + xmlHttp.statusText);
			//	var ld = document.getElementById("loadingdata");
			//	ld.style.display="";
		}
	}
}

function handleServerResponse(){
	// retrieve the server's response packaged as an XML DOM object
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("rg77 - Struttura dati non valida:\n" + xmlHttp.responseText);
 
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") 
		throw("rg82 - Struttura dati non valida:\n" + xmlHttp.responseText);
	// getting the root element (the document element)
	xmlRoot = xmlResponse.documentElement;

//	numero = xmlRoot.firstChild.childNodes.length;
	numero = xmlRoot.childNodes.length;
	
	for(var xnum=0; xnum < numero; xnum++){
//		alert(xmlRoot.childNodes[xnum].firstChild.nodeValue);
//		alert(xmlRoot.childNodes[xnum].getAttribute("targ"));
		// display the user message
		myDiv = document.getElementById(xmlRoot.childNodes[xnum].getAttribute("targ"));
		myDiv.innerHTML = xmlRoot.childNodes[xnum].firstChild.nodeValue;
	}

/*
    
	var testoX = xmlRoot.firstChild.firstChild.nodeValue;
	var targ = xmlRoot.firstChild.getAttribute("targ");
 
	responseText = testoX;

	// display the user message
	myDiv = document.getElementById(targ);
	myDiv.innerHTML = responseText;

*/


}

/*
function handleServerResponse(){
	// retrieve the server's response packaged as an XML DOM object
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("rg77 - Struttura dati non valida:\n" + xmlHttp.responseText);
 
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") 
		throw("rg82 - Struttura dati non valida:\n" + xmlHttp.responseText);
	// getting the root element (the document element)
	xmlRoot = xmlResponse.documentElement;
    
	var testoX = xmlRoot.firstChild.firstChild.nodeValue;
	var targ = xmlRoot.firstChild.getAttribute("targ");
 
	responseText = testoX;

	// display the user message
	myDiv = document.getElementById(targ);
	myDiv.innerHTML = responseText;

}
*/

function display_message_on_div(div,msg){
	document.getElementById(div).innerHTML=msg;
}


function start_control(divfield,tipo,urlpass){
	if (xmlHttp){
		// try to connect to the server
		try {
//			display_message_on_div("checknickresponse",msg04);

			// initiate the asynchronous HTTP request
			xmlHttp.open("GET", urlpass, true);
			// xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// display the error in case of failure
		catch (e){
			alert("Non è stato possibile connettersi alla sorgente dati. Non cliccare il bottone ripetutamente:\n" + e.toString());
			//	var ld = document.getElementById("loadingdata");
			//	ld.style.display="";
		}
	}
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	 if (str.indexOf(" ")!=-1){
	    return false
	 }
	 return true					
}

function managebutton(val){
	if(!val){
		document.form.bottone.disabled=true;
	}
	else{
		document.form.bottone.disabled=false;
	}
}

function checkform(){
	errore=false;
	if(isEmpty(document.form.nome.value) ){
		msg="<p><strong>Attenzione:</strong> occorre completare il campo Nome.</p>";
		display_message_on_div("validazione-msg",msg);
		errore=true;
		return false;
	}
	else if( isEmpty(document.form.cognome.value) ){
		msg="<p><strong>Attenzione:</strong> occorre completare il campo Cognome.</p>";
		display_message_on_div("validazione-msg",msg);
		errore=true;
		return false;
	}
	else if( isEmpty(document.form.oggetto.value) ){
		msg="<p><strong>Attenzione:</strong> occorre completare il campo Oggetto.</p>";
		display_message_on_div("validazione-msg",msg);
		errore=true;
		return false;
	}
	else if( isEmpty(document.form.richiesta.value)){
		msg="<p><strong>Attenzione:</strong> occorre completare il campo Richiesta.</p>";
		display_message_on_div("validazione-msg",msg);
		errore=true;
		return false;
	}
	else if(isEmpty(document.form.email.value) && isEmpty(document.form.telefono.value)){
		msg="<p><strong>Attenzione:</strong> occorre inserire telefono o indirizzo email per essere ricontattati.</p>";
		display_message_on_div("validazione-msg",msg);
		errore=true;
		return false;
	}
	else if(document.form.email.value!=""){
		if(!echeck(document.form.email.value)){
			msg="<p><strong>Attenzione:</strong> L'indirizzo email inserito non ha un formato corretto. Controlla per favore.</p>";
			display_message_on_div("validazione-msg",msg);
			return false;
			errore=true;
		}
	}
	if(isEmpty(document.form.codice.value)){
		msg="<p><strong>Attenzione:</strong> Devi inserire il codice antispam.</p>";
		display_message_on_div("validazione-msg",msg);
		errore=true;
		return false;
	}
	
	if(!errore){
		codice = document.form.codice.value;
		check_antispam(codice);
	}
}

function check_antispam(codice){
	var urlpass = "/ajax.php?do=check_antispam&codice=" + codice;
		
	if (xmlHttp){
		// try to connect to the server
		try {
			// display_message_on_div("box_wishlist",loading_msg);
			
			// initiate the asynchronous HTTP request
			xmlHttp.open("GET", urlpass, true);
			// xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// display the error in case of failure
		catch (e){
			msg="<p><strong>Attenzione!</strong> Si &egrave; verificato un errore nella connessione al server. Riprova tra qualche minuto.</p>";
			display_message_on_div("validazione-msg",msg);
		}
	}
}

function reimposta(){
	msg1="<p><input type=\"button\" name=\"bottone\" value=\"Verifica i dati inseriti\" id=\"bottone\" onclick=\"return checkform()\"/></p>";
	msg2=" ";
	display_message_on_div("validazione-btn",msg1);
	display_message_on_div("validazione-msg",msg2);
}

function reimposta_sugg_park(){
	msg1="<p><input type=\"button\" name=\"bottone\" value=\"Ricerca indirizzo\" id=\"bottone\" onclick=\"return checkform_suggerisciparcheggio()\"/></p>";
	vuoto=" ";
	display_message_on_div("validazione-btn",msg1);
	display_message_on_div("validazione-msg",vuoto);
	display_message_on_div("map_canvas",vuoto);
	display_message_on_div("conferma_mappa",vuoto);
	display_message_on_div("elenco_risultati",vuoto);
	
}



function checkform_suggerisciparcheggio(){
	errore=false;
	if(isEmpty(document.form.indirizzo.value) ){
		msg="<p><strong>Attenzione:</strong> occorre completare il campo Indirizzo.</p>";
		display_message_on_div("validazione-msg",msg);
		errore=true;
		return false;
	}
	else if( isEmpty(document.form.numero_civico.value) ){
		msg="<p><strong>Attenzione:</strong> occorre completare il campo Numero civico.</p>";
		display_message_on_div("validazione-msg",msg);
		errore=true;
		return false;
	}
	else if( isEmpty(document.form.citta.value) ){
		msg="<p><strong>Attenzione:</strong> occorre completare il campo Citt&agrave;.</p>";
		display_message_on_div("validazione-msg",msg);
		errore=true;
		return false;
	}
	else if( isEmpty(document.form.cap.value)){
		msg="<p><strong>Attenzione:</strong> occorre completare il campo Cap.</p>";
		display_message_on_div("validazione-msg",msg);
		errore=true;
		return false;
	}
	
	if(!errore){
		address = (document.form.indirizzo.value + ", " + document.form.numero_civico.value + " " + document.form.citta.value + " " + document.form.cap.value);
		msg="<p><img src=\"/resource/images/loading.gif\" /> Ricerca in corso: "+ address +"</p>";
		display_message_on_div("validazione-msg",msg);
		check_address(address);
	}
}



function check_address(address){
	var urlpass = "/ajax.php?do=check_address&address=" + address;
		
	if (xmlHttp){
		// try to connect to the server
		try {
			// display_message_on_div("box_wishlist",loading_msg);
			
			// initiate the asynchronous HTTP request
			xmlHttp.open("GET", urlpass, true);
			// xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// display the error in case of failure
		catch (e){
			msg="<p><strong>Attenzione!</strong> Si &egrave; verificato un errore nella connessione al server. Riprova tra qualche minuto.</p>";
			display_message_on_div("validazione-msg",msg);
		}
	}
}


function checkform_newsletter(){
	errore=false;
	msg="";
	nome = document.getElementById('field1');
	cognome = document.getElementById('field2');
	email = document.getElementById('email');
	if( isEmpty(email.value) ){
		msg+="occorre completare il campo E-mail.<br/>";
		email.style.border="3px solid #CC0000";
		errore=true;
	}
	else if(!echeck(email.value)){
		msg+="L'indirizzo email inserito non ha un formato corretto.<br/>";
		email.style.border="3px solid #CC0000";
		display_message_on_div("validazione-msg",msg);
		errore=true;
	}
	else{
		email.style.border="3px solid #008000";
	}

	if(isEmpty(nome.value) ){
		msg+="occorre completare il campo Nome.<br/>";
		nome.style.border="3px solid #CC0000";
		display_message_on_div("validazione-msg",msg);
		errore=true;
	}
	else{
		nome.style.border="3px solid #008000";
	}

	if( isEmpty(cognome.value) ){
		msg+="occorre completare il campo Cognome.<br/>";
		cognome.style.border="3px solid #CC0000";
		display_message_on_div("validazione-msg",msg);
		errore=true;
	}
	else{
		cognome.style.border="3px solid #008000";
	}
	
	if(errore){
		display_message_on_div("validazione-msg","<p><strong>Attenzione:</strong><br/>"+msg+"</p>");
		return false;
	}
	else{
		document.form.submit();
	}

}



function checkform_suggerisci_parcheggio2(){
	errore=false;
	msg="";
	nome = document.getElementById('nome');
	cognome = document.getElementById('cognome');
	email = document.getElementById('email');
	codice = document.getElementById('codice');

	if( isEmpty(email.value) ){
		msg+=" - occorre completare il campo E-mail.<br/>";
		email.style.border="3px solid #CC0000";
		errore=true;
	}
	else if(!echeck(email.value)){
		msg+=" - L'indirizzo email inserito non ha un formato corretto.<br/>";
		email.style.border="3px solid #CC0000";
		display_message_on_div("validazione-msg",msg);
		errore=true;
	}
	else{
		email.style.border="3px solid #008000";
	}

	if(isEmpty(nome.value) ){
		msg+=" - occorre completare il campo Nome.<br/>";
		nome.style.border="3px solid #CC0000";
		display_message_on_div("validazione-msg",msg);
		errore=true;
	}
	else{
		nome.style.border="3px solid #008000";
	}

	if( isEmpty(cognome.value) ){
		msg+=" - occorre completare il campo Cognome.<br/>";
		cognome.style.border="3px solid #CC0000";
		display_message_on_div("validazione-msg",msg);
		errore=true;
	}
	else{
		cognome.style.border="3px solid #008000";
	}


	if( isEmpty(codice.value) ){
		msg+=" - occorre completare il campo Codice antispam.<br/>";
		codice.style.border="3px solid #CC0000";
		display_message_on_div("validazione-msg",msg);
		errore=true;
	}
	else if(!ck_string(codice.value,"([0-9A-Z]){4}")){
		msg+=" - il campo Codice antispam deve contenere 4 caratteri in maiuscolo!<br/>";
		codice.style.border="3px solid #CC0000";
		display_message_on_div("validazione-msg",msg);
		errore=true;
	}
	else{
		codice.style.border="3px solid #008000";
	}
	
	if(errore){
		display_message_on_div("validazione-msg","<p><strong>Attenzione:</strong><br/>"+msg+"</p>");
		return false;
	}
	else{
		document.form.submit();
	}

}




function checkform_registrazione_privato(){
	errore=false;
	msg="";

	compra_prepagato100	= getCheckedValue(document.form.compra_prepagato100);
	nome = document.getElementById('pri_nome').value;
	cognome = document.getElementById('pri_cognome').value;
	citta = document.getElementById('pri_citta').value;
	via = document.getElementById('pri_via').value;
	domicilio = document.getElementById('pri_domicilio').value;


	provincia = document.getElementById('pri_provincia').value;
	cap = document.getElementById('pri_cap').value;
	domicilio = document.getElementById('pri_domicilio').value;

	telefono = document.getElementById('pri_telefono').value;
	cellulare = document.getElementById('pri_cellulare').value;
	fax = document.getElementById('pri_fax').value;

	email = document.getElementById('pri_email').value;
	professione = document.getElementById('pri_professione').value;
	citta_nascita = document.getElementById('pri_citta_nascita').value;
	data_nascita = document.getElementById('pri_data_nascita').value;

	tipo_identita = getCheckedValue(document.form.tipo_documento_identita);
	numero_identita = document.getElementById('pri_numero_documento_identita').value.replace(/\s+/g,'');
	numero_patente = document.getElementById('pri_numero_patente').value.replace(/\s+/g,'');
	categoria_patente = document.getElementById('pri_categoria_patente').value;
	codice_fiscale = document.getElementById('pri_cf').value.replace(/\s+/g,'');
	
	privacy	= getCheckedValue(document.form.set_privacy);


	antispam = document.getElementById('antispam').value;


	if(isEmpty(nome) ){
		msg+=" - occorre completare il campo Nome.<br/>";
		errore=true;
	}
	if( isEmpty(cognome) ){
		msg+=" - occorre completare il campo Cognome.<br/>";
		errore=true;
	}
	if(isEmpty(citta) ){
		msg+=" - occorre completare il campo Citta.<br/>";
		errore=true;
	}
	if(isEmpty(via) ){
		msg+=" - occorre completare il campo Via.<br/>";
		errore=true;
	}
	if(provincia=="NULL"){
		msg+=" - occorre completare il campo Provincia.<br/>";
		errore=true;
	}
	if( isEmpty(cap)  || (cap.length != 5) ){
		msg+=" - occorre completare il campo CAP.<br/>";
		errore=true;
	}
	else if(!isInteger(cap) ){
		msg+=" - occorre completare il campo CAP con il valore corretto.<br/>";
		errore=true;

	}
	if( isEmpty(domicilio) ){
		msg+=" - occorre completare il campo Cellulare.<br/>";
		errore=true;
	}
	if( isEmpty(cellulare) ){
		msg+=" - occorre completare il campo Cellulare.<br/>";
		errore=true;
	}
	if( isEmpty(email) ){
		msg+=" - occorre completare il campo E-mail.<br/>";
		errore=true;
	}
	else if(!echeck(email)){
		msg+=" - L'indirizzo email inserito non ha un formato corretto.<br/>";
		errore=true;
	}
	if( professione == "NULL" ){
		msg+=" - occorre completare il campo Professione.<br/>";
		errore=true;
	}
	if( isEmpty(data_nascita) ){
		msg+=" - occorre completare il campo Data di nascita.<br/>";
		errore=true;
	}
	if( isEmpty(citta_nascita) ){
		msg+=" - occorre completare il campo Citta di nascita.<br/>";
		errore=true;
	}

	if( isEmpty(numero_identita) ){
		msg+=" - occorre completare il campo Numero carta d'intentit&agrave; o del passaporto.<br/>";
		errore=true;
	}
	if( isEmpty(numero_patente) ){
		msg+=" - occorre completare il campo Numero patente.<br/>";
		errore=true;
	}
	if( isEmpty(categoria_patente) ){
		msg+=" - occorre completare il campo Ccategoria patente.<br/>";
		errore=true;
	}
	if( isEmpty(antispam) ){
		msg+=" - occorre completare il campo Codice antispam.<br/>";
		errore=true;
	}
	else if(!ck_string(antispam,"([0-9A-Z]){4}")){
		msg+=" - il campo Codice antispam deve contenere 4 caratteri in maiuscolo!<br/>";
		errore=true;
	}
	if(privacy=="NO"){
		msg+=" - occorre accettare le condizioni sulla privacy per poter continuare.<br/>";
		errore=true;
	}
	
	if(ControllaCF(codice_fiscale)==false){
		msg+=" - il codice fiscale inserito non e' corretto.<br/>";
		errore=true;
	}	
	
	if(errore){
		display_message_on_div("messaggi-form","<fieldset class=\"errore\"><legend>Errori da correggere</legend><p>"+msg+"</p></fieldset>");
		// return false;
	}
	else{
		
		// 
		display_message_on_div("messaggi-form","<fieldset class=\"errore\"><legend>Validazione in corso</legend><p><img src='/resource/images/loading.gif' alt='loading' /></p><p>Attenzione stiamo controllando i dati da te inseriti.</p><p>Attendi qualche istante per favore.</p></fieldset>");
		display_message_on_div("but_verifica","");
		
		url_pass= "&antispam=" + antispam;
		url_pass= url_pass + "&tipologia=privato";
		url_pass= url_pass + "&compra_prepagato100=" + compra_prepagato100;
		url_pass= url_pass + "&pri_nome=" + nome;
		url_pass= url_pass + "&pri_cognome=" + cognome;
		url_pass= url_pass + "&pri_citta=" + citta;
		url_pass= url_pass + "&pri_via=" + via;
		url_pass= url_pass + "&pri_domicilio=" + domicilio;
		url_pass= url_pass + "&pri_provincia=" + provincia;
		url_pass= url_pass + "&pri_cap=" + 	cap;
		url_pass= url_pass + "&pri_domicilio=" + domicilio;
		url_pass= url_pass + "&pri_telefono=" + telefono;
		url_pass= url_pass + "&pri_cellulare=" + 	cellulare;
		url_pass= url_pass + "&pri_fax=" + fax;
		url_pass= url_pass + "&pri_email=" + email;
		url_pass= url_pass + "&pri_professione=" + professione;
		url_pass= url_pass + "&pri_citta_nascita=" + citta_nascita;
		url_pass= url_pass + "&pri_data_nascita=" + data_nascita;
		url_pass= url_pass + "&pri_tipo_documento_identita=" + tipo_identita;
		url_pass= url_pass + "&pri_numero_documento_identita=" + numero_identita;
		url_pass= url_pass + "&pri_numero_patente=" + numero_patente;
		url_pass= url_pass + "&pri_categoria_patente=" + categoria_patente;
		url_pass= url_pass + "&pri_cf=" + codice_fiscale;
		
		//alert(url_pass);
		
		registrazione(url_pass);
	}

}

function registrazione(url_passato){
	var urlpass = "/ajax.php?do=registrazione" + url_passato;

	// alert(urlpass);
		
	if (xmlHttp){
		// try to connect to the server
		try {
			// initiate the asynchronous HTTP request
			xmlHttp.open("GET", urlpass, true);
			// xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// display the error in case of failure
		catch (e){
		display_message_on_div("messaggi-form","<fieldset class=\"errore\"><legend>Errore</legend><p>Attenzione si e verificato una errore nella connessione al server.</p></fieldset>");
		}
	}
}


function domicilio_set_form(){
	domicilio= getCheckedValue(document.form.set_domicilio);
	if(domicilio=="SI"){
		document.form.pri_domicilio.value = 
			document.getElementById('pri_nome').value + " " + 
			document.getElementById('pri_cognome').value + " " + 
			document.getElementById('pri_via').value + " " + 
			document.getElementById('pri_citta').value + " " + 
			document.getElementById('pri_cap').value + " " + 
			document.getElementById('pri_provincia').value;
	}
	else if(domicilio=="NO"){
		document.form.pri_domicilio.value="";
	}
}

/**************************************
	Controllo del Codice Fiscale
***************************************/

function ControllaCF(cf){
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return false;
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return false;
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return false;
	return true;
}

/*****************************************
	Controllo della Partita I.V.A.
******************************************/

function ControllaPIVA(pi){
	if( pi == '' )  return '';
	if( pi.length != 11 )
		return false;
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return false;
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return false;
	return true;
}



function checkform_registrazione_azienda(){
	errore=false;
	msg="";

	compra_prepagato100	= getCheckedValue(document.form.compra_prepagato100);
	nome = document.getElementById('azi_nome').value;
	cognome = document.getElementById('azi_cognome').value;
	ragione_sociale = document.getElementById('azi_ragione_sociale').value;
	attivita = document.getElementById('azi_attivita').value;
	citta = document.getElementById('azi_citta').value;
	via = document.getElementById('azi_via').value;
	provincia = document.getElementById('azi_provincia').value;
	cap = document.getElementById('azi_cap').value;
	telefono = document.getElementById('azi_telefono').value;
	cellulare = document.getElementById('azi_cellulare').value;
	fax = document.getElementById('azi_fax').value;
	email = document.getElementById('azi_email').value;
	codice_fiscale = document.getElementById('azi_cf').value.replace(/\s+/g,'');
	piva = document.getElementById('azi_piva').value.replace(/\s+/g,'');
	privacy	= getCheckedValue(document.form.set_privacy);
	antispam = document.getElementById('antispam').value;


	if(isEmpty(ragione_sociale) ){
		msg+=" - occorre completare il campo Ragione sociale.<br/>";
		errore=true;
	}
	if(isEmpty(attivita) ){
		msg+=" - occorre completare il Attivita'.<br/>";
		errore=true;
	}
	if(isEmpty(nome) ){
		msg+=" - occorre completare il campo Nome.<br/>";
		errore=true;
	}
	if( isEmpty(cognome) ){
		msg+=" - occorre completare il campo Cognome.<br/>";
		errore=true;
	}
	if(isEmpty(citta) ){
		msg+=" - occorre completare il campo Citta.<br/>";
		errore=true;
	}
	if(isEmpty(via) ){
		msg+=" - occorre completare il campo Via.<br/>";
		errore=true;
	}
	if(provincia=="NULL"){
		msg+=" - occorre completare il campo Provincia.<br/>";
		errore=true;
	}
	if( isEmpty(cap) || (cap.length != 5) ){
		msg+=" - occorre completare il campo CAP.<br/>";
		errore=true;
	}
	else if(!isInteger(cap) ){
		msg+=" - occorre completare il campo CAP con il valore corretto.<br/>";
		errore=true;

	}
	if( isEmpty(cellulare) ){
		msg+=" - occorre completare il campo Cellulare.<br/>";
		errore=true;
	}
	if( isEmpty(email) ){
		msg+=" - occorre completare il campo E-mail.<br/>";
		errore=true;
	}
	else if(!echeck(email)){
		msg+=" - L'indirizzo email inserito non ha un formato corretto.<br/>";
		errore=true;
	}
	if( isEmpty(antispam) ){
		msg+=" - occorre completare il campo Codice antispam.<br/>";
		errore=true;
	}
	else if(!ck_string(antispam,"([0-9A-Z]){4}")){
		msg+=" - il campo Codice antispam deve contenere 4 caratteri in maiuscolo!<br/>";
		errore=true;
	}
	if(privacy=="NO"){
		msg+=" - occorre accettare le condizioni sulla privacy per poter continuare.<br/>";
		errore=true;
	}
	if(ControllaPIVA(piva)==false){
		msg+=" - La partita IVA inserita non e' corretta. Ricontrolla con attenzione il dato inserito.<br/>";
		errore=true;
	}	
	if(getCheckedValue(document.form.set_cfpiva)=="NO"){
		if(ControllaCF(codice_fiscale)==false){
			msg+=" - il codice fiscale inserito non e' corretto.<br/>";
			errore=true;
		}	
	}	
	if(errore){
		display_message_on_div("messaggi-form","<fieldset class=\"errore\"><legend>Errori da correggere</legend><p>"+msg+"</p><p>Per eventuali problemi di registrazione non esitare a <a href=\"/area/dove-siamo\">contattarci</a></p></fieldset>");
		// return false;
	}
	else{
		
		// 
		display_message_on_div("messaggi-form","<fieldset class=\"errore\"><legend>Validazione in corso</legend><p><img src='/resource/images/loading.gif' alt='loading' /></p><p>Attenzione stiamo controllando i dati da te inseriti.</p><p>Attendi qualche istante per favore.</p></fieldset>");
		display_message_on_div("but_verifica","");
		
		url_pass= "&antispam=" + antispam;
		url_pass= url_pass + "&tipologia=azienda";
		url_pass= url_pass + "&compra_prepagato100=" + compra_prepagato100;
		url_pass= url_pass + "&azi_nome=" + nome;
		url_pass= url_pass + "&azi_cognome=" + cognome;
		url_pass= url_pass + "&azi_ragione_sociale=" + ragione_sociale;
		url_pass= url_pass + "&azi_attivita=" + attivita;
		url_pass= url_pass + "&azi_citta=" + citta;
		url_pass= url_pass + "&azi_via=" + via;
		url_pass= url_pass + "&azi_provincia=" + provincia;
		url_pass= url_pass + "&azi_cap=" + 	cap;
		url_pass= url_pass + "&azi_telefono=" + telefono;
		url_pass= url_pass + "&azi_cellulare=" + 	cellulare;
		url_pass= url_pass + "&azi_fax=" + fax;
		url_pass= url_pass + "&azi_email=" + email;
		url_pass= url_pass + "&azi_piva=" + piva;
		url_pass= url_pass + "&azi_cf=" + codice_fiscale;
		
		// alert(url_pass);
		
		registrazione(url_pass);
	}

}

function cf_set_form(){
	cf= getCheckedValue(document.form.set_cfpiva);
	if(cf=="SI"){
		document.form.azi_cf.value = document.getElementById('azi_piva').value;
		document.form.azi_cf.disabled=true;
	}
	else if(cf=="NO"){
		document.form.azi_cf.value="";
		document.form.azi_cf.disabled=false;
	}
}

function finalizza_registrazione(codice,tipologia){
	var urlpass = "/ajax.php?do=finalizza_registrazione&codice=" + codice + "&tipologia=" + tipologia;

	// alert(urlpass);
		
	if (xmlHttp){
		// try to connect to the server
		try {
			// initiate the asynchronous HTTP request
			xmlHttp.open("GET", urlpass, true);
			// xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// display the error in case of failure
		catch (e){
			alert("errore di connessione verso il server");
		}
	}
}
