function getId(id_elemento) 
{
var elemento;
 if(getId)
  elemento = document.getElementById(id_elemento);
 else
  elemento = document.all[id_elemento];

 return elemento;
} 
function chkRegXp(pattern,flag,str) 
{
 if(!pattern) return;
 var rex = new RegExp(pattern,flag);
 if(!rex.test(str)) return false;
 else return true;
}
function cform_login() 
{
 if(!getId('user').value) {alert('Devi specificare un username');return false;}
 if(!getId('pass').value) {alert('Devi specificare una password');return false;}
 if(checkRegXp("^[a-z0-9]{5,15}$","i",getId('user').value)) { alert('Username non valido;\n Deve essere composta da lettere o numeri di minimo 5 caratteri e massimo 15.\n\n La tua ne contiene '+getId('user').value.length+''); return false; }
 if(checkRegXp("^[a-z0-9]{5,15}$","i",getId('pass').value)) { alert('Password non valida; \nDeve essere composta da lettere o numeri di minimo 5 caratteri e massimo 15\n\n La tua ne contiene '+getId('pass').value.length+''); return false; }
 return true;
}

function cform_register()
{
 var hdler = document.forms['frmRegister'];
 for(var i = 0;i <hdler.length;i++) {
   if(hdler[i].type == 'text' || hdler[i].type == 'radio') {
     if(hdler[i].name == 'condizione' && hdler[i].checked && hdler[i].value == 'rifiuto') {alert('Per registrarti devi prima accettare le condizioni');return false;}
     else {
       if(hdler[i].name == 'username') {if(!hdler[i].value) {alert('Devi inserire un username');return false;} else if(!chkRegXp('^[a-z0-9]{5,15}$','i',hdler[i].value)) {alert('Il tuo username non è valido!\n Puoi inserire un username formato da caratteri e numeri,di minimo 5 caratteri e massimo 15 caratteri');return false;} }
     else if(hdler[i].name == 'email') {if(!hdler[i].value) {alert('Devi inserire un\'email');return false;} else if(!chkRegXp('^[a-z0-9._%-]+@+[a-z0-9.-]+\.+[a-z]{2,4}$','i',hdler[i].value)) {alert('Inserisci una mail valida;\n\nN.B: Lo scopo per cui viene richiesta la tua mail è solamente perchè in caso di dimenticanza delle password è l\'unico metodo per ricrearne una nuova, e non per scopi di spam o di altra natura malevola;\n\nPercui non hai motivo di metterne una fasulla');return false;} }       
     }
   }
 }
 return true;
}

function cform_myprofile() 
{
  if(!document.forms['chgUserDate'].password) var mode = 1;else var mode = 0;
   if(mode && document.forms['chgUserDate'].new_password.value.length == 0 && document.forms['chgUserDate'].retype_password.value.length == 0) {alert('E\' obbligatorio compilare il campo password  per completare la registrazione!');return false;}
   if (!mode) if (document.forms['chgUserDate'].password.value == null || document.forms['chgUserDate'].password.value.length == 0) {alert('Per cambiare questi dati è richiesta la password');return false;}
   // Controllo che i campi "retype" siano uguali
   if(document.forms['chgUserDate'].new_password.value != document.forms['chgUserDate'].retype_password.value) {alert('I campi "nuova password" non corrispondono tra loro!');return false;}
   if(document.forms['chgUserDate'].new_email.value != document.forms['chgUserDate'].retype_email.value) {alert('I campi "nuova email" non corrispondono tra loro!');return false;}
   // Controllo che password e nuova password siano diversi 
   if(!mode) if(document.forms['chgUserDate'].new_email.value == document.forms['chgUserDate'].password.value) {alert('Le password coincidono;\n\nSpecifica una "nuova passord" diversa da quella che già usi!');return false;}
  // Controllo regex 
  if(document.forms['chgUserDate'].new_password.value.length > 0 && !chkRegXp('[a-z0-9]{5,15}','i',document.forms['chgUserDate'].new_password.value)) {alert('Le password devono essere di tipo alfanumeriche (lettere e numeri) di minimo 5 caratteri e un massimo di 15');return false;}
  if (!mode) if(document.forms['chgUserDate'].password.value.length > 0 && !chkRegXp('[a-z0-9]{5,15}','i',document.forms['chgUserDate'].password.value)) {alert('Le password devono essere di tipo alfanumeriche (lettere e numeri) di minimo 5 caratteri e un massimo di 15');return false;}  
  if(document.forms['chgUserDate'].new_email.value.length > 0 && !chkRegXp('^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$','i',document.forms['chgUserDate'].new_email.value)) {alert('Inserisci una mail valida;\n\nN.B: Lo scopo per cui viene richiesta la tua mail è solamente perchè in caso di dimenticanza delle password è l\'unico metodo per ricrearne una nuova, e non per scopi di spam o di altra natura malevola;\n\nPercui non hai motivo di metterne una fasulla');return false;}  
   for(var i = 1;i < document.forms['chgUserDate'].length;i++) {
   if(document.forms['chgUserDate'][i].type == 'text' || document.forms['chgUserDate'][i].type == 'password') {
    // Assegno lo status disable ai campi nulli per non inviarli tramite form
    if(document.forms['chgUserDate'][i].value == null || document.forms['chgUserDate'][i].value.length == 0) document.forms['chgUserDate'][i].hidden = true;
   }
  }
 return true;
}

function add_attachment() {
if(document.createTextNode) {
  var tmp = 1;
  var el =  document.forms['addphoto']

       for (var x=0;x < el.length;x++)
    {
    if(document.forms['addphoto'][x].name == 'proto' && document.forms['addphoto'][x].checked == true) var proto = document.forms['addphoto'][x].value;
    }

   if(!proto) {alert('Devi selezionare un protocollo per poter aggiungere l\'allegato');return false;}
   tmp = getId('add_attachment').getElementsByTagName('div').length
   n = document.createElement('div');
   n.setAttribute('id','attachId_'+tmp);
   n.setAttribute('name','culo')
   getId('add_attachment').appendChild(n);    
           
  attach = document.createElement('input');
   if(proto == 'remote') {
       attach.setAttribute('type','text');
       attach.setAttribute('value','http://');
   }
   else attach.setAttribute('type','file');
   attach.setAttribute('name','url_'+tmp);
   attach.setAttribute('title','Allegato numero'+tmp);       
   
   attachDesc = document.createElement('input');
    attachDesc.setAttribute('type','text'); 
    attachDesc.setAttribute('title','Descrizione allegato'); 
    attachDesc.setAttribute('name','des_'+tmp);
    
  
  attachDel = document.createElement('input');
    attachDel.setAttribute('type','submit');
    attachDel.setAttribute('name','delete_'+tmp);
    attachDel.setAttribute('title','Elimina questo allegato');  
    attachDel.setAttribute('value','del');
    attachDel.setAttribute('onclick','javascript:attachDelete(this);'); 
   
        attachNl = document.createElement('br');
        
   getId('attachId_'+tmp).appendChild(attachNl);                              
   getId('attachId_'+tmp).appendChild(attach);     
   getId('attachId_'+tmp).appendChild(attachNl);                              
   getId('attachId_'+tmp).appendChild(attachDesc);                                                 
   getId('attachId_'+tmp).appendChild(attachDel);     
   getId('attachId_'+tmp).appendChild(attachNl);                                 
 
 }
 return false;
}

function attachDelete(child) 
{
 if(!confirm('Vuoi veramente eliminare questo allegato?')) return false;
 else {
  getId('add_attachment').removeChild(child.parentNode);
 return false;
 }
}

function selectAll(id) 
{
if(!id) return false;
var e = document.forms[id];
for(var i = 0;i < e.length;i++) 
 {
   if(e[i].type == 'checkbox' && !isNaN(e[i].name)) {
       if(e[i].checked == false) e[i].checked = true;else e[i].checked = false;
   }
 }
}

function fadeStar(sId)
{
 var e = document.images
   for(var i = 0;i < e.length;i++) {
     if(e[i].id.substr(0,5) == 'star_') {
     var t = e[i].id.split('_');
     if(t[1] >= sId) e[i].src = "layout/default/buttons/star_disable.gif"
     }
   }
  return false
}
function unfadeStar(sId) 
{
 var e = document.images
   for(var i = 0;i < e.length;i++) {
     if(e[i].id.substr(0,5) == 'star_') {
     var t = e[i].id.split('_');
     if(t[1] <= sId) e[i].src = "layout/default/buttons/star_enable.gif";
     }
   }
  return false
}
function getStrLength(handler,total,target)
{
  if(!handler) return

   h = handler.value
   getId(target).value = total - h.length
  
         if(h.length < total) return true
         else {
                     alert('Stai utilizzando troppi caratteri!\nPuoi utilizzare solo '+total+' caratteri!')      
                     return true;   
                   }
}

function displayDetails(label) 
{
  var el = getId('dbox_groups_cloud');
  for (var i=0;i< el.getElementsByTagName('span').length; i++)  {
  var e = el.getElementsByTagName('span')[i]
  if (e.className == 'stats') 
    {
    e.style.display = (e.style.display == 'inline' ? 'none' : 'inline')
    }
  }
  label.innerHTML = (label.innerHTML == 'Visualizza' ? 'Nascondi' : 'Visualizza')
}

function isValidProfileDescription(obj, size)
 {
 if (!obj) return false
 if (obj.value.length >= size) {
   getId('DescriptionStatus').style.display = 'block'
   getId('DescriptionStatusLabel').style.color = '#85d274'
   getId('DescriptionStatusLabel').innerHTML = ' valida'
   getId('DescriptionStatusLabel').className = 'valid'
   getId('submitDescr').disabled = false

   return true
 } else {
   getId('DescriptionStatus').style.display = 'block'
   getId('DescriptionStatusLabel').style.color = 'red'
   getId('DescriptionStatusLabel').innerHTML = ' non valida'
   getId('DescriptionStatusLabel').className = 'invalid'

   getId('submitDescr').disabled = true
  return false
 }
}

