//#########################################
// ## ENGLISH FORM SCRIPTS
//#########################################
// ## chk_send() 
// ## used in ENGLISH "feedback_en.asp" and "feedback-MED_en.asp"
function chk_send()
{
	if(document.frm_invio.consenso[0].checked)
	{				
		if(trim(document.frm_invio.Nome.value)=="") 
		{
			frm_invio.Nome.focus();
			alert("Field « Name » is required.");
			return false;
		}
		if(trim(document.frm_invio.Cognome.value)=="") 
		{
			frm_invio.Cognome.focus();
			alert("Field « Surname » is required.");
			return false;
		}
		if(trim(document.frm_invio.Email.value)=="") 
		{
			frm_invio.Email.focus();
			alert("Field « E-mail » is required.");
			return false;
		}
		if (!validateAddress(frm_invio.Email.value))
		{
			frm_invio.Email.focus();
			alert("Please provide a valid Email Address.");
			return false;
		}
		return true;
	} 
	else 
        {
		alert("In order to send this message you have to authorize us to process your personal information.");
		return false;
	}
}
//#########################################
// ## chk_send_mans()
// ## used in english "feedback-MED_en.asp"
function chk_send_mans()
{
	if(document.frm_invio.consenso[0].checked)
	{				
		if(trim(document.frm_invio.Nome.value)=="") 
		{
			frm_invio.Nome.focus();
			alert("Field « Name » is required.");
			return false;
		}
		if(trim(document.frm_invio.Cognome.value)=="") 
		{
			frm_invio.Cognome.focus();
			alert("Field « Surname » is required.");
			return false;
		}
		if(trim(document.frm_invio.Email.value)=="") 
		{
			frm_invio.Email.focus();
			alert("Field « E-mail » is required.");
			return false;
		}
                if (!validateAddress(frm_invio.Email.value))
		{
			frm_invio.Email.focus();
			alert("Please provide a valid Email Address.");
			return false;
		}
		if(trim(document.frm_invio.Mansione.value)=="") 
		{
			frm_invio.Mansione.focus();
			alert("Field « Function » is required.");
			return false;
		}
		return true;
	} 
	else 
        {
		alert("In order to send this message you have to authorize us to process your personal information.");
		return false;
	}
}
//#########################################
// ## chk_send_copia()
// ## used in english "copia_en.asp"
function chk_send_copia()
{
	if(document.frm_invio.consenso[0].checked)
	{				
		if(trim(document.frm_invio.Nome.value)=="") 
		{
			frm_invio.Nome.focus();
			alert("Field « Name » is required.");
			return false;
		}
		if(trim(document.frm_invio.Cognome.value)=="") 
		{
			frm_invio.Cognome.focus();
			alert("Field « Surname » is required.");
			return false;
		}
		if(trim(document.frm_invio.Email.value)=="") 
		{
			frm_invio.Email.focus();
			alert("Field « E-mail » is required.");
			return false;
		}
                if (!validateAddress(frm_invio.Email.value))
		{
			frm_invio.Email.focus();
			alert("Please provide a valid Email Address.");
			return false;
		}
		if(trim(document.frm_invio.Indirizzo.value)=="") 
		{
			frm_invio.Indirizzo.focus();
			alert("Field « Address » is required.");
			return false;
		}
		if(trim(document.frm_invio.CAP.value)=="") 
		{
			frm_invio.CAP.focus();
			alert("Field « Zip Code » is required.");
			return false;
		}
		if(trim(document.frm_invio.Provincia.value)=="") 
		{
			frm_invio.Provincia.focus();
			alert("Field « State » is required.");
			return false;
		}
		if(trim(document.frm_invio.Citta.value)=="") 
		{
			frm_invio.Citta.focus();
			alert("Field « City » is required.");
			return false;
		}
		if(trim(document.frm_invio.Nazione.value)=="") 
		{
			frm_invio.Nazione.focus();
			alert("Field « Country » is required.");
			return false;
		}		
		return true;
	} 
	else {
		alert("In order to send this message you have to authorize us to process your personal information.");
		return false;
	}
}

function validateAddress(incoming) 
        {
        var emailstring = incoming;
        var ampIndex = emailstring.indexOf("@");
        var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
        // find a dot in the portion of the string after the ampersand only
        var dotIndex = afterAmp.indexOf(".");
        // determine dot position in entire string (not just after amp portion)
        dotIndex = dotIndex + ampIndex + 1;
        // afterAmp will be portion of string from ampersand to dot
        afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
        // afterDot will be portion of string from dot to end of string
        var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
        var beforeAmp = emailstring.substring(0,(ampIndex));
        //old regex did not allow subdomains and dots in names
        //var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
        var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
        // index of -1 means "not found"
        if ((emailstring.indexOf("@") != "-1") &&
        (emailstring.length > 5) &&
        (afterAmp.length > 0) &&
        (beforeAmp.length > 1) &&
        (afterDot.length > 1) &&
        (email_regex.test(emailstring)) ) 
        {
                return true;
        } else {                
                return false;
        }
}

function LTrim(stringa){
  var str=''
  str = stringa.replace(/^ +/,'')
  return str
}
function RTrim(stringa){
  var str=''
  str = stringa.replace(/ +$/,'')
  return str
}
function trim(stringa){
  var str=''
  str = stringa.replace(/^ +/,'')
  str = str.replace(/ +$/,'')
  return str
}