/*GLOBAL VARIABLES*/
//utilizzata per chiudere la finestra della progress bar che appare quando carichi un video
var sonPage;
//isErrorOn viene utilizzata dalle pagine ErrorPage.jsp e AdminErrorPage.jsp
//e serve a visualizzare la pagina di errore in modo che occupi tutta la pagina del browser 
var isErrorOn = false;

function changeImage(fieldId, ref)
  {
  //alert("document.getElementById(fieldId).src: "+document.getElementById(fieldId).src);
  //alert("ref: "+ref);
  document.getElementById(fieldId).src = ref;
  }
  
function changeClassName(fieldId, ref)
  {
  //alert("document.getElementById(fieldId).className: "+document.getElementById(fieldId).className);
  //alert("ref: "+ref);
  document.getElementById(fieldId).className = ref;
  }
  
function changeJCaptcha(action) 
  {
  document.formSignup.signup_action.value = action;
  document.formSignup.submit();
  } 
   
function closeSon()
  {
  sonPage.close();
  }
  
function confirmAction(form)
  {
  return confirm("Vuoi aggiungere l'utente "+form.userName.value+" ai tuoi contatti?");
  }
  
function copy(fieldId) {
var inElement = document.getElementById(fieldId);
  if (inElement.createTextRange) {
    var range = inElement.createTextRange();
    if (range)
      range.execCommand('Copy');
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}
  
function copyAddressBook(addressBook, fieldId)
  {
  document.getElementById(fieldId).value += addressBook;
  }

function checkAdminUpdateFormFields()
  {
  var isCheckSuccesfull = false;
  
  var channel = document.getElementById("category").selectedIndex;
  var tag1 = document.getElementById("keyword1").value;
  var tag2 =   document.getElementById("keyword2").value;
  var tag3 =   document.getElementById("keyword3").value;
  
  if (document.getElementById("mediaName").value == "") 
    {
    alert("Il campo titolo è obbligatorio");
    }
  else if (document.getElementById("mediaDescription").value == "") 
    {
    alert("Il campo descrizione è obbligatorio");
    }
  else if (tag1 == "") 
    {
    alert("Il campo Prima Tag è obbligatorio");
    }
  else if (tag2 == "") 
    {
    alert("Il campo Seconda Tag è obbligatorio");
    }
  else if (tag3 == "") 
    {
    alert("Il campo Terza Tag è obbligatorio");
    }
  else if ((tag1 == tag2) || (tag1 == tag3) || (tag2 == tag3)) 
    {
    alert("Le parole chiave devono essere distinte");
    }
  else if (channel == 0 || channel == null) 
    {
    alert("Selezionare un canale");
    }
  else 
    {
    isCheckSuccesfull = true;
    }
    //alert ("isCheckSuccesfull: "+isCheckSuccesfull + " canale:" +channel);
  return isCheckSuccesfull;
  }

function checkSignupFormFields()
  {
  var isCheckSuccesfull = false;
  
  // Variabili associate ai campi del modulo

  var username = document.getElementById("usernameId");
  var password = document.getElementById("passwordId");
  var confirmPassword = document.getElementById("confirmPasswordId");
  var birthdate = document.getElementById("birthdateId");
  var email = document.getElementById("emailId");
  var j_captcha_response = document.getElementById("j_captcha_response");
  var acceptance = document.getElementById("acceptance");

  // Espressione regolare dell'email
  var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;

  //Effettua il controllo sul campo username
  if (username.value == "") 
    {
    alert("Il campo username è obbligatorio.");
    username.focus();
    }
  else if (username.value.length > 20) 
    {
    alert("Il campo username non può essere superiore a 20 caratteri.");
    username.focus();
    }
  //Effettua il controllo sul campo password
  else if ((password.value == "")) 
    {
    alert("Il campo password è obbligatorio.");
    password.focus();    
    }
  //Effettua il controllo sul campo confirmPassword
  else if ((confirmPassword.value == "")) 
    {
    alert("Il campo conferma password è obbligatorio.");
    confirmPassword.focus();
    }
  //Effettua il controllo sul campo confirmPassword
  else if ((password.value != confirmPassword.value)) 
    {
    alert("Le due password inserite non coincidono.");
    confirmPassword.focus();
    }
  else if (!email_reg_exp.test(email.value) || (email.value == "")) 
    {
    alert("Inserire un indirizzo email corretto.");
    email.select();    
    }
  //Effettua il controllo sul campo birthdate
  else if ((birthdate.value == "") || (birthdate.value.length != 10)) 
    {
    alert("Il campo data di nascita deve essere nel formato gg/mm/aaaa.");
    birthdate.focus();    
    }
  //Effettua il controllo sul campo DATA DI NASCITA
  //verifica che la data sia inserita in formato gg/mm/aaaa 
  //che sia una data esistente (ad esempio non 36/18/2000) 
  //e non sia inferiore al 1900
  else if (birthdate.value.substring(2,3) != "/" ||
    birthdate.value.substring(5,6) != "/" ||
    isNaN(birthdate.value.substring(0,2)) ||
    isNaN(birthdate.value.substring(3,5)) ||
    isNaN(birthdate.value.substring(6,10))) 
    {
    alert("Inserire data di nascita in formato gg/mm/aaaa");
    //document.modulo.nascita.value = "";
    birthdate.focus();
    }
  else if (birthdate.value.substring(0,2) > 31) 
    {
    alert("Impossibile utilizzare un valore superiore a 31 per i giorni della data di nascita");
    birthdate.focus();
    }
  else if (birthdate.value.substring(3,5) > 12) 
    {
    alert("Impossibile utilizzare un valore superiore a 12 per i mesi della data di nascita");
    birthdate.focus();    
    }
  else if (birthdate.value.substring(6,10) < 1900) 
    {
    alert("Impossibile utilizzare un valore inferiore a 1900 per l'anno della data di nascita");    
    birthdate.focus();    
    } 
  else if (j_captcha_response.value == "") 
    {
    alert("Il codice sicurezza non è corretto");    
    j_captcha_response.focus();    
    }
  else if (!acceptance.checked) 
    {
    alert("E' necessario esprimere l'adesione ai termini di utilizzo e privacy");    
    acceptance.focus();    
    } 
  //form valido
  else 
    {
    isCheckSuccesfull = true;
    }
  
  return isCheckSuccesfull;
  }

function checkAdminUserUpdateFormFields()
  {
  return true;
  }
   
function deleteMedia(mediaName, mediaId, topicId)
  {
  if(confirm("Sei sicuro di voler eliminare il video '"+mediaName+"'?"))
    self.location.href="DeleteMediaServlet?mediaId="+mediaId+"&topicId="+topicId;
  }

function deleteUser(userName, userId)
  {
  if(confirm("Sei sicuro di voler eliminare l'utente '"+userName+"' in maniera definitva dal database?"))
    self.location.href="AdminDeleteUserServlet?userId="+userId;
  }
   
function deleteUserFromGroup(username, userId, groupId)
  {
  if(confirm("Sei sicuro di voler eliminare il contatto '"+username+"'?"))
    self.location.href="RemoveUserFromGroupServlet?contactId="+userId+"&groupId="+groupId;
  }
  
function deleteGroup(groupName, groupId)
  {
  if(confirm("Sei sicuro di voler eliminare il gruppo '"+groupName+"'?"))
    self.location.href="RemoveGroupServlet?groupId="+groupId;
  }

function deleteVideoRoom(videoRoomName, topicId)
  {
  if(confirm("Sei sicuro di voler eliminare la Video Room '"+videoRoomName+"'?"))
    self.location.href="DeleteTopicServlet?topicId="+topicId;
  }

/*Gestisce la problematica sul layout nel caso venga generato un errore all'interno di una pagina jsp*/
function isErrorFound()
  {
  if(isErrorOn)
    location.href="ErrorPage.jsp";
  }

function openInformazioniProfilo(userId)
  {
  var urlContratto='ShowProfileServlet?userId='+userId;
  open(urlContratto , 'Contratto', 'scrollbars=yes,menubar=no,resizable=no,height=520,width=330');
  }
  
function openPopup(path, nomeDocumento) 
   {
   window.open(path+"/"+nomeDocumento+".html",nomeDocumento,"scrollbars=yes,toolbar=no,menubar=no,location=no,width=700,height=700,top=100,left=200");  
   }
   
function openPopupJsp(path, nomeDocumento) 
   {
   window.open(path+"/"+nomeDocumento+".jsp",nomeDocumento,"scrollbars=yes,toolbar=no,menubar=no,location=no,width=700,height=700,top=100,left=200");  
   }
   
function openPopupJsp(path, nomeDocumento, tracklistKey) 
   {
   window.open(path+"/"+nomeDocumento+".jsp?tracklistKey="+tracklistKey,nomeDocumento,"scrollbars=yes,toolbar=no,menubar=no,location=no,width=700,height=700,top=100,left=200");  
   }
   
function openPopupMediaAdvertisementJsp(nomeDocumento, mediaId) 
   {
   window.open(nomeDocumento+".jsp?mediaId="+mediaId,nomeDocumento,"scrollbars=yes,toolbar=no,menubar=no,location=no,width=800,height=500,top=100,left=200");  
   }

function openRelativePopup( relativePath) 
   {
   window.open(relativePath+".html",nomeDocumento,"scrollbars=yes,toolbar=no,menubar=no,location=no,width=700,height=700,top=100,left=200");  
   }
   
function openPopup(path, nomeDocumento) 
   {
   window.open(path+"/"+nomeDocumento+".html",nomeDocumento,"scrollbars=yes,toolbar=no,menubar=no,location=no,width=700,height=700,top=100,left=200");  
   }
   
function openPopupSmall(path, nomeDocumento) 
   {
   window.open(path+"/"+nomeDocumento+".html",nomeDocumento,"scrollbars=no,toolbar=no,menubar=no,location=no,width=700,height=480,top=100,left=200");  
   }
   
function openPopupSmallJsp(path, nomeDocumento) 
   {
   sonPage = window.open(path+"/"+nomeDocumento+".jsp",nomeDocumento,"scrollbars=no,toolbar=no,menubar=no,location=no,width=700,height=450,top=100,left=200");  
   }
   
function openURL(URL) 
   {
    window.open( URL, "YourVideo")
   }
  

function hideDiv(divId)
  {
  document.getElementById(divId).style.display='none';  
  }

/**Mostra un div se è nascosto, lo nasconde se è visibile*/
function hideShowDiv(divId)
  {
  //alert(divId);
  //alert(document.getElementById(divId).style.display);
  if (document.getElementById(divId).style.display=='block')
	  document.getElementById(divId).style.display='none';  
  else
	  document.getElementById(divId).style.display='block';  
  }
  
function showDiv(divId)
  {
  document.getElementById(divId).style.display='block';  
  }
  
function imposeMaxLength(field, maxLength)
  {
  if (field.value.length>maxLength)
    {
    alert("Non si possono inserire più di "+maxLength+" caratteri.");
    field.value=field.value.substring(0, maxLength);
    }
  }
function resetChannel()
  {
  //alert(document.getElementById("channelCheckbox").checked)
  if (document.getElementById("channelMenuCheckbox").checked==true) 
    {
	document.getElementById("categoryMenu").disabled = false;
    }
  else 
    {
	document.getElementById("categoryMenu").disabled = true;
    document.getElementById("categoryMenu").selectedIndex = 0;
    }
  }

function resetUsername()
  {
  //alert(document.getElementById("usernameCheckbox").checked)
  if (document.getElementById("usernameMenuCheckbox").checked==true) 
    {
	document.getElementById("usernameMenu").disabled = false;
    }
  else 
    {
	document.getElementById("usernameMenu").disabled = true;
    document.getElementById("usernameMenu").value = 'Tutti gli utenti';
    }
  }

//scommentare quando dovrà essere attivato
//tipoTvId nella Signup.jsp
function showLayer(layerId)
  {
  //if (BrowserDetect.browser == "Firefox")
    //document.getElementById(layerId).style.display='table-row';   
  //else
    //document.getElementById(layerId).style.display='inline';  
  }

function hideBox(box)
  {
  document.getElementById(box).style.display='none';
  }

function showBox(box)
  {
  document.getElementById(box).style.display='block';
}

function showHideBox(box)
  {
  if(document.getElementById(box).style.display == "none") 
    {
    document.getElementById(box).style.display='block';      
    }
  else
    {
    document.getElementById(box).style.display='none';
    }
  }

function submitAdminMediaPlayDelete() 
  {  
  if(confirm("Sei sicuro di voler eliminare il video?"))
    {
    document.formAdminMediaPlay.action = "AdminDeleteMediaServlet";
    document.formAdminMediaPlay.submit();
    }
  } 

function submitAdminMediaPlayUpdate() 
  {  
  if (checkAdminUpdateFormFields()) 
    {
    document.formAdminMediaPlay.action = "AdminUpdateMediaServlet";
    document.formAdminMediaPlay.submit();
    }
  }

function submitAdminUserUpdateForm() 
  {  
  if (checkAdminUserUpdateFormFields()) 
    {
    document.formAdminUserUpdate.action = "AdminUpdateUserServlet";
    document.formAdminUserUpdate.submit();
    }
  }  

function submitAdminUserListUpdateForm() 
  {
  //document.formAdminUserListUpdate.action = "AdminUpdateUserListServlet";
  document.formAdminUserListUpdate.submit();
  }
  
function submitSignupUpdateForm() 
  {
  document.formSignupUpdate.action = "DeleteUserImageServlet";
  document.formSignupUpdate.submit();
  }
