/* Modifyed by ST
   v2.0 20081119 includes a check for the quotaFree field
   The whole script should be more generic
   function CheckRequired ()
   function emailCheck (emailStr)
   CheckQuota(qfree,vquota)
   function checkQuota05()
   
*/

/* ValidateFields_ita.js
   Check if the required fields are filled and the format of the email is correct
   ST 20071126

   The code for the mail validation is from:
   Sandeep V. Tamhankar (stamhankar - hotmail.com)
   The JavaScript Source!! http://javascript.internet.com
*/

function CheckRequired () {
var emptyFields=''
var errMsg="Non sono stati riempiti i seguenti campi obbligatori:\n\n"

if (document.form1.cognome.value=="") {
emptyFields = "   Cognome\n";
}
if (document.form1.nome.value=="") {
emptyFields = emptyFields + "   Nome\n";
}
if (document.form1.indirizzo.value=="") {
emptyFields = emptyFields + "   Indirizzo\n";
}
if (document.form1.cap.value=="") {
emptyFields = emptyFields + "   CAP\n";
}
if (document.form1.citta.value=="") {
emptyFields = emptyFields + "   Citta\n";
}
if (document.form1.nazione.value=="") {
emptyFields = emptyFields + "   Nazione\n";
}
if (document.form1.email.value=="") {
emptyFields = emptyFields + "   Email\n";
}
if (emptyFields=='') {
   // Validate the email
   return emailCheck(document.form1.email.value);
} else {
   errMsg = errMsg + emptyFields;
   alert(errMsg);
   return false;
}

}

//
// mail validation
//
function emailCheck (emailStr) {

var emailPat=/^(.+)@(.+)$/

var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"

var validChars="\[^\\s" + specialChars + "\]"

var quotedUser="(\"[^\"]*\")"

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

var atom=validChars + '+'

var word="(" + atom + "|" + quotedUser + ")"

var userPat=new RegExp("^" + word + "(\\." + word + ")*$")

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Indirizzo Email non valido (check '@' and '.')");
	document.form1.email.select(); return false;
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    // user is not valid
    alert("Indirizzo Email non valido:\n\nControllare lo 'username'.");
    document.form1.email.focus(); return false;
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
    for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
		alert("Indirizzo Email non valido:\n\nDestination IP address is invalid!");
		document.form1.email.focus(); return false;
	    }
    }
    return true;
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Indirizzo Email non valido:\n\nManca il dominio.")
	document.form1.email.focus(); return false;
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>4) {
   // the address must end in a two letter or three letter word.
   alert("Indirizzo Email non valido:\n\nLa parte finale ha piu' di 4 lettere\n o meno di 2.")
   document.form1.email.focus(); return false;
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="Indirizzo Email non valido:\n\nNon sembra della forma 'user@domain.com'";
   alert(errStr);
   document.form1.email.focus(); return false;
}

// Check tel and cel 20100329
var telFormat = /^\+{0,1}[0-9,\s,\-,\/]+$/;
var matchTel = document.form1.tel.value.match(telFormat);
var matchCel = document.form1.fax.value.match(telFormat);

if (document.form1.tel.value && (matchTel==null || document.form1.tel.value.length < 6)) {
	alert("\nCaratteri non permessi nel campo Telefono\n");
	document.form1.tel.focus(); return false;
}
if (document.form1.fax.value && (matchCel==null || document.form1.fax.value.length < 6)) {
	alert("\nCaratteri non permessi nel campo Cellulare\n");
	document.form1.fax.focus(); return false;
}

// altro check
var noteFormat = /[<,>]/;
var matchNote = document.form1.note.value.match(noteFormat);
if (document.form1.note.value && matchNote!=null) {
	alert("\nCaratteri non permessi nel campo Note\n");
	document.form1.note.focus(); return false;
}

if ( document.form1.quota05.checked ) {
   return CheckQuota(document.form1.quotaFree.value, document.form1.minQuota.value);
}

// If we've gotten this far, everything's valid!
return;
}
//  End -->


/* v1.0 ST 20081118 for jgb
   Check the value of 'qoutaFree' with 'minQuota' and a hardcoded limit.
   The check is almost generic, only the hardcoded limit and the focus...
   Is called from function 'function emailCheck (emailStr)' after validating the email
*/

function CheckQuota(qfree,vquota) {

// Harcoded limit of 30.000 euro
var maxAmount = 30000;

var onlyNumbers = /^\d+$/;

var matchNumbers = qfree.match(onlyNumbers);

if (matchNumbers==null) {
	alert("\nNel campo quota sono solo\npermessi numeri interi: 0123456789\n");
	document.form1.quotaFree.focus(); return false;
}

// Trasformiamo i parametri in numero, forse c'e' un modo piu' semplice....
var Amount = qfree * 1;
var minAmount = vquota * 1;

if (Amount < minAmount) {
	var errMsg = "\nIl contributo deve essere maggiore o\nuguale alla quota d\'iscrizione: " + minAmount + " euro\n\n";
	alert(errMsg);
	document.form1.quotaFree.focus(); return false;
}

if (Amount > maxAmount) {
	alert("Il contributo non puo' superare 30.000 euro\n");
	document.form1.quotaFree.focus(); return false;
}

return true;
}

/* From Stefano Bargioni 20081119
   Check the radio button quota5 when focus on quotaFree
*/
function checkQuota05() {
 document.getElementById('quota05').checked=true;
}

