function nextSEL(Obj, SELarray, All){
	if (Obj.type!="hidden"){
		i=0
		add=0
		if(All){
			Obj.options[0] = new Option('--Tutte--',0);	
			add=1
		}
		if(SELarray!=null){
			while (SELarray[i]!=null){
				Obj.options[i+add] = new Option(SELarray[i][1],SELarray[i][0]);
				i=i+1
			}
		}
	}

}

function clearSEL(Obj){
	if (Obj.type!="hidden"){
		i=0;
		while (Obj.options[i]!=null){
			Obj.options[i] = null;
		}
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function moveLayer(obj, x, y) {
  x = x + "px";
  y = y + "px";
  obj.style.left = x;
  obj.style.top = y;
}

function cambiaimmagine(obj){
	img=obj.src;
	len=img.length;
	trimOVER=img.lastIndexOf("_")
	trimEXT=img.lastIndexOf(".")
	if (trimOVER){
		over=img.substr((trimOVER+1),(trimEXT-trimOVER-1))
		if (over=="over"){
			obj.src=img.substr(0,trimOVER)+img.substr(trimEXT);
		}else{
			obj.src=img.substr(0,trimEXT)+"_over"+img.substr(trimEXT);
		}
	}else{
		obj.src=img.substr(0,trimEXT)+"_over"+img.substr(trimEXT);
	}
}

function switchIMG (Bobj,Sobj){
	var img_name= new Array(2);
	var img_path= new Array(2);
	img_path[0]=Bobj.src;
	img_path[1]=Sobj.src;
	i=0;
	
	while(img_path[i]!=null){
		len=img_path[i].length;
		trimP=img_path[i].lastIndexOf("/")
		img_name[i]=img_path[i].substr((trimP+1));
		img_path[i]=img_path[i].substr(0,(trimP+1));
		i++;
	}
	Bobj.src=img_path[0]+img_name[1];
	Sobj.src=img_path[1]+img_name[0];		//attivando questo, l'immagine viene switcata tra i 2 oggetti
}


function NOquote(text){					//change "char in \" and ' char in \'
	txt=text.value.replace(/"/g,'&#34;');
	txt=txt.replace(/'/g,"&#39;");
	text.value=txt;
}

function DELquote(form){
	j=0;
	while (form.elements[j]!=null){
		if (form.elements[j].type=="text" || form.elements[j].type=="password" || form.elements[j].type=="textarea"){
			NOquote(form.elements[j]);
		}
		j++;
	}
}
function CTRLdata(form){//change "char in \" and ' char in \'	
	j=0;
	count=1;
	while (form.elements[j]!=null){
		if (form.elements[j].name.substr(0,4)=="data"){			
			X=form.elements[j].value;
			if(X!=""){	
				pos1=X.indexOf("-");
				if (pos1==2){
					pos2=X.substr(pos1+1,X.length-pos1);
					pos2=pos2.indexOf("-");
					if (pos2==2){
						pos3=X.substr(pos1+pos2+1,X.length-pos2);
						pos3=pos3.indexOf("-");
					}else{
						pos3=100;
					}
				}else{
					pos3=100;
				}
				if ((X.length!=10)||(pos3!=0)){			
					form.elements[j].focus();
					alert ("ATTENZIONE!\n Formato errato nel "+count+"� campo data\n la data deve essere immessa con formato: 23-02-1978.");
					return false;	
				}
			}
			count++;
		}
	j++;
	}
	return true;
}
function CTRL_char(form){		//extract all text fields from form and truncate text by text field size attribute		j=0;
	while (form.elements[j]!=null){
		if (form.elements[j].type=="text" || form.elements[j].type=="password" || form.elements[j].type=="textarea"){
			if(form.elements[j].value.length!=form.elements[j].maxLength){
				form.elements[j].focus();
				alert("ATTENZIONE: Il numero di caratteri inseriti nel campo indicato\n � diverso dal numero richiesto:"+form.elements[j].maxLength);
				return false;
			}
		}
		j++;
	}
	return true;
}	
	function CTRLmail(form){				//a mail must have only one @ cahr and one or more . char
		j=0;
		OK=true;
		Error="";
		while (form.elements[j]!=null){
			if (form.elements[j].name=="mail" && form.elements[j].value!=""){			
				mail_addr=form.elements[j].value;
				
				if (mail_addr.indexOf("@")!=-1){
					if (mail_addr.indexOf("@")!=mail_addr.lastIndexOf("@")){
						Error=Error+"Il formato dell'indirizzo mail � errato.\n";
						OK=false;
					}
				}else{
					Error=Error+"Ha inserito un indirizzo mail senza @.\n";
						OK=false;
				}
				if (mail_addr.indexOf(".")==-1){
					Error=Error+"Ha inserito un indirizzo mail senza un punto.\n";
					OK=false;
				}	
				form.elements[j].value=form.elements[j].value.toLowerCase();
			}
			j++;
		}
		if (Error!=""){
			alert("ATTENZIONE!\n"+Error);
		}
		return OK;
	}
	
	function reqOK(form, identifier){		/*extract from a form all the field that have id=req%n with %n=progressive number	
											control that value in that fields are !="". If verified return true else false.*/			
		j=0;
		k=0;
		idlen=identifier.length;				//aggiunta per XHTML per cambiare gli id
												//controlla solo la stringa per la lunghezza dell'identificatore.
		while (form.elements[j]!=null){
			X=form.elements[j]; 
			if (X.id.substr(0,idlen)==(identifier)){	//+k
				//k++;
				if (X.value==""){
					alert ("ATTENZIONE!\n Compili tutti i campi richiesti (*).");
					return false;
				}
			}
			j++;
		}
		return true;
	}
// HERE

	function CTRL_form(form, id){   //limit text field value by text field size, verify correc form of mail address and the presence of all requested fields.
		//limit_char(form);
		DELquote(form);
		if ( reqOK(form,id)){
			X=form;
			if (CTRLmail(X)){	
				if (CTRLdata(form)){
					if (CTRL_char(form)){
						return true;
					}
				}
			}
		}
		return false;
	}

