//
// preload in memoria di immagini
//
var plusImg = new Image();
var minusImg = new Image();
plusImg.src = "/videolive/images/plus.gif";
minusImg.src = "/videolive/images/minus.gif";


//
// Restiruisce il browser
//
function browserWeb() {
  if (document.layers) //Netscape 4.x 
    return "NS4"
  else if (document.all)  //explorer
    return "IE";
  else if (document.getElementById) // Netscape 6.x
    return "NS6";

  return NULL;
}

function openPopup(url, name, width, height) {
  newWindow = window.open(url, name, 'width=' + width + ',height=' + height + ',status=no toolbar=no, location=no, directories=no, menubar=no, scrollbars=yes, resizable=yes');
  return false;
}

function PopupNoScroll(url, name, width, height) {
  newWindow = window.open(url, name, 'width=' + width + ',height=' + height + ',status=no toolbar=no, location=no, directories=no, menubar=no, scrollbars=yes, resizable=no');
}

//
// controlla dimensioni video PC e ridimensiona per apertura popup
//
function ShowReg(url, resX, resY, delta_x, delta_y, width_adv) {
  var altezza = window.screen.height; // altezza schermo
  var larghezza = window.screen.width; //larghezza schermo

  var widthPopup = resX + delta_x + width_adv;
  var heightPopup = resY +  delta_y;
  var setRatio = 1;

  var tipoBrowser = browserWeb();

  //ingombri browser
  var hPadd = 90;
  var wPadd = 50;
  altezza -= hPadd;
  larghezza -= wPadd;

  // controllo se ridimensionare popup
  if (altezza < heightPopup || larghezza < widthPopup) {
    //maxWidth = larghezza*0.85;
    maxWidth = larghezza*0.90;
    //maxHeight = altezza*0.80;
    maxHeight = altezza*0.85;
    widthRatio = (maxWidth - delta_x - width_adv) / resX;
    heightRatio = (maxHeight - delta_y) / resY;
    setRatio = Math.min(widthRatio, heightRatio);
  }
  widthPopup = Math.floor(setRatio * resX) + delta_x + width_adv;
  heightPopup = Math.floor(setRatio * resY) + delta_y;
  objX = Math.floor(resX * setRatio) +  delta_x;
  objY = Math.floor(resY  * setRatio) +  delta_y;

  if (tipoBrowser == "IE") {
    widthPopup += 35;
    heightPopup += 15;
  } else {
    widthPopup += 25;
    heightPopup += 10;
  }

  url += "&widthPopup="+widthPopup+"&heightPopup="+heightPopup+"&objX="+objX+"&objY="+objY;
  openPopup(url, 'registrazione', widthPopup, heightPopup);
}


//controllo dati form inserimento relatori
function checkDataRel(form) {

  var ragionesociale = document.getElementById('ragionesociale').value;
  var utente = document.getElementById('utente').value;
  var password = document.getElementById('password').value;
  var mail = document.getElementById('email').value;
  var codprop = document.getElementById('codproprietario').value;

  var goodEmail = mail.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,3}))$)\b/gi); 
  var ret = true;

  var showMsg = "";
  if (ragionesociale == "" || utente == "" || password == "" || codprop == "") {
    showMsg = "Perfavore inesrire i dati obbligatori";
    ret = false;
  }


  if ( ! goodEmail && mail != "") {
    showMsg += "- mail non corretta";
    ret = false;
  }

  if (showMsg != "")
    alert(showMsg);

  return ret;
}

// mostra un elemento
function overlay(curobj, subobjstr, imgobj){
  if (document.getElementById){
    var subobj = document.getElementById(subobjstr);
    subobj.style.display = (subobj.style.display != "block") ? "block" : "none";
    if (subobj.style.display != "block")
      document.getElementById(imgobj).src = "/videolive/images/plus.png";
    else document.getElementById(imgobj).src = "/videolive/images/minus.png";

    return false
  }
  else
  return true
}

//
// gestione sistema immagine upload in progress
//
function showUploadFile(idFrame, idFileUpload) {

  try {
    var idFile = document.getElementById(idFileUpload);
    var valId = idFile.value;

    // mostro l' iframe
    if (valId != '') {
      document.getElementById(idFrame).width = '450';
      document.getElementById(idFrame).height = '100';
    }
  } catch (e) {}
}


//
//  mostra messaggio JAVA e nasconde l'applet
//
function alertJava() {
  alert("ATTENZIONE! JAVA NON INSTALLATO");
}

//
// cambia i valori delle select per video visibile in ricerca, votabile, etc. etc.
//
function update_select(ricerca) {

  try {

    var index = ricerca.selectedIndex;

    if (index != -1) {
      var valRicerca = ricerca.options[index].value;
    }

    // privato potrebbe non essere una select
    try {
      var p = document.getElementById('privato');
      if (index != -1) {
        p.selectedIndex = index;
      }
    }  catch (e) {}

    var rate = document.getElementById('allow_ratings');
    if (index != -1) {
      rate.selectedIndex = index;
    }
  }  catch (e) {}
}

//
//mostra o meno il blocco dei permessi registrazione
//
function displayParam() {
  try {
    var par =  document.getElementById('paramYvl');
    par.style.display = (par.style.display == "block") ? "none" : "block";
  } catch (e) {}
}

//
// controllo se posso impostare un valore piuttosto che un altro nella select video privato
//
function check_privato(privato) {

  try {
    var visible = document.getElementById('ricerca');
    var index = visible.selectedIndex;
    // controllo se selezionato ricerca = N
    if (visible.options[index].value == "N") {
      if (privato.selectedIndex != 0) {
        alert("Operazione non consentita. Il video risulta \"non visibile nella ricerca\"");
        privato.selectedIndex = 0;  // forzo privato a NO
      }
      privato.selectedIndex = 0;  // forzo privato a NO
      return;
    }
  }  catch (e) {}
}

//
// fa il trim di una stringa
//
function trim(str) {
  return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

//
// aggiunta tag nella form inserimento titoli
//
function addTags(txt) {
  var idTag = document.getElementById('tags');
  var maxL = idTag.maxLength;
  var val = trim(idTag.value);

  if(txt != '') {
    if (val != '') {
      if (val.charAt(val.length -1) != ',') 
        txt = "," + txt + ",";
    } else txt =  txt + ",";
    if ((val + txt).length < maxL)
      idTag.value = val + txt;
  }

}

//
// Mostra / Nasconde un box
//
function showBox(idClick, idOther) {
  var boxClick = document.getElementById(idClick);
  var boxOther = document.getElementById(idOther);

  boxClick.style.display = (boxClick.style.display == "none") ? "block" : "none";
  boxOther.style.display= "none";

  var imgTag1 = document.getElementById('ico'+idClick);
  var imgTag2 = document.getElementById('ico'+idOther);
  imgTag1.src = (boxClick.style.display == "none") ? eval("plusImg.src") : eval("minusImg.src");
  imgTag2.src = eval("plusImg.src");
}

function changeExpand(id) {
/*
  var imgTag = document.getElementById('moreTag');
  var imgOwn = document.getElementById('moreOwnTag');

  var statusMoreTag = document.getElementById('ownTagUsed').style.display;
  var statusMoreOwnTag = document.getElementById('ownTagUsed').style.display;
*/
  //imgTag.src = (statusMoreTag == "none") ? eval("minusImg.src") : eval("plusImg.src");
}

//
// Mostra / nasconde immagine di loading
//
function loadContentImg() {
  var statusShow = $('#loader').css('display')
  if (statusShow == "none") {
    $('#dialogOverlay').show();
    $('#loader').show();
  } else {
    $('#dialogOverlay').hide();
    $('#loader').hide();
  }
}

