// LOGIN 

function InputDel(campo,default_val) {
	if (campo.value == default_val) {
		campo.value = '';
	}	
}

function InputFill(campo,default_val) {
	if (campo.value == '') {
		campo.value= default_val;
	}	
}

function controllo_login(theForm){
	if (theForm.login_username.value == "" || theForm.login_username.value == "matricola") {
    	alert("Per favore inserire il proprio numero di matricola.\nGrazie.");
    	theForm.login_username.focus();
    	return (false);
	}
	if (theForm.login_password.value == "" || theForm.login_password.value == "password") {
    	alert("Per favore inserire la propria password.\nGrazie.");
    	theForm.login_password.focus();
    	return (false);
	}
}

function controllo_ricerca(theForm){
	if (theForm.testo_ricerca.value == "" || theForm.testo_ricerca.value == " ") {
    	alert("E' necessario inserire il testo da ricercare.\nGrazie.");
    	theForm.testo_ricerca.focus();
    	return (false);
	}
}

// ACCORDION 

function initAccordion(){

	var toggles = document.getElementsByClassName('menu_title');
	var stretchers = document.getElementsByClassName('menu_content');

	var myAccordion = new fx.Accordion(
		toggles, stretchers, {opacity: true, duration: 400}
	);
	
	IdTastoAttivo = 'content_'+tastoAttivo;
	myAccordion.showThisHideOpen(document.getElementById(IdTastoAttivo));

  document.getElementById(tastoAttivo).style.color = '#FF6600';
  document.getElementById(tastoAttivo).style.backgroundPosition  = '0px -25px';
}

function attivaTasto(tasto){	
	var toggles = document.getElementsByClassName('menu_title');	
	for(n=0; n<toggles.length; n++) {
			toggles[n].style.color = '#999999';
		    toggles[n].style.backgroundPosition  = '0px 3px';
	}	
	
	var tasto_menu = document.getElementById(tasto);	
	tasto_menu.style.color = '#FF6600';
	tasto_menu.style.backgroundPosition  = '0px -25px';
	tastoAttivo = tasto;
}

function overTasto(tasto){
	if (tastoAttivo != tasto){
		document.getElementById(tasto).style.color = '#FF6600';
		document.getElementById(tasto).style.backgroundPosition  = '0px -25px';
	}
}

function outTasto(tasto){	
	if (tastoAttivo != tasto){
		document.getElementById(tasto).style.color = '#999999';
		document.getElementById(tasto).style.backgroundPosition  = '0px 3px';
	}
}

///////////// CALENDARIO /////////////

function goToDay(day, incr) {
  var oday = new String;
  oday += day;
  var year = oday.replace(/(.{4}).{2}.{2}/, "$1");
  var month = oday.replace(/.{4}(.{2}).{2}/, "$1");
  var day = oday.replace(/.{4}.{2}(.{2})/, "$1");
  var nday = new Date(year, month - 1, day);
  nday.setDate(nday.getDate() + incr);
  nday = nday.getFullYear() * 10000 + (nday.getMonth() + 1) * 100 + nday.getDate();
  document.location.href= "/adon.pl?act=Calendar&Mode=Src&day="+nday;
}

//function attivaTasto(tasto){	
//	var toggles = document.getElementsByClassName('menu_title');	
//	for(n=0; n<toggles.length; n++) {		
//		if (n == tasto){
//			toggles[n].style.color = '#FF6600';
//			toggles[n].style.backgroundPosition  = '0px -25px';
//			tastoAttivo = tasto;
//		} else {
//			toggles[n].style.color = '#999999';
//		    toggles[n].style.backgroundPosition  = '0px 3px';
//		}
//	}
//}
//
//function overTasto(tasto){
//	if (tastoAttivo != tasto){
//		var toggles = document.getElementsByClassName('menu_title');	
//		toggles[tasto].style.color = '#FF6600';
//		toggles[tasto].style.backgroundPosition  = '0px -25px';
//	}
//}
//
//function outTasto(tasto){	
//	if (tastoAttivo != tasto){
//		var toggles = document.getElementsByClassName('menu_title');	
//		toggles[tasto].style.color = '#999999';
//		toggles[tasto].style.backgroundPosition  = '0px 3px';
//	}
//}

// VARIE 

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 PopupPic(sPicURL) { 
// dimensioni iniziali finestra prima che si riadatti
 popwidth = 250;
 popheight = 400;
 // apre il popup e passa la url immagine
 window.open( "/popups/pop_fit.html?"+sPicURL, "", 
 "left=" + Math.floor((screen.width - popwidth) / 2) + ",top=" + Math.floor((screen.height - popheight) / 2) +
 "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width="+popwidth+",height="+popheight); 
}

function PopAmico(page) {
popwidth = 500;
popheight = 300;
OpenWin = this.open(page, "PopWin2", "toolbar=no, menubar=no, location=no, scrollbars=no, resizable=no, width="+popwidth+", height="+popheight+", left=" + Math.floor((screen.width - popwidth) / 2) + ", top=" + Math.floor((screen.height - popheight) / 2));
}

// URL ENCODE

function URLEncode(s)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = s;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	
	return encoded;
};

function URLDecode(s)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = s;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   
   return plaintext;
};
