// JavaScript Document
var expl_bug_attribute=0;

if(navigator.appVersion.search("MSIE")!="-1")
{ if(navigator.appVersion.search("6.0")!="-1" || navigator.appVersion.search("7.0")!="-1") { expl_bug_attribute=1; } else { expl_bug_attribute=0; } }

/* FUNZIONE -- VISUALIZZAZIONE CAMPO RECUPERO PASSWORD ----------------------------------------------------------------------------- */
function view_or_hide_objects(object_view,object_hide,tipo_visual){

	for(i=0;i<object_view.length;i++)
	{ document.getElementById(object_view[i]).style.display=tipo_visual; } 

	for(i=0;i<object_hide.length;i++)
	{ document.getElementById(object_hide[i]).style.display="none"; } 
}


/* FUNZIONE -- VISUALIZZA I MESSAGGI DI ERRORE ----------------------------------------------------------------------------- */
function show_error_message(el_form_name,style_view_msg_errore,message_type,testo_msg)
{
	window.document.getElementById("errore_"+el_form_name).style.display=style_view_msg_errore;
	if(expl_bug_attribute==0)
	{ window.document.getElementById("errore_"+el_form_name).setAttribute("class",message_type); }
	else
	{ window.document.getElementById("errore_"+el_form_name).setAttribute("className",message_type); }
	window.document.getElementById("errore_"+el_form_name).innerHTML=testo_msg;
	return;
}


/* FUNZIONE -- CONTROLLO VALIDITA' INDIRIZZO E-MAIL ----------------------------------------------------------------------------- */
function check_mail(el_form_name,style_view_msg_errore,highlight_wrong_tag,style_wrong,style_ok,view_ok)
{
	var errore=0;
	var myRegExp=/^[-a-zA-Z0-9_-]+([.][-a-zA-Z0-9_-]+)*[@]{1}[-a-zA-Z0-9_-]+([.][-a-zA-Z0-9_-]+)*[.]\D{2,5}$/i;
	var controlla_mail=document.getElementById(el_form_name);
	
	if(!myRegExp.test(controlla_mail.value))
	{ 
		errore=1;
		if(window.document.getElementById("errore_rec_password")!=null) { window.document.getElementById("errore_rec_password").style.display="none"; }
		if(highlight_wrong_tag==1) { document.getElementById(el_form_name).style.border=style_wrong; }
		window.document.getElementById("errore_"+el_form_name).style.display=style_view_msg_errore;
		if(expl_bug_attribute==0)
		{ window.document.getElementById("errore_"+el_form_name).setAttribute("class","errore"); }
		else
		{ window.document.getElementById("errore_"+el_form_name).setAttribute("className","errore"); }
		window.document.getElementById("errore_"+el_form_name).innerHTML="La mail inserita non &egrave; valida!";		
	}
	else
	{   
		errore=0;  
		if(highlight_wrong_tag==1) { document.getElementById(el_form_name).style.border=style_ok; }
		if(view_ok==1)
		{
			window.document.getElementById("errore_"+el_form_name).style.display=style_view_msg_errore;
			if(expl_bug_attribute==0)
			{ window.document.getElementById("errore_"+el_form_name).setAttribute("class","ok"); }
			else
			{ window.document.getElementById("errore_"+el_form_name).setAttribute("className","ok"); }
			window.document.getElementById("errore_"+el_form_name).innerHTML="La mail inserita &egrave; valida!";
		}
		else
		{
			window.document.getElementById("errore_"+el_form_name).style.display="none";
		}

	}
	
	return errore;
}

/* FUNZIONI -- CONTROLLI SUI CAMPI OBBLIGATORI ----------------------------------------------------------------------------- */
function show_message_error(i,el_form_name,style_view_msg_errore,labels)
{
	if(window.document.getElementById("errore_accesso")!=null) { window.document.getElementById("errore_accesso").style.display="none"; }
	if(window.document.getElementById("errore_rec_password")!=null) { window.document.getElementById("errore_rec_password").style.display="none"; }

	window.document.getElementById("errore_"+el_form_name).style.display=style_view_msg_errore;
	if(expl_bug_attribute==0)
	{ window.document.getElementById("errore_"+el_form_name).setAttribute("class","errore"); }
	else
	{ window.document.getElementById("errore_"+el_form_name).setAttribute("className","errore"); }

	window.document.getElementById("errore_"+el_form_name).innerHTML="Compila il campo "+labels[i].innerHTML;
	
	return;
}

function highlight_empty_fields(nome_form,i,style_empty)
{
	/* evidenzio gli elementi vuoti */
	eval("window.document."+nome_form).elements[i].focus();
	eval("window.document."+nome_form).elements[i].style.border=style_empty;
	
	return;
}

function back_style_fields_full(nome_form,style_full)
{
	eval("window.document."+nome_form).elements[i].style.border=style_full;
	
	return;
}

function message_ok(view_ok,el_form_name,style_view_msg_errore,labels,i,messaggi_ok)
{
	var msg_ok=0;
	if(messaggi_ok!="all")
	{
		for(k=0;k<messaggi_ok.length;k++)
			{
				if(el_form_name==messaggi_ok[k])
				{ msg_ok=1; }
			}
	}
	else
	{ msg_ok=1; }

	if(view_ok==1 && msg_ok==1)
	{
		window.document.getElementById("errore_"+el_form_name).style.display=style_view_msg_errore;
		if(expl_bug_attribute==0)
		{ window.document.getElementById("errore_"+el_form_name).setAttribute("class","ok"); }
		else
		{ window.document.getElementById("errore_"+el_form_name).setAttribute("className","ok"); }
		window.document.getElementById("errore_"+el_form_name).innerHTML="Campo "+labels[i].innerHTML+" compilato in modo corretto!";
	}
	else
	{
		window.document.getElementById("errore_"+el_form_name).style.display="none";
	}
	
	return;
}

function check_form(nome_form,escludi,highlight_empty_tag,style_empty,style_full,view_ok,style_view_msg_errore,messaggi_ok)
{
	var empty=0;
	var num_el_form=eval("window.document."+nome_form+".elements.length");
	var labels=document.getElementsByTagName("label");
	
	for(i=0;i<num_el_form-1;i++)
		{
			el_form=eval("window.document."+nome_form).elements[i].value;
			el_form_fieldset=eval("window.document."+nome_form).elements[i];
			el_form_checked=eval("window.document."+nome_form).elements[i].checked;
			el_form_name=eval("window.document."+nome_form).elements[i].name;
			el_form_type=eval("window.document."+nome_form).elements[i].type;
			el_form_display=eval("window.document."+nome_form).elements[i].style.display;

			var escludi_elementi=0;
			for(esc=0;esc<escludi.length;esc++)
				{
					if(el_form_name==escludi[esc])
					{ escludi_elementi=1; }
				}
			
			if(el_form_type!="hidden" && el_form_type!="reset" && el_form_type!="button" && el_form_fieldset.nodeName!="FIELDSET" && escludi_elementi==0 && el_form_display!="none")
			{
			
				if(el_form_type!="radio" && el_form_type!="checkbox")
				 {
					if(el_form=="")
					  {
						empty=1;
						show_message_error(i,el_form_name,style_view_msg_errore,labels);
						if(highlight_empty_tag==1) { highlight_empty_fields(nome_form,i,style_empty); }
					  }
					  else
					  {
						if(highlight_empty_tag==1) { back_style_fields_full(nome_form,style_full); }
						message_ok(view_ok,el_form_name,style_view_msg_errore,labels,i,messaggi_ok);
					  }		
				 }
				 else
				 {
					 if(el_form_checked!=true && el_form_display!="none")
					 {
						empty=1;
						show_message_error(i,el_form_name,style_view_msg_errore,labels);
						if(highlight_empty_tag==1) { highlight_empty_fields(nome_form,i,style_empty); }
						}
					  else
					  {
						if(highlight_empty_tag==1) { back_style_fields_full(nome_form,style_full); }
						message_ok(view_ok,el_form_name,style_view_msg_errore,labels,i,messaggi_ok);
					  }		
				 }
			}
		}
	return empty;
}


/* FUNZIONE -- ELENCO ED ATTIVAZIONE DEI CONTROLLI DEI DATI RELATIVI ALLA FORM DI LOGIN E RECUPERO PASSWORD ---------------------------------------------------- */
function check_form_login(check_this_form)
{
	var tot_errori=0;
	
	var border_style_wrong='1px solid #f00';
	var border_style_ok='1px solid #555';
	var display_style_error='block';
	var highlight_empty_tag=0;
	var show_msg_ok=0;
	
	switch(check_this_form)
	{
		case "login":    tot_errori=check_form('form1','',highlight_empty_tag,border_style_wrong,border_style_ok,show_msg_ok,display_style_error,'');
						 if(tot_errori==0) { window.document.form1.submit(); }
					     break;

		case "password": tot_errori_rec_passw=check_form('rec_password','',highlight_empty_tag,border_style_wrong,border_style_ok,show_msg_ok,display_style_error,'');
						 if(tot_errori_rec_passw==0)
						 { tot_errori_rec_passw=check_mail('e_mail_rec_passw',display_style_error,highlight_empty_tag,border_style_wrong,border_style_ok,show_msg_ok); }
	                     if(tot_errori_rec_passw==0) { window.document.rec_password.submit(); }
	}
}
