// JavaScript Document

// Common Vars
var pageW;
var pageH;
var ELEMENT_NODE = 1;

//Color constants
var fgColorOver = '#FF0000';
var fgColorOut = '#003399';
var bgColorOver = '#BFDBFF';
var bgColorOut = '#FFFFFF';
var rowBgColorOdd = '#FFFFFF';
var rowBgColorEve = '#F0F0F0';
var rowBgColorOver = '#BFDBFF';
var rowBgColorSel = '#FFFFCE';

function backPage(){
  window.back();
}

function openPage(page){
  window.location.href = page;
}

function closeWin(){
  window.close();
}

function showFocus(obj){
  if(!document.getElementById) return;
  obj.style.border = "1px solid #FFCC66";
}

function showBlur(obj){
  if(!document.getElementById) return;
  obj.style.border = "1px solid #6699CC";
}

function setBgColor(cell, action,bgc){
	if (action=='over'){
		cell.style.backgroundColor = bgColorOver;
		cell.style.cursor='pointer';
		}
	if (action=='out'){
		cell.style.backgroundColor = bgc!=''?bgColorOut:bgc;
		cell.style.cursor='default';
		}
}

function setLiBgColor(obj, action){
	if (action=='over'){
		obj.style.backgroundColor = '#3366CC';
		obj.style.color='#FFFFFF';
		//obj.style.backgroundColor = bgColor;
		//obj.style.cursor='default';
		}
	if (action=='out'){
		obj.style.backgroundColor = '#FFFFFF';
		obj.style.color='#000000';
		//obj.style.backgroundColor = bgColor;
		//obj.style.cursor='default';
		}
}

function setFgColor(cell, action){
	if (action=='over'){
		cell.style.color = fgColorOver;
		cell.style.cursor='pointer';
	}	
	if (action=='out'){
		cell.style.color = fgColorOut;
		cell.style.cursor='default';
	}
}

function setRowBgColor(Row, action, bgColor){
	if (action=='over'){
		Row.style.backgroundColor = rowBgColorOver;
		Row.style.cursor='pointer';
		}
	if (action=='out'){
		Row.style.backgroundColor = bgColor;
		Row.style.cursor='default';
		}
}

function setRowBgColor1(Row, action, bgColor){
  // Per le righe non clikkabili
	if (action=='over'){
		Row.style.backgroundColor = rowBgColorOver;
		}
	if (action=='out'){
		Row.style.backgroundColor = bgColor;
		}
}

function setCheckBoxes(frmName, boolChk, chkName, chkNum){

	for (i=1;i<=chkNum;i++){
		if (!boolChk){
			document.forms[frmName].elements[chkName+i].checked = false;
			document.getElementById('Row'+i).style.backgroundColor = (i%2)==0 ? rowBgColorEve : rowBgColorOdd;
		} else{
			document.forms[frmName].elements[chkName+i].checked = true;
			document.getElementById('Row'+i).style.backgroundColor = rowBgColorSel;
		}
	}
}

function setRowBgColorChk(formName, chBoxName, obj, action, bgrColor){
  if (action=='over'){
  	obj.style.cursor='pointer';
	obj.style.backgroundColor = rowBgColorOver;
  }
  if (action=='out'){
	obj.style.cursor='default';
	if (document.forms[formName].elements[chBoxName].checked==true){
		obj.style.backgroundColor = rowBgColorSel;
		//obj.style.fontWeight = 'bold';
	}else {
		obj.style.backgroundColor = bgrColor;
	}
	
  }
  if (action=='click'){
   		obj.style.cursor='pointer';
		if (document.forms[formName].elements[chBoxName].checked==false){
			obj.style.backgroundColor = bgrColor;
			//obj.style.fontWeight = 'normal';
		}else {
			obj.style.backgroundColor = rowBgColorSel;
			//obj.style.fontWeight = 'bold';
		}
	}
}

function setRowBgColorNode(nodeId, obj, action, bgrColor){
  if (action=='over'){
  	if (nodeId!=''){
	  obj.style.cursor='pointer';
	}else {
	  obj.style.cursor='default';
	}
	obj.style.backgroundColor = rowBgColorOver;
  }
  if (action=='out'){
	obj.style.cursor='default';
	if (document.getElementById(nodeId).src==minusImg.src ){
		obj.style.backgroundColor = rowBgColorSel;
		obj.style.fontWeight = 'bold';
	}else {
		obj.style.backgroundColor = bgrColor;
		obj.style.fontWeight = 'normal';
	}
  }
  if (action=='click'){
   		obj.style.cursor='pointer';
		if (document.getElementById(nodeId).src==plusImg.src){
			obj.style.backgroundColor = bgrColor;
		}else {
			obj.style.backgroundColor = rowBgColorSel;
		}
	}
}
function centerObj(obj,dir,offSetW,offSetH){
  // Posiziona obj al centro di Container oppure centra tutto il suo contenuto
  // obj: oggetto da posizionare o contenitore oggetti
  // dir: 0=sia w che h, 1=solo w, 2=solo h
  // tiene conto anche di eventuali offset;
  var mdivW = 0;
  var mdivH = 0;
  var tmp;
  var mdiv=document.getElementById(obj);
  // Ricavo l'area utile
  pageW=document.layers?innerWidth:document.body.clientWidth - offSetW;
  pageH=document.layers?innerHeight:document.body.clientHeight - offSetH;
  // verifico se obj ha dimensioni. Se no č un container. 
  if (mdiv.offsetHeight>0){
    mdivH = mdiv.offsetHeight;
    mdivW = mdiv.offsetWidth;
  }
  var sum;
  for (var i=0;i<mdiv.childNodes.length;i++){
	// Cerco il pių a destra ed il pių in basso
	if (mdiv.childNodes[i].nodeType == ELEMENT_NODE) {
	  sum = mdiv.childNodes[i].offsetWidth + mdiv.childNodes[i].offsetLeft;
	  if (sum > mdivW) mdivW = sum;
	  sum = mdiv.childNodes[i].offsetHeight + mdiv.childNodes[i].offsetTop;
	  if (sum > mdivH) mdivH = sum;
	}	
  }
  if (pageH>mdivH && (dir==0 || dir ==2)) mdiv.style.top=(pageH-mdivH)/2 + offSetH;
  if (pageW>mdivW && (dir==0 || dir ==1)) mdiv.style.left=(pageW-mdivW)/2 + offSetW;
}

function getOffSetTop(obj){
  var top = obj.offsetTop;
  while (obj.offsetParent){
    obj = obj.offsetParent;
	top += (obj.offsetTop - obj.scrollTop);
  }
  return top;
}

function getOffSetLeft(obj){
  var left = obj.offsetLeft;
  while (obj.offsetParent){
    obj = obj.offsetParent;
	left += (obj.offsetLeft -obj.scrollLeft);
  }
  obj = null;
  return left;
}

function sumTime(aT, bT){
  // sum of 2 time  in hh.mm.ss format
  var HH = parseInt(aT.substring(0,2),10) + parseInt(bT.substring(0,2),10);
  var mm = parseInt(aT.substring(3,5),10) + parseInt(bT.substring(3,5),10);
  var ss = parseInt(aT.substring(6,8),10) + parseInt(bT.substring(6,8),10);
  
  HH += Math.floor(mm/60);
  mm = mm - Math.floor(mm/60)*60 + Math.floor(ss/60);
  ss -= Math.floor(ss/60)*60;
  return (HH.toString().length==1?'0'+HH:HH) + '.' + (mm.toString().length==1?'0'+mm:mm) + '.' + (ss.toString().length==1?'0'+ss:ss);
}

function checkTime(time, threshold){
  var res = time;
  var tH = parseInt(time.substring(0,2),10);
  var tm = parseInt(time.substring(3,5),10);
  var ttH = parseInt(threshold.substring(0,2),10);
  var ttm = parseInt(threshold.substring(3,5),10);

  if (tH > ttH) {
	  res = '<font color=\"#CC0000\">' + time + '<\/font>';
  } else {
    if (tH==ttH) {
	  if (tm > ttm){
	    res = '<font color=\"#CC0000\">' + time + '<\/font>';
	  }
	} 
  }
  return res;
}

function getBestHeight(tableId,compareH){
  var sumOffsetH = 0;
  var i = 0;
  var table = document.getElementById(tableId);
  while (sumOffsetH + table.childNodes[0].childNodes[i].offsetHeight<compareH) {
  sumOffsetH += table.childNodes[0].childNodes[i++].offsetHeight;
  } 
  return sumOffsetH;  
}

function addShadow(objTarget,objId,offsetTop,offsetLeft){
  document.getElementById(objId).style.top = document.getElementById(objTarget).offsetTop + offsetTop;
  document.getElementById(objId).style.left = document.getElementById(objTarget).offsetLeft + offsetLeft; 
  document.getElementById(objId).style.width = document.getElementById(objTarget).offsetWidth;
  document.getElementById(objId).style.height = document.getElementById(objTarget).offsetHeight;
  document.getElementById(objId).style.opacity = .50;
  document.getElementById(objId).style.filter = 'alpha(opacity=50)';
}	
function getStrong(txt){
  return '<strong>'+txt+'</strong>';
}
function checkBorderWidth(objId){
  if (parseInt(document.getElementById(objId).style.width)<document.getElementById(objId).offsetWidth){
    //per browser compatybility
    document.getElementById(objId).style.width = document.getElementById(objId).offsetWidth-4;
  }
}

