function getViewMailECS(){
		
		var id_azienda = document.getElementById("x_id_azienda").value;
		var data1 = getCorrectDateForDB(document.getElementById("x_data1").value);
		var data2 = getCorrectDateForDB(document.getElementById("x_data2").value);
		var promo = document.getElementById("x_promo").value;		
		window.open("/gestionale/stasbranger/s/fserver.php?service_ecs_view_mail&id_azienda=" + id_azienda + "&data1="+ data1 + "&data2="+ data2 + "&promo="+ promo);
}

function getViewMailECM(){
		
		var data1 = getCorrectDateForDB(document.getElementById("x_data1").value);
		var data2 = getCorrectDateForDB(document.getElementById("x_data2").value);
		var promo = document.getElementById("x_promo").value;	
		var id_categoria = document.getElementById("x_id_categoria").value;		
		window.open("/gestionale/stasbranger/s/fserver.php?service_ecm_view_mail&data1="+ data1 + "&data2="+ data2 + "&promo="+ promo + "&id_categoria="+ id_categoria);
		
		
		
//	var request = zXmlHttp.createRequest();
//	request.onreadystatechange = function() {
//		try{
//			if(request.readyState==4){
//				if(request.status==200){
//				alert("risposta positiva");
//				}else {
//                	//request.status is 503  if the application isn't available;
//                	//500 if the application has a bug
//                	/*
//alert("A problem occurred with communicating between"+
//                    	" the XMLHttpRequest object and the server program.");
//*/
//            		}
//            	}			
//			}catch (err){
//        		alert("It does not appear that the server "+
//           			   "is available for this application. Please"+
//            		  " try again very soon. \nError: "+err.message);
//
//   			 }
//
//		};
//	var _url ="/gestionale/stasbranger/s/fserver.php?service_ecm_view_mail&data1="+ data1 + "&data2="+ data2 + "&promo="+ promo + "&id_categoria="+ id_categoria;
//	request.open("GET", _url, true);
//	request.send(null);
		
		
}

function getFattureInsolute(){
		
		var data1 = getCorrectDateForDB(document.getElementById("x_data1").value);
		var data2 = getCorrectDateForDB(document.getElementById("x_data2").value);
		var data_fatt = getCorrectDateForDB("x_data_fatt").value;	
		var id_categoria = document.getElementById("x_id_categoria").value;		
		window.open("/gestionale/fattureinsolute.php?data1="+ data1 + "&data2="+ data2 + "&data_fatt="+ data_fatt + "&id_categoria="+ id_categoria);
}

function getFattureTotale(){
		
		var data1 = getCorrectDateForDB(document.getElementById("x_fattura_data").value);
		var data2 = getCorrectDateForDB(document.getElementById("y_fattura_data").value);	
		window.open("/gestionale/fatturetotale.php?data1="+ data1 + "&data2="+ data2);
}

function getContest(){
	
	var data1 = getCorrectDateForDB(document.getElementById("x_datains").value);
	var data2 = getCorrectDateForDB(document.getElementById("y_datains").value);	
	window.open("/gestionale/contestazioniriepilogo.php?data1="+ data1 + "&data2="+ data2);
}
function getSendPassatiAziendeProva(){
		
		var data1 = getCorrectDateForDB(document.getElementById("x_data1").value);
		var data2 = getCorrectDateForDB(document.getElementById("x_data2").value);
		var id_categoria = document.getElementById("x_id_categoria").value;		
		window.open("/gestionale/stasbranger/s/fserver.php?service_passati_multipli_prova&data1="+ data1 + "&data2="+ data2 + "&id_categoria="+ id_categoria);
}

function getMailUtenti(){
	
	var data1 = getCorrectDateForDB(document.getElementById("x_data_preventivo").value);
	var data2 = getCorrectDateForDB(document.getElementById("y_data_preventivo").value);
	var id_categoria = document.getElementById("x_id_categoria").value;	
	var prov = document.getElementById("x_provincia_sigla").value;
	window.open("/gestionale/mail_user.php?data1="+data1+"&data2="+data2+"&categoria="+id_categoria+"&prov="+prov);
}

function getMailAziende(){
	
	
	var id_categoria = document.getElementById("x_id_categoria").value;	
	var prov = document.getElementById("x_provincia_sigla").value;
	window.open("/gestionale/mail_aziende.php?categoria="+id_categoria+"&prov="+prov);
}

function switchAzienda(){
	
	var data1 = getCorrectDateForDB(document.getElementById("x_data_preventivo").value);
	var data2 = getCorrectDateForDB(document.getElementById("y_data_preventivo").value);
	var id_categoria = document.getElementById("x_id_categoria").value;	
	var azienda1 = document.getElementById("x_id_azienda").value;
	var azienda2 = document.getElementById("y_id_azienda").value;
	window.open("/gestionale/switch.php?data1="+data1+"&data2="+data2+"&categoria="+id_categoria+"&az1="+azienda1+"&az2="+azienda2);
}


function whitoutAzienda(){
	
	var data1 = getCorrectDateForDB(document.getElementById("x_data_preventivo").value);
	var data2 = getCorrectDateForDB(document.getElementById("y_data_preventivo").value);
	var id_categoria = document.getElementById("x_id_categoria").value;	
	window.open("/gestionale/aziende_whitout_q.php?data1="+data1+"&data2="+data2+"&categoria="+id_categoria);
}

function getViewMailFattS(){
		var id_azienda = document.getElementById("x_id_azienda").value;
		var data1 = getCorrectDateForDB(document.getElementById("x_data1").value);
		var data2 = getCorrectDateForDB(document.getElementById("x_data2").value);
		var periodo = document.getElementById("x_periodo").value;	
		var pagamento = document.getElementById("x_pagamento").value;		
		var data_fatt = document.getElementById("x_data_fatt").value;
		var num_fatt = document.getElementById("x_num_fatt").value;
		var prezzo = document.getElementById("x_prezzo").value;
		window.open("/gestionale/stasbranger/s/fserver.php?service_fatts_view_mail&id_azienda="+id_azienda+"&data1="+ data1 + "&data2="+ data2 + "&periodo="+ periodo + "&pagamento="+ pagamento + "&data_fatt="+ data_fatt + "&num_fatt=" + num_fatt + "&prezzo=" + prezzo);
}

function calcolo(provincia, ricevuti) {
	var id_categoria = document.getElementById("x_categoria").value;
	var request = zXmlHttp.createRequest();
	request.onreadystatechange = function() {
		try {
			if (request.readyState == 4) {
				if (request.status == 200) {
					var resposta =  request.responseText;
					if (resposta != null) {
						var risp = eval("(" + resposta + ")");
						if (risp[0].limite == 'illimitato' && risp[0].residuo == 'illimitato') {
							document.getElementById("limite" + provincia).innerHTML = "illimitato";
							document
									.getElementById("disponibilita" + provincia).innerHTML = "illimitato";
						} else {
							if (risp[0].residuo <= 0) {
								document.getElementById("limite" + provincia).innerHTML = risp[0].limite;
								document.getElementById("disponibilita"+ provincia).innerHTML = risp[0].residuo;
								document.getElementById("tupla_" + provincia).style.backgroundColor = "#ff7575";
							} else {
								document.getElementById("limite" + provincia).innerHTML = risp[0].limite;
								document.getElementById("disponibilita" + provincia).innerHTML = risp[0].residuo;
							}
						}
					} else {
						// request.status is 503 if the application isn't
						// available;
						// 500 if the application has a bug
						alert("A problem occurred with communicating between"
								+ " the XMLHttpRequest object and the server program.");
					}
				}
			}
		} catch (err) {
			alert("It does not appear that the server "
					+ "is available for this application. Please"
					+ " try again very soon. \nError: " + err.message);

		}

	};
	var _url = "/gestionale/adwords_calcolo.php?x_categoria=" + id_categoria
			+ "&x_provincia=" + provincia;
	request.open("GET", _url, true);
	request.send(null);
}

function getRagioneSocialeById(val){
	var request = zXmlHttp.createRequest();
	request.onreadystatechange = function() {
		try{
			if(request.readyState==4){
				if(request.status==200){
					var resp =  request.responseText;
                	if(resp != null){
                    	var ragioneSocialeTextField = document.getElementById('x_ragione_sociale');
                   		var objt = eval("(" + resp + ")");
                    	ragioneSocialeTextField.value = objt.ragione_sociale;
   					}else{
   						var error = document.getElementById('error1');
   						error.innerHTML = "Azienda non trovata"
   					}
				}else {
                	//request.status is 503  if the application isn't available;
                	//500 if the application has a bug
                	alert("A problem occurred with communicating between"+
                    	" the XMLHttpRequest object and the server program.");
            		}
            	}			
			}catch (err){
        		alert("It does not appear that the server "+
           			   "is available for this application. Please"+
            		  " try again very soon. \nError: "+err.message);

   			 }

		};
	var _url ="/gestionale/stasbranger/s/fserver.php?aziende&id_azienda=" + encodeURIComponent(val);
	request.open("GET", _url, true);
	request.send(null);
}

function getFatturaById(val){
	var request = zXmlHttp.createRequest();
	request.onreadystatechange = function() {
		try{
			if(request.readyState==4){
				if(request.status==200){
					var resp =  request.responseText;
                	if(resp != null){
                   		var objt = eval("(" + resp + ")");
                   		document.getElementById('x_fatt_nr').value = objt.fattura_numero;
                    	document.getElementById('x_fatt_data').value = getCorrectDate(objt.fattura_data); 
   					}else{
   						var error = document.getElementById('error1');
   						error.innerHTML = "Fattura non trovata"
   					}
				}else {
                	//request.status is 503  if the application isn't available;
                	//500 if the application has a bug
                	alert("A problem occurred with communicating between"+
                    	" the XMLHttpRequest object and the server program.");
            		}
            	}			
			}catch (err){
        		alert("It does not appear that the server "+
           			   "is available for this application. Please"+
            		  " try again very soon. \nError: "+err.message);

   			 }

		};
	var _url ="/gestionale/stasbranger/s/fserver.php?fatture&id_fattura=" + encodeURIComponent(val);
	request.open("GET", _url, true);
	request.send(null);
}

function getFatturaByFatturaNumero(val){
	var request = zXmlHttp.createRequest();
	request.onreadystatechange = function() {
		try{
			if(request.readyState==4){
				if(request.status==200){
					var resp =  request.responseText;
                	if(resp != null){
                   		var objt = eval("(" + resp + ")");
                   		document.getElementById('x_id_fattura').value = objt.id_fattura;
                    	document.getElementById('x_fatt_data').value = getCorrectDate(objt.fattura_data);            
   					}else{
   						var error = document.getElementById('error1');
   						error.innerHTML = "Fattura non trovata"
   					}
				}else {
                	//request.status is 503  if the application isn't available;
                	//500 if the application has a bug
                	alert("A problem occurred with communicating between"+
                    	" the XMLHttpRequest object and the server program.");
            		}
            	}			
			}catch (err){
        		alert("It does not appear that the server "+
           			   "is available for this application. Please"+
            		  " try again very soon. \nError: "+err.message);

   			 }

		};
	var _url ="/gestionale/stasbranger/s/fserver.php?fatture&fattura_numero=" + encodeURIComponent(val);
	request.open("GET", _url, true);
	request.send(null);
}

function getOneZero(val,segment){
		if(val.toUpperCase() == 'SI' || val.toUpperCase() == 'S'){
			document.getElementById(segment).value = '1';
		}else if(val.toUpperCase() == 'NO' || val.toUpperCase() == 'N'){
				document.getElementById(segment).value = '0';	
			}else if(val == '1' || val == '0'){
				document.getElementById(segment).value = val;	
				}else{
					document.getElementById(segment).value = '';
				}
}

function getCorrectDate(val){
	return val.replace(/(\d\d\d\d).(\d\d).(\d\d)/, "$3/$2/$1");            
}

function getCorrectDateForDB(val){
	return val.replace(/(\d\d).(\d\d).(\d\d\d\d)/, "$3/$2/$1");            
}

function getProvince(val, segment){
	var origOptions = null;
	var request = zXmlHttp.createRequest();
		
	request.onreadystatechange = function() {
		try{
			if(request.readyState==4){
				if(request.status==200){
					var resp =  request.responseText;
                	if(resp != null){
                   		var _select = document.getElementById(segment);
    					var el;
   						//first remove all options
   						while(_select.hasChildNodes()){
   							//_select.value='';
        					for(var i = 0; i < _select.childNodes.length; i++){
            					_select.removeChild(_select.firstChild);
        					}
    					}
    					//now add just the original options
    					//for(var h=0; h < origOptions.length;h++) {
        				//	_select.appendChild(origOptions[h]);
    					//}
    					
    					var obj = eval("(" + resp + ")");
    					
    					//obj is an array of new options values
    					for(var i=0; i < obj.length;i++) {
        					el = document.createElement("option");
							el.setAttribute('value',obj[i]["provincia_sigla"]);
        					el.appendChild(document.createTextNode(obj[i]["provincia"]));
        					_select.insertBefore(el,_select.firstChild);
    					}
						el = document.createElement("option");
						el.setAttribute('selected','selected');
    					el.appendChild(document.createTextNode(''));
    					_select.insertBefore(el,_select.firstChild);
   					}else{
   						var error = document.getElementById('error1');
   						error.innerHTML = "Province non trovate"
   					}
				}else {
                	//request.status is 503  if the application isn't available;
                	//500 if the application has a bug
                	alert("A problem occurred with communicating between"+
                    	" the XMLHttpRequest object and the server program.");
            		}
            	}			
			}catch (err){
        		alert("It does not appear that the server "+
           			   "is available for this application. Please"+
            		  " try again very soon. \nError: "+err.message);

   			 }

		};
	

	var sel2 = document.getElementById(segment);
	
	origOptions = new Array();
    for(var i = 0; i < sel2.options.length; i++){
        origOptions[i] = sel2.options[i];
    }	

	var _url ="/gestionale/stasbranger/s/fserver.php?province&regType=" + encodeURIComponent(val);
	request.open("GET", _url, true);
	request.send(null);
}

function getComuni(val, segment){
	var origOptions = null;
	var request = zXmlHttp.createRequest();
		
	request.onreadystatechange = function() {
		try{
			if(request.readyState==4){
				if(request.status==200){
					var resp =  request.responseText;
                	if(resp != null){

                   		var obj = eval("(" + resp + ")");
                   		var _select = document.getElementById(segment);
    					var el;
    					//first remove all options
    					while(_select.hasChildNodes()){
        					for(var i = 0; i < _select.childNodes.length; i++){
      					     	_select.removeChild(_select.firstChild);
       						}
    					}
    					//now add just the original options
    					//for(var h=0; h < origOptions.length;h++) {
        				//	_select.appendChild(origOptions[h]);
    					//}	
    					//obj is an array of new options values
    					for(var i=0; i < obj.length;i++) {
        				el = document.createElement("option");
						el.setAttribute('value',obj[i]["comune"]);
        				el.appendChild(document.createTextNode(obj[i]["comune"]));
        				_select.insertBefore(el,_select.firstChild);
   					}
					el = document.createElement("option");
					el.setAttribute('selected','selected');
    				el.appendChild(document.createTextNode(''));
    				_select.insertBefore(el,_select.firstChild);
   				}else{
   						var error = document.getElementById('error1');
   						error.innerHTML = "Comuni non trovati"
   					}
			}else {
                	//request.status is 503  if the application isn't available;
                	//500 if the application has a bug
                	alert("A problem occurred with communicating between"+
                    	" the XMLHttpRequest object and the server program.");
            		}
            	}			
		}catch (err){
        		alert("It does not appear that the server "+
           			   "is available for this application. Please"+
            		  " try again very soon. \nError: "+err.message);

   			 }

		};
	

	var sel2 = document.getElementById(segment);
	
	origOptions = new Array();
    for(var i = 0; i < sel2.options.length; i++){
        origOptions[i] = sel2.options[i];
    }

	var _url ="/gestionale/stasbranger/s/fserver.php?comuni&provType=" + encodeURIComponent(val);
	request.open("GET", _url, true);
	request.send(null);
}

function getComuniMultipli(val, segment){
	var origOptions = null;
	var request = zXmlHttp.createRequest();
		
	request.onreadystatechange = function() {
		try{
			if(request.readyState==4){
				if(request.status==200){
					var resp =  request.responseText;
                	if(resp != null){
                   		var obj = eval("(" + resp + ")");
                   		var _select = document.getElementById(segment);
    					var el;
    					//first remove all options
    					while(_select.hasChildNodes()){
        					for(var i = 0; i < _select.childNodes.length; i++){
      					     	_select.removeChild(_select.firstChild);
       						}
    					}
    					//now add just the original options
    					//for(var h=0; h < origOptions.length;h++) {
        				//	_select.appendChild(origOptions[h]);
    					//}	
    					//obj is an array of new options values
    					for(var i=0; i < obj.length;i++) {
        				el = document.createElement("option");
						el.setAttribute('value',obj[i]["comune"]);
        				el.appendChild(document.createTextNode(obj[i]["comune"]));
        				_select.insertBefore(el,_select.firstChild);
   					}
					//el = document.createElement("option");
					//el.setAttribute('selected','selected');
    				//el.appendChild(document.createTextNode(''));
    				//_select.insertBefore(el,_select.firstChild);
   				}else{
   						var error = document.getElementById('error1');
   						error.innerHTML = "Comuni non trovati"
   					}
			}else {
                	//request.status is 503  if the application isn't available;
                	//500 if the application has a bug
                	alert("A problem occurred with communicating between"+
                    	" the XMLHttpRequest object and the server program.");
            		}
            	}			
		}catch (err){
        		alert("It does not appear that the server "+
           			   "is available for this application. Please"+
            		  " try again very soon. \nError: "+err.message);

   			 }

		};
	

	var sel2 = document.getElementById(segment);
	
	origOptions = new Array();
    for(var i = 0; i < sel2.options.length; i++){
        origOptions[i] = sel2.options[i];
    }

	var _url ="/gestionale/stasbranger/s/fserver.php?comuni&provType=" + encodeURIComponent(val);
	request.open("GET", _url, true);
	request.send(null);
}


function setIVATotFattura(val, segment1, segment2){
	document.getElementById(segment1).value = val*0.2;
    document.getElementById(segment2).value = val*1 + val*0.2;            
}

function getECS(val){
	var request = zXmlHttp.createRequest();
	 request.onreadystatechange = function() {
		try{
			if(request.readyState==4){
				if(request.status==200){
					var resp =  request.responseText;
                	if(resp != null){
                   		var objt = eval("(" + resp + ")");
                   		}else{
   						var error = document.getElementById('error1');
   						error.innerHTML = "Fattura non trovata"
   					}
				}else {
                	//request.status is 503  if the application isn't available;
                	//500 if the application has a bug
                	alert("A problem occurred with communicating between"+
                    	" the XMLHttpRequest object and the server program.");
            		}
            	}			
			}catch (err){
        		alert("It does not appear that the server "+
           			   "is available for this application. Please"+
            		  " try again very soon. \nError: "+err.message);

   			 }

		};
	var _url ="/gestionale/stasbranger/s/services.php?service_ec_s&id=" + encodeURIComponent(val);
	request.open("GET", _url, true);
	request.send(null);
}

function getECSTest(val){
	alert(val);
}

function getCategorie(val, segment){
	var origOptions = null;
	var request = zXmlHttp.createRequest();
		
	request.onreadystatechange = function() {
		try{
			if(request.readyState==4){
				if(request.status==200){
					var resp =  request.responseText;
                	if(resp != null){
                   		var _select = document.getElementById(segment);
    					var el;
   						//first remove all options
   						while(_select.hasChildNodes()){
   							//_select.value='';
        					for(var i = 0; i < _select.childNodes.length; i++){
            					_select.removeChild(_select.firstChild);
        					}
    					}
    					//now add just the original options
    					//for(var h=0; h < origOptions.length;h++) {
        				//	_select.appendChild(origOptions[h]);
    					//}
    					
    					var obj = eval("(" + resp + ")");
    					
    					//obj is an array of new options values
    					for(var i=0; i < obj.length;i++) {
        					el = document.createElement("option");
							el.setAttribute('value',obj[i]["id_categoria"]);
        					el.appendChild(document.createTextNode(obj[i]["categoria"]));
        					_select.insertBefore(el,_select.firstChild);
    					}
						el = document.createElement("option");
						el.setAttribute('selected','selected');
    					el.appendChild(document.createTextNode(''));
    					_select.insertBefore(el,_select.firstChild);
   					}else{
   						var error = document.getElementById('error1');
   						error.innerHTML = "Categorie non trovate"
   					}
				}else {
                	//request.status is 503  if the application isn't available;
                	//500 if the application has a bug
                	alert("A problem occurred with communicating between"+
                    	" the XMLHttpRequest object and the server program.");
            		}
            	}			
			}catch (err){
        		alert("It does not appear that the server "+
           			   "is available for this application. Please"+
            		  " try again very soon. \nError: "+err.message);

   			 }

		};
		
	var sel2 = document.getElementById(segment);
	
	origOptions = new Array();
    for(var i = 0; i < sel2.options.length; i++){
        origOptions[i] = sel2.options[i];
    }	

	var _url ="/gestionale/stasbranger/s/fserver.php?categorie&catType=" + encodeURIComponent(val);
	request.open("GET", _url, true);
	request.send(null);
}


