var dialogw = null;
var dropID = '';
var ctlID ='';
var dopost = true;
var postbackCtl = '';
var eventArg = '';
var returnAction= 'replace';
var delta = 0;
if(navigator.userAgent.indexOf("Firefox") != -1) {delta=10}

function disableEnterKey(e, that, DoTab){
	var key =  window.event ? window.event.keyCode : e.which;
	if (typeof DoTab == 'undefined'){DoTab = "yes"}
	if (key == 13) {
		if(navigator.userAgent.indexOf("Firefox") == -1) {
			if (DoTab == "yes") {
				event.returnValue=true;
				event.keyCode = 9;
				event.cancel = false;
			} else {
				event.returnValue=false;
				event.keyCode = 0;
				event.cancel = true;
			}
		} else {
			correctSubmitHandler(e);
		}
    }
}

function trapOnKeyPress(that) {
		var aevnt = document.createEvent('KeyEvents');
		aevnt.initKeyEvent('keypress',false,false,null,false,false,false,false,0,aevnt.DOM_VK_TAB);
		that.dispatchEvent(aevnt);
		//alert(that.value);
}


 function MoveFocusToNext(that){
	el =document.forms[0].elements;
	for (i = 0; i <= el.length; i++) {
	   if (el[i].id==that.id) {
	 	for (k = i+1; k <= el.length; i++) {
	 		if (el[k].tagName == 'INPUT' || el[k].tagName=='SELECT' || el[k].tagName=='TEXTAREA') {
	 	 	el[k].focus();
	 	 	that.value=that.value + el[k].id
	 	 	return null
	 		}
	 	}   
	   }
	 }
	 return null;
 }


function make(e){
		var new_event=document.createEvent("KeyEvents");
		new_event.initKeyEvent("keydown", false, true, null, false, false, false, false, 0x09,0);
		//alert(e.target);
		window.dispatchEvent(new_event);
}
        
function ToggleCheckers(that)
{
    var grid = wgGetGridByElement(that)
    var table = wgGetTableByElement(that);
    var grid = ISGetObject(wgGetGridByElement(that));
    var row = wgGetRowByElement(that);
    for (i=0; i < table.GetRowsCount(); i++){
       table.GetRow(i).Uncheck()
    }
    row.Check()
    table.UpdateUI()
    return true;
}

function HideHeaderChecker()
{
	var grid = ISGetObject(gridID);
	var r = grid.GetRootTable().GetElement(WG40.COLHEADER, WG40.HTMLTABLE).rows[0];
	var rcCell = wgGetCellByName(r, grid.GetRootTable().GetRowCheckerColumn());
	var TmpIH = rcCell.innerHTML.replace("class=DefChkBox", "class=\"DefChkBox\"")
	rcCell.innerHTML = TmpIH.replace("class=\"DefChkBox\"", "class='DefChkBox HIDDEN'")
	return true;
}


function ClientNodeClick(node){
	 var aspxPage = node.Value;
	 p = aspxPage.indexOf("_");
	 var wtitle = aspxPage;
	 if (p>-1) {wtitle = aspxPage.substring(0,p)}
	 var w=window.open('',wtitle + SID,'height=' + (600 + delta) + ',width=' + (800 + delta) + ',status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
     aspxPage = aspxPage.replace("_","&");
     w.location = 'ShowPage.aspx?Page=' + aspxPage
     w.focus();
     return;
}

function NavigateTo(pagename,id,args,w,h,aspx){
     if (typeof w == 'undefined'){w = 800}
     if (typeof h == 'undefined'){h = 600}
     if (typeof aspx == 'undefined'){aspx = 'ShowPage'}
     var wx=window.open('',pagename + SID,'height=' + (h + delta) + ',width=' + (w + delta) +',status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
     wx.location = aspx + '.aspx?Page=' + pagename + '&ID=' + id + '&' + args;
     wx.focus();
     return;
}

function ShowOPAC(web,id) {
		var wx=window.open('','opac' + SID);
		wx.location = 'opac.aspx?WEB=' + web + '&IDS=' + id;
		wx.focus();
		return;
}

function openWindow(URL, WindowName, status, toolbar, menubar, location, resizable, scrollbars){
   	if (typeof status == 'undefined'){status = "yes"}
	if (typeof toolbar == 'undefined'){toolbar = "yes"}
	if (typeof menubar == 'undefined'){menubar = "yes"}
	if (typeof location == 'undefined'){location = "no"}
	if (typeof resizable == 'undefined'){resizable = "yes"}
	if (typeof scrollbars == 'undefined'){scrollbars = "yes"}
   var w=window.open('',WindowName + SID,'height=' + (600 + delta) + ',width=' + (800 + delta) + ',status='+ status + ',toolbar=' + toolbar + ',menubar=' + menubar + ',location=' + location + ',resizable=' + resizable + ',scrollbars='+ scrollbars);
   w.location = URL;
   w.focus();
}

function fnTrapKD1(btn){
if (window.event.keyCode == 13)
   { 
     postbackCtl=btn.id;
     __doPostBack(postbackCtl.replace('_', '$'),'TEXTBOX');
     window.event.returnValue=false;
     window.event.keyCode = 0;
     window.event.cancel = true;
   } 
}

function fnTrapKDEVARG(btn, e, EvArg){
	//document.getElementById("__EVENTARGUMENT").value = EvArg;
	document.all("__EVENTARGUMENT").value = EvArg;
	fnTrapKD(btn, e)
}

function fnTrapKD(btn, e){
	var key =  window.event ? window.event.keyCode : e.which;
	if (key == 13)
	{ 
		//alert(document.getElementById("__EVENTARGUMENT").value);
		//if (window.event) {
		if (navigator.userAgent.indexOf("Firefox") == -1) {
			e.returnValue=false;
			e.cancel = true;
			btn.click();
		} else {
			correctSubmitHandler(e);
			btn.click();
		}
	} 
}

function correctSubmitHandler(e)
{
	if (e && e.preventDefault)
		e.preventDefault();
	return false;
}


function DefaultTextbox(that){
	var key =  window.event ? window.event.keyCode : e.which;
	if (key == 13) {
		if (navigator.userAgent.indexOf("Firefox") == -1){
			event.returnValue=false;
			event.cancel = true;
			event.keyCode = 0;
			__doPostBack(that.id.replace('_', '$'),'');
		}  else {
			correctSubmitHandler(e);
			__doPostBack(that.id.replace('_', '$'),'');
		}
	} 
}


function CloseDialogs(){
     if (dialogw && !dialogw.closed) {
		dialogw.close();
     }
     dialogw=null;
}

function ShowImage(ImgPath, tbl, ID)
{
	var wsv = window.open('DlgContainer.aspx?PAGE=ShowDialog&TITLE=Visualizzatore&ImgPath' + ImgPath + '&tbl=' + tbl + '&ID=' + ID,'Visualizzatore' + SID,'height=' + (h + delta) + ',width=' + (w + delta) + ',status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
	wsv.focus();
}

function ShowSNVDialog(aspx, page, title, mode, parm, retCtl, dropCtl, w, h, post, retAction, pbCtl, evArg) {
    dropID = dropCtl;
    ctlDrop = document.getElementById(dropID);
    if (ctlDrop != null) {
    	if (ctlDrop.options[ctlDrop.selectedIndex].value != '') {
			parm = parm + '&SNV=' +  ctlDrop.options[ctlDrop.selectedIndex].value;
       }
    }
    ShowDialog(aspx, page, title, mode, parm, retCtl, w, h, post, retAction, pbCtl, evArg)
}

function ShowCLCDialog(aspx, page, title, mode, parm, txtCtl, w, h, post, retAction, pbCtl, evArg) {
    ctlID = txtCtl;
    ctlTXT = document.getElementById(ctlID);
    if (ctlTXT != null) {
    	if (ctlTXT.value != '') {
			parm = parm + '&BASEVALUE=' +  ctlTXT.value;
       }
    }
    ShowDialog(aspx, page, title, mode, parm, txtCtl, w, h, post, retAction, pbCtl, evArg)
}

function ShowDialog(aspx, page, title, mode, parm, retCtl, w, h, post, retAction, pbCtl, evArg){
	ctlID = retCtl;
	postbackCtl = retCtl;
	eventArg = '';
	if (typeof post != 'undefined'){dopost = post}
	if (typeof retAction != 'undefined'){returnAction = retAction}
	if (typeof w == 'undefined'){w = 800}
	if (typeof h == 'undefined'){h = 600}
	if (typeof pbCtl != 'undefined'){postbackCtl = pbCtl}
	if (typeof evArg != 'undefined'){eventArg = evArg}
	scrollbars = 'no';
	if (title == 'PRINT') {
		scrollbars = 'yes';
	}
	CloseDialogs()
	dialogw = window.open('', page, 'height=' + (h + delta) + ',width=' + (w + delta) + ',scrollbars=' + scrollbars + ',status=yes,resizable=yes,toolbar=no');
	dialogw.location = aspx + '.aspx?page=' + page + '&title=' + title + '&parm=' + parm + '&mode=' + mode;
	dialogw.focus();
	window.SID='pippo'
}

function SetReturnValue(returnValue){
	var lastCtlval = document.getElementById(window.ctlID).value;
	if (typeof returnValue != 'undefined'){
		switch (returnAction) {
		case 'replace':
			document.getElementById(window.ctlID).value=returnValue;
			break;
		case 'add':
			document.getElementById(window.ctlID).focus();
			lastChar = '';
			txt = getSel();
			if (txt.length > 0 && txt != ' '){;
				if (txt.substring(txt.length, txt.length-1) == ' ') {lastChar = ' ';}
			}
			setSel(returnValue + lastChar);
			break;
		}
	}
	if (dopost && (lastCtlval != document.getElementById(window.ctlID).value)){
		__doPostBack(postbackCtl.replace('_', '$'),window.eventArg);
	}
}

function getSel(){
	if (window.getSelection) {
		el =document.getElementById(ctlID);
		return el.value.substring(el.selectionStart,el.selectionEnd)
	}
	else if (document.getSelection) {return document.getSelection();}
	else if (document.selection) {return document.selection.createRange().text;}
	else {return '';}
}

function setSel(value){
	if (window.getSelection) {
		el =document.getElementById(ctlID);
		el.value = el.value.substring(0, el.selectionStart) + value + el.value.substring(el.selectionEnd, el.value.length); 
	}
	else if (document.getSelection) {}
	else if (document.selection) {
 document.getElementById(ctlID).caretPos = document.selection.createRange().duplicate()
 document.getElementById(ctlID).caretPos.text=value;}
	else {return;}
}

function print_window(){
	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
	WebBrowser.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box 
	WebBrowser.outerHTML = '';
}

function ShowHelp(url,w,h)
{
	if (typeof w == 'undefined'){w = 800}
	if (typeof h == 'undefined'){h = 600}
	var wsh = window.open(url,'Help' +SID,'height=' + (h + delta) + ',width=' + (w + delta) + ',status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
	wsh.focus();
}

function SetSearchReturnValue(RetVal)
{
	if (!top.opener.closed){;
		top.opener.SetReturnValue(RetVal);
	}
	window.close();
}

function SetSearchReturnValue1(RetVal)
{
	parent.SetReturnValue(RetVal);
}

function MsgConfirm(msg)
{
	return confirm(msg)
}

function ToggleDIV(Img,obj1ID,caseType,ifrID){
     objDIV = document.getElementById(obj1ID);
     if (caseType == 'STYLE'){
 	ObjVisibility = objDIV.style.visibility;
 	objstate = 'visible';
     } else {
 	ObjVisibility = objDIV.className;
 	objstate = 'VISIBLE';
     }
     if (ObjVisibility.indexOf(objstate) != -1){
 	if (caseType == 'STYLE'){
     		objDIV.style.visibility = 'hidden';
     		objDIV.style.display = 'none';
 	} else {
     		objDIV.className = objDIV.className.replace('VISIBLE','HIDDEN');
 	}
	 MoveDiv(Img, objDIV, false, caseType, ifrID)
     } else {
	if (caseType == 'STYLE'){
	     objDIV.style.visibility = 'visible';
	     objDIV.style.display = 'block';
     	} else {
  	     objDIV.className = objDIV.className.replace('HIDDEN','VISIBLE');
     	}
     	MoveDiv(Img, objDIV, true, caseType, ifrID)
     }
}

function MoveDiv(Img, dv, show, caseType,ifrID){
     ifr = document.getElementById(ifrID);
     if (show){
 CloseDIV(ifrID, caseType);
 var y = GetElementTop(Img) + Img.offsetHeight
 var x = GetElementLeft(Img) + + Img.offsetWidth - dv.clientWidth
 var id = dv.id
 dv.style.top = y;
 dv.style.left = x;
 ifr.setAttribute('dv', dv.id);
 ifr.style.width = dv.clientWidth;
 ifr.style.height = dv.clientHeight;
 ifr.style.top = dv.style.top;
 ifr.style.left = dv.style.left;
 ifr.style.zIndex = dv.style.zIndex - 1;
 // make the div and the iframe visible
 ifr.style.display  = 'block';
     } else {
 ifr.style.display  = 'none';
     }
}

function CloseDIV(ifrID, caseType){
     ifr = document.getElementById(ifrID);
     if (ifr.style.display == 'block' && ifr.getAttribute('dv') != null){
 lastdv = document.getElementById(ifr.getAttribute('dv'));
 if (caseType == 'STYLE'){
     lastdv.style.visibility = 'hidden';
     lastdv.style.display = 'none';
 } else {
     lastdv.className=lastdv.className.replace('VISIBLE','HIDDEN')
 }
 ifr.setAttribute('dv', null);
 ifr.style.display  = 'none';
     }
}

function GetElementLeft(eElement){
	if (!eElement && this) {
   		eElement = this;
	}
	var nLeftPos = eElement.offsetLeft;
	var eParElement = eElement.offsetParent;
	while (eParElement != null){
		nLeftPos += eParElement.offsetLeft;
		eParElement = eParElement.offsetParent;
	}
	return nLeftPos;
}

function GetElementTop(eElement){
	if (!eElement && this) {
		eElement = this;
	}
	var nTopPos = eElement.offsetTop;
	var eParElement = eElement.offsetParent;
	while (eParElement != null){
		nTopPos += eParElement.offsetTop;
		eParElement = eParElement.offsetParent;
	}
	return nTopPos;
}

function ToggleState(ImgID,obj1ID,obj2ID,ImgExpandSrc,ImgCollapseSrc,TooltipExpand,TooltipCollapse,caseType){
if (caseType == 'STYLE'){
     ObjExpanded = document.getElementById(obj1ID).style.visibility;
     objstate = 'visible';
} else {
     ObjExpanded = document.getElementById(obj1ID).className;
     objstate = 'VISIBLE';
}
img=document.getElementById(ImgID);
Obj1=document.getElementById(obj1ID);
Obj2=document.getElementById(obj2ID);
if (ObjExpanded.indexOf(objstate) != -1){
     if (caseType == 'STYLE'){
     Obj1.style.visibility = 'hidden';
     Obj2.style.visibility = 'hidden';
     Obj1.style.display = 'none';
     Obj2.style.display = 'none';
     } else {
 Obj1.className = Obj1.className.replace('VISIBLE', 'HIDDEN');
 Obj2.className = Obj2.className.replace('VISIBLE', 'HIDDEN');
     }
     img.src= ImgExpandSrc;
     img.title=TooltipExpand;
} else {
     if (caseType == 'STYLE'){
     Obj1.style.visibility = 'visible';
     Obj2.style.visibility = 'visible';
     Obj1.style.display = 'block';
     Obj2.style.display = 'block';
     } else {
 Obj1.className = Obj1.className.replace('HIDDEN', 'VISIBLE');
 Obj2.className = Obj2.className.replace('HIDDEN', 'VISIBLE');
     }
     img.src= ImgCollapseSrc;
     img.title=TooltipCollapse;
}
}

function ToggleStateNL(ImgID,obj1ID,obj2ID,ImgExpandSrc,ImgCollapseSrc,TooltipExpand,TooltipCollapse,caseType){
if (caseType == 'STYLE'){
     ObjExpanded = document.getElementById(obj2ID).style.visibility;
     objstate = 'visible';
} else {
     //ObjExpanded = document.getElementById(obj1ID).className;
     
     //objstate = 'VISIBLE';
}
img=document.getElementById(ImgID);
if (ObjExpanded == objstate){
     if (caseType == 'STYLE'){
     var wcbo = wcGetComboById(obj1ID);
     wcbo.FrameObj.style.visibility = 'visible';
     wcbo.FrameObj.style.display = 'block';
     document.getElementById(obj2ID).style.visibility = 'hidden';
     document.getElementById(obj2ID).style.display = 'none';
     document.getElementById(obj2ID).value = '';
     } else {
 //document.getElementById(obj1ID).className = 'HIDDEN';
 //document.getElementById(obj2ID).className = 'HIDDEN';
     }
     img.src= ImgExpandSrc;
     img.title=TooltipExpand;
} else {
     if (caseType == 'STYLE'){
     var wcbo = wcGetComboById(obj1ID);
     wcbo.FrameObj.style.visibility = 'hidden';
     wcbo.FrameObj.style.display = 'none';
     wcbo.Value = '';
     document.getElementById(obj2ID).style.visibility = 'visible';
     document.getElementById(obj2ID).style.display = 'block';
     } else {
 //document.getElementById(obj1ID).className = 'VISIBLE';
 //document.getElementById(obj2ID).className = 'VISIBLE';
     }
     img.src= ImgCollapseSrc;
     img.title=TooltipCollapse;
}
}



