var toDoOnUnload = "void(0);";
var bulBackup = window.onbeforeunload;
var oulBackup = window.onunload;

function showInsertForm(title){
    document.getElementById("foregroundInsert").innerHTML = "";
    setTitle(title); 
    showObj("loadingDiv"); 
    showObj("blur"); 
    showObj("foregroundInsertContainer");
    hideObj("tablesSelector");
    window.scrollTo( 1, 1);
    window.onbeforeunload = preventExits;
    window.onunload=function() { eval( toDoOnUnload ) };
}

function hideInsertForm(){
    hideObj("loadingDiv"); 
    hideObj("blur"); 
    hideObj("foregroundInsertContainer");
    showObj("tablesSelector");
    window.scrollTo( 1, 1);
    //document.getElementById("foregroundInsert").innerHTML = "";
    window.onbeforeunload = bulBackup;
    window.onunload = oulBackup;
}

function openUrl(url){
    document.getElementById("topWindow").style.height = "400px";
    document.getElementById("topWindow").style.border = "none";
    
    showObj("topWindow");
    document.getElementById("topWindow").src=url;
}

function hideObj(id){
    try{
	document.getElementById(id).style.display = "none";
    }catch(ex){
    }
}
function showObj(id){
    try{
	document.getElementById(id).style.display = "block";
    }catch(ex){
    }
}

function toggleVisibility(id){
    try{
	if(document.getElementById(id).style.display == "block")
	    document.getElementById(id).style.display = "none";
	else
	    document.getElementById(id).style.display = "block";
    }catch(ex){
    }
}

function generalAlert(text){
  alert(text);
}


function clearHome(){
  document.getElementById("homeDiv").innerHTML = "";
}

function ajaxExecute(f, c){
  showObj("loadingDiv");
  f = "x_" + f;
  f = f.substr(0, f.length - 1);
  f = f + ", " + c + ");";
  //alert(f);
  eval(f);
}


function generalAjaxAlert(focus, text){
    if(text != "")
	alert(text);
}

function generalAjaxCallback(focus, z){
    document.getElementById("homeDiv").innerHTML = z;
    try{
	setFocusWithSelect(focus);
    }catch (ex){
	
    }
}

function nullCallback(focus, z){
}

function evalCallback(focus, z){
  eval(z);
}

function getEvalString(st){
  var thereAreEvalText = 1;

  if(st.indexOf("~~{[(") != -1){
    while(thereAreEvalText){
      fp = st.indexOf("~~{[(");
      lp = st.indexOf(")]}~~");
      a = st.substr(fp+5, lp-fp-5);
      eval(a);
      
      st = st.substr(0, fp) + st.substr(lp+5);
      
      if(st.indexOf("~~{[(") != -1)
	thereAreEvalText = 1;
      else
	thereAreEvalText = 0;
    }
  }
  return st;
  
}

function insertAjaxCallback(focus, z){
  document.getElementById("foregroundInsert").innerHTML = getEvalString(z);
  //eval( toDoOnUnload );
  //alert(toDoOnUnload);
  document.getElementById("blur").style.height = "2000px";
  hideObj("loadingDiv");
  showObj("foregroundInsertContainer");
  try{
      setFocusWithSelect(focus);
  }catch (ex){

  }

}

function nullAjaxCallback(focus, z){

}

function setFocus(focus){

  try{
    document.getElementById(focus).focus();
    document.getElementById(focus).select();
  }catch (ex){

  }
}

function setFocusWithSelect(focus){
  try{
    document.getElementById(focus).focus();
    document.getElementById(focus).select();
  }catch (ex){

  }
}

function keyUp(e, keys, evalText){
  var keyArray = keys.split(",");

  if(!e){
    if(window.event){
      e = window.event;
    }else{
      return ;
    }
  }

  if(typeof(e.keyCode) == 'number'){
    e = e.keyCode;
  }else if(typeof(e.which) == 'number'){
    e = e.which;
  }else if(typeof(e.charCode) == 'number'){
    e = e.charCode;
  }else {
    return ;
  }

  //alert(e);

  for(i = 0; i < keyArray.length; i++){
    if(e == keyArray[i])
      eval(evalText);
  }
  
  //alert(e);

}

function changeTables(obj, selected, funct){
    var newTable = obj.options[selected].text;
    //alert(funct);
    x_changeTables(newTable, funct, generalAjaxCallback);
}

function suggerimentoCallback(focus, z){
  document.getElementById("suggerimento").innerHTML = z;
  try{
      setFocusWithSelect(focus);
  }catch (ex){

  }
}

function setTitle(title){
    document.getElementById("titleC").innerHTML = title;
}

var currentDate = new Date();

function showCalendar(giorno, mese, anno, nextFocus){
    openUrl("showCalendar.php?giorno=" + giorno + "&mese=" + mese + "&anno=" + anno + "&nextFocus=" + nextFocus);
}

function showCalendarCallOnblur(giorno, mese, anno, nextFocus, onblurcallid){
    openUrl("showCalendar.php?giorno=" + giorno + "&mese=" + mese + "&anno=" + anno + "&nextFocus=" + nextFocus + "&onBlurCallId=" + onblurcallid);
}

boxmove=false;
function clearBaloon(){
    document.getElementById("baloon").innerHTML = "";
}

function hideBaloon(){
    document.getElementById("baloon").innerHTML = "";
    stopbox(null, "baloonContainer");
}

function stopstartbox(evt, divid) {
    //alert(divid);
    boxmove=!boxmove;
    if(boxmove)
	document.getElementById(divid).style.display = "block";
    else
	document.getElementById(divid).style.display = "none";
    updatebox(evt, divid);
}

function startbox(evt, divid) {
    boxmove=true;
    updatebox(evt, divid);
    document.getElementById(divid).style.display = "block";
}

function stopbox(evt, divid) {
    boxmove=false;
    document.getElementById(divid).style.display = "none";
}

function updatebox(evt, divid) {
    var deltaX;
    var deltaY = 10;
    mouseX= (evt.pageX) ? evt.pageX : evt.clientX;
    mouseY= (evt.pageY) ? evt.pageY : evt.clientY;


    if (mouseX > screen.width - 450)
	deltaX = -420;
    else
	deltaX = 15;

    if (mouseY > screen.height - 470)
	deltaY = -300;
    else
	deltaY = 15;

    if(boxmove){
	document.getElementById(divid).style.left = mouseX + deltaX;
	document.getElementById(divid).style.top = mouseY + deltaY;
    }
}

function updateValue(tablename, colname, idcol, idval, newval){
    x_updateValue(tablename, colname, idcol, idval, newval, nullCallback);
}

function upadateRegNum(tablename){
    //x_updateRegNum(tablename, nullAjaxCallback);
}
function buildMySQLDate(d,m,y){
    var ds = document.getElementById(d);
    var ms = document.getElementById(m);
    var ys = document.getElementById(y);

    var dv = ds.options[ds.selectedIndex].text;
    var mv = ms.options[ms.selectedIndex].text;
    var yv = ys.options[ys.selectedIndex].text;

    var retTimestamp = yv + "-" + mv + "-" + dv;

    return retTimestamp;
}

function buildMySQLTimestamp(d,m,y,h,mm){
    var ds = document.getElementById(d);
    var ms = document.getElementById(m);
    var ys = document.getElementById(y);
    var hs = document.getElementById(h);
    var mms = document.getElementById(mm);

    var dv = ds.options[ds.selectedIndex].text;
    var mv = ms.options[ms.selectedIndex].text;
    var yv = ys.options[ys.selectedIndex].text;
    var hv = hs.options[hs.selectedIndex].text;
    var mmv = mms.options[mms.selectedIndex].text;

    var retTimestamp = yv + "-" + mv + "-" + dv + " " + hv + ":" + mmv + ":00";

    //    alert(retTimestamp);

    return retTimestamp;
}


function enableDate(d, m, y){
  document.getElementById(d).disabled = false;
  document.getElementById(m).disabled = false;
  document.getElementById(y).disabled = false;
}

function disableDate(d, m, y){
  document.getElementById(d).disabled = true;
  document.getElementById(m).disabled = true;
  document.getElementById(y).disabled = true;
}


