﻿function validate(lingua)
{
	document.getElementById('response').innerHTML="";
	var p = document.forms['form_prenotazione'];

	var JSONObject = new Object;
	JSONObject.lingua=lingua; 
	JSONObject.nome =  p['nome'].value;
	JSONObject.cognome =  p['cognome'].value;
	JSONObject.telefono = p['telefono'].value;
	JSONObject.citta = escape(p['citta'].value);
	JSONObject.email = p['email'].value;
	JSONObject.days_arr = p['days_arr'].value;
	JSONObject.months_arr = p['months_arr'].value;
	JSONObject.years_arr = p['years_arr'].value;
	JSONObject.days_par = p['days_par'].value;
	JSONObject.months_par = p['months_par'].value;
	JSONObject.years_par = p['years_arr'].value;
	JSONObject.struttura = p['id_struttura'].value;
	JSONObject.nazione = p['nazione'].value;
	JSONObject.trattamento = escape(p['trattamento'].value);
	JSONObject.persone = p['persone'].value;
	JSONObject.bimbi = p['bimbi'].value;
	if (p['risorsa1']!=null)
		JSONObject.risorsa1 = p['risorsa1'].value;
	else
		JSONObject.risorsa1 = 0;
	if (p['risorsa2']!=null)
		JSONObject.risorsa2 = p['risorsa2'].value;
	else
		JSONObject.risorsa2 = 0;
	if (p['risorsa3']!=null)
		JSONObject.risorsa3 = p['risorsa3'].value;
	else
		JSONObject.risorsa3 = 0;
	JSONObject.messaggio = escape(p['messaggio'].value);
	JSONObject.captcha = p['captcha'].value;
	if (p['consenso'].checked)
		JSONObject.consenso = p['consenso'].value;
	else
		JSONObject.consenso = "0";
	JSONstring = JSON.stringify(JSONObject);
	getMessageResponse(JSONstring);
}
  		
function getMessageResponse(str)
{
	var xmlHttp;
	try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
	catch (e)
		{
			// Internet Explorer
			try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
			catch (e)
		{
			try
				{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch (e)
				{
					alert("Your browser does not support AJAX!");
					return false;
				}
		}
		}
		xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 && xmlHttp.status==200)
			{
			document.getElementById('wait').style.visibility = "hidden";
				document.getElementById('response').innerHTML=xmlHttp.responseText;
			if (xmlHttp.responseText.indexOf("art-postsuccess")!=-1)
			{
				document.form_prenotazione.nome.value = '';
				document.form_prenotazione.cognome.value = '';
				document.form_prenotazione.telefono.value = '';
				document.form_prenotazione.citta.value = '';
				document.form_prenotazione.email.value = '';
				document.form_prenotazione.persone.value = '0';
				document.form_prenotazione.nazione.value = '0';
				document.form_prenotazione.messaggio.value = '';
				document.form_prenotazione.consenso.checked = false;
				document.form_prenotazione.captcha.value = '';
				var now = new Date();
				document.form_prenotazione.days_arr.value = now.getDate();
				document.form_prenotazione.months_arr.value = now.getMonth()+1;
				document.form_prenotazione.years_arr.value = '0';
				document.form_prenotazione.days_par.value = now.getDate();
				document.form_prenotazione.months_par.value = now.getMonth()+1;
				document.form_prenotazione.years_par.value = '0';
				document.form_prenotazione.trattamento.value = '0';
				if (document.form_prenotazione.risorsa1!=null)
					document.form_prenotazione.risorsa1='0';
				if (document.form_prenotazione.risorsa2!=null)
					document.form_prenotazione.risorsa2='0';
				if (document.form_prenotazione.risorsa3!=null)
					document.form_prenotazione.risorsa3='0';
			}
			
			refresh_captcha();
			}
		else
		{
			document.getElementById('wait').style.visibility = "visible";
		}

	}

	var url="include/check_prenotazione/check_message.php";
	var content = "data="+str;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", content.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(content);
}

function refresh_captcha()
{
	document.getElementById('img_captcha').src = 'include/visual-captcha-informazioni.php?' + Math.random(); 
	return false;
}
