	
	var itemID      = '';
	var itemName    = '';
	var itemGenId   = '';
	
	var clickedMenu  = '';
	
	function in_array(needle, haystack, strict){
		var found = false, key, strict = !!strict;  
			for (key in haystack) {  
				if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {  
					found = true;  
					break;  
				}  
			}  
		return found;  
	} 
 
	
	function ById(pPrmById){
		if(document.getElementById(pPrmById)){
			return document.getElementById(pPrmById);
		}else{
			return false;	
		}
	}
	
	function ShowMenu(num){
		if(ById('menu_'+num+'.0_cld')){
			if(ById('menu_'+num+'.0_cld').style.display == 'block'){
				ById('menu_'+num+'.0_cld').style.display = 'none';
			}else if(ById('menu_'+num+'.0_cld').style.display == 'none'){
				ById('menu_'+num+'.0_cld').style.display = 'block';
			}
		}
	}
	
	function AjaxRequest(data0, data1, data2, data3, data4, data5, data6, data7) {
			
		var req = new JsHttpRequest();
		req.onreadystatechange = function() {
			
			if (req.readyState == 4) {
				if (req.responseJS) { 
					result        = req.responseJS.result;
					target_result = req.responseJS.target_result;
					resultMsg     = req.responseJS.resultMsg;
					functionRun   = req.responseJS.functionRun;
					
					if( result != ''){
						if(target_result == 'msgAlert'){
							alert(result);
						}else if(result == 'Close_ExternalWindow'){
							if(ById(target_result)) ById(target_result).innerHTML = '';
							if(ById(target_result)) ById(target_result).style.display = 'none';
						}else if(result == 'Close_InfoForm'){
							clearForm();
							HideAllForm();
							scroll(0,0);
						}else{
							if(target_result=='ShowElement') ShowDetElement('on');
							if(ById(target_result)){
								ById(target_result).style.display = ''; 
								ById(target_result).innerHTML = result;
							}
						}
					}
					
					if( resultMsg!="" && typeof resultMsg != "undefined"  ) alert(resultMsg);
					
					if( typeof(functionRun)!= "undefined" ){
						setTimeout("RunAfterRequest('"+functionRun+"')", 50);
					}
				}
				if(ById('debug')) ById('debug').innerHTML = req.responseText + '<br> Target:' + target_result + ' <br> ' + '<textarea style="width:100%; height:200px;" >' + result + '</textarea>';
			}
		}
		req.open( "POST", 'loader'+data0+'.php', true);
		req.send( { PrmPage: data0, PrmFunc: data1, PrmTargObj : data2, PrmArg1 : data3, PrmArg2 : data4, PrmArg3 : data5, PrmArg4 : data6, PrmArg5 : data7 } );
	}
	
	function AjaxLoad(PrmPage, PrmFunc, PrmTargObj, PrmArg1, PrmArg2, PrmArg3, PrmArg4, PrmArg5 ){
		AjaxRequest(PrmPage, PrmFunc, PrmTargObj, PrmArg1, PrmArg2, PrmArg3, PrmArg4, PrmArg5 );
	}
	
	function RunAfterRequest(runPrm){
		if( runPrm=='AgiornaUsersModResult' ){
			if(ById('AccountDesc')){
				AjaxLoad('_mod', 'utenti', 'ExternalWindow', 'AgiornaDescAccount' );
			}else if(ById('ListUsers')){
				AjaxLoad('_admin', 'utenti', 'ExternalWindow', 'AgiornaListUsers');
			}
		}else if( runPrm=='CloseExternalWindow' ){
			if(ById('ExternalWindow')) ById('ExternalWindow').innerHTML = '';
			if(ById('ExternalWindow')) ById('ExternalWindow').style.display = 'none';
		}
	}
	
	function ShowDetElement(action,anch){
		if(action=='on'){
			if(ById('listElements'))   ById('listElements').style.display = 'none';
			if(ById('ShowElement'))    ById('ShowElement').style.display = 'block';
			if(ById('descResultList')) ById('descResultList').style.display = 'none';
		}else if(action=='off'){
			if(ById('listElements'))   ById('listElements').style.display = 'block';
			if(ById('ShowElement'))    ById('ShowElement').style.display = 'none';
			if(ById('descResultList')) ById('descResultList').style.display = 'block';
			if(anch) document.location='#'+anch;
		}
	}
	
	function ShowIstruzioni(){
		if(ById('istruzioni')){
			if(ById('istruzioni').style.display == 'none'){
				ById('istruzioni').style.display = 'block';
			}else{
				ById('istruzioni').style.display = 'none';
			}
		}
	}
	
	//----- W H L R ---------------------------------------------------------------
	
	function getPageScrollY () {
	  if (window.pageYOffset) {
		return window.pageYOffset;
	  } else if (document.body && document.body.scrollTop) {
		return document.body.scrollTop;
	  } else if (document.documentElement && document.documentElement.scrollTop) {
		return document.documentElement.scrollTop;
	  }
	  return 0;
	};
	function getPageScrollX () {
	  if (window.pageXOffset) {
		return window.pageXOffset;
	  } else if (document.body && document.body.scrollLeft) {
		return document.body.scrollLeft;
	  } else if (document.documentElement && document.documentElement.scrollLeft) {
		return document.documentElement.scrollLeft;
	  }
	  return 0;
	};
	
	function objParametri (objElement) {
	  var iLeft = iTop = iWidth = iHeight = 0;
	  if (objElement.getBoundingClientRect) {
		// IE
		var objRectangle = objElement.getBoundingClientRect();
		iLeft = objRectangle.left;
		iTop = objRectangle.top;
		iWidth = objRectangle.right - iLeft;
		iHeight = objRectangle.bottom - iTop;
		// getBoundingClientRect returns coordinates relative to the window
		iLeft += getPageScrollX() - 2;
		iTop += getPageScrollY() - 2;
	  } else if (document.getBoxObjectFor) {
		// Mozilla
		var objRectangle = document.getBoxObjectFor(objElement);
		iLeft = objRectangle.x;
		iTop = objRectangle.y;
		iWidth = objRectangle.width;
		iHeight = objRectangle.height;
	  } else {
		// Others
		iWidth = objElement.offsetWidth;
		iHeight = objElement.offsetHeight;
		while (objElement.offsetParent) {
		  iLeft += objElement.offsetLeft;
		  iTop += objElement.offsetTop;
		  objElement = objElement.offsetParent;
		}
	  }
	  return {
		left: iLeft,
		top: iTop,
		x: iLeft,
		y: iTop,
		width: iWidth,
		height: iHeight
	  };
	};
	function cTop(objDest) {
		var elementOffset = objParametri(objDest);
		var top = elementOffset.top;
		return top;
	}
	function cLeft (objDest) {
		var elementOffset = objParametri(objDest); //this.config.button
		var left = elementOffset.left;
		return left;
	}
	function cWidth (objDest) {
		var elementOffset = objParametri(objDest); //this.config.button
		var width = elementOffset.width;
		return width;
	}
	function cHeight (objDest) {
		var elementOffset = objParametri(objDest); //this.config.button
		var height = elementOffset.height;
		return height;
	}

	function cCenterX(){
		var scr_w = screen.availWidth;
		var browseWidth = 0;
		if(document.layers||(document.getElementById&&!document.all)){ 
		   browseWidth=Math.round(window.outerWidth/2);
		}else if(document.all){
		   browseWidth=Math.round(document.body.clientWidth/2);
		}
		return browseWidth+document.body.scrollLeft;
	}
	function cCenterY(){
		var scr_h = screen.availHeight;
		var browseHeight = 0;
		if(document.layers||(document.getElementById&&!document.all)){ 
		   browseHeight=Math.round(window.outerHeight/2);
		}else if(document.all){
		   browseHeight=Math.round(document.body.clientHeight/2); 
		}
		return browseHeight+document.body.scrollTop;
	}
	
	//----------------------------------------------------------------
	
	function drag_object( evt, obj ){  

         evt = evt || window.event;  

         obj.clicked = true;  
         obj.mousePosX = evt.clientX;  
         obj.mousePosY = evt.clientY;  
         if( evt.preventDefault ) evt.preventDefault();   
         else evt.returnValue = false;  
         document.onmouseup = function(){ obj.clicked = false }  
         document.onmousemove = function( evt ){  
             evt = evt || window.event;  
             if( obj.clicked ){  
                 posLeft = !obj.style.left ? obj.offsetLeft : parseInt( obj.style.left );  
                 posTop = !obj.style.top ? obj.offsetTop : parseInt( obj.style.top );  
                 mousePosX = evt.clientX;  
                 mousePosY = evt.clientY;  
                 obj.style.left = posLeft + mousePosX - obj.mousePosX + 'px';  
                 obj.style.top = posTop + mousePosY - obj.mousePosY + 'px';  
                 obj.mousePosX = mousePosX;  
                 obj.mousePosY = mousePosY;  
             }  
         }  
     }  

	//--------------------------------------------------------------
	
	function validateEmail(eml) {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(eml) == false) { return false; }else{ return true; }
	}
	function CheckForm( frmobj, prmId ){
		cmpObl = new Array('Nome', 'Cognome', 'Azienda', 'EMail', 'Tel', 'Legge' );
		var Forma = frmobj.rg_in.value
		for(a=0;a<cmpObl.length;a++){
			if( ById(cmpObl[a]) ){
				frmCamp = frmobj[cmpObl[a]];
				if(frmCamp){
					if(cmpObl[a] == 'EMail' && !validateEmail(frmCamp.value) ){
						alert('EMail non valido.');
						frmCamp.focus();
						return false;
					}else{
						if( frmCamp.value == '' || frmCamp.value == 0 || ( frmCamp.type == 'checkbox' && frmCamp.checked == false) ){
							alert('Campo "'+frmCamp.title+'" obbligatorio');
							frmCamp.focus();
							return false;
						}
					}
				}
			}
		}
		frmobj.rg_init.value = PAGEIDENT;
		if(Forma=='frm_InfForm' || Forma=='frm_InfFormDistributore'){
			AjaxLoad('', 'info_eml', 'ExternalWindow', '', frmobj );
		}else if(Forma=='frm_RegForm'){
			AjaxLoad('_mod', 'logreg', 'ExternalWindow', 'newusr', frmobj );
		}else if(Forma=='frm_ModRegForm'){
			AjaxLoad('_mod', 'utenti', 'ExternalWindow', 'modutente', prmId, frmobj);
		}
	}
	
	function HideAllForm(){
		if(ById('InfForm')) ById('InfForm').style.display='none';
		if(ById('InfFormDistributore')) ById('InfFormDistributore').style.display='none';
		if(ById('regForm')) ById('regForm').style.display='none';
		if(ById('ComeRaggiungerci')) ById('ComeRaggiungerci').style.display='none';
		if(ById('lgnForm')) ById('lgnForm').style.display='';
		if(ById('MainContent')) ById('MainContent').style.display='';
	}
	
	function clearForm(){
		var frm = new Array('frm_InfForm','frm_InfFormDistributore','frm_RegForm' );
		var ign = new Array('rg_in', 'btnInvia', 'recnews');
		for(a=0;a<frm.length;a++){
			if(ById(frm[a])){
				var frmX = ById(frm[a]);
				for(b=0;b<frmX.length;b++){
					if(!in_array(frmX[b].id, ign)){
						if( frmX[b].type == 'checkbox' || frmX[b].type == 'radio' ){
							frmX[b].checked = false;
						}else{
							frmX[b].value = '';
						}
					}
				}
			}
		}
	}
	
	function SendSelInfo(tip, toml, thema){
		if(ById(tip)){
			if(ById('MainContent')) ById('MainContent').style.display='none';
			if(ById(tip+'_thema'))  ById(tip+'_thema').value = (thema?thema:'');
			if(ById(tip+'_emlsnd')) ById(tip+'_emlsnd').value = toml;
			
			if(ById(tip)) ById(tip).style.display='block';
			scroll(0,0);
		}
	}
	
	//--------------------------------------------------------------
	
	var _overed = '';
	var _gestmenu = new Array("#e5e5e5","#f5f5f5");
	function over(idOver,tipmenu){
		if(ById(idOver)){
			if(ById(_overed)) ById(_overed).style.background = tipmenu[1];
			ById(idOver).style.background = tipmenu[0];
			_overed = idOver;
		}
	}
	
	function showMenu(idMn, objBtn){
		if(ById(idMn)){
			if(clickedMenu != idMn) hideMenu(clickedMenu);
			if(ById(idMn).style.display=='none'){
				clickedMenu = idMn;
				ById(idMn).style.left    = cLeft(objBtn)+5+'px';
				ById(idMn).style.top     = (cTop(objBtn)+cHeight(objBtn)+5)+'px';
				ById(idMn).style.display = '';
			}else{
				hideMenu(clickedMenu);
			}
		}
	}
	
	function hideMenu(idMn){
		clickedMenu = '';
		if(ById(idMn)) ById(idMn).style.display = 'none';
	}
	
	//--------------------------------------------------------------
	
	function OpenModalWindow(pageWindow, nomeWindow, wW, wH){
		WinW = (wW) ? wW : 600;
		WinH = (wH) ? wH : 400;
		WinX = Math.round(cCenterX()-(WinW/2));
		WinY = Math.round(cCenterY()-(WinH/2));
		if (window.showModalDialog) {
			modalWindow = window.showModalDialog(pageWindow,nomeWindow,'dialogWidth:'+WinW+'px;dialogHeight:'+WinH+'px;dialogTop:'+WinY+'px;dialogLeft:'+WinX+'px;');
		} else {
			modalWindow = window.open(pageWindow,nomeWindow,'height='+WinH+',width='+WinW+',top='+WinY+';left='+WinX+';toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,modal=yes');
		}
	}
			
	//--------------------------------------------------------------
