var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll;
window.name='Backoffice';
win = window.open(mypage,myname,settings);
}



function IsNumber(number)  //  boolean function
{
   var adecimal = false,
       inputStr = "" + number;

   if( inputStr.length == 0 )
      return false;

   for(var i = 0; i < inputStr.length; i++)
   {
      var chr = inputStr.charAt(i);

      if( i != 0 && chr == "-")   // - can only be first
     return false;

   if( chr == ","  && ! adecimal)
   {
      adecimal = true;
   continue;
      }

      if( chr < "0" || chr > "9" )
      return false;
    }

 return true;

}



function IsNumberInteger(number)  //  boolean function
{
   var adecimal = false,
       inputStr = "" + number;

   if( inputStr.length == 0 )
      return false;

   for(var i = 0; i < inputStr.length; i++)
   {
      var chr = inputStr.charAt(i);
      if( i != 0 && chr == "-")   // - can only be first
     return false;

      if( chr < "0" || chr > "9" )
      return false;
    }

 return true;

}



function AggiornaTendina(Id,descrizione,codice)
{
window.opener.document.getElementById('CampoTendina'+Id).value=descrizione;
window.opener.document.getElementById('CampoHiddenTendina'+Id).value=codice;
self.close();
}

function AzzeraTendina(Id)
{
window.document.getElementById('CampoTendina'+Id).value='';
window.document.getElementById('CampoHiddenTendina'+Id).value=null;
}

function Valore(which,catena)
{
ret="";
if (document.images)
		{
		for (i=0;i<which.length;i++)
			{
			var tempobj=which.elements[i]		
			if (tempobj.name==catena)
				ret=tempobj.value;
			}
		}
return ret;
}

function ValoreTextArea(which,catena)
{
ret="";
if (document.images)
		{
		for (i=0;i<which.length;i++)
			{
			var tempobj=which.elements[i]		
			if (tempobj.name==catena)
				ret=tempobj.editor.getHTML();
			}
		}
return ret;
}


function controllo(which,catena)
{
ret=null;
if (document.images)
		{
		for (i=0;i<which.length;i++)
			{
			var tempobj=which.elements[i]		
			if (tempobj.name==catena)
				ret=tempobj;
			}
		}
return ret;
}




function del(Id)
{
var ml;
var button=null;
if (document.Ricerca!=null)
	{
	ml= document.Ricerca;
	var len = ml.elements.length;
	for (var i = 0; i < len; i++)
		{
		var e = ml.elements[i];
		if (e.name == "Eliminazione")
			e.checked=(e.value==Id);
		if (e.name == "Submit")
			if (e.value == "Elimina selezione")
				button = ml.elements[i];
		}
	}
if (button!=null) button.click();
}



function ControllaNull(catena,nome)
{
ret="";
//alert(nome+": "+ catena);
if (catena==null || catena=='' || catena=='null') ret="- \""+nome+"\" non puņ essere nullo\n";
return ret;
}

function ControllaNoInteger(catena,nome)
{
ret="";
if (catena!='')
	if (!IsNumber(catena)) ret="- \""+nome+"\" deve essere un numero\n";
return ret;
}

function ControllaNoIntegerInt(catena,nome)
{
ret="";
if (catena!='')
	if (!IsNumberInteger(catena)) ret="- \""+nome+"\" deve essere un numero intero\n";
return ret;
}





function ControllaNoData(strDate,nome) {
    var strDate;
    var strDateArray;
    var strDay;
    var strMonth;
    var strYear;
    var intday;
    var intMonth;
    var intYear;
    var dateSeparator=new Array("/");
	var errMsg="- \""+nome+"\" deve avere il formato gg/mm/aaaa\n";
    var error=0;
    if (strDate.length > 0) 
		{
        if (strDate.indexOf(dateSeparator) != -1) 
			{
            strDateArray = strDate.split(dateSeparator);          
            if (strDateArray.length != 3) 
				{
                return(errMsg);
            	}
                
            strMonth = strDateArray[1];
            strDay = strDateArray[0];
            strYear = strDateArray[2];
            if ((strMonth=='') || (strDay=='') || (strYear=='')) 
				{
                return(errMsg)
            	}
                
            if (!IsNumber(strMonth) || !IsNumber(strDay) || !IsNumber(strYear)) 
				{
                return(errMsg)
            	}
                
            intMonth=strMonth;
            intDay=strDay;
			
			
            if (strYear.length != 4) 
				{
                return(errMsg)
            	}
				
            intYear=parseInt(strYear);

            if (intMonth > 12 || intMonth < 1) 
				{                
                error=1;
            	}
            
            if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1)) 
				{              
                error=1;
            	}
            
            if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
				{                
                error=1;
            	}
                
            if (intMonth == 2) 
				{
                if (intDay < 1) 
					{                    
                    error=1;
                	} 			
   	         	}
                
            if (error == 1) 
				{
                return(errMsg);
            	}
        	}
        else 
			{
            return(errMsg);
        	}
    }

    return("");
}


function calendaropen(src,controlloname) {
  w = 150;
  h = 138;
  self.document.controlloname=controlloname;
  win = window.open(src, '', 'scrollbars=no,resizable=no,width=' + w + ',height=' + h+',top='+( screen.height - w ) / 1.5+',left='+( screen.width - h ) / 2);
}



var xOffset = 30;
var yOffset = -5;

function showPopup (targetObjectId, eventObj) {
    if(eventObj) {
	// hide any currently-visible popups
	hideCurrentPopup();
	// stop event from bubbling up any farther
	eventObj.cancelBubble = true;
	// move popup div to current cursor position 
	// (add scrollTop to account for scrolling for IE)
	var newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
	var newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + yOffset + ((document.body.scrollTop)?document.body.scrollTop:0);
	moveObject(targetObjectId, newXCoordinate, newYCoordinate);
	// and make it visible
	if( changeObjectVisibility(targetObjectId, 'visible') ) {
	    // if we successfully showed the popup
	    // store its Id on a globally-accessible object
	    window.currentlyVisiblePopup = targetObjectId;
	    return true;
	} else {
	    // we couldn't show the popup, boo hoo!
	    return false;
	}
    } else {
	// there was no event object, so we won't be able to position anything, so give up
	return false;
    }
} // showPopup

function hideCurrentPopup() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
	changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
	window.currentlyVisiblePopup = false;
    }
} // hideCurrentPopup



// ***********************
// hacks and workarounds *
// ***********************

// initialize hacks whenever the page loads
window.onload = initializeHacks;

// setup an event handler to hide popups for generic clicks on the document
document.onclick = hideCurrentPopup;

function initializeHacks() {
    // this ugly little hack resizes a blank div to make sure you can click
    // anywhere in the window for Mac MSIE 5
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    // this next function creates a placeholder object for older browsers
    createFakeEventObj();
}

function createFakeEventObj() {
    // create a fake event object for older browsers to avoid errors in function call
    // when we need to pass the event object to functions
    if (!window.event) {
	window.event = false;
    }
} // createFakeEventObj

function resizeBlankDiv() {
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	getStyleObject('blankDiv').width = document.body.clientWidth - 20;
	getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix () {
    location.reload(false);
}

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.left = newXCoordinate;
	styleObject.top = newYCoordinate;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} // moveObject


function fnDef()
{
    Elem=document.getElementById("div"+event.srcElement.id);
    oPopup.document.body.innerHTML = Elem.innerHTML;
    var popupBody = oPopup.document.body;
    oPopup.show(0, 0, 300, 0);
    var realHeight = popupBody.scrollHeight;
    oPopup.hide();
    oPopup.show(0, 15, 300, realHeight, event.srcElement);
}


/*function getInnerWidth(iframe){
 var d=iframe.contentWindow ?
         iframe.contentWindow.document :
         iframe.contentDocument;
 var h=0;
 if(d){
  if(d.documentElement && d.compatMode &&
     d.compatMode=="CSS1Compat")
   h=d.documentElement.scrollWidth;
  else if(d.body)
   h=d.body.scrollWidth;
  if(h) h+=getInsets2(d);
 } return (h||300)+"px"; //300 is a default value
}
*/

function getBody(iframe){
 var d=iframe.contentWindow ?
         iframe.contentWindow.document :
         iframe.contentDocument;
 if(d){
  if(d.documentElement && d.compatMode && d.compatMode=="CSS1Compat")
   return (d.body);   
  else 
  	if(d.body) return (d.body);   
 }
}


/*function getInsets2(d){
 if(d.body.currentStyle)
  with (d.body.currentStyle)
   return (parseInt(marginLeft)||0) + 
          (parseInt(marginRight)||0) +
          (parseInt(paddingLeft)||0) + 
          (parseInt(paddingRight)||0);
 if(d.defaultView && d.defaultView.getComputedStyle)
  with (d.defaultView)
   return parseInt(getComputedStyle(d.body,"").
            getPropertyValue("margin-left"))+
          parseInt(getComputedStyle(d.body,"").
            getPropertyValue("margin-right"))+
          parseInt(getComputedStyle(d.body,"").
            getPropertyValue("padding-left"))+
          parseInt(getComputedStyle(d.body,"").
            getPropertyValue("padding-right"));
 return 0;
}
*/


function getInnerHeight(body){

 var h=0;
 h=body.scrollHeight;
 if(h) h+=getInsets(body);
 return (h||300)+"px"; //300 is a default value
}

function getInsets(d){
 if(d.currentStyle)
  with (d.currentStyle)
   return (parseInt(marginTop)||0) + 
          (parseInt(marginBottom)||0) +
          (parseInt(paddingTop)||0) + 
          (parseInt(paddingBottom)||0);
 if(d.defaultView && d.defaultView.getComputedStyle)
  with (d.defaultView)
   return parseInt(getComputedStyle(d,"").
            getPropertyValue("margin-top"))+
          parseInt(getComputedStyle(d,"").
            getPropertyValue("margin-bottom"))+
          parseInt(getComputedStyle(d,"").
            getPropertyValue("padding-top"))+
          parseInt(getComputedStyle(d,"").
            getPropertyValue("padding-bottom"));
 return 0;
}

function Resize1(this1)
{
body=getBody(this1);
w1=(body.scrollWidth);
w2=(body.offsetWidth);
cn=this1.parentElement.parentElement.parentElement.parentElement.parentElement;
if (cn.className!='tabellaForm') 
	cn=this1.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;

s=cn.style.width;
i=-1;
if (s.indexOf('px',0)==s.length-2)
	{
	s1=s.substring(0,s.length-2);
	i=parseInt(s1);
	}
if ((w1>w2 || i==-1) && w1>i)
	{
	w1=w1+30;	
	cn.style.width=w1+"px";
	}
cn.style.width;
body.scrollWidth;
this1.style.height="10px"; 
body.scrollHeight;
this1.style.height=getInnerHeight(body); 
body.scrollHeight;
}

function ShowRicerca()
{
tbody1.style.display='block';tbody2.style.display='none';
if (tbody1)
if (tbody1.ownerDocument)
if (tbody1.ownerDocument.parentWindow)
if (tbody1.ownerDocument.parentWindow.frameElement)
Resize1(tbody1.ownerDocument.parentWindow.frameElement);
return false;
}

function HideRicerca()
{
tbody2.style.display='block';tbody1.style.display='none';
if (tbody1)
if (tbody1.ownerDocument)
if (tbody1.ownerDocument.parentWindow)
if (tbody1.ownerDocument.parentWindow.frameElement)
Resize1(tbody1.ownerDocument.parentWindow.frameElement);
return false;
}

