/* 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_es.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="Los siguientes campos obligatorios no han sido rellenados:\n\n"

if (document.form1.cognome.value=="") {
emptyFields = "   Apellido\n";
}
if (document.form1.nome.value=="") {
emptyFields = emptyFields + "   Nombre\n";
}
if (document.form1.indirizzo.value=="") {
emptyFields = emptyFields + "   Direccion\n";
}
if (document.form1.cap.value=="") {
emptyFields = emptyFields + "   Codigo Postal\n";
}
if (document.form1.citta.value=="") {
emptyFields = emptyFields + "   Ciudad\n";
}
if (document.form1.nazione.value=="") {
emptyFields = emptyFields + "   Nacion\n";
}
if (document.form1.email.value=="") {
emptyFields = emptyFields + "   Email\n";
}
if (emptyFields=='') {
   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("Direccion Email invalida (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("Direccion Email invalida:\n\nControlar el '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("Direccion Email invalida:\n\nDestination IP address is invalid!");
		document.form1.email.focus(); return false;
	    }
    }
    return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Direccion Email invalida:\n\nFalta el 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("Direccion Email invalida:\n\nLa parte final tiene mas de 4 letras\n o menos de 2.");
   document.form1.email.focus(); return false;
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="Direccion Email invalida:\n\nNo parece de la forma 'user@domain.com'"
   alert(errStr);
   document.form1.email.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("\nEn el campo quota solo pueden\nintroducirse numeros enteros: 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 = "\nEl donativo debe ser mayor o\nigual que la cuota de inscripcion: " + minAmount + " euro\n\n";
	alert(errMsg);
	document.form1.quotaFree.focus(); return false;
}

if (Amount > maxAmount) {
	alert("El donativo no puede superar los 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;
}
