/*
File: COMMON.JS
---------------
incluso nel file principale
contiene tutte le funzioni COMUNI javascript richiamate
dalle varie pagine nelle varie sezioni
*/
//<SCRIPT language="javascript">
function players(sez){
 
  document.frmmenu.sz.value  = sez;
  addparam("search",""); 
  addparam("npage","0");
  document.frmmenu.submit();

}
function go_(sez){
 
  document.frmmenu.sz.value  = sez;
  addparam("search",""); 
  addparam("npage","0");
  document.frmmenu.submit();

}
function Indietro_news(sz){
 
  document.frmmenu.sz.value  = sz;
  document.frmmenu.submit();

}
//------------------------------------------------
function News(id){
 
  document.frmmenu.id.value  = id;
  document.frmmenu.sz.value  = 74;
  document.frmmenu.submit();

}

//------------------------------------------------
function CNU(id){
 
  document.frmmenu.id.value  = id;
  document.frmmenu.sz.value  = 96;
  document.frmmenu.submit();

}
//------------------------------------------------
function CORSI(id){
 
  document.frmmenu.id.value  = id;
  document.frmmenu.sz.value  = 101;
  document.frmmenu.submit();

}
//----------------------------------------------------------------------------------
function isValidEmail(email)
// controlla la correttezza sintattica di una email
//**** funzione da modificare con l'ausilio delle espressioni regolari ***//
{

	len = email.length;
	okc = 0;
	okp = 0;
    for(i=0;i<len;i++) { 
		c = email.substring(i,i+1);
		if (c=='@') okc = 1;
		if (c=='.') okp = 1;
	}
	if ((len > 5) && (okc == 1) && (okp == 1))
	 {	return true;  }
	else  { return false; }
}

function Check_reg(nomeform,lan)
{

	if (eval(nomeform).nome.value == "" && (eval(nomeform).nome.value.length < 2))
	{
 		if(lan=="it") alert ("Compilare il campo nome.");
		else		  alert ("First Name is required.");
		eval(nomeform).nome.focus();
		return false;
	}
 	if (eval(nomeform).cognome.value == "" && (eval(nomeform).cognome.value.length < 2))
 	{
 		if(lan=="it") alert ("Compilare il campo cognome.");
		else		  alert ("Second Name is required.");
		eval(nomeform).cognome.focus();
		return false;
	}	

	if (!isValidEmail(eval(nomeform).email.value))
	{
 		if(lan=="it") alert ("Compilare il campo email.");
		else		  alert ("Email address is required.");
		eval(nomeform).email.focus();
		return false;
	}
	/*if (!eval(nomeform).acconsento.checked)
	{
		alert("Per proseguire è necessario dare il proprio consenso \n  al TRATTAMENTO DEI DATI PERSONALI.")
		eval(nomeform).acconsento.focus();
		return false;
	}	*/
	eval(nomeform).sz.value='101';
	eval(nomeform).submit();
}


/* funzione per la sezione contatti */
function Check_contatti(nomeform,lan)
{

	if (eval(nomeform).nome.value == "" && (eval(nomeform).nome.value.length < 2))
	{
 		if(lan=="it") alert ("Compilare il campo nome.");
		else		  alert ("First Name is required.");
		eval(nomeform).nome.focus();
		return false;
	}
 	if (eval(nomeform).cognome.value == "" && (eval(nomeform).cognome.value.length < 2))
 	{
 		if(lan=="it") alert ("Compilare il campo cognome.");
		else		  alert ("Second Name is required.");
		eval(nomeform).cognome.focus();
		return false;
	}	
 	if (eval(nomeform).messaggio.value == "" && (eval(nomeform).messaggio.value.length < 15))
 	{
 		if(lan=="it") alert ("Inserire il messaggio.");
		else		  alert ("Insert your message.");
		eval(nomeform).messaggio.focus();
		return false;
	}	
	if (!eval(nomeform).acconsento.checked)
	{
		alert("Per proseguire è necessario dare il proprio consenso \n  al TRATTAMENTO DEI DATI PERSONALI.")
		eval(nomeform).acconsento.focus();
		return false;
	}	
	eval(nomeform).sz.value='102';
	
	eval(nomeform).submit();
}

/* funczione per la ricerca */
function Find_it()
{
      document.frmmenu.sz.value = 2;
	  document.frmmenu.cerca.value = document.frm_search.cerca.value;
	  document.frmmenu.submit();

}


function showhidemenu(id_){
	
	if(id_>0){
	 
	eval("disp=document.all.m"+id_+".style.display;"); 
	if(disp=='inline') {
		eval("document.all.m"+id_+".style.display='none';");
		eval("document.all.tmeno"+id_+".style.display='none';");
		eval("document.all.tpiu"+id_+".style.display='inline';");
	}
	else {
		eval("document.all.m"+id_+".style.display='inline';");
		eval("document.all.tmeno"+id_+".style.display='inline';");
		eval("document.all.tpiu"+id_+".style.display='none';");
	}
	
	
	}
	
	document.frmmenu.menu.value = id_;
	
}
//---------------------------MyAJAX---------------------------------
function read_statevar(nomevar){
	//alert(nomevar);
	document.formappo.azione.value="r";
	document.formappo.param.value=nomevar;
	document.formappo.submit();
}

//------------------------PAGINAZIONE----------------------------

function pnext(){
	page = document.frmmenu.npage.value;
	//
	if(isNaN(page)) page = 0;
	document.frmmenu.npage.value = page*1+1;
	document.frmmenu.submit();
	
}
function plast(){
	page = document.frmmenu.npage.value;
	document.frmmenu.npage.value = page*1-1;
	document.frmmenu.submit();
	
}
function pstart(){
	document.frmmenu.npage.value = 0;
	document.frmmenu.submit();
	
}
function pend(){
	page = document.frmmenu.ntotpage.value;
	document.frmmenu.npage.value = page*1-1;
	document.frmmenu.submit();
	
}
function go_page(numpage){

	document.frmmenu.npage.value = numpage*1-1;
	document.frmmenu.submit();
	
}

//------------------------PAGINAZIONE2----------------------------

function pnext_(frm){
	eval("page = "+frm+".npage.value;");
	//
	if(isNaN(page)) page = 0;
	newpage = page*1+1;
	eval(frm+".npage.value = newpage;");
	eval(frm+".submit();");
	
}
function plast_(frm){
	eval("page = "+frm+".npage.value;");
	//
	if(isNaN(page)) page = 0;
	newpage = page*1-1;
	eval(frm+".npage.value = "+newpage+";");
	eval(frm+".submit();");
	
}
function pstart_(frm){
	eval(frm+".npage.value = 0;");
	eval(frm+".submit();");
	
}
function pend_(frm){
	eval("page = "+frm+".ntotpage.value;");
	newpage = page*1-1;
    eval(frm+".npage.value = "+newpage+";"); 
	eval(frm+".submit();");
	
}
//-------------------------------------------------------------------------------
function addparam(nome,valore){

strparam   = document.frmmenu.strparam.value;
strparam   = strparam + "&" +nome +"="+valore;
document.frmmenu.strparam.value = strparam;

}

 // ---------------- GESTIONE DI LISTE / ELENCHI ---------------- 
 function Add_id(val)
  {
  
   // vengono memorizzati in una stringa gli id delle caselle selezionate 
    if (tot_id == "") tot_id = "|";
    tot_id += val + "|";
	//alert("tot_id = "+ tot_id);
  }
  function Remove_id(val)
  {
    val = "|"+ val + "|";
    len = val.length;
  	cerca = tot_id.indexOf(val);
	a = tot_id.substring(0,cerca);
	a += tot_id.substring(cerca + len-1,tot_id.length);
	tot_id = a;
	if(tot_id == "|") tot_id = "";
	//alert(tot_id);
  }
 
function open_banner(img,w,h)
//---------------------------
// apre l'immagine in una finestra popup l'immagine alle sue dimensioni originali
 {
   var new_win;
   w= w*1;
   h=h*1;
   w1 = w;
   h1 = h;
   prop = "location=no,width=" + w1 + ",height=" + h1;
   new_win = window.open('','',prop);
   new_win.document.write("<html><head><title>Immagine</title></head><body leftmargin=0 topmargin=0><div align='center'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='"+w+"' height='"+h+"' align='middle'>");
   new_win.document.write("<param name='movie' value='immagini/immagini.swf?filebig=" + img + "' />");
   new_win.document.write("<param name='quality' value='high' />");
   new_win.document.write("<embed src='immagini/immagini.swf?filebig=" + img + "' width='"+w+"' height='"+h+"' align='middle' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'></embed>");
   new_win.document.write("</object></div></body></html>");
 }

function open_img(img,w,h)
//---------------------------
// apre l'immagine in una finestra popup l'immagine alle sue dimensioni originali
 {
   var new_win;
   prop = "location=no,width=" + w + ",height=" + h;
   new_win = window.open('','',prop);
   new_win.document.write("<html><head><title>Immagine</title></head><body leftmargin=0 topmargin=0><img src='"+img+"'></body></html>");
 }
function open_image(title,img,w,h)
//---------------------------
// apre l'immagine in una finestra popup l'immagine alle sue dimensioni originali
 {
   var new_win;
   prop = "location=no,width=" + w + ",height=" + h;
   new_win = window.open('','',prop);
   new_win.document.write("<html><head><title>"+title+"</title></head><body leftmargin=0 topmargin=0><img src='"+img+"'></body></html>");
 }

function open_page(page,prop)
//----------------------------
// apre in pop-up la pagina page con le caratteristiche definite in prop
{
	//alert(page);
   n = window.open(page,'',prop);

}

function open_poll()
//---------------------------
// apre l'immagine in una finestra popup l'immagine alle sue dimensioni originali
 {
   var new_win;
   prop = "location=no,width=360,height=250";
   new_win = window.open('poll.php','',prop);
 }

function open_gallery(id)
//---------------------------
// apre l'immagine in una finestra popup l'immagine alle sue dimensioni originali
 {
   var new_win;
   prop = "location=no,width=840,height=640";
   new_win = window.open('gallery.php?id='+id,'',prop);
 }
 
function credits() {
	var w = screen.width;
	var h = screen.height;
	var x = Math.round(w / 2) - Math.round(340 / 2);
	var y = Math.round(h / 2) - Math.round(230 / 2);
	finestra = window.open ('contents/credits.htm', 'CREDITS', 'left=' + x + ',screenX=' + x + ',top=' + y + 'screenY=' + y +',width=340,height=230,status=no' ); 
}
//--------------------------------- IMMAGINI (solo JPG) --------------------------
function Check_img(img)
// controlla che l'immagine caricata sia una jpg
{
	
	if (img != "")
 	{
 		var len = img.length;
		var estensione = "";
		estensione = img.substr(len-3);
		if(estensione!="jpg" && estensione!="JPG")
		{
		 			alert("Attenzione: solo immagini in formato JPG");
					return false;
		}
		
	}

	return true;
}



//---------------------------- funzioni per nascondere/mostrare colonne nascoste ------------
	function Mostra_colonna(num,sub_num,cols)
	{
	 		var colonna = colonna_0 = colonna_v = "";
			// cols: numero di colonne
			// num: numero della colonna su cui agire
			// sub_num: numero della sottocolonna da visualizzare
			for(i=1;i<=cols;i++)
			{
			 	if(i!=num){
						 colonna_0 = eval("document.all.r_"+i+"_0.style");
						 colonna_0.display = "inline"; 
				
        				for(j=1;j<=2;j++)
        				{
        				 		 colonna = eval("document.all.r_"+i+"_"+j+".style");
        						 colonna.display = "none";			 
        				}
				}else{
					  	 
        				for(j=0;j<=2;j++)
        				{
        				 		 if(j==sub_num){
								 			   //alert("r_"+i+"_"+j);
											   colonna = eval("document.all.r_"+i+"_"+j+".style");
        						 			   colonna.display = "inline";
								}else{
								 			   colonna = eval("document.all.r_"+i+"_"+j+".style");
        						 			   colonna.display = "none";								
								}		 
        				}
				
				}	
			}
			
	}



//-----------------------------------------------------------
function isDate(dateStr) {
// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	var esito = true;
	
	if (matchArray == null) {
		//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		alert("Inserire la data nel formato gg/mm/aaaa or gg-mm-aaaa.");
		esito = false;
	}
	
	//nel formato mm/dd/yyyy:
		//month = matchArray[1]; // p@rse date into variables
		//day = matchArray[3];
	
	month = matchArray[3]; // p@rse date into variables
	day = matchArray[1];
	year = matchArray[5];
	
	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		alert("Il mese deve essere compreso tra 1 e 12");
		esito = false;
	}
	
	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
		alert("Il giorno deve essere compreso tra 1 e 31.");
		esito = false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn`t have 31 days!")
		alert("Il mese "+month+" non ha 31 giorni!")
		esito = false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn`t have " + day + " days!");
			alert("Febbraio " + year + " non ha " + day + " giorni!");
			esito = false;
		}
	}
	
	return esito; // date is valid
}



//----------------------------------------
function Open_Rubrica(campoformtoreturn)
{
	//apre in pop-up il controllo calendario e selezionando un dato giorno, memorizza la
	//data selezionata nel campo form passato, chiudento il pop-up
	page = "index_frame.php?content=admin/poprubrica.php&campoformtoreturn="+campoformtoreturn;
	prop = "location=no, height=300, width=400, resizable=yes";

	var newpage = window.open(page,'', prop);//
			
}
//---------------- aggiungere aree riservate ----------------
function insertFormat(format, target)
{
	var insert = '';
	if (!target) 
	{
		target = 'insertTarget';
	}
	
	switch(format)
	{
		case 'url':
			var url = prompt("Inserisci l'indirizzo della pagina web che vuoi linkare", "http://");
			if(url!="")
			{
			    var nome = prompt("Inserisci il nome della pgina web che vuoi linkare", "");
			    if(nome=="") nome=url;
				insert = "[URL]"+url+"[/URL][URL_NAME]"+nome+"[/URL_NAME]";
			}
			else insert = "";
		break;
		case 'color':
			var color = prompt("Inserisci il testo", "");
			insert = "[PINK]"+color+"[/PINK]";
		break;
		case 'color_bold':
			var color = prompt("Inserisci il testo", "");
			insert = "[PINK_BOLD]"+color+"[/PINK_BOLD]";
		break;
		case 'email':
			var email = prompt("Inserisci l'indirizzo email completo (tiziocaio@account.xx) che vuoi inserire.", "");
			insert = "[EMAIL]"+email+"[/EMAIL]";
		break;
			
		case 'image':
			var image = prompt("Inserisci l'URL completo dell'immagine.", "http://");
			insert = "[IMG]"+image+"[/IMG]";	
		break;
				
		case 'bold':
			var bold = prompt("Inserisci il testo da rendere Bold.", "");
			insert = "[B]"+bold+"[/B]";
		break;
		
		case 'italics':
			var italics = prompt("Inserisci il testo da rendere Italic.", "");
			insert = "[I]"+italics+"[/I]";	
		break;	
		
	}
	document.getElementById(target).value += insert;
	document.getElementById(target).focus();			
}
//--------------------------------------
function OpenCalendar(campoformtoreturn)
{
	//apre in pop-up il controllo calendario e selezionando un dato giorno, memorizza la
	//data selezionata nel campo form passato, chiudento il pop-up
	page = "pop_calendar.php?campoformtoreturn="+campoformtoreturn;
	prop = "location=no, height=200, width=220, resizable=yes";

	var newpage = window.open(page,'', prop);//
	
}
//-----------------------------------------
function ReturnValue()
	{
		campoformtoreturn = document.frm_rec.campoformtoreturn.value;
		valore = selezionati();
		eval("opener.document."+campoformtoreturn+".value='"+valore+"'");
		
		window.close();
	}
	
	function selectAll()
	{
	  with (document.frm_rec) {
		for (var i=0; i < elements.length; i++) {
			if (elements[i].type == 'checkbox')
			   elements[i].checked = true;
		}
	  }
	}
	//-------------------------------------------------
	function deselectAll()
	{
	  with (document.frm_rec) {
		for (var i=0; i < elements.length; i++) {
			if (elements[i].type == 'checkbox')
			   elements[i].checked = false;
		}
	  }
	}
	//-------------------------------------------------
	
	function selezionati()
	{
	  var val = "";
	  with (document.frm_rec) {
		for (var i=0; i < elements.length; i++) {
			if (elements[i].type == 'checkbox' && elements[i].checked == true)
			{
				val += elements[i].value; 
				if(elements.length>0 && i<elements.length-1) val += ","; 
			}   
		}
	  }
	 
	 
	  return val;
	}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);return false;
}
function MM_displayStatusMsg(msgStr) { 
// Imposta il testo nella barra di stato
  status=msgStr;
  document.MM_returnValue = true;
  }
 

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function SwitchImg()
{ //start
  var rem, keep=0, store, obj, switcher=new Array, history=document.Data;
    for (rem=0; rem < (SwitchImg.arguments.length-2); rem+=3) {
    	store = SwitchImg.arguments[(navigator.appName == 'Netscape')?rem:rem+1];
    if ((store.indexOf('document.layers[')==0 && document.layers==null) ||
        (store.indexOf('document.all[')==0 && document.all==null))
         store = 'document'+store.substring(store.lastIndexOf('.'),store.length);
         obj = eval(store);
    if (obj != null) {
   	   switcher[keep++] = obj;
      switcher[keep++] = (history==null || history[keep-1]!=obj)?obj.src:history[keep];
      obj.src = SwitchImg.arguments[rem+2];
  } }
  document.Data = switcher;
} //end

function RestoreImg()
{ //start
  if (document.Data != null)
    for (var rem=0; rem<(document.Data.length-1); rem+=2)
      document.Data[rem].src=document.Data[rem+1];
} //end