/*
---------------------------------------------
-     This JS code was last modified by     -
-     Gianluigi Cosari on January  2003     -
-          E-mail: glcosari@isys.it         -
-       Subject : Active Document 3.0       -
---------------------------------------------
*/


// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
    	// Check that current character is number.
    	var c = s.charAt(i);
    	if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
    	var c = s.charAt(i);
    	if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
  // EXCEPT for centurial years which are not also divisible by 400.
  return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
  	} 
  	return this;
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("La data deve essere nel formato: gg/mm/aaaa");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Mese non valido nella data.");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Giorno non valido nella data.");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("L'anno indicato nella data deve essere compreso tra "+minYear+" e "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Data non valida.");
		return false;
	}
	return true;
}

function isCurrency(dtStr){
	var ValidCurrencyChars = "0123456789.,";
	var vInvalid=0;
	for(i=0;i<dtStr.length;i++){
		ValidCurrencyChars.indexOf(dtStr.charAt(i))<0?vInvalid+= 1:null;
	}
	dotPos=dtStr.indexOf(".");
	if (dotPos != -1) {
		if (dotPos > 0) {
			dotPos!=dtStr.lastIndexOf(".") || dotPos<1 ? vInvalid+=1:null;
		} else {
			vInvalid+=1;
		}
	}
	dotPos=dtStr.indexOf(",");
	if (dotPos != -1) {
		if (dotPos > 0) {
			dotPos!=dtStr.lastIndexOf(",") || dotPos<1 ? vInvalid+=1:null;
		} else {
			vInvalid+=1;
		}
	}
	if(vInvalid!=0){
		alert("Uno o più campi valuta contengono dati non validi.");
		return false;
	}
	return true;
}

function ValidateForm(){
	var dt=document.frmSample.txtDate;
	if (isDate(dt.value)==false){
		dt.focus();
		return false;
	}
    return true;
}
 
// Fa la validazione dell'indirizzo di E-Mail
function ckEmailAddr(eAddr){
// Ritorna true se ok, false se non ok
	var DomainValidChars="1234567890abcdefghijklmnopqrstuvwxyz-.",UserValidChars=DomainValidChars+"_";
	var eInvalid=0,atPos,strCk;
	var msgEmpt="L'indirizzo di E-Mail è vuoto.",msgInva="L'indirizzo di E-Mail non è valido o contiene caratteri illegali.";
	if(eAddr==""){
		alert(msgEmpt);
		return false;
	}
	atPos=eAddr.indexOf("@");
	atPos!=eAddr.lastIndexOf("@") || atPos<1 || atPos>(eAddr.length-5)?eInvalid+=1:null;
	strCk=eAddr.substring(0,atPos);
	for(i=0;i<strCk.length;i++){
		UserValidChars.indexOf(strCk.charAt(i))<0?eInvalid+= 1:null;
	}
	strCk=eAddr.slice(atPos+1);
	for(i=0;i<strCk.length;i++){
		DomainValidChars.indexOf(strCk.charAt(i))<0?eInvalid+=1:null;
	}
	strCk.indexOf(".")<0?eInvalid+=1:null;
	strCk=strCk.charAt(strCk.length-1);
	strCk=="." || strCk=="-"?eInvalid+=1:null;
	if(eInvalid!=0){
		alert(msgInva);
		return false;
	}
	return true;
}

// Fa la validazione del form
function FrmChk(which){
	var pass=true;
	if (document.images){		
		for (ff=0;ff<which.length;ff++){
			var tempobj=which.elements[ff];
			var strToTest=tempobj.name.substring(0,2);
			switch (strToTest) {
				case "t_": // Il campo da testare è un testo
					if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&(tempobj.selectedIndex==-1)||tempobj.value=="")){
						alert("Uno o più campi obbligatori mancanti.")
						return false;
					}
					break;
				case "e_": // Il campo da testare contiene un indirizzo di e-mail
					if (!ckEmailAddr(tempobj.value)) {
						return false;
					}
					break;
				case "d_": // Il campo da testare contiene una data ed è obbligatorio
					if (!isDate(tempobj.value)) {
						return false;
					}
					break;
				case "c_": // Il campo da testare contiene una data ma non è obbligatorio (viene testato solo se non è vuoto)
					if (!tempobj.value=='') {
						if (!isDate(tempobj.value)) {
							return false;
						}
					}
					break;
				case "v_": // Il campo da testare contiene una valuta ed è obbligatorio
					if (!isCurrency(tempobj.value)) {
						return false;
					}
					break;
				case "u_": // Il campo da testare contiene una valuta ma non è obbligatorio (viene testato solo se non è vuoto)
					if (!tempobj.value=='') {
						if (!isCurrency(tempobj.value)) {
							return false;
						}
					}				
				 break;
			}
		}
	}
	return true;
}	