//LIBRERIA CONTROLLI JAVASCRIPT

//variabili gestione oggetti form
var nelem 
var value
var i

var tipo
var type
var modo
var nome
var lbl

//fine def variabili


//Evidenzia il campo selezionato
function borderit(which,color){
	if (document.all||document.getElementById){
		which.style.borderColor = color
	}
}


//Evidenzia un campo che riporta un errore di testo o numerico
function errore(callingForm,code,lbl){

	callingForm.elements[i].style.borderColor="red"
	m58Alert(code,lbl);
	callingForm.elements[i].focus()
}
//fine errore


//Elimina segnalazione campi testo errati
function init(callingForm){
	nelem = callingForm.length
	for(var j=0; j<nelem; j++)
		callingForm.elements[j].style.borderColor=""
}
//fine init


//Corpo centrale dei controlli; richiama poi le funzioni specificate altrove
function checkform(callingForm){

	var tempData, blnIsDate
	var a
	
	nelem = callingForm.length	
	init(callingForm)

//alert("nelem= [" + nelem + "] - modo= [" + modo + "]")
	
	//Per ogni elemento
	for ( i=0; i< nelem ;i++){

		tipo = callingForm.elements[i].getAttribute('tipo');
		name = callingForm.elements[i].getAttribute('name');
		lbl  = callingForm.elements[i].getAttribute('lbl');
		modo = callingForm.elements[i].getAttribute('modo');
		type = callingForm.elements[i].getAttribute('type');
		lung = callingForm.elements[i].getAttribute('lung');
		

//alert("name= [" + name + "] - modo= [" + modo + "]")

		//Se richiesto testo oppure numero
		if( modo=="richiesto" ){
			if(type=="checkbox") { 
			
				r=callingForm.elements[i].checked;
				
				while(callingForm.elements[i+1]){
					if (callingForm.elements[i+1].getAttribute('name') != name) {
						break;
					}
					i++;
					if(!r){
						r=callingForm.elements[i].checked;
					}	
				}
			
				
				if(!r){
					errore(callingForm, "FRM001", lbl) ;
					return false;
				}
			}
			else {
				valore=callingForm.elements[i].value;
				valore=valore.replace(/\s+$|^\s+/g,"");
				if(valore==""){
					errore(callingForm, "FRM002", lbl) ;
					return false
				}
			}
		}

		//Se deve essere Testo
		if(tipo=="text"){
			valore=callingForm.elements[i].value
			if(valore!="" && isstring(valore)==0){
				errore(callingForm, "FRM003", lbl) ;
				return false
			}
			
		}

		//Se deve essere Numero
		if(tipo=="number"){
			valore=callingForm.elements[i].value
			if(valore!="" && isnumero(valore)==0){
				errore(callingForm, "FRM004", lbl);
				return false
			}
		}
		//Se la lunghezza deve essere Limitata
		if(tipo=="limitato"){	
			valore=callingForm.elements[i].value			
			if(valore.length > lung ){
				errore(callingForm, "FRM005", lbl);
				return false
			}			
		}

		//Controllo menu a tendina
		if( modo=="richmenu" ){
			if(callingForm.elements[i].selectedIndex=="0"){
				errore(callingForm, "FRM006", lbl);
				return false
			}	
		}

		//Se deve essere Data
		if(tipo=="date" && callingForm.elements[i].value != ""){
			tempData = callingForm.elements[i].value
			
			// Controlla se è una data in vari formati
			blnIsDate = false;

//alert("Data= [" + tempData + "] ");
			
			if(!blnIsDate) blnIsDate = isDate(tempData,"ddMMy");
			if(!blnIsDate) blnIsDate = isDate(tempData,"d-M-y");
			if(!blnIsDate) blnIsDate = isDate(tempData,"d/M/y");

			if(!blnIsDate){
				
//alert("Data= [" + tempData + "] NON VALIDA");
				
				errore(callingForm, "FRM007", lbl)
				return false
			}

//alert("Data= [" + tempData + "] VALIDA ");

		}
		
		//Se deve essere Ora
		if(tipo=="time" && callingForm.elements[i].value != ""){
			tempData = callingForm.elements[i].value
			
			// Controlla se è un'ora in vari formati
			blnIsDate = false;

//alert("Ora= [" + tempData + "] ");
			
			if(!blnIsDate) blnIsDate = isDate(tempData,"k-m");
			if(!blnIsDate) blnIsDate = isDate(tempData,"k/m");					
			if(!blnIsDate) blnIsDate = isDate(tempData,"kk-mm");
			if(!blnIsDate) blnIsDate = isDate(tempData,"kk/mm");
									
			if(!blnIsDate){
				
//alert("Ora= [" + tempData + "] NON VALIDA");
				
				errore(callingForm, "FRM008", lbl)
				return false
			}

//alert("Data= [" + tempData + "] VALIDA ");

		}	
	

		if(type=="text" && tipo=="email"){
			valore=callingForm.elements[i].value
			if(!isEmail(valore)){
				errore(callingForm, "FRM009", lbl)
				return false
			}
		}

		// Privacy CheckBox
		if(type=="checkbox" && modo=="privacy"){
			valore=callingForm.elements[i].checked
			if(!valore){
				alert("Please, AGREE with PRIVACY terms before to continue.")
				return false
			}
		}

	}
	//fine for
	return true;
}
//fine checkform


//controllo se è una stringa incluso anche il carettere -> ' <- indispensabile per accenti etc..
function isstring(valore){

	if(valore.length<1)
		return 0
		
	for(var i=0; i< valore.length ; i++){
		if( (valore.charAt(i) <'A'  || valore.charAt(i) >'z' ||  (valore.charAt(i)>'Z' && valore.charAt(i) < '_' ) )&& escape(valore.charAt(i))!="%27")
			return 0
	}
	
	return 1
}
//fine isstring


//verifica la presenza di un numero
function isnumero(valore){

	if(valore.length<1)
		return 0

	for(var i=0; i< valore.length ; i++){
		if( valore.charAt(i) <'0'|| valore.charAt(i) >'9')
			return 0
	}

	return 1
}
//fine  isnumero


//funzione data controlla che mese e giorno siano corretti è possibile //inserire anni dal 1900 al 2000
var gg
var mm
var an

function data(gg, mm, an){
	if( gg=="" || mm=="" || an=="")
		return 0

	if( eval(mm) < 1 || eval(mm) > 12)
		return 0

	if( eval(mm) == 1 || eval(mm)== 3 || eval(mm)== 5 || eval(mm)==7 || eval(mm)==8 || eval(mm)==10 || eval(mm)==12)
		if( eval(gg) > 31 || eval(gg)<0)
			return 0

	if( eval(mm) == 4 || eval(mm)== 6 || eval(mm)== 9 || eval(mm)==11)
		if( eval(gg) > 30 || eval(gg)<0)
			return 0

	if( eval(mm)==2)
		if( eval(gg)>28 || eval(gg)<0 )
			return 0

	if( anno(an)==0)
		return 0

	return 1
}
//fine data


function anno(an){
	if( eval(an) < 0001 || eval(an) > 9999)
		return 0
	return 1
}


//function controllo email
function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}
//fine isEmail
