	var myRequest = null;

	function CreateXmlHttpReq(handler) {
	  var xmlhttp = null;
	  try {
		xmlhttp = new XMLHttpRequest();
	  } catch(e) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	  xmlhttp.onreadystatechange = handler;
	  return xmlhttp;
	}
	
	function myHandler(id) {
		if (myRequest.readyState == 4 && myRequest.status == 200) {
			document.getElementById(id).innerHTML = myRequest.responseText;
		}
	}
	
	function invia_richiesta(pag, id) {		
		myRequest = CreateXmlHttpReq(function(){myHandler(id)});
		myRequest.open("GET", pag);
		myRequest.send(null);
	}	
	
	var tot = 0;
	var tot_req = 0;
	
	function gest_privato_azienda(){
		if (document.getElementById('radio_privato').checked) {
			document.getElementById('riga_PIVA').style.visibility = 'hidden';
			if (document.getElementById('radio_fatturazione').checked) 
				tot_req = 8;
			else
				tot_req = 14;		
		}
		else {
			document.getElementById('riga_PIVA').style.visibility = 'visible';
			if (document.getElementById('radio_fatturazione').checked) 
				tot_req = 9;
			else
				tot_req = 15;			
		}
	}
	
	function gest_indirizzi(){
		if (document.getElementById('radio_fatturazione').checked) {
			document.getElementById('area_indirizzo_fatturazione').style.display = 'inline';
			document.getElementById('area_indirizzo_spedizione').style.display = 'none';
			if (document.getElementById('radio_azienda').checked) 
				tot_req = 9;
			else 
				tot_req = 8;	
		}
		else {
			document.getElementById('area_indirizzo_fatturazione').style.display = 'inline';
			document.getElementById('area_indirizzo_spedizione').style.display = 'inline';
			if (document.getElementById('radio_azienda').checked) 
				tot_req = 15;
			else 
				tot_req = 14;
		}			
	}
	
	function set_ok(id){
		var id_chk = "chk_" + id;
		document.getElementById(id_chk).className = 'ok';
		return 1;
	}
	
	function check_required(id){		
		switch (id){
			case 'CF_fatturazione':
			case 'PIVA_fatturazione':
			case 'cap_fatturazione':
			case 'CF_spedizione':
			case 'PIVA_spedizione':
			case 'cap_spedizione':
				if ((id == 'PIVA_fatturazione') || (id == 'PIVA_spedizione')) len = 11;
				else if ((id == 'CF_fatturazione') || (id == 'CF_spedizione')) len = 16;
				else len = 5;
				if (document.getElementById(id).value.length == len) 
					tot += set_ok(id);
			break;
			case 'email_fatturazione':
			case 'email_spedizione':
				var str = document.getElementById(id).value;
				var at="@";
				var dot=".";
				var lat=str.indexOf(at);
				var lstr=str.length;
				var ldot=str.indexOf(dot);
				var res = true;
				if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
				   res = false;
				if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
					res = false;
				if (str.indexOf(at,(lat+1))!=-1)
					res = false;	
				if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
					res = false;
				if (str.indexOf(dot,(lat+2))==-1)
					res = false;				
				if (str.indexOf(" ")!=-1)
					res = false;
				if (res)
					tot += set_ok(id);
				else {
					alert("Formato email non valido");
					document.getElementById(id).focus();
				}
			break;
			default :
				if (document.getElementById(id).value != '')
					tot += set_ok(id);
				
			break;
		}
		if (tot >= tot_req) {
			document.getElementById('puls_invia_ordine').disabled = false;
			document.getElementById('puls_invia_ordine').className = 'puls_invia_ordine_OK';
		}
	}
	
	function invia_ordine(){
		t = tot_req-tot;
		if (t > 0) {
			if (t > 1) 
				alert("Ancora " + t + " campi obbligatori devono essere compilati")			
			else 
				alert("Ancora 1 campo obbligatorio deve essere compilato")
			return false;
		}
		else 
			return true;
	}
	
	
	