var isNS4 = (document.layers) ? true : false;
var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;


var PopupArray = new Array();
var myWin = null;
var ReloadMe = false;
var myWidth = 0, myHeight = 0;

if( typeof( window.innerWidth ) == 'number' ) {
  //Non-IE
  myWidth = window.innerWidth;
  myHeight = window.innerHeight;
} else if( document.documentElement &&
    ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  //IE 6+ in 'standards compliant mode'
  myWidth = document.documentElement.clientWidth;
  myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  //IE 4 compatible
  myWidth = document.body.clientWidth;
  myHeight = document.body.clientHeight;
}

if (myHeight >screen.height) {
  window.resizeTo(myWidth,screen.height-100);
  moveTo((screen.width/2)-(myWidth/2),(screen.height/2)-((screen.height-100)/2)-20);
}



function popup(url,w,h,sb)
{
	var d = new Date();
	myWin = window.open(url,'popupwindow'+escape(d.getTime()),'left='+((screen.width/2)-(w/2))+',top='+((screen.height/2)-(h/2)-40)+',screenX='+((screen.width/2)-(w/2))+',screenY='+((screen.height/2)-(h/2)-40)+',Width='+w+',height='+h+',alwaysRaised=yes,history=no,resizable=yes,status=no,scrollbars=' + sb +',menubar=no,toolbar=no,location=no')	
	myWin.opener = self;	
	PopupArray[PopupArray.length] = myWin;
	myWin.focus();
}


function CheckPopUp()
{
   setTimeout("TestPopUp()", 50);
}

function TestPopUp()
{
   var PopupArrayTemp = new Array();
		
	if (PopupArray.length >0)
	{   
	   for (var i=0; i<PopupArray.length; i++)
	   {
	      myWin = PopupArray[i];	      
	      if (!myWin.closed)
	      {
            myWin.focus();
	         PopupArrayTemp[PopupArrayTemp.length]=myWin;	         
	      }
	   }   
	   PopupArray = PopupArrayTemp;
	}
	
	if (PopupArray.length == 0)
	{
	   if (ReloadMe == true)
	   {
	      ReloadMe = false;
	      self.location.reload();	      
	   }
     return true;	
	}
	else
	{		   
		return false;		
	}
}


function ClosePopUp()
{
	if (PopupArray.length >0)
	{   
	   for (var i=0; i<PopupArray.length; i++)
	   {
	      myWin = PopupArray[i];
	      if (myWin && !myWin.closed)
	      {	
	         myWin.close(); 
	      }
	   }   
		return true;					
	}
}

function ReloadOpener()
{
   if (self.opener != null)
   {
      self.opener.ReloadMe=true;    
      self.opener.ReloadOpener();
   }   
}


function ReloadPage()
{
   if (self.opener != null)
   {
      self.opener.ReloadMe=true;    
   } 
   self.location.reload();

}


function mostra(x)
{
	document.getElementById(x).style.display = '';
}


function nascondi(x)
{
  document.getElementById(x).style.display = 'none';
}


function switchDiv(strDivName,bolVisible)
{
	if (isNS4)
	{
	 objElement = document.layers[strDivName];
	}
	else if (isIE4)
	{
	 objElement = document.all[strDivName];
	}
	else if (isIE5 || isNS6)
	{
	 objElement = document.getElementById(strDivName);
	}
	
	if(isNS4){
	   if(!bolVisible) {
		 objElement.visibility ="hidden"
	   } else {
		 objElement.visibility ="visible"
	   }     
	 }else{
	   if(!bolVisible) {
		 objElement.style.visibility = "hidden";
	   } else {
		 objElement.style.visibility = "visible";
	   }
	}
	
}

function IsInteger(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

   for (var i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (var i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
}

 
var day=''; 
var month=''; 
var year=''; 
var color='';
var datefield=''; 
var colorfield='';


function y2k(number)
{
   return (number < 1000) ? number + 1900 : number;
} 
   
function SetToday() 
{ 
   var today = new Date(); 
   day   = today.getDate(); 
   month = today.getMonth(); 
   year  = y2k(today.getYear()); 
} 
 
function padout(number)
{
   return (number < 10) ? '0' + number : number;
} 

function restart() 
{ 
  setfield=eval(datefield) ; 
  // setfield.value= '' + padout(day) + '/' + padout(month - 0 + 1) + '/' + year; 

setfield.value= '' + year + '/' + padout(month - 0 + 1) + '/' + padout(day); 
  mywindow.close(); 
}
 
function DateSelector(x) 
{ 
  datefield=x; 
  var w=350; 
  var h=250; 
  setfield=eval(datefield) ; 
  if ( setfield.value!='') 
  { 

   year=setfield.value.substr(0,4);
   month=setfield.value.substr(5,2)-1; 
   day=setfield.value.substr(8,2); 

   if (year.indexOf(" ")>0)
   {
      year=year.substr(0,2);
   }
  
  } 
  else 
  { 
  	SetToday(); 
  }
  mywindow=open('../js/selectors/date_selector.htm','date_selector','left='+((screen.width/2)-(w/2))+',top='+((screen.height/2)-(h/2))+',screenX='+((screen.width/2)-(w/2))+',screenY='+((screen.height/2)-(h/2))+',Width='+w+',height='+h+',alwaysRaised=yes,history=no,resizable=no,status=no,scrollbars=no,menubar=no'); 
  mywindow.location.href = '../js/selectors/date_selector.htm'; 
  if (mywindow.opener == null) mywindow.opener = self; 
}
 
 

function ColorSelector(x) 
{ 
  colorfield = x; 
  setfield=eval(colorfield) ; 
  color = setfield.value;
  var w=220; 
  var h=200; 
  mywindow=open('js/selectors/color_selector.htm','color_selector','left='+((screen.width/2)-(w/2))+',top='+((screen.height/2)-(h/2))+',screenX='+((screen.width/2)-(w/2))+',screenY='+((screen.height/2)-(h/2))+',Width='+w+',height='+h+',alwaysRaised=yes,history=no,resizable=no,status=no,scrollbars=no,menubar=no'); 
  mywindow.location.href = 'js/selectors/color_selector.htm'; 
  if (mywindow.opener == null) mywindow.opener = self; 
}

function SetValue(x,y)
{
  setfield=eval(x) ; 
  setfield.value= ''+ y;
  setfield.onchange();
  mywindow.close(); 
}


 
function Get_Cookie(name)
{
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure)
{
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}


function addOption(field,text,value,selected)
{
	var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
	var NSX = (navigator.appName == "Netscape");
	var IE4 = (document.all) ? true : false;
	
	var obj=eval("document." + field);
	var newOpt  = new Option(text, value, false, selected);
			
	if (NSX)
	{	
		obj.options[obj.length] = newOpt;		
	}
	else if (IE4)
	{
		if(obj!=null && obj.options!=null)
		{
			obj.options[obj.options.length] = newOpt;
		}
	}
  
}

 
function fitimage(img,maxsize)
{
  oldWidth=img.width;
  oldHeight=img.height;  
  newWidth=oldWidth;
  newHeight=oldHeight;
 
  if (oldWidth>=oldHeight)
  {
    if (oldWidth>maxsize)
    {
      newWidth=maxsize;
      newHeight=(newWidth / oldWidth) * oldHeight;
    }  
  }
  else
  {
    if (oldHeight>maxsize)
    {
      newHeight=maxsize;
      newWidth=(newHeight / oldHeight) * oldWidth;
    }    
  }
  img.width=newWidth;
  img.height=newHeight;  
}

function loadimg(src,img)
{
  newimg= new Image();
  newimg.src=src;
  img.width=newimg.width;
  img.height=newimg.height;
  img.src=newimg.src;
}

function ResidualLength(tx,max)
{ 
 a = max - tx.value.length;    
 if ( a < 0)
  {
    tx.value = tx.value.substring(0, tx.value.length + a);
    a = 0;
    alert("Attenzione è stata raggiunta la lunghezza massima consentita di " + max + " caratteri");
 }
 document.getElementById(tx.name + "_ResidualLength").innerHTML = "Caratteri residui " + a;
}


function ExpandCollapse(doOnThis)
{

  var expandList;
  expandList = Get_Cookie('expandList');

  if (expandList=='null') expandList='';

	if(document.getElementById(doOnThis).style.display == '')
  {
    expandList = expandList.replace('|' + doOnThis + '|','');
		document.getElementById(doOnThis).style.display = 'none';
    document.getElementById(doOnThis + '_img').src='img/expand.gif';
	}
  else
  {
    expandList = expandList + '|' + doOnThis + '|' ;

		document.getElementById(doOnThis).style.display = '';
    document.getElementById(doOnThis + '_img').src='img/collapse.gif';
		//document.getElementById('pagebody').scrollTop = document.getElementById('pagebody').scrollHeight - document.getElementById(doOnThis).clientHeight;
  }

  Set_Cookie('expandList',expandList);
  //alert(expandList);

}

function SetMenu(x)
{
  parent.consolemenu.SelectTab(x);
}



function InizializeInputFields(){

  var inp=document.getElementsByTagName("input");
  for(var i=0;i<inp.length;i++){   
    if (inp[i].getAttribute('type')!='button' && inp[i].getAttribute('type')!='submit' && inp[i].getAttribute('type')!='reset')
      {
        inp[i].onfocus=function(){myOnFocus(this)};
        inp[i].onblur=function(){myOnBlur(this)};
        inp[i].originalvalue = inp[i].value;
      }  
  }
 
  var inp=document.getElementsByTagName("select");
  for(var i=0;i<inp.length;i++){
      inp[i].onfocus=function(){myOnFocus(this)};
      inp[i].onblur=function(){myOnBlur(this)};
      inp[i].originalvalue = inp[i].value;
    }
  
  var inp=document.getElementsByTagName("textarea");
  for(var i=0;i<inp.length;i++){
    inp[i].onfocus=function(){myOnFocus(this)};
    inp[i].onblur=function(){myOnBlur(this)};
    inp[i].originalvalue = inp[i].value;
  }

}


function FormChanged() {
  
  var cambiamenti = 0;

  var inp=document.getElementsByTagName("input");
  for(var i=0;i<inp.length;i++){  
    if (inp[i].value != inp[i].originalvalue) cambiamenti = cambiamenti + 1;
  } 

  var inp=document.getElementsByTagName("select");
  for(var i=0;i<inp.length;i++){
    if (inp[i].value != inp[i].originalvalue) cambiamenti = cambiamenti + 1;
  }

  var inp=document.getElementsByTagName("textarea");
  for(var i=0;i<inp.length;i++){
    if (inp[i].value != inp[i].originalvalue) cambiamenti = cambiamenti + 1;
  }

  return ( cambiamenti > 0 );

}



var oldbackgroundColor;

function myOnFocus(el) {
  oldbackgroundColor = el.style.backgroundColor;  
  el.style.backgroundColor = "#FEFFCF";  
}

function myOnBlur(el) {
  el.style.backgroundColor = oldbackgroundColor;  
}



window.onload = function() {

  if(document.getElementsByTagName)
    InizializeInputFields();

  var pagetoolbar = document.getElementById("pagetoolbar");
  var pagebody    = document.getElementById("pagebody");
  var pagefooter  = document.getElementById("pagefooter");

  var riduzione = 0;

  if (pagetoolbar) {
    if(pagetoolbar.offsetHeight) riduzione = riduzione + pagetoolbar.offsetHeight;
    else if(pagetoolbar.style.pixelHeight) riduzione = riduzione + pagetoolbar.style.pixelHeight;
  } 

  if (pagefooter) {
    if(pagefooter.offsetHeight) riduzione = riduzione + pagefooter.offsetHeight;
    else if(pagefooter.style.pixelHeight) riduzione = riduzione + pagefooter.style.pixelHeight;
  } 

  if (pagebody) {
    pagebody.style.height = (document.body.clientHeight - riduzione) + 'px';
  }

  var expandList = Get_Cookie('expandList');
  if (expandList==null) expandList='';
  expandList = expandList.replace('||','|');
  var arrexpand = expandList.split("|");

  var id; 
  for (var i=0; i<arrexpand.length; i++) 
  { 
    id = arrexpand[i];
    if (id!='') {
      var elemento = document.getElementById(id);
      if (elemento!=null) {
		    elemento.style.display = '';
        document.getElementById(id + '_img').src='img/collapse.gif';
		    //document.getElementById('pagebody').scrollTop = document.getElementById('pagebody').scrollHeight - elemento.clientHeight;
      }
    }
  } 

}


var originalTitle = window.document.title;

window.onresize = function() {

  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  //window.document.title = originalTitle + " " + myWidth + " x " + myHeight;

  var pagetoolbar = document.getElementById("pagetoolbar");
  var pagebody    = document.getElementById("pagebody");
  var pagefooter  = document.getElementById("pagefooter");

  var riduzione = 0;

  if (pagetoolbar) {
    if(pagetoolbar.offsetHeight) riduzione = riduzione + pagetoolbar.offsetHeight;
    else if(pagetoolbar.style.pixelHeight) riduzione = riduzione + pagetoolbar.style.pixelHeight;
  } 

  if (pagefooter) {
    if(pagefooter.offsetHeight) riduzione = riduzione + pagefooter.offsetHeight;
    else if(pagefooter.style.pixelHeight) riduzione = riduzione + pagefooter.style.pixelHeight;
  } 

  if (pagebody) {
    pagebody.style.height = (document.body.clientHeight - riduzione) + 'px';
  }

}
