// crea l'oggetto per la comunicazione AJAX con il server
// compatibile con tutti i browser che supportano AJAX
function crea_http_req() {
	var req = false;
	if (typeof XMLHttpRequest != "undefined")
		req = new XMLHttpRequest();
	if (!req && typeof ActiveXObject != "undefined") {
		try {
			req=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				req=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				try {
					req=new ActiveXObject("Msxml2.XMLHTTP.4.0");
				} catch (e3) {
					req=null;
				}
			}
		}
	}

	if(!req && window.createRequest)
		req = window.createRequest();

	if (!req) alert("Il browser non supporta AJAX");

	return req;
}

function checkcf(i) {
	var xmlhttp = crea_http_req();

	var pars = '';
		pars += 'nome=' + document.forms[i].nome.value;
		pars += '&cognome=' + document.forms[i].cognome.value;
		pars += '&sesso=' + document.forms[i].sesso.value;
		pars += '&giorno=' + document.forms[i].giorno.value;
		pars += '&mese=' + document.forms[i].mese.value;
		pars += '&anno=' + document.forms[i].anno.value + document.forms[i].anno1.value + document.forms[i].anno2.value;
		pars += '&comune=' + document.forms[i].codcomune.value;
		pars += '&cf=' + document.forms[i].codicefiscale.value;
		
  xmlhttp.onreadystatechange=function() {
  	if (xmlhttp.readyState==4) {
  		
  		if (xmlhttp.responseText == 'true') {
  			document.getElementById('cfverify').innerHTML = "<img src=\"./i/chk.gif\" alt=\"\" title=\"\" />";
  			document.forms[i].vcf.value = 1;
  		} else {
  			document.getElementById('cfverify').innerHTML = "<img src=\"./i/cancel.gif\" alt=\"\" title=\"\" />";
  			document.forms[i].vcf.value = 0;
  		}

   	}
  }
 xmlhttp.open("POST", "./verifica_cf.aspx", true);
 xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 xmlhttp.setRequestHeader("Content-length", pars.length);
 xmlhttp.setRequestHeader("Connection", "close");
 xmlhttp.send(pars);
}

function checklogin(i) {
	var xmlhttp = crea_http_req();
	
	var pars = '';
		pars += 'login=' + document.forms[i].login.value;
		
	xmlhttp.onreadystatechange=function() {
  	if (xmlhttp.readyState==4) {
  		
  		//alert(xmlhttp.responseText);
  		if (xmlhttp.responseText == 'true') {
  			document.getElementById('loginverify').innerHTML = "<img src=\"./i/chk.gif\" alt=\"\" title=\"\" />";
  			document.forms[i].vlg.value = 1;
  		} else {
  			document.getElementById('loginverify').innerHTML = "<img src=\"./i/cancel.gif\" alt=\"\" title=\"\" />";
  			document.forms[i].vlg.value = 0;
  		}

   	}
  }
 xmlhttp.open("POST", "./verifica_login.aspx", true);
 xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 xmlhttp.setRequestHeader("Content-length", pars.length);
 xmlhttp.setRequestHeader("Connection", "close");
 xmlhttp.send(pars);
}

function checkiban(i) {
	var xmlhttp = crea_http_req();
	var pars = '';
	    pars += 'iban=' + document.forms[i].iban.value;
	    
	xmlhttp.onreadystatechange=function() {
  		if (xmlhttp.readyState==4) {
  			if (xmlhttp.responseText == 'true') {
  				document.forms[i].checkiban.value = 1;
  			} else {
  				document.forms[i].checkiban.value = 0;
  			}
  		}
  	}
  	
  	xmlhttp.open("POST", "./verifica_iban.aspx", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", pars.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(pars);
}

function populate_combo(i) {
	var xmlhttp = crea_http_req();
	
	var pars = '';
		pars += 'prov=' + document.forms[i].prov.value;
	
	document.getElementById('comune').innerHTML = "<img src=\"/i/loader.gif\" alt=\"\" title=\"\" style=\"margin: 3px 0px 3px 50%;\" />";
	xmlhttp.onreadystatechange=function() {
  		if (xmlhttp.readyState==4) {
			document.getElementById('comune').innerHTML = xmlhttp.responseText;
   		}
  }
 xmlhttp.open("POST", "./popola_comuni.aspx", true);
 xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 xmlhttp.setRequestHeader("Content-length", pars.length);
 xmlhttp.setRequestHeader("Connection", "close");
 xmlhttp.send(pars);
}

function manda_mail(guid) {

	var xmlhttp = crea_http_req();
	var pars = '';
	    pars += 'guid=' + guid;
	 
	xmlhttp.onreadystatechange=function() {
  		if (xmlhttp.readyState==4) {
  			if (xmlhttp.responseText == 'true') {
  				alert("Mail inviata!");
  			} else {
  				alert("Mail non inviata!");
  			}
  		}
  	}
  	
  	xmlhttp.open("POST", "./manda_mail_ant2friends.aspx", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", pars.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(pars);
}

function CheckCountry(b)
{
	Country = b.substring(0,2)

	if ( Country == "AT" && b.length == 20){
		return true
		}

	if ( Country == "BE" && b.length == 16){
		return true
		}

	if ( Country == "DE" && b.length == 22){
		return true
		}

	if ( Country == "DE" && b.length == 22){
		return true
		}

	if ( Country == "DK" && b.length == 18){
		return true
		}

	if ( Country == "ES" && b.length == 24){
		return true
		}

	if ( Country == "FI" && b.length == 18){
		return true
		}

	if ( Country == "FR" && b.length == 27){
		return true
		}

	if ( Country == "GB" && b.length == 22){
		return true
		}

	if ( Country == "GR" && b.length == 27){
		return true
		}

	if ( Country == "IE" && b.length == 22){
		return true
		}

	if ( Country == "IT" && b.length == 27){
		return true
		}

	if ( Country == "LU" && b.length == 20){
		return true
		}

	if ( Country == "NL" && b.length == 18){
		return true
		}

	if ( Country == "PT" && b.length == 25){
		return true
		}

	if ( Country == "SE" && b.length == 24){
		return true
		}

	return false; 

}

function IBANChk(iban)
{
	iban = iban.toUpperCase() ;

	s = iban.substring(4) + iban.substring(0, 4);
	for (i = 0, r = 0; i < s.length; i++ )
	{
		c = s.charCodeAt(i);
		if (48 <= c && c <= 57)
		{
			if (i == s.length-4 || i == s.length-3) { alert("Positions 1 and 2 cannot contain digits"); return false; }
			k = c - 48;
		}
		else if (65 <= c && c <= 90)
		{
			if (i == s.length-2 || i == s.length-1) { alert("Positions 3 and 4 cannot contain letters"); return false; }
			k = c - 55;
		}
		else { alert("Only digits and uppercase letters are allowed"); return false; }
		if (k > 9)
			r = (100 * r + k) % 97;
		else
			r = (10 * r + k) % 97;
	}
	if (r != 1) { return false; }

	if (CheckCountry (iban)) 
		{
		 return true;
		}

	return false;
}
