
var cimages = new Array();

function XImageTransform(idx, u) {  
  
  if( (cimages[idx].width < cimages[idx].savewidth) && (u=-1) ) {
    window.clearInterval( cimages[idx].tmr ); 
       
    cimages[idx].style.width = cimages[idx].savewidth;
    cimages[idx].style.height = cimages[idx].saveheight;
    cimages[idx].style.position = "";
    cimages[idx].flag = false;
    return;
  }  
  
  if  (u==1) {
  
      if(!cimages[idx].startcounter) cimages[idx].startcounter = 0;
      
      cimages[idx].startcounter = cimages[idx].startcounter + 1;
      
      if(cimages[idx].startcounter<100) u=0;
      
      if( cimages[idx].height >= 100 ) {
            window.clearInterval( cimages[idx].tmr )
            cimages[idx].flag = true;
            return;
      }
  }
  
  cimages[idx].style.width = u + parseInt(cimages[idx].style.width);  
  cimages[idx].style.height = u + parseInt(cimages[idx].style.height);
  
}


function ImageMouseEnter(target) {   
    
    var indeximage = cimages.length;
    for (i=0;i<cimages.length;i++)
    {       
        //mybody.style.cursor= "not-allowed"
        cimages[i].style.zIndex = 0;
        if(target==cimages[i]) indeximage = i;
    } 
    if(!target.flag) {
        target.style.zIndex = 99999;        
        target.flag = true;
       
        if(!target.savewidth) {
            //alert(absoluteLeft(target))
            //var x = getCoord(target, 0, 0)
            //alert(x.top)
            //target.style.left = x.left -2;
            //target.style.top = x.top -2;
            target.savewidth = target.width;
            target.style.width = target.width;
            
            target.saveheight = target.height;
            target.style.height = target.height; 
        }
        
        cimages[indeximage] = target ;          
        cimages[indeximage].style.position = "absolute";
        window.clearInterval( cimages[indeximage].tmr );
        
        cimages[indeximage].tmr = window.setInterval("XImageTransform(" + indeximage + ", 1 )",1);
        
    }    
}


function ImageMouseLeave(target) {
    //alert(c.length)
    for (var i=0;i<=cimages.length;i++)
    {        
        if(target==cimages[i]) {
            
            if(cimages[i].flag) {                
                
                window.clearInterval( cimages[i].tmr );
                cimages[i].flag = false;
                cimages[i].tmr = window.setInterval("XImageTransform(" + i + ", -1 )",1);
                //alert("flag:" + cimages[i].flag)
            }
        }
    }

}



function GetMegaTag(obj, NodeName) {
	var x = obj;
	var i = 0;	
	while (x.tagName!=NodeName) {
		if (i++>10) break;
		x = x.parentNode;				
	}
	return(x);
}

function CheckBoxAll(IDS, value) {
    var s = new String(IDS)
    var a = s.split("|")
    for(var i = 0; i<a.length; i++) {
        try {
            document.all(a[i]).checked = value;
        } catch(e) {}
        
    }
}
function ShowMegaHideSession(target, scroll) {
	// RIGA.innerText
	
	var TABX = GetMegaTag(target, "TABLE");
	var RX = GetMegaTag(target, "TR");
	
	//alert(TABX.rows)
	var ix = RX.rowIndex;
	var RXNext = TABX.rows[++ix];
	
	var i
	var d = RX.tagName + "->";
	var explode = (target.value=="+");
	
	while (RXNext!=null) {
		if(i++>100) break;
		//alert(RXNext.tagName);
		//d = d + RXNext.tagName + ">";
		
		if(explode) {
			
			target.value="-";
				
			RXNext.style.display="";
			if(scroll)
				RXNext.scrollIntoView(false);
		}else{
			target.value="+";
			RXNext.style.display="none";
		}
		RXNext = TABX.rows[++ix];  //RXNext.nextSibling;
	}	
	window.status = d;
}

function AreaSize(target, size) {
	var str = new String(target.value);		
	if(!size) size=0;
	//for(var e in target) {alert(e)}
	
	
	if(target.Size) size = parseInt(target.Size)
		else if(target.size) size = parseInt(target.size)
			else if(target.size) size = parseInt(target.maxLength)
		
	if(target.value.length > size) {
		alert("Superato limite del testo inseribile");
		target.value = str.substring(0, size)		
	}	
	window.status = "( " + str.length + " di " + size + " ) " + target.title
}	

function SelectRow(target) {		
		var riga=null
		var element=target;		
		while (element.tagName!="TABLE") {				
			if(element.tagName=="TR") riga = element;	
			var element=element.parentElement
		}
		var table=element;
		if(table.SaveRow) {
			table.SaveRow.bgColor=table.saveRowBgColor;
		}
		
		table.saveRowBgColor = riga.bgColor;
		table.SaveRow = riga;
		riga.bgColor='Cyan';
		//alert(riga.cells[0].bgColor)
		riga.cells[0].bgColor = table.saveRowBgColor;
	}
	
	function absoluteL(obj) {
		var a = 0;		
		var t = 0;
		window.status = "";
		do {
		    window.status = "." + obj.id + window.status
		    
			a = a + obj.offsetLeft  //+ obj.clientLeft;			
			if(obj.id=='MEGATABCONTAINERS') {
			    break;
			}			
			obj = obj.offsetParent									
		} while (obj != null)		
		
		return a;
	}
	
	function absoluteT(obj) {
		var a = 0;		
		var t = 0;
        //window.status = "";
		do {	
		    //window.status += "." + obj.tagName
			
			a = a + obj.offsetTop  //+ obj.clientTop;			
			if(obj.id=='MEGATABCONTAINERS') {
			    break;
			}			
			obj = obj.offsetParent									
		} while (obj != null)		
		
		return a;
	}
	
    function absoluteLeft(obj) {
		var a = 0;		
		do {			
			a = a + obj.offsetLeft  //+ obj.clientLeft;			
			if((obj.style.overflow=='hidden')||(obj.style.overflow=='scroll')) {
			    break;
			}	
			//obj = obj.parentElement;						
			obj = obj.offsetParent
		} while (obj != null)				
		return a;
	}
	
	function absoluteTop(obj) {
		var a = 0;		
		do {			
			a = a + obj.offsetTop  //- obj.scrollTop //+ obj.clientTop;
			if((obj.style.overflow=='hidden')||(obj.style.overflow=='scroll')) {
			    break;
			}	
			//obj = obj.parentElement;						
			obj = obj.offsetParent
		} while (obj != null)				
		return a;
	}
	
	function getCoord(obj, offsetLeft, offsetTop){
    	var orig = obj;
    	var left = 0;
    	var top = 0;
    	if(offsetLeft) left = offsetLeft;
    	if(offsetTop) top = offsetTop;
    	if(obj.offsetParent){
    		left += obj.offsetLeft;
    		top += obj.offsetTop;
  		    while (obj = obj.offsetParent) {
   			    left += obj.offsetLeft +obj.clientLeft  -obj.scrollLeft;
   			    top += obj.offsetTop +obj.clientTop -obj.scrollTop;
   		    }
   	    }
   	return {left:left, top:top, width: orig.offsetWidth, height: orig.offsetHeight};
   }

	
	

		
function setCaretToEnd (input) {
  if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(false);
    range.select();
  }
}
function scrollToBottom (element) {
  if (document.all)
    element.scrollTop = element.scrollHeight;
}



    
function isDate(str)
{ // anno a 4 cifre !!!
  var spz = str.split("/");
  if (spz.length != 3) return(false);
 
  dat = new Date(spz[2], spz[1] - 1, spz[0]);
 
  if (dat.getFullYear() == parseFloat(spz[2]) &&
      dat.getMonth() == (parseFloat(spz[1]) - 1) &&
      dat.getDate() == parseFloat(spz[0])) return(dat);
 
  return(false);
}
    
function toDate(dat) {
    var x = new String(dat);
    var s = x.split("/");
    
    var d, m, y;
    
    if(s.length == 3) {                
        d = parseInt(parseVal(s[0]));
        m = parseInt(parseVal(s[1]));
        y = parseInt(parseVal(s[2]));
    } else {
        d = parseInt(parseVal(x.substr(0,2)));
        m = parseInt(parseVal(x.substr(2,2)));
        y = parseInt(parseVal(x.substr(4,4)));   
    }
    
    
    
    
    if( d <= 9 ) d = "0" + d;
    if( m <= 9 ) m = "0" + m; 
    if( y > 70 && y < 100 ) y = y + 1900 
    if( y < 100 ) y = y + 2000 
    //var nd = new Date(y,  m ,d)   
    var sd = d + "/" + m + "/" + y;
    
    //alert(parseInt(" 9"));
    
    return (sd) ;
}




//** Gestione conversione del tempo**//

 function OpenTimeX(id) {
        var hndTimeX = document.all(id).hndTimeX;
        if(hndTimeX ){window.clearTimeout(hndTimeX);}
        document.all(id).style.display = '';
    }
    
    function CloseTimeX(id) {
        hndTimeX = window.setTimeout("document.all('" + id + "').style.display = 'none'", 100);       
        document.all(id).hndTimeX = hndTimeX ;
    }
    
    
    function parseVal(val)
    {
       while (val.charAt(0) == '0')
          val = val.substring(1, val.length);
        if(val=='')val=0;
       return val;
    }    
    
    function CalcolaOrario(O, H, M, S, Direzione) {
    
        var x = new String
        //x.replace(
        if(O.value=="") O.value=0;
        if(H.value=="") H.value=0;
        if(M.value=="") M.value=0;
        if(Direzione==1) {
            H.value = parseInt(O.value);
            M.value = parseInt(parseFloat(O.value.replace(",",".")) * 60 % 60);
            S.value = parseInt(parseFloat(O.value.replace(",",".")) * 3600 % 60);
            
            if(H.value.length == 1) H.value = "0" + H.value;
            if(M.value.length == 1) M.value = "0" + M.value;
            if(S.value.length == 1) S.value = "0" + S.value;
            
        } else {            
            if(parseInt(parseVal(M.value)) != Math.min(59, parseInt(parseVal(M.value)))) M.value = Math.min(59, parseInt(parseVal(M.value)));
            if(H.value.indexOf(".")>=0){                
                H.value = H.value.replace(".",",");
            }
            O.value = (( parseFloat(parseVal(H.value.replace(",","."))) * 60 + parseInt(parseVal(M.value))  ) / 60).toString().replace(".",",") ;
        }
    }
    
    
    function debugObject(x){
	    var div = document.createElement("DIV")
	    div.style.position = "absolute";
	    div.style.overflow = "scroll";
	    div.style.border = "1px solid silver"
	    div.style.backgroundColor = "white"
	    div.style.left = "200"
	    div.style.top = "200"
	    div.style.width = "600"
	    div.style.height = "400"
	    
	    document.body.appendChild(div) 
	    var str = ""   
	    var i =0; 
	    for(e in x) {
	        str += "\n<LI>" + e  + "=";
	        try {
	            //var t = escape(  ) 
	            str += escape(x[e])
	        } catch(e){
	            str += e.description;
	        }
	        //if(i++ >= 2000) break;
	    }
	    div.innerHTML = str;
	}    
	
	
	
	
    function MegaDisplayTab(index, MegaTabsName, MegaTabContainer) {		
	    
        
        window.status = "MegaDisplayTab"
        
        var tabs;
        var tabcs;
        if(document.all) {
            tabs = document.all("ID" + MegaTabsName);
            tabcs = document.all("ID" + MegaTabContainer);
        } else {            
            tabs = document.getElementsByName(MegaTabsName);
            tabcs = document.getElementsByName(MegaTabContainer);
        }
        
        
        
        try {
            window.status += " TabIndex:" + index;
            
            window.status += " Count:" + tabs.length;
           	
		    for(var i=0;i<tabs.length;i++) {
			    var tab = tabs[i];
			    var tabc = tabcs[i];
                //alert(tabc.offsetTop)
                //tabc.style.height = document.body.clientHeight - absoluteTop(MEGATABCONTAINERS) -50;
                
			    if(i==index) {
				    tab.style.backgroundColor="Gray";				
				    tab.style.color="white";
				    //tab.innerHTML = tab.id ;
				    if(tabc) {
				        tabc.style.display = "block"
				        try {
				            var f = tabc.ondblclick;				    
				            if(f) f();
				        } catch(e) {
				            alert("Errore nel caricamento del tab " + index +  ", provate a fare doppioclick\n\n" + e.description)
				        }
				    }
			    } else {
				    //tab.innerHTML = tab.id;
				    tab.style.backgroundColor="#ECE9D8";
				    tab.style.color="black";
				    if(tabc) tabc.style.display = "none";
			    }
		    }
		} catch(e) {
		    alert("Errore nel tab avvertire immediatamente Bontempo\n\n" + e.description)
		}
	}	