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_swapImagex(idimg){
	myimg=document.getElementById(idimg).src;
	
	nsla=myimg.lastIndexOf("/")+1;
	myimg1=myimg.substring(nsla,myimg.length);
	
	if(myimg1=="button_piu.gif")
		myimg2="img/button_meno.gif";
	else
		myimg2="img/button_piu.gif";
	
	MM_swapImage(idimg,'',myimg2,1);
}

function tblsort(mytab,col)
{
	var tblEl = document.getElementById(mytab).getElementsByTagName("TBODY")[0];
	var tmpEl;
	var i, j;
	var minVal, minIdx;
	var testVal;
	var cmp;

  	for (i = 0; i < tblEl.rows.length - 1; i++) 
    {

		// Assegnamo alla prima linea il valore piu' basso
	
		minIdx = i;
		minVal = tblEl.rows[i].cells[col].firstChild.nodeValue;
	
		// cerca la riga col valore minimo.
	
		for (j = i + 1; j < tblEl.rows.length; j++) {
			testVal = tblEl.rows[j].cells[col].firstChild.nodeValue;
			if (minVal > testVal) {
				minIdx = j;
				minVal = testVal;
			}
		}
		if (minIdx > i) {
			tmpEl = tblEl.removeChild(tblEl.rows[minIdx]);
			tblEl.insertBefore(tmpEl, tblEl.rows[i]);
		}
	}
	return (false);
}

function ColoraRighe(){
	var tabelle=document.getElementsByTagName("table");
	
	for(i=0;i<tabelle.length;i++){
		if(tabelle[i].className.indexOf("righealterne")>=0){
			trs=tabelle[i].tBodies[0].rows;
			for(j=1;j<trs.length;j+=2)
				trs[j].className="alterna";
		}
	}
}

function ShowMenu(menu){
	if(document.getElementById("menu_"+menu).style.display=="none"){
		document.getElementById("menu_"+menu).style.display="block";
	}
	else{
		document.getElementById("menu_"+menu).style.display="none";
	}
}

function toggle(id)

{

        if(document.getElementById(id).style.display=="none")

          document.getElementById(id).style.display="";

        else

          document.getElementById(id).style.display="none";

}

function xoopsGetElementById(id) {
if (document.getElementById(id)) {
	return document.getElementById(id);
	} else if (document.all[id]) {
		return document.all[id];
		} else if (document.layers && document.layers[id]) {
			return (document.layers[id]);
			} else {
				return false;
			}
}

function toggle_visibility(id, flag)  {

if (xoopsGetElementById(id)) {
	xoopsGetElementById(id).style.visibility = (flag) ? 'visible' : 'hidden';
	}
}

function showImgSelected(imgId, selectId, imgDir) {
imgDom     = xoopsGetElementById(imgId);
selectDom  = xoopsGetElementById(selectId);
imgDom.src = "http://www.altropc.it/"+ imgDir + "/" + selectDom.options[selectDom.selectedIndex].value;
}


function setVisible(id) {
	xoopsGetElementById(id).style.visibility = "visible";
}


function setHidden(id) {
	xoopsGetElementById(id).style.visibility = "hidden";
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}


function validChar(campo)
{
	var x = document.getElementById(campo).value;
	var filter  = new RegExp("[a-z0-9]$","i");
	
	if ((x!="") && (!filter.test(x))){
		alert('Il carattere inserito non è ammesso, puoi inserire solo numeri e lettere');
		document.getElementById(campo).value='';
	}
}

function validChars(campo,minimo,massimo)
{
	var x = campo;
	var filter  = new RegExp("[A-Za-z0-9]{" + minimo + "," + massimo + "}$");
	
	if (filter.test(x))
		return true;
	else
		return false;
}

function validEmail(campo)
{
	var x = campo;
	var filter  = new RegExp("([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
	
	if (filter.test(x))
		return true;
	else
		return false;
}

function CheckFormUserReg(){	
	if(trim(document.frmReguser.username.value)==""){
		alert("Non hai inserito l'username");
		document.frmReguser.username.focus();
		return false;
	}
	if(!validChars(trim(document.frmReguser.username.value),'3','12')){
		alert("Il campo username contiene caratteri non validi");
		document.frmReguser.username.focus();
		return false;
	}
	
	if(trim(document.frmReguser.password.value)==""){
		alert("Non hai inserito la password");
		document.frmReguser.password.focus();
		return false;
	}
	if(trim(document.frmReguser.password.value)!="" && trim(document.frmReguser.password2.value)!=""){
		if(!validChars(trim(document.frmReguser.password.value),'4','12')){
			alert("Il campo password contiene caratteri non validi");
			document.frmReguser.password.focus();
			return false;
		}
	}
	if(trim(document.frmReguser.password.value) != trim(document.frmReguser.password2.value)){
		alert("Le password non coincidono");
		return false;
	}
	if(trim(document.frmReguser.email.value)==""){
		alert("L'email e' obbligatoria.");
		document.frmReguser.email.focus();
		return false;
	}
	if((trim(document.frmReguser.email.value)!="") && (!validEmail(trim(document.frmReguser.email.value)))){
		alert("L'email inserita non e' corretta");
		document.frmReguser.email.focus();
		return false;
	}
	if(trim(document.frmReguser.nome.value)==""){
		alert("Non hai inserito il nome");
		return false;
	}
	if(trim(document.frmReguser.cognome.value)==""){
		alert("Non hai inserito il cognome");
		return false;
	}
	if(trim(document.frmReguser.codicefiscale.value)==""){
		alert("Non hai inserito il codice fiscale");
		return false;
	}
	
	return true;
}

function CheckFormRic(){	
	if(trim(document.formric.all_fields.value)==""){
		alert("Non hai inserito la chiave di ricerca");
		return false;
	}
	
	var strTemp=trim(document.formric.all_fields.value);
	
	if(strTemp.length < 2){
		alert("Devi inserire almeno 2 lettere");
		return false;
	}
	return true;
}

function CheckFormRecupero(){	
	
	if(trim(document.frmRecupero.username.value)==""){
		alert("Non hai inserito l'username");
		return false;
	}
	
	if(trim(document.frmRecupero.email.value)==""){
		alert("Non hai inserito l'email");
		return false;
	}

	return true;
}

function CheckFormNews(){
	if(trim(document.frmNews.nome.value)==""){
		alert("Non hai inserito il nome");
		document.frmNews.nome.focus();
		return false;
	}
	if(trim(document.frmNews.data.value)==""){
		document.frmNews.data.value=Oggi2();
	}
	
	return true;
}

function CheckFormLogin(){	
	
	if(trim(document.frmLogin.user.value)==""){
		alert("Non hai inserito l'username");
		return false;
	}
	
	if(trim(document.frmLogin.pwd.value)==""){
		alert("Non hai inserito la password");
		return false;
	}

	return true;
}

// setta la variabile MyText con la data odierna
function Oggi(MyText,Over)
{
	var now= new Date();
	
	var d=now.getDate();
	var giorno = (d < 10) ? '0' + d : d;

	var m=(now.getMonth()+1);
	var mese=(m < 10) ? '0' + m : m;
	
	var yy = now.getYear();
	var anno = (yy < 1000) ? yy + 1900 : yy;

	if(Over=='s')
		document.getElementById(MyText).value=giorno + "/" + mese + "/" + anno;
	else
		document.getElementById(MyText).value=document.getElementById(MyText).value + giorno + "/" + mese + "/" + anno;
}

function openWin (URL,larg,alt) {
	sinistra=(screen.width-larg)/2;
	alto=(screen.height-alt)/2;
    window.open(URL,"","width="+larg+",height="+alt+",left="+sinistra+",top="+alto+",toolbar=no, location=no,status=no,menubar=no,scrollbars=yes,resizable=no");
}

// setta la variabile MyText con la data odierna
function Oggi2()
{	
	var now= new Date();

	var g=now.getDay();
	
	var d=now.getDate();
	var giorno = (d < 10) ? '0' + d : d;

	var m=(now.getMonth()+1);
	var mese = (m < 10) ? '0' + m : m;
	
	var yy = now.getYear();
	var anno4 = (yy < 1000) ? yy + 1900 : yy;
	
	var miadata=giorno + '/' + mese + '/' + anno4;
	
	return (miadata);
}

function GeneratePasswd(plength){
	var keylist = "abcdefghijklmnopqrstuvwxyz1234567890_-@.";
	
	temp = '';
	for (i=0; i<plength; i++)
		temp += keylist.charAt(Math.floor(Math.random()*keylist.length));
	
	return temp;
}

function UploadComplete(){
	alert("L'upload dei file è avvenuto correttamente");
}

