var CurrentWinChildOpen=null;
var MenuList = new Array();

function Browser() {

this.isOp	=(navigator.userAgent.toLowerCase().indexOf('opera') !=-1)?true:false
this.isOp5	=(this.isOp && navigator.userAgent.toLowerCase().indexOf('5') !=-1)?true:false

var nome	=(navigator.appName=="Microsoft Internet Explorer")?true:false;
var wt		=nome?(navigator.appVersion.split(';').toString().split(" ").toString().split(',')[4] ):null;
var isie5	=((wt)>=5 && nome)?true:false;
var isie4	=((wt <5 && wt >=4) && nome)?true:false;
var isNS4	=(document.layers)?true:false;
var isNS6	=document.getElementById && (navigator.appName=="Netscape")?true:false;
var isIE4	=document.all?true:false;
var isIE5	=document.getElementById && (navigator.appName=="Microsoft Internet Explorer")?true:false;
var sty		=(isie4 || isie5 || isNS6 || this.isOp5)?'hidden':(isNS4)?'hide':'hidden'
var stv		=(isie4 || isie5 || isNS6 || this.isOp5)?'visible':(isNS4)?'show':'visible'
var isd		=(isNS4)?document.layers:(isie4 || this.isOp5)?document.all:document

this.name	=nome	
this.wt		=wt		
this.isie5	=isie5	
this.isie4	=isie4	
this.isNS4	=isNS4	
this.isNS6	=isNS6	
this.isIE4	=isIE4	
this.isIE5	=isIE5	
this.sty	=sty			
this.stv	=stv			
this.isd	=isd			
this.isIE	= (isie4||isie5||isOP)
this.isNS	= (isNS6||isNS4)
}

var browser = new Browser();

function MouseX(event)
{var x;	
  if (browser.isNS) {x = event.clientX + window.scrollX;}
  if (browser.isIE) {x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;}
  return x;
}

function MouseY(event)
{var y;
  if (browser.isNS) {y = event.clientY + window.scrollY;}
  if (browser.isIE) {y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;}
  return y;
}

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

function getObj(name,vreturn)
{
  if (arguments.length<2)vreturn='obj';
  if (document.getElementById)
  {
  	var obj = document.getElementById(name);
  	if (obj!=null){var style = document.getElementById(name).style;}
  }
  else if (document.all)
  {
	var obj = document.all[name];
	if (obj!=null){
	var style = document.all[name].style;}
  }
  else if (document.layers)
  {
	var obj = getObjNN4(document,name);
	if (obj!=null){
	var style = obj;}
  }
  return eval(vreturn)
}


// **************************************************************************** URL *****************************

function RequestQueryString()
{
var QueryString=location.search;
var arResult = new  Array()
if (QueryString!=''){QueryString=QueryString.substr(1)}
var ar = QueryString.split('&')
for (i=0;i<ar.length;i++){
	if (ar[i]!=''){
		arValues=ar[i].split('=');
		value='';
		if (arValues.length>0){value=arValues[1]}
		arResult[arValues[0]]=value;
	}
}
return arResult
}

function ModifyURL1(Param,NewValue,Insert,DeleteList,ar){
	if (DeleteList!=null){DeleteList=DeleteList +','}
	var Result =''
	test=false
	for (elm in ar){
		if (elm.toLowerCase()==Param.toLowerCase()){
			if (NewValue!=''&&NewValue!=null){
			Result = Result + Param + "=" + NewValue + "&"
			}
			test=true
		}
		else{
			if (DeleteList!=null)
				{if (DeleteList.indexOf(elm +',')==-1){Result = Result + elm + "=" + ar[elm] + "&"}}
			else	{Result = Result + elm + "=" + ar[elm] + "&"}
			
		}
	}
	if (!test&&Insert){Result = Result + Param + "=" + NewValue + "&"}
	Result = Result.substring(0,Result.length-1)
	return Result
} 

function ModifyURL(Param,NewValue,Insert,DeleteList,EscapeMe){
	if (EscapeMe){NewValue=escape(NewValue)}
	return ModifyURL1(Param,NewValue,Insert,DeleteList,RequestQueryString())
} 

function ChangeUrl(Param,NewValue,DeletetList,EscapeString)
{
	var href=self.location.href
	var ar = href.split('?')
	var search = '?' + ModifyURL(Param,NewValue,true,DeletetList,EscapeString)
	if (search=='?') {search=''}
	self.location.href= ar[0] + search
}


// **************************************************************************** Tree *****************************

function HideShow(objName,SaveCookie)
{
if(getObj(objName,'style').display=='none')getObj(objName,'style').display='';
else getObj(objName,'style').display='none'
}

function HideShowLevel(objName,objSymbol,sCollapse,sExpanded)
{
var sValue =''
if(getObj(objName,'style').display=='none')
	{getObj(objName,'style').display=''
	 sValue = sCollapse 
	}
	else
	{getObj(objName,'style').display='none'
	sValue = sExpanded
	}
	if (sValue!=''){objSymbol.src=sValue}
}



// **************************************************************************** Menu *****************************
function cMB(id,bgcolor,color)
{
id.bgColor = bgcolor;
id.style.color = color;
}
function mCL(TableName,Level){if (MenuExists(TableName)) {MenuClose(TableName,Level)}}
function MenuClose(TableName,Level){
	if (MenuExists(TableName)){
	var l = MenuList[TableName].length
	var i=l;
	while (i >= Level)
	{
		getObj('span' + MenuList[TableName][i-1],'style').display='none'
		var ar = MenuList[TableName].slice(0,i-1)
		MenuList[TableName] = ar
		i--;
	}
	}
}
function MenuExists(TableName){return (MenuList[TableName] ? true : false);}
function MenuAdd(objName,TableName,Level){MenuList[TableName][MenuList[TableName].length] = objName}

//*************************************************** Popup ***************************************************
function OpenWinCenter(uRl,NomeFinestra,wHeight,wWidth,par){
	if (wHeight==-1&&wWidth==-1||arguments.length<3){
		wHeight=screen.availHeight
		wWidth=screen.availWidth
	}
	var remote = null 
	var sY=(screen.availHeight/2) - (wHeight/2);
	var sX=(screen.availWidth/2) - (wWidth/2);
	var remote = open(uRl,NomeFinestra,'width='+wWidth+',height='+ wHeight +',top='+ sY +',Left='+sX +',z-lock=1,' + par);
	while (remote==null){}
	if (remote.opener == null) {remote.opener = window;}
	return remote;
}


function OpenWinBlank(par){
	var o = OpenWinCenter("about:blank","blankwin",1,350,par)
	o.document.write("<body style='margin:0px;'><table width='100%' height='100%' style='background-color:#D6D3CE;color:black;border-left:2 solid silver;border-top:2 solid silver;border-bottom:2 solid gray;border-right:2 solid gray;font-size:11px;font-family:arial;'><tr><td align='center'>Apertura in corso. Attendere prego...</td></tr></table></body>")
	CurrentWinChildOpen = o
	return o;
}

function ResizeWindow(o){
	wHeight=o.offsetHeight+60;
	if (wHeight>screen.availHeight){wHeight=screen.availHeight-50}
	wWidth=parseInt(o.offsetWidth) + 50
	if (wHeight>screen.availWidth){wHeight=screen.availWidth-50}
	var sY=(screen.Height/2) - (wHeight/2);
	var sX=(screen.Width/2) - (wWidth/2);
	self.resizeTo(wWidth,wHeight)
	self.moveTo(sX,sY)
}

function SetWindowSize(wWidth,wHeight,IsCenter){
	self.resizeTo(wWidth,wHeight)
	if (IsCenter){
	var sY=(screen.Height/2) - (wHeight/2);
	var sX=(screen.Width/2) - (wWidth/2);
	self.moveTo(sX,sY)}
}

function AdminRequest_Webpart(idpage,idobj,par,FilterName,Filter,WinName){
	var o = OpenWinCenter(HomePage + "?page="+idpage +"&id_obj="+idobj+par+"&pr"+ FilterName + idobj + "=" + Filter + "&RedirTo="+ID_PAGE,WinName,1,1,"scrollbars=1,resizable=1,status=1")
	o.focus()
}

function AdminForm_Submit(idpage,idobj,idform,idop,chk,WinName){
	var o = OpenWinCenter(FormRedir + "?page="+idpage +"&id_obj="+idobj + "&idop=" + idop +"&id_form=" + idform + "&chk=" + chk+ "&RedirTo="+ID_PAGE,WinName,1,1,"scrollbars=1,resizable=1,status=1")
	o.focus()
}

function Form_Submit(Page,id,minCheck,reqform,NewWindow,IDReq,maxCheck){
	if (arguments.length<7||maxCheck==0||minCheck>maxCheck)maxCheck=-1
	var n = GetChkSelect(id)
	var sNewWindow=''
	if (maxCheck==-1){TestCheck = (n>=minCheck)}
	else{TestCheck = (n>=minCheck&&n<=maxCheck)}
	if (TestCheck){
		if (NewWindow){var oW=OpenWinBlank("scrollbars=1,status=1,menubar=0,location=0,toolbar=0,directories=0");sNewWindow='&rsize=1'}
		var o=eval("document.forms[\'TbForm_" + id + "\']")
		var redirTo = '';
		if (Page==ID_PAGE){Page=ID_PAGE}else{redirTo='&RedirTo=' + ID_PAGE.toString()}

		o.action = FORMPAGE + "?page=" + Page + "&ID_OBJ=" +id + redirTo + sNewWindow

		if (location.search!=''){
		var  a= getObj("openerQS_"+ id)
		a.value = location.search.substr(1) 
		}

		var a = getObj("REQFORMID_"+ id)
		a.value = IDReq
		
		var a = getObj("REQFORM_"+ id)
		a.value = reqform

		//var a = getObj("prREQFORM_"+ id)
		//a.value = reqform

		if (NewWindow){o.target = "blankwin";oW.focus()}else{o.target = ""}
		o.submit()
	}else{alertSelect(minCheck,maxCheck)}
}
function ExistsObject(oName){if (getObj(oName)==null){return false}else{return true}}
function ReloadOpener(){self.opener.location.reload()}
function ChangeOpenerURL(value){self.opener.location.href = value}
function GoTo(url){self.location.href = url}
function GetSelectedOption(obj){var o = getObj(obj);return o.options[o.selectedIndex].value}
function ClosePopUp(){self.opener.location.reload();self.close();}
function CallFunction(strEval,TableID){eval(unescape(strEval))}
function CallFnc(id,nchk,sFunc){
	if (GetChkSelect(id)>=nchk){
	eval(unescape(sFunc))}
	else{alertSelect(nchk)}}
function alertSelect(min,max){
	desc=' oggetto'
	if (arguments.length<2||max==-1){
		if (min>1){desc=' oggetti'}
		alert('Selezionare almeno ' + min + desc)
	}else if(min==max){
		if (min>1){desc=' oggetti'}
		alert('Selezionare ' + min + desc)
	}
	else {alert('Selezionare da ' + min + ' a ' + max + ' oggetti')}
}
function IsWinOpen(o){if (o && o.open && !o.closed) {o.focus()}}
function SendSelection(id,idname,fnc,min,max){
	ar = GetChkSelectAndValues(id)
	if(ar[0]>=min&&ar[0]<=max){
	eval("self.opener." + fnc +"(idname,ar[1],ar[0])")
	self.focus();
	}
	
	else{alert('E\' consentito selezionare minimo '+min+' valori, massimo '+max+' valore/i')}
	
}
