// opening popups
// Use like this:
// <a href="popup.html" onclick="return Popup.openup(this);">Popup with default size</a>
// <a href="popup.html" onclick="return Popup.openup(this,1000,400);">Popup with special size</a>
Popup = {
	openup:function(obj, w, h) {
		var url = (obj.getAttribute)? obj.getAttribute('href') : obj.href;
		if(!url) return true; // if href is not given, no popup will be opened
		var w = (w === undefined)? 850 : w;
		var h = (h === undefined)? 600 : h;
		var pop = window.open(url, '', 'width='+w+', height='+h+', resizable=yes, scrollbars=yes');
		if (pop) {
			if (pop.focus) pop.focus();
			return false; // prevents link to open in parent window
		} else {
			return true; // if popup could not be opened link opens in original window
		}
	}
}


function addslashes( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: marrtins
    // +   improved by: Nate
    // +   improved by: Onno Marsman
    // *     example 1: addslashes("kevin's birthday");
    // *     returns 1: 'kevin\'s birthday'
 
    return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}


function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}

function ScrollToElement(theElement){

  var selectedPosX = 0;
  var selectedPosY = 0;
              
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
                        		      
 window.scrollTo(selectedPosX,selectedPosY);

}


function populate_date(month, day, year) {
  ge('date_month').value = month;
  ge('date_day').value = day;
  ge('date_year').value = year;
}

function ge(elem) {
  return document.getElementById(elem);
}

/*
 * Simple Ajax call method.
 *
 * From http://en.wikipedia.org/wiki/XMLHttpRequest
 */
function ajax(url, vars, callbackFunction) {
  var request =  new XMLHttpRequest();
  request.open("POST", url, true);
  request.setRequestHeader("Content-Type",
                           "application/x-www-form-urlencoded");

  request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
      if (request.responseText) {
        callbackFunction(request.responseText);
      }
    }
  };
  request.send(vars);
}


function isset(varname)  {
  if(typeof( window[ varname ] ) != "undefined") return true;
  else return false;
}

function stripHTML(){
	var re = /(<([^>]+)>)/gi;
	for (i=0; i<arguments.length; i++)
	arguments[i].value=arguments[i].value.replace(re, "")
}


Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function formatEuro(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+'.'+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '' + dblValue + ',' + strCents);
}

function EuroToNum(strVal){
			newval  = strVal.replace(".","");
	  		newval  = newval.replace(",",".");
	  		return parseFloat(newval);
}

function inputfocus(){

	var sfEls = document.getElementsByTagName("INPUT");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onfocus=function() {
			this.className+=" sffocus";
		}
		sfEls[i].onblur=function() {
			this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
		}
	}

}

function openInfoMsg(idobj,msg){
	
		closeinfobox();
	
		var infodiv = document.createElement('div');
		
		infodiv.setAttribute('id','infoBox');
	
		idobj.appendChild(infodiv);

		var thumbform="";
		thumbform+="<div id='iarrow' class='infoarrow'></div>";

		thumbform+="<div id='infostr' class='smallwindow'>"+msg+"</div>";
		
		document.getElementById('infoBox').className="infomsg";
		
		document.getElementById("infoBox").innerHTML=thumbform;
		var parenth		=(idobj.offsetParent.offsetParent.offsetParent.offsetHeight);
		var infoheight 	= document.getElementById('infoBox').offsetHeight;
		
		
		document.getElementById('infoBox').style.top="-100px";//(-infoheight-parenth+20)+"px";
		document.getElementById('infoBox').style.left="0px";//(idobj.offsetLeft)+"px";

}

function closeinfobox(){
     
	if(document.getElementById('infoBox')){
		var parentObj = document.getElementById('infoBox').parentNode;
	    var childObj = document.getElementById('infoBox'); 
	    parentObj.removeChild(childObj); 
    }

}


// AJAX UTILITY
var xmlHttp

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}


function stripslashes( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +      fixed by: Mick@el
    // +   improved by: marrtins
    // +   bugfixed by: Onno Marsman
    // +   improved by: rezna
    // *     example 1: stripslashes('Kevin\'s code');
    // *     returns 1: "Kevin's code"
    // *     example 2: stripslashes('Kevin\\\'s code');
    // *     returns 2: "Kevin\'s code"
 
    return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
}


function POD_findObj(n, d) { //v4.01
  var p,i,x;  
  if(!d) d = document; 
  if((p=n.indexOf("?"))>0&&parent.frames.length) {d = parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x = POD_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// Suggeritore per editing multilingua
//-----------------------------------------------------------------------------
var oldmlstr;
function suggeststr(str,fname){
	oldmlstr=document.getElementById(fname).value;
	document.getElementById(fname).value=str;
}
function setfstr(fname){
	document.getElementById(fname).value = oldmlstr;
}
//-----------------------------------------------------------------------------

var dw_event = {
  
  add: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
    else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
  }, 

  remove: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
    else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
  }, 

  DOMit: function(e) { 
    e = e? e: window.event;
    e.tgt = e.srcElement? e.srcElement: e.target;
    
    if (!e.preventDefault) e.preventDefault = function () { return false; }
    if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }
        
    return e;
  }
  
}

var viewport = {
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		this.width = document.body.clientWidth;
  },
  
  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		this.height = document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		this.height = document.body.clientHeight;
  },
  
  getScrollX: function () {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft) 
  		this.scrollX = document.body.scrollLeft; 
  	else if (window.scrollX) this.scrollX = window.scrollX;
  },
  
  getScrollY: function () {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop) 
  		this.scrollY = document.body.scrollTop; 
  	else if (window.scrollY) this.scrollY = window.scrollY;
  },
  
  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  }
  
}

var Tooltip = {
    followMouse: true,
    offX: 10,
    offY: 10,
    tipID: "tipDiv",
    showDelay: 100,
    hideDelay: 200,
	ready:false,
	timer:null,
	tip:null,
	init:function(){
			if(document.createElement&&document.body&&typeof document.body.appendChild!="undefined"){
				if(!document.getElementById(this.tipID)){var el=document.createElement("DIV");
					el.id=this.tipID;
					document.body.appendChild(el);
				}
			this.ready=true;
			}
		},
	show:function(e,msg){
		if(this.timer){
			clearTimeout(this.timer);
			this.timer=0;
		}
		this.tip=document.getElementById(this.tipID);
		if(this.followMouse)dw_event.add(document,"mousemove",this.trackMouse,true);
		this.writeTip("");
		this.writeTip(msg);
		viewport.getAll();
		this.positionTip(e);
		this.timer=setTimeout("Tooltip.toggleVis('"+this.tipID+"', 'visible')",this.showDelay);
	},
	writeTip:function(msg){
		if(this.tip&&typeof this.tip.innerHTML!="undefined")this.tip.innerHTML=msg;
	},
	positionTip:function(e){
		if(this.tip&&this.tip.style){
			var x=e.pageX?e.pageX:e.clientX+viewport.scrollX;
			var y=e.pageY?e.pageY:e.clientY+viewport.scrollY;
			if(x+this.tip.offsetWidth+this.offX>viewport.width+viewport.scrollX){x=x-this.tip.offsetWidth-this.offX;
					if(x<0)x=0;
				}else x=x+this.offX;
				if(y+this.tip.offsetHeight+this.offY>viewport.height+viewport.scrollY){
					y=y-this.tip.offsetHeight-this.offY;
					if(y<viewport.scrollY)y=viewport.height+viewport.scrollY-this.tip.offsetHeight;
				}else y=y+this.offY;
			this.tip.style.left=x+"px";
			this.tip.style.top=y+"px";
		}
	},
	hide:function(){
		if(this.timer){
			clearTimeout(this.timer);
			this.timer=0;
		}
		this.timer=setTimeout("Tooltip.toggleVis('"+this.tipID+"', 'hidden')",this.hideDelay);
		if(this.followMouse)dw_event.remove(document,"mousemove",this.trackMouse,true);
		this.tip=null;
	},
	toggleVis:function(id,vis){
		var el=document.getElementById(id);
		if(el)el.style.visibility=vis;
	},
	trackMouse:function(e){
		e=dw_event.DOMit(e);
		Tooltip.positionTip(e);
	}
};


function doTooltip(e, msg) {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.show(e, msg);
}

function hideTip() {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.hide();
}


// Trova l'x di un oggetto
  function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }
// Trova l'y di un oggetto
  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

  
  // NAVIGATION
  
  
function jumpMenu(targ,selObj,restore){ //v3.0 
	if(targ!='null'){
		eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	}
	if (restore) selObj.selectedIndex=0; 
} 

function clearIframe(iframeID) { 
	if(document.getElementById("cache")!= null ){
		parent.document.getElementById(iframeID).src="";
	}
}

function resizeIframe(iframeID) {
	var myDetect=navigator.userAgent.indexOf("Firefox")
	var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
	var FFextraHeight=getFFVersion>=0.1? 16 : 0

	if(self==parent) return false;
	if(myDetect==-1){
		//alert("Explorer")
		FramePageHeight = framePage.scrollHeight;
		parent.     document.getElementById(iframeID).style.height=FramePageHeight;
		
	}else{
		//alert("Mozilla/Firefox")
		FramePageHeight = parent.     document.getElementById(iframeID).contentDocument.body.offsetHeight + FFextraHeight;
		parent.     document.getElementById(iframeID).height=FramePageHeight;
	}
} 


function openPage(page,name,w,h,sb){
	if(h==null || w==null){
		h=575;
		w=580;
	}
	xpos=document.body.clientWidth/2-w/2;
	ypos=document.body.clientHeight/2-h/2;
	props='scrollbars='+sb+',toolbar=0,status=1,resizable=0,width='+ w +',height='+ h +', left ='+xpos+', top ='+ypos
	window.open(page,name,props)
}


function fitPic(hoffset, title) {
    document.title = title;
	iWidth = (NS)?window.innerWidth:     document.body.clientWidth;
	iHeight = (NS)?window.innerHeight:     document.body.clientHeight;
	iWidth =      document.images[0].width - iWidth;
	iHeight =      document.images[0].height - iHeight+hoffset;
	window.resizeBy(iWidth+45, iHeight-1);
	self.focus();
};


function hideShowLayer(idList){
	//window.alert(hideArray);
	var hideArray=idList.split(",");
	if(hideArray[0]!=""){	
		for(i=0;i< hideArray.length ;i++) {
			if(     document.getElementById(hideArray[i]).className=="hidden"){
					 document.getElementById(hideArray[i]).className="visible";
			}else{
					 document.getElementById(hideArray[i]).className="hidden";
			}
		}
	}
}

function showLayer(idList){
	var hideArray=idList.split(",");
	if(hideArray[0]!=""){	
		for(i=0;i< hideArray.length ;i++) {
			 document.getElementById(hideArray[i]).className="visible";
		}
	}
}

function hideLayer(idList){
	var hideArray=idList.split(",");
	if(hideArray[0]!=""){	
		for(i=0;i< hideArray.length ;i++) {
			 document.getElementById(hideArray[i]).className="hidden";
		}
	}
}


function chiudi(){
	window.close();
}

function stampa() {
	window.print();  
}

 /* 
function getAnagrafica(valore,idobject,idobject2,idobject3,opertipo){

			anagxmlHttp = GetXmlHttpObject() //See if it works with users browser.
							
			anagxmlHttp.oggetto = idobject;
			anagxmlHttp.oggetto2 = idobject2;
			anagxmlHttp.oggetto3 = idobject3;
			anagxmlHttp.tipo = opertipo;
			anagxmlHttp.valore = valore;
					
			var url = RootPath+'www/ajaxindex.php?pag=ajaxphp/admin/anagrafiche.php';

			var params = Array();
			
			params.push('tipo='+opertipo);
			params.push('valore='+valore);
			
			var mypars=params.join("&");
			
			anagxmlHttp.open("POST", url, true);
			
			//Send the proper header information along with the request
			anagxmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			anagxmlHttp.setRequestHeader("Content-length", mypars.length);
			anagxmlHttp.setRequestHeader("Connection", "close");
		
			anagxmlHttp.onreadystatechange = function (){
						
							if (anagxmlHttp.readyState==4 || anagxmlHttp.readyState=="complete"){
									switch(this.tipo){
										case 'combocomune':
										document.getElementById(this.oggetto).innerHTML = anagxmlHttp.responseText;
										document.getElementById(this.oggetto2).oggetto=this.oggetto3;
										document.getElementById(this.oggetto2).onchange=function(){getAnagrafica(this.value,this.oggetto,'','','capvalue')};
										break;
										
										case 'capvalue':
											document.getElementById(this.oggetto).value = anagxmlHttp.responseText;
										break;
									}
									
									
							} 
						
			
					};
			
			anagxmlHttp.send(mypars);
}  
  */
  
var popinfoobj;
function assignPopInfo(){

		document.onmouseover=function(){
			closeinfobox();
		}
		
		divElements = document.getElementsByTagName("a");
	    relElems = [];

	    for (i = 0; i < divElements.length; i++) {
	        
		        if (divElements[i].getAttribute("rel")){

							var listrel = divElements[i].getAttribute('rel').split(",");

							if(listrel.inArray('info')){
					
				 					divElements[i].onmouseout=function(){
				 						closeinfobox();
				 					}
				 					
				 					divElements[i].onmouseover=function(){
		 					
			 								popinfoobj = this;
			 							
											infoxmlHttp = GetXmlHttpObject() //See if it works with users browser.
												
											var url = RootPath+"www/ajaxindex.php?pag=../engine/ajaxphp/info.php";
										
											var params = Array();
										
											params.push('id=' + this.id);
									
											var mypars = params.join("&");
										
											infoxmlHttp.open("POST", url, true);
										
											//Send the proper header information along with the request
											infoxmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
											infoxmlHttp.setRequestHeader("Content-length", mypars.length);
											infoxmlHttp.setRequestHeader("Connection", "close");
									
											infoxmlHttp.onreadystatechange = function (){
								
													if (infoxmlHttp.readyState==4 || infoxmlHttp.readyState=="complete")
													{ 									
														var msg = infoxmlHttp.responseText;
														
														openInfoMsg(popinfoobj,msg);
		
													};
											}
											infoxmlHttp.send(mypars);
									}
							
							}
		 	
										        
		         	            
		        }
	 
	    }

}
  
function addEvent(obj,ev,fn){
	if(obj.addEventListener) {
		// metodo w3c
		obj.addEventListener(ev, fn, false);
	} else if(obj.attachEvent) {
		// metodo IE
		obj.attachEvent('on'+ev, fn);
	} else {
		// se i suddetti metodi non sono applicabili
		// se esiste gia' una funzione richiamata da quel gestore evento
		if(typeof(obj['on'+ev])=='function'){
			// salvo in variabile la funzione gia' associata al gestore
			var f=obj['on'+ev];
			// setto per quel gestore una nuova funzione 
			// che comprende la vecchia e la nuova
			obj['on'+ev]=function(){if(f)f();fn()}
		}
		// altrimenti setto la funzione per il gestore
		else obj['on'+ev]=fn;
	}
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

function refresh_page(){
	location.reload(true);
}

function facebook_onlogin_ready(){
	refresh_page();
}

function user_logout(){
    xmlHttp=GetXmlHttpObject() //See if it works with users browser.

	var url="../www/ajaxindex.php?pag=../engine/ajaxphp/userajax.php";

	var params=Array();
	params.push('oper=logout');
	var mypars=params.join("&");
	
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", mypars.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(mypars);
}


/*
  window.addEvent('domready', function() {

	var sfEls = document.getElementsByTagName("INPUT");

	for (var i=0; i<sfEls.length; i++) {
		if(!sfEls[i].disabled){
			addEventPod(sfEls[i],'focus',function() {this.className+=" sffocus";});
			addEventPod(sfEls[i],'blur' ,function() {this.className=this.className.replace(new RegExp(" sffocus\\b"), "");});
		}
	}

});*/  
