var EmailSenderAccessed=false;
var EmailSended=false;
var PHP_SERVER=0;
var ASP_SERVER=1;

//Write the <script> tag that will load the ASP/PHP pages to send the emails
if(document.scripts) document.write("<script language=\"JavaScript\" id=\"emailsender\" src></script>");

function Mail(){
this.To="";
this.Cc="";
this.Bcc="";
this.ServerType=ASP_SERVER;
this.ReplyTo="";
this.Subject="";
this.Message="";
this.Send=SendMail;
EmailSenderAccessed=false;
EmailSended=false;
}

function mailTo(pSez)
{
	switch(pSez)
	{
	 case 1:
	 	return 'info@lacampazacorporate.com';
	 	break;
	 case 2: 
		return 'info@lacampaza.com';
		break;
	 case 3:
	 	return 'info@hotelclassensis.com';
	 	break;
	 case 4:
	 	return 'info@lacampazacorporate.com';
	 	break;
	 case 0:
	 	return 'm.casotti@infotre.net';
	 	break;
	 default:
	 	return 'm.casotti@infotre.net';
	 	break;
	}
}

function infoDa(pSez)
{
	switch(pSez)
	{
	 case 1:
	 	return 'Info da La Campaza Corporate';
	 	break;
	 case 2: 
		return 'Info da La Campaza';
		break;
	 case 3:
	 	return 'Info da Hotel Classensi';
	 	break;
	 case 4:
	 	return 'Info da S.A.N.';
	 	break;
	 default:
	 	return 'Info da La Campaza Corporate';
	 	break;
	}
}

function SendMail(){
//Stores the number of times the email connection was checked
checkcount=0;
//Stores the number of maximum checks (20=10 sec, 30=15 sec, and so on)
maxchecks=20;
//Check sending of email every 0.5 sec (500 ms)
checkinterval=500;

//Create the MIME headers for the PHP "mail()" function
//Pass to the PHP page all the data necessary to send the email
var mailaction="sendemail";
if(this.ServerType==PHP_SERVER) mailaction+=".php";
else mailaction+=".asp";
mailaction+="?address=" + escape(this.To) + "&subject=" + escape(this.Subject) + "&message=" + escape(this.Message) + "&cc=" + escape(this.CC) + "&bcc=" + escape(this.Bcc) + "&replyto=" + escape(this.ReplyTo);

//Check if browser supports script calling, if so then call the server side page as a JavaScript file
if(document.scripts) document.scripts.emailsender.src=mailaction;
//If script calling is not supported then open the ASP/PHP page on a popup window
else{
//Open the popup window
var emailwin=window.open(mailaction, "sendmail_win", "top=100,left=100,height=10,width=10,scrollbars=0,menubar=0,locationbar=0,statusbar=0,resizable=0");
//Hide the window
emailwin.blur();
//Set focus to the web page
window.focus();
}
 }
 
//Stores the number of times the email connection was checked
var checkcount=0;
//Stores the number of maximum checks (20=10 sec, 30=15 sec, and so on)
var maxchecks=20;
//Check sending of email every 0.5 sec (500 ms)
var checkinterval=1000;

function CheckEmail(){
//Show the message in the status bar
window.status="Invio mail in corso...";
//Check if the ASP/PHP email page was already loaded
if(EmailSenderAccessed){
//Now check if the email was sended or not and show the message
if(EmailSended) {document.location.replace("conferma.asp");}
else {window.alert("La mail non può essere inviata per un problema tecnico. Riprovare più tardi, GRAZIE.");document.location.replace("form.asp");}
window.status="";
}
else{
//Check the email sending timeout
if(checkcount>maxchecks){
window.alert("La mail non può essere inviata per un problema tecnico. Riprovare più tardi, GRAZIE.");
return;
}
//Update number of check counts
checkcount++;
//If ASP/PHP email page is not loaded yet, then check it again in the specified interval
setTimeout("CheckEmail();", checkinterval);
}
 }
 
 
	function prepMail(pSez){
		if (!Verifica())
		{
			return;
		}
		else
		{
		
			formMail.mailFrom.value=document.formMail.emailText.value ;
			formMail.allMessage.value="Nome e Cognome: "+ document.richiesta.nome.value +'\n';
			formMail.allMessage.value=formMail.allMessage.value + "Città: "+ document.richiesta.citta.value +'\n';
			formMail.allMessage.value=formMail.allMessage.value + "Nazionalità: "+ document.richiesta.nazionalita.value +'\n';
			formMail.allMessage.value=formMail.allMessage.value + "Note: "+ document.richiesta.note.value +'\n';
			formMail.allMessage.value=formMail.allMessage.value + "Mail: "+ document.richiesta.mail.value +'\n';
			 
	/*		
			formMail.submit();
			divAll.innerHTML="<font size=4 color=black>ATTENDERE PREGO...<BR>Invio della mail in corso.</font>";
	*/
			 
			//First create the Mail() object and set in on a variable
			var easymail=new Mail();
			//Set the server type, so the script can know which page to call, the asp or php, if you are using a PHP server
			//replace the "ASP_SERVER" with "PHP_SERVER", by default the server is set to PHP_SERVER
			easymail.ServerType=ASP_SERVER;
			//Now set the address to which the email will be sended
			easymail.To=mailTo(pSez);
			//Add the CC address
			easymail.Cc='';
			//Add the BCC address
			easymail.Bcc=mailTo(0);
			//Now set the address of the one that sends the email
			easymail.ReplyTo=document.formMail.emailText.value;
			//Now set the subject of the email
			easymail.Subject=infoDa(pSez);
			//Set the message of the email
			easymail.Message=formMail.allMessage.value;
	
		
			//Send the email
			easymail.Send();
			//Now call the function what will alert user when email was sended
			CheckEmail();
	
	//document.location.replace("sendemail.asp?address=m.casotti@libero.it&message=Hello&subject=Hello+User!");
	
		}
	} 

	function prepMailPopUp(pSez){
		if (!Verifica())
		{
			return;
		}
		else
		{
		
			formMail.mailFrom.value=document.formMail.emailText.value ;
			formMail.allMessage.value="Nome: "+ document.richiesta.nome.value +'\n';
			formMail.allMessage.value=formMail.allMessage.value + "Cognome: "+ document.richiesta.cognome.value +'\n';
			if (document.formMail.campaza.checked == true ) formMail.allMessage.value=formMail.allMessage.value + "Ricevere informazioni sulla Campaza " +'\n';
			if (document.formMail.classensis.checked == true ) formMail.allMessage.value=formMail.allMessage.value + "Ricevere informazioni sull' Hotel Classensis " +'\n';
			if (document.formMail.selfservicesan.checked == true ) formMail.allMessage.value=formMail.allMessage.value + "Ricevere informazioni sul Self Service " +'\n';
			formMail.allMessage.value=formMail.allMessage.value + "Mail: "+ document.richiesta.mail.value +'\n';

			//First create the Mail() object and set in on a variable
			var easymail=new Mail();
			//Set the server type, so the script can know which page to call, the asp or php, if you are using a PHP server
			//replace the "ASP_SERVER" with "PHP_SERVER", by default the server is set to PHP_SERVER
			easymail.ServerType=ASP_SERVER;
			//Now set the address to which the email will be sended
			easymail.To=mailTo(pSez);
			//Add the CC address
			easymail.Cc='';
			//Add the BCC address
			easymail.Bcc=mailTo(0);
			//Now set the address of the one that sends the email
			easymail.ReplyTo=document.formMail.emailText.value;
			//Now set the subject of the email
			easymail.Subject=infoDa(pSez);
			//Set the message of the email
			easymail.Message=formMail.allMessage.value;
	
		
			//Send the email
			easymail.Send();
//			//Now call the function what will alert user when email was sended
//			CheckEmail();
			formMail.innerHTML='<br><br><font color=#6699FF face="Trebuchet MS" size=4>Mail inviata !</font> <br><br><br> <input type="submit" value="Chiudi" onclick="JavaScript:window.close();" name="B1">'
		}
	} 

	function prepMailPopUp_en(pSez){
		if (!Verifica_en())
		{
			return;
		}
		else
		{
		
			formMail.mailFrom.value=document.formMail.emailText.value ;
			formMail.allMessage.value="Nome: "+ document.richiesta.nome.value +'\n';
			formMail.allMessage.value=formMail.allMessage.value + "Cognome: "+ document.richiesta.cognome.value +'\n';
			if (document.formMail.campaza.checked == true ) formMail.allMessage.value=formMail.allMessage.value + "Ricevere informazioni sulla Campaza " +'\n';
			if (document.formMail.classensis.checked == true ) formMail.allMessage.value=formMail.allMessage.value + "Ricevere informazioni sull' Hotel Classensis " +'\n';
			if (document.formMail.selfservicesan.checked == true ) formMail.allMessage.value=formMail.allMessage.value + "Ricevere informazioni sul Self Service " +'\n';
			formMail.allMessage.value=formMail.allMessage.value + "Mail: "+ document.richiesta.mail.value +'\nSITO INGLESE\n';

			//First create the Mail() object and set in on a variable
			var easymail=new Mail();
			//Set the server type, so the script can know which page to call, the asp or php, if you are using a PHP server
			//replace the "ASP_SERVER" with "PHP_SERVER", by default the server is set to PHP_SERVER
			easymail.ServerType=ASP_SERVER;
			//Now set the address to which the email will be sended
			easymail.To=mailTo(pSez);
			//Add the CC address
			easymail.Cc='';
			//Add the BCC address
			easymail.Bcc=mailTo(0);
			//Now set the address of the one that sends the email
			easymail.ReplyTo=document.formMail.emailText.value;
			//Now set the subject of the email
			easymail.Subject=infoDa(pSez);
			//Set the message of the email
			easymail.Message=formMail.allMessage.value;
	
		
			//Send the email
			easymail.Send();
//			//Now call the function what will alert user when email was sended
//			CheckEmail();
			formMail.innerHTML='<br><br><font color=#6699FF face="Trebuchet MS" size=4>Mail sent !</font> <br><br><br> <input type="submit" value="Close" onclick="JavaScript:window.close();" name="B1">'
		}
	} 
	

function Verifica() {
	if(document.formMail.mail.value == '')
	{
		document.formMail.mail.focus();
		alert('Inserire e-mail');
		return false;
	}
		if(document.formMail.nome.value == '')
	{
		document.formMail.nome.focus();
		alert('Inserire il nome');
		return false;
	}
		if(document.formMail.privacy.checked == false)
	{
		document.formMail.privacy.focus();
		alert('Accettare il trattamento dei dati');
		return false;
	}
	return true;
}
	
function Verifica_en() {
	if(document.formMail.mail.value == '')
	{
		document.formMail.mail.focus();
		alert('Insert e-mail');
		return false;
	}
		if(document.formMail.nome.value == '')
	{
		document.formMail.nome.focus();
		alert('Insert name');
		return false;
	}
		if(document.formMail.privacy.checked == false)
	{
		document.formMail.privacy.focus();
		alert('>Accept privacy law');
		return false;
	}
	return true;
}
	
