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

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


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

/*
* mostra una conferenza
*/
function showConference(url, resX, resY, delta_x, delta_y, width_adv, dateStart, dateStop, minHour) {
  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;
  }

  var minElapse = 15;  // minuti prima della partenza

  var d = new Date();

  var year = d.getFullYear();
  var month = d.getMonth();
  var day = d.getDate();

  month += 1;
  if (month >=0 && month <= 9)
    month = 0+""+month;
  if (day >=0 && day <= 9)
    day = 0+""+day;

  var toDay = parseInt(year+""+month+""+day);

  // data in periodo trasmissione
  if (toDay >=dateStart && toDay <= dateStop) {
    var hh = d.getHours();
    var min = d.getMinutes();
    //converto l'orario in minuti
    var minutes = hh*60 + min;
    
///    if (minutes >= (minHour - minElapse))
    var diffMin = minHour - minutes;
    if (diffMin >= minElapse)
      alert("La conferenza non è ancora iniziata. Collegati in data e ora indicate!");
    else {
      url += "&widthPopup="+widthPopup+"&heightPopup="+heightPopup+"&objX="+objX+"&objY="+objY;
      PopupNoScroll(url, 'registrazione', widthPopup, heightPopup);
    }
  } else alert("Collegati in data e ora indicate!");

}

//
// Mostra messaggio se java NON attivo
//
function javaNotEnabled() {
  var box = document.getElementById('alertJava');
  (navigator.javaEnabled()) ? box.style.display = 'none': box.style.display = 'block';
}

//
// 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");

  return false;
}

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");
}

//
// 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) {}
}

