/*-----------------------------------
        MOSTRA LE VARIE PAGINE
------------------------------------*/

var controlloform = Class.create();


controlloform.prototype = {
	
	initialize: function(esito) {
		this.id = 1;
		this.esitoRichiesta = esito ? esito : false;
		//alert(this.esitoRichiesta);
	},
	
	controlloForm: function(nomeForm){
		var msg='Warning:\n\n';
		var valore=true;
		var x = 0;
		var campi;
		var contenuto = '<b>X</b>';
		var stilecontenuto = 'testo_rosso';
		var divcampo = 'f-';
		var divcampotmp;
		
		campi=this.arrayform(nomeForm);
		var radioprecedente='';
		var radioval=false;
		for (x=0; x<campi.length; x++){
			
			divcampotmp= campi[x][0];
			
			this.eliminaElemento(divcampotmp+'-w');
			//alert(campi[x][1]);
			switch (campi[x][1]) { 
  				case 'obl-testo': 
					if($F(campi[x][0]) == ''){
						valore=false;
						
						msg=msg + $(campi[x][0]).title + '\n'; 
						//this.cambiaStile(divcampotmp,'obbligatorio');
						this.creaElementoDopo(divcampotmp,contenuto,stilecontenuto);
					}
  					break; 
				case 'obl-numeri':
					var pattern = /^[0-9]*$/;
					if($F(campi[x][0]) == '' || !pattern.test($F(campi[x][0]))){
						valore=false;
						msg=msg + $(campi[x][0]).title + '\n'; 
						//this.cambiaStile(divcampotmp,'obbligatorio');
						this.creaElementoDopo(divcampotmp,contenuto,stilecontenuto);
					}
  					break; 
				case 'obl-email':
					var pattern = /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;
					if($F(campi[x][0]) == '' || !pattern.test($F(campi[x][0]))){
						valore=false;
						msg=msg + $(campi[x][0]).title + '\n'; 
						//this.cambiaStile(divcampotmp,'obbligatorio');
						this.creaElementoDopo(divcampotmp,contenuto,stilecontenuto);
					}
  					break; 
  				case 'obl-password':
					var pattern = /^[a-zA-Z0-9]+$/;
					if($F(campi[x][0]) == '' || !pattern.test($F(campi[x][0])) || $F(campi[x][0]).length<8){
						valore=false;
						msg=msg + $(campi[x][0]).title + '\n'; 
						//this.cambiaStile(divcampotmp,'obbligatorio');
						tmpcontenuto = contenuto + ' 8 lettres min.';
						this.creaElementoDopo(divcampotmp,tmpcontenuto,stilecontenuto);
					}
  					break; 
  				case 'obl-password-ctr':
					var pattern = /^[a-zA-Z0-9]+$/;
					if($F(campi[x][0]) == '' || !pattern.test($F(campi[x][0])) || $F(campi[x][0]) != $F('password')){
						valore=false;
						msg=msg + $(campi[x][0]).title + '\n'; 
						//this.cambiaStile(divcampotmp,'obbligatorio');
						this.creaElementoDopo(divcampotmp,contenuto,stilecontenuto);
					}
  					break; 
  				case 'obl-user':
					var pattern = /^[a-zA-Z0-9]+$/;
					if($F(campi[x][0]) == '' || !pattern.test($F(campi[x][0])) || $F(campi[x][0]).length<6){
						valore=false;
						msg=msg + $(campi[x][0]).title + '\n'; 
						tmpcontenuto = contenuto + ', min. 6 caratteri';
						//this.cambiaStile(divcampotmp,'obbligatorio');
						this.creaElementoDopo(divcampotmp,tmpcontenuto,stilecontenuto);
					}
  					break;
				case 'obl-solo-testo':
					var pattern = /^[a-zA-Z]+$/;
					if($F(campi[x][0]) == '' || !pattern.test($F(campi[x][0]))){
						valore=false;
						msg=msg + $(campi[x][0]).title + '\n'; 
						//this.cambiaStile(divcampotmp,'obbligatorio');
						this.creaElementoDopo(divcampotmp,contenuto,stilecontenuto);
					}
  					break;
				case 'obl-dollari':
					// [$]1[##][.###]+[,##]
					// [$]1###+[,##]
					// [$]0,##
					// [$],##
					//var pattern = /^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/;
					var pattern = /^\$?\-?([1-9]{1}[0-9]{0,2}(\.[0-9]{4})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/;
					if($F(campi[x][0]) == '' || !pattern.test($F(campi[x][0]))){
						valore=false;
						msg=msg + $(campi[x][0]).title + '\n'; 
						this.cambiaStile(divcampotmp,'obbligatorio');
						this.creaElementoDopo(divcampotmp,contenuto,stilecontenuto);
					}
  					break;
				case 'obl-tendina':
					if($(campi[x][0]).selectedIndex == 0){
						valore=false;
						msg=msg + $(campi[x][0]).title + '\n'; 
						//this.cambiaStile(divcampotmp,'obbligatorio');					
						this.creaElementoDopo(divcampotmp,contenuto,stilecontenuto);
					}
  					break;
				default: altezzaCella=200; 
			}
			
		}
		if(valore==false){
			//alert(msg);
		}
		
		return valore;
	},
	arrayform: function(nomeform){
		var campi = new Array();
		var i = 0;
		this.form = $(nomeform);
		Form.getElements(this.form).each(function(input){
			if(input.id !='' && input.className!=''){
				campi[i] = new Array(input.id,input.className);
				i = i+1;
			}
		}
	)
	return campi;
	},
	
	cambiaStile: function (campo,stile){
		if($(campo)!=null)
			$(campo).className = stile;	
	},
	
	eliminaElemento: function(id){
		try{
			Element.remove(id);	
		}catch(e){		
		}
	},
	
	creaElementoDopo: function(campo,contenuto,stile){
		var contenitore = '';
		
		contenitore = '<span id="'+campo+'-w" class="'+stile+'">'+contenuto+'</span>';
		if($(campo)!=null)
			new Insertion.After(campo,contenitore);

	},
	
	returnesitorichiesta: function(){
		return this.esitoRichiesta;
	},
	
	allerta: function(msg){
		alert(msg);
		return true;
	}
	
}

var cc = new controlloform();

function controllo(nomeform){
	var valore=false;
	valore=cc.controlloForm(nomeform);
	//alert(valore);
	return valore;
}