function screenInfo(){
   var widthUser;
   var heightUser;

   if(typeof(window.innerWidth) == 'number'){
      widthUser = window.innerWidth;
      heightUser = window.innerHeight;
   }
   else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
      widthUser = document.documentElement.clientWidth;
      heightUser = document.documentElement.clientHeight;
   }
   else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
      widthUser = document.body.clientWidth;
      heightUser = document.body.clientHeight;
   }

   return Array(widthUser,heightUser);
}

function getProv(idrg, val){
   $('comuni_cnt').innerHTML = '<select name="idcm"></select>';
   $('province_cnt').innerHTML = '<select name="provincia"></select>';
   if(idrg != '0'){
      $('formLoginForm').regione.className = $('formLoginForm').regione.className.replace(/ obbField/g,'');
      divNfo(0, $('formLoginForm').regione, '', '');
      divNfo(0, $('formLoginForm').provincia, '', '');
      jsrsExecute('/linkomm/public/remote.php', evalBack,  'getProv', Array(''+ idrg, ''+ val), 0);
   }
}
function getCom(idpr, val, val2){
   $('comuni_cnt').innerHTML = '<select name="idcm"></select>';
   if(idpr != '0'){
      $('formLoginForm').provincia.className = $('formLoginForm').provincia.className.replace(/ obbField/g,'');
      divNfo(0, $('formLoginForm').provincia, '', '');
      divNfo(0, $('formLoginForm').idcm, '', '');
      jsrsExecute('/linkomm/public/remote.php', evalBack,  'getCom', Array(''+ idpr, ''+ val, ''+ val2), 0);
   }
}
function evalBack(ret){ eval(ret); }


function setData(){
   var myDate = new Date();
   myDate.setFullYear($('data_nascita_aa').value, ($('data_nascita_mm').value-1), $('data_nascita_gg').value);
   $('formLoginForm').data_nascita.value = Math.round(myDate.getTime()/1000);
}

function divNfo(mode, obj, msg, icon){
   if(mode){
      var a = document.createElement('DIV');
      a.setAttribute('id', 'nfoDiv'+ obj.name);
      $('formLoginForm').appendChild(a);

      $('nfoDiv'+ obj.name).className = 'nfoDiv';
      $('nfoDiv'+ obj.name).innerHTML = msg;

      var myObjPos = Position.cumulativeOffset(obj);
      if(icon) $('nfoDiv'+ obj.name).style.backgroundImage = 'url(\'/linkomm/supp/img/information.gif\') !important';
      $('nfoDiv'+ obj.name).style.left = (myObjPos[0] + Element.getDimensions(obj).width + 5) +'px';
      $('nfoDiv'+ obj.name).style.top = (myObjPos[1] - 2) +'px';
   }
   else if($('nfoDiv'+ obj.name))$('formLoginForm').removeChild($('nfoDiv'+ obj.name));
}

function checkEmail(v){
   var posAt = v.lastIndexOf('@');
   var posDot = v.lastIndexOf('.');
   if(posAt && posDot && posDot>posAt) return true;
   else return false;
}

function ctrlItem(name, isObb){
   var myObj = $('formLoginForm').elements[name];
   var myVal = myObj.value;

   if(name != 'aut1'){
      myObj.className = myObj.className.replace(/ obbField/g,'');
      divNfo(0, myObj, '', '');
   }

   if(isObb && (!myVal || myVal=='0')){
      myObj.className+= ' obbField';
      divNfo(1, myObj, 'Campo obbligatorio', '');
   }
   switch(name){
      case 'data_nascita':
         if(myObj.value && (!$('data_nascita_gg').value || !$('data_nascita_mm').value || !$('data_nascita_aa').value)){
            divNfo(1, $('data_nascita_aa'), 'La data di nascita non &egrave; completa e quindi non verr&agrave; salvata.', 1);
         }
         else divNfo(0, $('data_nascita_aa'), '', '');
      break;
      case 'tel_mobile':
         if(!myObj.value && !$('formLoginForm').tel_ufficio.value && !$('formLoginForm').tel_casa.value){
            divNfo(1, $('formLoginForm').tel_ufficio, 'Inserire almeno un numero telefonico.', '');
            myObj.className+= ' obbField';
            $('formLoginForm').tel_ufficio.className+= ' obbField';
            $('formLoginForm').tel_casa.className+= ' obbField';
         }
         else {
            divNfo(0, $('formLoginForm').tel_ufficio, '', '');
            myObj.className = myObj.className.replace(/ obbField/g, '');
            $('formLoginForm').tel_ufficio.className = $('formLoginForm').tel_ufficio.className.replace(/ obbField/g, '');
            $('formLoginForm').tel_casa.className = $('formLoginForm').tel_casa.className.replace(/ obbField/g, '');

            if(!myObj.value) divNfo(1, myObj, 'Senza il cellulare non si possono ricevere messaggi.', 1);
         }
      break;
      case 'titoli_studio':
         if(!myObj.value)divNfo(1, myObj, 'L\'ammissione all\'associazione &egrave; subordinata alla laurea in scienze motorie o al diploma ISEF', 1);
         else divNfo(0, myObj, '', '');
      break;
      case 'email':
         if(!myObj.value)divNfo(1, myObj, 'Senza un indirizzo email per effetuare l\'accesso sar&agrave; necessario inserire: '+ $('formLoginForm').cognome.value +'.'+ $('formLoginForm').nome.value, 1);
         else divNfo(0, myObj, '', '');
      break;
      case 'email2':
         if(myObj.value){
            if(!checkEmail(myObj.value)){
               myObj.className+= ' obbField';
               divNfo(1, myObj, 'Inserire un indirizzo Email esistente', '');
            }
            else if(myObj.value == $('formLoginForm').email.value){
               myObj.className+= ' obbField';
               divNfo(1, myObj, 'La seconda email deve essere diversa dalla prima.', '');
            }
            else {
               myObj.className = myObj.className.replace(/ obbField/g, '');
               divNfo(0, myObj, '', '');
            }
         }
      break;
      case 'web':
         if(myObj.value){
            if(myObj.value.search('http://') != 0){
               myObj.className+= ' obbField';
               divNfo(1, myObj, 'L\'URL Personale deve essere completo di http://', '');
            }
            else {
               myObj.className = myObj.className.replace(/ obbField/g, '');
               divNfo(0, myObj, '', '');
            }
         }
      break;
      case 'aut1':
         if(!myObj[0].checked || !$('formLoginForm').aut2[0].checked){
            alert('Occorre autorizzare il trattamento dei dati per proseguire.');
         }
      break;
   }
}

function clearDivsNfo(){
   $('formLoginForm').removeChild($('nfoDivConf'));
   $('formLoginForm').removeChild($('nfoDivBack'));
}

function ctrlForm(){      try{
   if(!inModUser){
       ctrlItem('cognome', 1);
       ctrlItem('nome', 1);
       if(!superUser)ctrlItem('aut1', 0);
   }
   ctrlItem('data_nascita', 0);
   ctrlItem('regione', 1);
   ctrlItem('provincia', 1);
   ctrlItem('idcm', 1);
   ctrlItem('tel_mobile', 0);
   ctrlItem('email', 0);
   ctrlItem('email2', 0);
   ctrlItem('titoli_studio', 0);
   ctrlItem('web', 0);

   if(!document.getElementsByClassName('obbField').length){
      var infos = document.getElementsByClassName('nfoDiv');
      if(infos.length){
         var txtInfo = '<p class="int">Conferma delle modifiche</p><ul>';
         for(i=0;i<infos.length;i++){
             txtInfo+= '<li>'+ infos[i].innerHTML +'</li>';
         }

         txtInfo+= '</ul>';

         txtInfo+= '<p class=\"butBot\">';
         txtInfo+= '<input type=\"button\" value=\"Continua\" onclick=\"$(\'formLoginForm\').submit();\"/>';
         txtInfo+= '&nbsp;&nbsp;&nbsp;&nbsp;';
         txtInfo+= '<input type=\"button\" value=\"Annulla\" onclick=\"clearDivsNfo();\" />';
         txtInfo+= '</p>';

         var screen = screenInfo();

         var a = document.createElement('DIV');
         a.setAttribute('id', 'nfoDivConf');
         $('formLoginForm').appendChild(a);
         $('nfoDivConf').className = 'nfoDiv';
         $('nfoDivConf').innerHTML = txtInfo;
         $('nfoDivConf').style.left = ((screen[0] - 400)/2) +'px';
         $('nfoDivConf').style.top = document.body.scrollTop + 250 +'px';

         var b = document.createElement('DIV');
         b.setAttribute('id', 'nfoDivBack');
         $('formLoginForm').appendChild(b);
         $('nfoDivBack').style.height = screen[1] + document.body.scrollHeight +'px';

         return false;
      }
      else return true;
   }
   else {
      alert('Ricontrollare i campi obbligatori');
      return false;
   }

   }
   catch(r){ return false;}
}

function getProv2(idrg){
   $('registro_res').innerHTML = '';
   if(idrg != '0') jsrsExecute('/linkomm/public/remote.php', evalBack,  'getProv', Array(''+ idrg, 'registro'), 0);
}

function getCom2(idpr){
   $('registro_res').innerHTML = '';
   if(idpr != '0') jsrsExecute('/linkomm/public/remote.php', evalBack,  'getCom', Array(''+ idpr, 'registro', '0'), 0);
}

function getRegistro(limit){
   var myVal = $F('regione');
   var myTip = 'r';
   if(myVal <= 0 && $F('provincia') <=0) alert('Selezionare almeno la regione o la provincia');
   else {
      if($F('comune') > 0) {
         myVal = $F('comune');
         myTip = 'c';
      }
      else if($F('provincia') > 0) {
         myVal = $F('provincia');
         myTip = 'p';
      }


      jsrsExecute('/linkomm/public/remote.php', evalBack,  'getRegistro', Array(''+ myTip, ''+ myVal, ''+ limit), 0);
   }
}

function getDetail(mode, ida){
   if(mode){
      var a = document.createElement('DIV');
      a.setAttribute('id','divDet'+ ida);
      $('registro_res').appendChild(a);
      $('divDet'+ ida).className = 'divDet';
      $('divDet'+ ida).style.display = 'none';

      var screen = screenInfo();
      $('divDet'+ ida).style.left = ((screen[0] - 400)/2) +'px';
      $('divDet'+ ida).style.top = document.body.scrollTop + 250 +'px';

      new Draggable($('divDet'+ ida));
      jsrsExecute('/linkomm/public/remote.php', evalBack,  'getDetail', ''+ ida, 0);
   }
   else{
      $('registro_res').removeChild($('divDet'+ ida));
   }
}