//function beforeUnload(e) {
//
//		if(validLogin){
//
//			if(!e){			
//				 	var y = parseInt(""+window.event.clientY);
//
//				 	if( (y * -1) > 0 ){
//				 		inviaRichiesta(contextPath+"/logons/logout.php","");
//				 		alert("Il Sistema ha effettuato il logout!");
//						return ;
//				 	}
//			}else{
//					if(e.explicitOriginalTarget){
//						if(e.explicitOriginalTarget.tagName == 'HTML'){
//							inviaRichiesta(contextPath+"/logons/logout.php","");
//			 				alert("Il Sistema ha effettuato il logout!");
//							return ;
//						}
//					}else{
//
//							if(e.target+"" == "[object HTMLDocument]"){
//								inviaRichiesta(contextPath+"/logons/logout.php","");
//					 			alert("Il Sistema ha effettuato il logout!");
//								return ;
//							}
//				}
//			}
//		}
//	}
//	window.onbeforeunload = beforeUnload;
	
function redirect(){
	var loc= window.location+"";
	if(loc.indexOf(welcomePage) == -1){
		
		window.location=contextPath;			
	}
	
}
if(window.name != 'MB_POP_UP_WINDOW'){
		//redirect();
}
function $addListener(obj, ev, lis) {
    if (!obj.events) obj.events = {}
    if (!obj.events[ev]) obj.events[ev] = [];
    obj.events[ev].push(lis);
    if (obj.addEventListener)
        obj.addEventListener(ev, lis, 0);
    else if (obj.attachEvent && !obj.events.iefix) {
        obj.events.iefix = true;
        obj.attachEvent("on" + ev, function() {
            $callListeners(obj, ev);
        });
    }
}

function $callListeners(obj, ev) {
    for (var i = 0, l = obj.events[ev].length; i != l; i++) {
        obj.events[ev][i].call(obj);
    }
}

var getElementProcessor =
{
    getElementsByTagName : function(parentElem, tagName)
    {
        var elements;
        try {
            elements = parentElem.getElementsByTagName(tagName);
        }
        catch(e) {
            elements = [];
            this._getElementsByTagNameInternal(parentElem, tagName, elements);
        }
        return elements;
    },

    _getElementsByTagNameInternal : function(parentElem, tagName)
    {
        var activeElem;
        for (var x = 0; x < parentElem.childNodes.length; x++)
        {
            activeElem = parentElem.childNodes[x];
            if (activeElem.tagName === tagName) {
                collection.push(activeElem);
            }
            if (activeElem.childNodes.length > 0 && activeElem.nodeType == 1) {
                this._getElementsByTagNameInternal(activeElem, tagName, collection);
            }
        }
    }
};

function setButtonOnClick() {
    try {
    	ajaxReady();
    	//button
        var parent = document;
        var commandList = getElementProcessor.getElementsByTagName(parent, "input");
        for (i = 0; i < commandList.length; i++) {
            obj = commandList[i];
            id = commandList[i].id;
            type = commandList[i].type;
            
            //if (id.toLowerCase().indexOf("cmd") != -1 || styleClass == "genericCommandButton") {
                if (type == 'submit' || type == 'button') {
                    $addListener(obj, "click", function() {
                    	ajaxWait();
                    });
                }
            //}
        }
     //link
    parent = document;
    var linkList = getElementProcessor.getElementsByTagName(parent, "a");
    for (j = 0; j < linkList.length; j++) {
        obj = linkList[j];
        id = linkList[j].id;
        target=""+linkList[j].target;
        
        //if (id.toLowerCase().indexOf("cmd") != -1 || styleClass == "genericCommandButton") {
           		if(target == "#" || target == "_blank")continue;
                $addListener(obj, "click", function() {
                	ajaxWait();
                });
            
        //}
    }
    } catch(err) {
        // do nothing
    }
}

var AJAX_WAIT_DIV_ID = "AJAX_WAIT_DIV_ID";
var AJAX_WAIT_IMG = "AJAX_WAIT_IMG";
var AJAX_WAIT_IFRAME_ID = "AJAX_WAIT_IFRAME_ID";

function getDocHeight() {
    var D = document;
    var max = 0;
    
     max =Math.max(
            Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
            Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
            Math.max(D.body.clientHeight, D.documentElement.clientHeight)
        );
  
    return max
}
function getDocWidth() {
    var D = document;
    var max = 0;
    
     max =Math.max(
            Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
            Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
            Math.max(D.body.clientWidth, D.documentElement.clientWidth)
        );
  
    return max
}


// questa funzione restituisce un array con la posizione x,y dello scroll

function getYScroll()
{
    var yScroll;
    if (self.pageYOffset)
    {
        yScroll = self.pageYOffset;
        //xScroll = self.pageXOffset;
    } 
    else if (document.documentElement && document.documentElement.scrollTop)
    {
        yScroll = document.documentElement.scrollTop;
        //xScroll = document.documentElement.scrollLeft;
    }
    else if (document.body)
    {
        yScroll = document.body.scrollTop;
        //xScroll = document.body.scrollLeft;
    }
    return yScroll;
}

function getXScroll()
{
    var xScroll;
    if (self.pageXOffset)
    {
        xScroll = self.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    {
        xScroll = document.documentElement.scrollLeft;
    }
    else if (document.body)
    {
        xScroll = document.body.scrollLeft;
    }
    return xScroll;
}


function getPageScroll() {

var xScroll, yScroll;

// ---> RICAVO LE COORDINATE x ED y DEGLI SCROLL <--- //

// self.pageYOffset : indica la coordinata y di dove si trova la barra dello scroll verticale
// self.pageXOffset : indica la coordinata x di dove si trova la barra dello scroll orizzontale
// (non valido per explorer 6)

// document.documentElement.scrollTop  : indica la coordinata y di dove si trova la barra dello scroll verticale
// document.documentElement.scrollLeft : indica la coordinata x di dove si trova la barra dello scroll orizzontale
// (valido per explorer 6)

if (self.pageYOffset) {
    yScroll = self.pageYOffset;
    xScroll = self.pageXOffset;
}else if (document.documentElement && document.documentElement.scrollTop) {
            yScroll = document.documentElement.scrollTop;
            xScroll = document.documentElement.scrollLeft;
    }else if (document.body) {
                yScroll = document.body.scrollTop;
                xScroll = document.body.scrollLeft;
    }

arrayPageScroll = new Array(xScroll,yScroll);
return arrayPageScroll;
}



function getViewPortHeight()
{
     var viewportwidth;
    var viewportheight;

     // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

     if (typeof window.innerWidth != 'undefined')
     {
          viewportwidth = window.innerWidth,
          viewportheight = window.innerHeight
     }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

     else if (typeof document.documentElement != 'undefined'
         && typeof document.documentElement.clientWidth !=
         'undefined' && document.documentElement.clientWidth != 0)
     {
           viewportwidth = document.documentElement.clientWidth,
           viewportheight = document.documentElement.clientHeight
     }

     // older versions of IE

     else
     {
           viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
           viewportheight = document.getElementsByTagName('body')[0].clientHeight
     }
    //document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
    return viewportheight;

}

function createDivAjaxWait() {

   var docH = getDocHeight();
   var docHBis = getViewPortHeight();
   var yScroll = getYScroll();
   var docW = getDocWidth();
   var docWBis = getDocWidth();
   var xScroll = getXScroll();

   var posY = docHBis + (yScroll * 2);
   var posX = docWBis + (xScroll * 2);

   getPageScroll();

   var newdiv = document.createElement('DIV');
       newdiv.setAttribute("z-index", 9002);
       newdiv.setAttribute("id", AJAX_WAIT_DIV_ID);
       var op = 2;
       newdiv.style.opacity = op/10;
       newdiv.style.filter = 'alpha(opacity=' + op*10 + ')';
       newdiv.style.background="black";
       newdiv.style.display='';
       newdiv.style.top=0;
       newdiv.style.left=0;
       newdiv.style.width = docW;
       newdiv.style.heigth = posY;
       newdiv.style.paddingBottom = docH + 'px';//1000px';
       //newdiv.style.paddingTop = '1000px';

       //alert(contextPath + '/images/punto-interrogativo-blu.png');

       newdiv.innerHTML="<table width='"+(docW)+"px' height='"+(posY)+"px'>" +
                        "<tr align='center' valign='center'>" +
                        "<td align='center' valign='center'>" +
                        "<img id='AJAX_WAIT_IMG'style='z-index:9003;opacity:1;filter:alpha(opacity=100)'; SRC='" + contextPath + "/images/punto-interrogativo-blu.png'/>" +
                        "</td>" +
                        "</tr>" +
                        "<table> ";
       newdiv.style.position = "absolute";
       document.body.appendChild(newdiv);
      
       //createIframeAjaxWait(newdiv);

    return newdiv;
}
/*
function createIframeAjaxWait(ref) {

 var iframe = document.createElement('IFRAME');

     iframe.setAttribute("z-index", 9001);
     iframe.setAttribute("id", AJAX_WAIT_IFRAME_ID);
     iframe.style.filter = 'alpha(opacity=' + 1 + ')';
     iframe.style.display='';
     iframe.style.top=ref.style.top;
     iframe.style.left=ref.style.left;
     iframe.style.width=ref.offsetWidth+'px';
     iframe.style.height=ref.offsetHeight+'px';
     iframe.style.position = "absolute";
     iframe.setAttribute("scroll", "none");
    document.body.appendChild(iframe);

}*/

function ajaxWait(){
     document.body.style.cursor = 'wait';
     return createDivAjaxWait();
}
function ajaxReady(){

    document.body.style.cursor = '';
    var waitDiv = document.getElementById(AJAX_WAIT_DIV_ID);
    if(waitDiv) {

        //document.body.removeChild(document.getElementById(AJAX_WAIT_IFRAME_ID));
        document.body.removeChild(waitDiv);
        
    }
}

