//' alcune funzioni utili in js by Yomo ( yomo@mmdev.net )
//' (22 dec 2004) vietato l'uso/vendita/copia non autorizzata!


//------------------------------------------------------------------------------
//	visualizza un messaggio in cui l'utente può rispondere si o no
//------------------------------------------------------------------------------

function question(txt)	{
	return window.confirm(txt);
}

//------------------------------------------------------------------------------
//	controlla se il sito è all'interno di un altro frame
//------------------------------------------------------------------------------

function check_location()
{
	if ( top.location.href != self.location.href && self.location.href.search("export")==-1 )
		top.location = self.location
}


//-----------------------------------------------------------------------------
//	basic image swap
//-----------------------------------------------------------------------------

function swap(name,file)
{
	name.src = file;
}


//-----------------------------------------------------------------------------
// controlla la validità di un indirizzo e-mail
//-----------------------------------------------------------------------------

function check_mail(mail)   {
	Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (Filtro.test(mail))
		return true;
	else
		return false;
}

//-----------------------------------------------------------------------------
// controlla il form per la registrazione
//-----------------------------------------------------------------------------

function check_form_registrazione( form )	{
	errors = 0;
	msg = "";

	// nome utente
	if ( form.txt_user.value.length < 4 )	{
		errors++;
		msg += "\n\tnome utente\t- il nome utente deve essere di almeno 4 caratteri";
	}
	// password
	if ( form.txt_password.value.length < 4 )	{
		errors++;
		msg += "\n\tpassword\t\t- la password deve essere di almeno 4 caratteri";
	}
	// nome
	if ( form.txt_nome.value.length < 4 )	{
		errors++;
		msg += "\n\tnome\t\t- il nome deve essere di almeno 4 caratteri";
	}
	// mail
	if ( !check_mail(form.txt_mail.value) )	{
		errors++;
		msg += "\n\te-mail\t\t- l'indirizzo e-mail non è valido";
	}
	// privacy
	if ( form.cbk_privacy.checked == false)	{
		errors++;
		msg += "\n\tprivacy\t\t- dare il consenso per il trattamento dei propri dati";
	}
	// mostra messaggio
	if ( errors)	{
		msg_header = "_________________________________________________________________\n\n";
		msg_header += "I dati immessi non sono stati inviati poiché si sono verificati i seguenti problemi:\n";
		msg_header += "_________________________________________________________________\n";
		msg_footer = "\n_________________________________________________________________\n";
		msg_footer += "\nper poter continuare è necessario correggere i problemi e reinviare il form...\n";
		msg = msg_header + msg + msg_footer;
		alert ( msg );
		return false;
	}
	else
		return true;
}


//------------------------------------------------------------------------------
// popup file function
//------------------------------------------------------------------------------

function popup(file)
{
  window.open(file,"","height=300,width=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes");
}


//-----------------------------------------------------------------------------
// Function to open pop up window
//-----------------------------------------------------------------------------

function openWin(theURL,winName,features) {
  	window.open(theURL,winName,features);
}


//-----------------------------------------------------------------------------
// Function to open pop up image album
//-----------------------------------------------------------------------------

function popup_image(file)
{
	window.open(file,"","height=600,width=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes");
}


//-----------------------------------------------------------------------------
// invert menu
//-----------------------------------------------------------------------------

function invert_menu(objRef){
    objRef.style.display = (objRef.style.display=="none") ? "" : "none";
}


//-----------------------------------------------------------------------------
//	general message box
//-----------------------------------------------------------------------------

function msg(msg)
{
	alert(msg);
}


//-----------------------------------------------------------------------------
//	displaydate
//-----------------------------------------------------------------------------

function displaydate( obj, day, month, year )
{
	var d = new Date( year, month, day, 0, 0 );
	var m = new Array("Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");
	var w = new Array("Domenica","Lunedì","Martedì","Mercoledì","Giovedì","Venerdì","Sabato");
	obj.innerText = ' ' + w[d.getDay()] + " " + d.getDate() + " " + m[d.getMonth()] + " " + year;
}

//-----------------------------------------------------------------------------
//	hidedate
//-----------------------------------------------------------------------------

function hidedate( obj )
{
	obj.innerText = ' ';
}

//-----------------------------------------------------------------------------
//	hilite
//-----------------------------------------------------------------------------

function hilite( obj, color )
{
	obj.defbgColor = obj.bgColor;
	obj.bgColor = color;
}


//-----------------------------------------------------------------------------
//	unhilite
//-----------------------------------------------------------------------------

function unhilite( obj )
{
	obj.bgColor = obj.defbgColor;
}



//-----------------------------------------------------------------------------
//	get_obj given id else return 0 
//-----------------------------------------------------------------------------

function get_obj( elemID )
{
	var obj;
	if ( document.all )
	{
		obj = document.all( elemID );
	}
	else if( document.getElementById )
	{
		obj = document.getElementById( elemID );
	}
	else if( document.layers )
	{
		obj = document.layers[ elemID ];
	}
	
	if ( obj ) return obj;
	else return 0;
}


//-----------------------------------------------------------------------------
//	init_turni
//-----------------------------------------------------------------------------

function init_turni( form, color )
{
	form.turno.day = new Array(366);
	form.turno.day[0] = color;
	
	//' aggiorna array
	for( i=1; i<366; i++ )
	{
		var obj = get_obj( "day_" + i );
		if( obj.bgColor == color )
			form.turno.day[i] = true;
	}
}


//'-----------------------------------------------------------------------------
//'	restituisce un oggetto date dato il numero del giorno (1-365)
//'-----------------------------------------------------------------------------

function getdate( n, y )
{
	var d = new Date( y, 0, 1, 0, 0 );
	
	var j = 0
	while( j < n-1 )
	{
		j++;
		d.setDate( 1 + d.getDate() );
	}

	return d;
}


//-----------------------------------------------------------------------------
//	selday
//-----------------------------------------------------------------------------

function selday( form, color, year, day, obj )
{
    obj = get_obj( "day_" + day );

    //' init
	if( !form.turno.day )
	{
		init_turni( form, color );
	}

	// hilite
	if( form.turno.day[day] )
	{
		form.turno.day[day] = false;
		obj.bgColor = '';

	}
	else
	{
		form.turno.day[day] = true;
		obj.bgColor = color;

		//' sel venerdi
		if( form.chksel7.checked )
		{
			if( form.chkven.checked )
			{
				var d = getdate( day, year );
				var i = d.getDay();
				
				if( i < 5 )
					day = day-(i+2);

				if( i > 5 )
					day--;

				for( i= day; i<=day+6; i++ )
				{
					var o = get_obj( "day_" + i );
					o.bgColor = color;
					form.turno.day[i] = true;
				}
			}
			//' sel sabato
			else if( form.chksab.checked )
			{
				var d = getdate( day, year );
				var i = d.getDay();
				
				if( i != 6 )
					day = day-(i+1);

				for( i= day; i<=day+6; i++ )
				{
					var o = get_obj( "day_" + i );
					o.bgColor = color;
					form.turno.day[i] = true;
				}
			}
			//' 7 giorni
			else
			{
				var i;
				for( i= day+1; i<=day+6; i++ )
				{
					var o = get_obj( "day_" + i );
					o.bgColor = color;
					form.turno.day[i] = true;
				}
			}
		}

		//' ripeti selezione
		if( form.chkrep.checked && form.nrep.value >= 1 && form.nday.value >= 1 )
		{
			var i = day + ( form.nrep.value * form.nday.value );
			while( i <= 365 )
			{
				var o = get_obj( "day_" + i );
				o.bgColor = color;
				form.turno.day[i] = true;
				i+= ( form.nrep.value * form.nday.value );
			}
		}
	}
}


//-----------------------------------------------------------------------------
//	compile
//-----------------------------------------------------------------------------

function compile( form, color )
{
	//' init
	if( !form.turno.day )
	{
		init_turni( form, color );
	}
	
	//' aggiorna il campo turni
	var t="|";
	for( i=1; i<366; i++ )
	{
		if( form.turno.day[i] )
			t += i + "|";
	}
	
	form.turno.value = t;
	//alert(t);

	return true;
}


//-----------------------------------------------------------------------------
//	fill_calendario
//-----------------------------------------------------------------------------

function fill_calendario( form, color )
{
	var strturno = form.turno.value;

	//' clear
	clear_turni( form, color );
	
	for( i=1; i<366; i++ )
	{
		if( strturno.indexOf( "|" + i + "|" ) != -1 )
		{
			form.turno.day[i] = true;
			var obj = get_obj( "day_" + i );
			obj.bgColor = color
		}
	}
	
	//' clear
	compile( form, color );
}



//-----------------------------------------------------------------------------
//	clear_turni
//-----------------------------------------------------------------------------

function clear_turni( form, color )
{
	//' init
	if( !form.turno.day )
	{
		init_turni( form, color );
	}

	//' aggiorna il campo turni
	for( i=1; i<366; i++ )
	{
		if( form.turno.day[i] )
		{
			form.turno.day[i] = false;
			var obj = get_obj( "day_" + i );
			obj.bgColor = ''
		}
	}
	
	form.turno.value = "";
}


//-----------------------------------------------------------------------------
//	call
//-----------------------------------------------------------------------------

function call( dis, day )
{
	//alert( "" + day + " - " + dis );
	window.location.href="farmacisti.asp?day=" + day + "&dis=" + dis;
}



