function MM_openBrWindow(theURL,winName,features) { //v2.0
my_window = window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function nomeregione(ilnome)
{
	if(ilnome!=''){document.getElementById('regionechoose').innerHTML=ilnome;}
	else{document.getElementById('regionechoose').innerHTML='';}
}

function checkstringa(valore,max)
{
	if(valore==''){return '0';}

	var lungo = valore.length;
	if (lungo<'1' || lungo>max )
	{
		return 0;
	}

}

function fadesione()
{
	var urlTOcall = 'include/azioni.html';

	// cognome
	var cognome = document.forms['registra'].cognome.value;
	var verifica = checkstringa(cognome,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare NOME E COGNOME");
		document.forms['registra'].cognome.focus();
		return false;
	}

	// ruolo
	var ruolo = document.forms['registra'].ruolo.value;
	var verifica = checkstringa(ruolo,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare la QUALIFICA/MANSIONE");
		document.forms['registra'].ruolo.focus();
		return false;
	}

	// azienda
	var azienda = document.forms['registra'].azienda.value;
	var verifica = checkstringa(azienda,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare la RAGIONE SOCIALE");
		document.forms['registra'].azienda.focus();
		return false;
	}

	// cf
	var cf = document.forms['registra'].cf.value;
	var verifica = checkstringa(cf,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare il CODICE FISCALE");
		document.forms['registra'].azienda.focus();
		return false;
	}

	// pi
	var pi = document.forms['registra'].pi.value;
	var verifica = checkstringa(pi,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare la PARTITA IVA");
		document.forms['registra'].pi.focus();
		return false;
	}

	// indirizzo
	var indirizzo = document.forms['registra'].indirizzo.value;
	var verifica = checkstringa(indirizzo,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare l'INDIRIZZO");
		document.forms['registra'].pi.focus();
		return false;
	}

	// comune
	var comu = document.forms['registra'].comu.value;
	var verifica = checkstringa(comu,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare il COMUNE");
		document.forms['registra'].comu.focus();
		return false;
	}

	// prov
	var prov = document.forms['registra'].prov.value;
	var verifica = checkstringa(prov,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare la PROVINCIA");
		document.forms['registra'].prov.focus();
		return false;
	}

	// cap
	var cap = document.forms['registra'].cap.value;
	var verifica = checkstringa(cap,'6');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare il CAP");
		document.forms['registra'].cap.focus();
		return false;
	}

	// telaz
	var telaz = document.forms['registra'].telaz.value;
	var verifica = checkstringa(telaz,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare il NUMERO TELEFONO AZIENDA");
		document.forms['registra'].telaz.focus();
		return false;
	}

	// web
	var web = document.forms['registra'].web.value;
	var verifica = checkstringa(web,'80');
	if (verifica == 0)
	{
	//	alert("Attenzione! Devi indicare il SITO WEB AZIENDALE");
	//	document.forms['registra'].web.focus();
	//	return false;
	}

	// numerodip
	var numerodip = document.forms['registra'].numerodip.value;
	var verifica = checkstringa(numerodip,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare il NUMERO DIPENDENTI");
		document.forms['registra'].numerodip.focus();
		return false;
	}

	// telref
	var telref = document.forms['registra'].telref.value;
	var verifica = checkstringa(telref,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare il TELEFONO UFFICIO DEL RICHIEDENTE");
		document.forms['registra'].telref.focus();
		return false;
	}

	// mobref
	var mobref = document.forms['registra'].mobref.value;
	var verifica = checkstringa(mobref,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare il TELEFONO CELLULARE DEL RICHIEDENTE");
		document.forms['registra'].mobref.focus();
		return false;
	}

	// emailref
	var emailref = document.forms['registra'].emailref.value;
	var verifica = checkstringa(emailref,'80');
	if (verifica == 0)
	{
		alert("Attenzione! Devi indicare l'indirizzo E-MAIL DEL RICHIEDENTE");
		document.forms['registra'].emailref.focus();
		return false;
	}

	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(emailref))
	{
		alert('Attenzione! Devi indicare un indizzo E-MAIL DEL RICHIEDENTE valido');
		document.forms['registra'].emailref.focus();
		return false;
	}

	document.getElementById('prosegui').style.display="none";
	document.getElementById('wait').style.display="block";

	//call webservice
	var myRandom=parseInt(Math.random()*99999999);  // cache buster
	var param = "action=adesione&cognome=" + encodeURI(cognome) + "&ruolo=" + encodeURI(ruolo) + "&azienda=" + encodeURI(azienda) + "&cf=" + encodeURI(cf) + "&pi=" + encodeURI(pi) + "&indirizzo=" + encodeURI(indirizzo) + "&comu=" + encodeURI(comu) + "&prov=" + encodeURI(prov) + "&cap=" + encodeURI(cap) + "&telaz=" + encodeURI(telaz) + "&web=" + encodeURI(web) + "&numerodip=" + encodeURI(numerodip) + "&telref=" + encodeURI(telref) + "&mobref=" + encodeURI(mobref) + "&emailref=" + encodeURI(emailref) + "&rnd=" + myRandom;

	var http = false;
	try{http = new XMLHttpRequest();}
	catch(e)
	{
		try{http = new ActiveXObject('Msxml2.XMLHTTP');}
		catch(e){http = new ActiveXObject('Microsoft.XMLHTTP');}
	}

	if (!http)
	{
		document.getElementById('prosegui').style.display="block";
		document.getElementById('wait').style.display="none";

		alert('ATTENZIONE, si è verificato un problema tecnico.');
		return false;
	}

	http.onreadystatechange = function()
	{
		if (http.readyState == 4 && http.status == 200)
		{
			var reply = http.responseText;
			var reponse=reply.split("|");

			//alert(reply);
			//return false;

			if (reponse[0] == 'ok')
			{
				document.forms['registra'].convenzione.value=reponse[1];

				if (document.forms['registra'].logo.value == '')
				{
					document.forms['registra'].action = 'convenzioni.html?ok=1';
				}
				else
				{
					document.forms['registra'].action = 'include/azioni.html';
				}

				document.forms['registra'].submit();
			}
			else
			{
				alert('ATTENZIONE, tutti i campi indicati sono obbligatori.');
				return false;
			}
		}
	}

	var urlTOcall = 'include/azioni.html';
	http.open('POST', urlTOcall, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", param.length);
	http.setRequestHeader("Connection", "close");
	http.send(param);


}

