/* centra i pop up sullo schermo */
function centerPopX(popW){ if(!popW) popW=500; return ((screen.availWidth - popW)/2);  }
function centerPopY(popH){ if(!popH) popH=270; return ((screen.availHeight - popH)/2); }

function goBack()
{
	history.back();
	return false;
}

function imgPopPreview(imgId)
{
	winBig = window.open(PATH2ROOT + 'dynamic/imgview.php?id=' + imgId + '&' + SID,'_blank','width=100,height=100,screenX=50,left=50,screenY=50,top=50,resizable=1');
	winBig.focus();
}


function resizeImageProportional(w,h,mw,mh)
{
	// trova il lato pił grande
	whR = w - h;

	if(whR >= 0)
	{
		if(mw && (w - mw > 0)) { h = h*mw/w; w = mw; }
		if(mh && (h - mh > 0)) { w = w*mh/h; h = mh; }

	}
	else
	{
		if(mh && (h - mh > 0)) { w = w*mh/h; h = mh; }
		if(mw && (w - mw > 0)) { h = h*mw/w; w = mw; }
	}

	// prepara l'array con i due valori da restituire
	var dim = new Array();
	dim[0] = Math.round(w);  // larghezza
	dim[1] = Math.round(h);  // altezza
	return dim;
}

/* centra i pop up sullo schermo */
function centerPopX(popW){ if(!popW) popW=500; return ((screen.availWidth - popW)/2);  }
function centerPopY(popH){ if(!popH) popH=270; return ((screen.availHeight - popH)/2); }

function goBack()
{
	history.back();
	return false;
}


function formatEuroPrice(val,dec)
{
	var nval;
	dec = dec||2;
	var p = val.toString().split('.');
	var intPart = '';
	var decPart = null;
	var cifra = 1;
	while(cifra <= p[0].length)
	{
		intPart = p[0].charAt(p[0].length-cifra) + intPart;
		if((p[0].length-cifra) > 0 && (cifra%3)==0) intPart = '.' + intPart;
		cifra++;
	}
	if(p.length == 1) decPart = '00';
	else decPart = Math.round(p[1]/(Math.pow(10,p[1].length-dec)));
	if(decPart == '0') decPart = '00';
	nVal = intPart + ((dec>0) ? (',' + decPart):(''));
	return nVal;
}

function formatMigliaia(val)
{
	var p = val.toString().split('.');
	var intPart = '';
	var cifra = 1;
	while(cifra <= p[0].length)
	{
		intPart = p[0].charAt(p[0].length-cifra) + intPart;
		if((p[0].length-cifra) > 0 && (cifra%3)==0) intPart = '.' + intPart;
		cifra++;
	}
	return intPart;
}

function addElClass(el,className)
{
	if(el!==null)
	{
		var elClass = el.className;
		if(elClass===null) elClass = '';
		if(elClass !=='')
		{
			var elencoClassi = elClass.split(' ');
			for(var i = 0; i < elencoClassi.length; i++)
			{
				if(elencoClassi[i]==className) return;
			}
			elencoClassi.push(className);
			el.className = elencoClassi.join(' ');
		}
		else
		{
			el.className = className;
		}
	}
}

function removeElClass(el,className)
{
	if(el!==null)
	{
		var elClass = el.className;
		if(elClass===null) elClass = '';
		if(elClass !=='')
		{
			var elencoClassi = elClass.split(' ');
			for(var i = 0; i < elencoClassi.length; i++)
			{
				if(elencoClassi[i]==className)
				{
					elencoClassi.splice(i,1);
					i=elencoClassi.length;
				}
			}
			el.className = elencoClassi.join(' ');
		}
	}
}

function setCookie(cookieName, cookieValue, expires, path)
{
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '');
}

function getCookie(cookieName)
{
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
}



function clearRightMouseButton(e)
{
	if(!e) e = window.event;
	switch(e.type)
	{
		case 'mousedown':
			if(e.which == 1) return true;
		case 'contextmenu':
		case 'selectstart':
		case 'select':
		case 'dragstart':
			return clearEvent(e);
			break;
	}
}

function clearEvent(e)
{
	e.cancelBubble = true;
	e.returnValue = false;
	return false;
}

function myAddEventListener(obj,ev,cbFunc)
{
	if(obj!==null)
	{
		if(obj.attachEvent)
		{
			obj.attachEvent('on'+ev,cbFunc);
		}
		else if(obj.addEventListener)
		{
			obj.addEventListener(ev,cbFunc,false);
		}
		else
		{
			obj['on'+ev] = cbFunc;
		}
	}
}


function myRemoveEventListener(obj,ev,cbFunc)
{
	if(obj!==null)
	{
		if(obj.detachEvent)
		{
			obj.detachEvent('on'+ev,cbFunc);
		}
		else if(obj.removeEventListener)
		{
			obj.removeEventListener(ev,cbFunc,false);
		}
		else
		{
			obj['on'+ev] = null;
		}
	}
}

