// Code by Andrea Moi (IUX) - Mind srl - http://wwmind.com 

// COMMON JS CODE

var browserVer=parseInt(navigator.appVersion);
var Win=navigator.userAgent.indexOf("Win")!=-1;	
var InternetExplorer=navigator.userAgent.indexOf("MSIE")!=-1;
var	IE4=((typeof document.all!="undefined") && (browserVer >= 4))?1:0;
var NetscapeNavigator=navigator.appName.indexOf("Netscape")!=-1;
var	NS4=(typeof document.layers!="undefined")?1:0;
var NS6=navigator.userAgent.indexOf("Gecko")!=-1;
var Gecko=navigator.userAgent.indexOf("Gecko")!=-1;
var Safari=navigator.userAgent.indexOf("Safari")!=-1;
var Opera=navigator.userAgent.indexOf("Opera")!=-1;
var	ver4=(NS4||IE4||NS6)?1:0;


// get element by ID (with browser check)
function getObj(objID) {
	if (!document.getElementById)	return null;	// not compatible
	//else
	return document.getElementById(objID);
}

// show and hide DIV
function alternateShow(divToShow,divToHide) {

	if (divToHide!=null&&divToHide.style!=null) {	// hide
		divToHide.style.display="none";
		if (Opera)		divToHide.style.visibility="hidden";	// aggiro baco background menu di Opera
	}
	if (divToShow!=null&&divToShow.style!=null) {	// show
		divToShow.style.display="block";
		if (Opera)		divToShow.style.visibility="visible";	// aggiro baco background menu di Opera
	}
}


// show or hide a object
function switchBox(obj,show)	{
	if (!obj)	return;

	if (show)	obj.style.display = "block";	// show
	else		obj.style.display = "none";		// hide
	
	if (Opera)	{	// aggiro baco background menu di Opera
		if (show)	obj.style.visibility="visible";	// show
		else		obj.style.visibility="hidden";		// hide
	}
}

// check a object visibility
function isVisible(obj)	{
	return (obj&&obj.style.display!="hidden"&&obj.style.display!="none");
}

// toggle a object
function toggleObj(obj)	{
	if (!obj)	return;
	if (isVisible(obj))		switchObj(obj,false);	// hide
	else					switchObj(obj,true);	// show
}

// show or hide page select field (IE bug with float menu)
var selectFieldList=null;
function switchSelectField (show)	{
	if (!InternetExplorer||!floatMenuOpen)	return;
	var s;

	if (show)	{	// mostro field
		if (!selectFieldList||selectFieldList.length<1)	return;
		
		// show stored fields
		for (s=0;s<selectFieldList.length;s++)	selectFieldList[s].style.visibility="visible";
		
	} else {	// nascondo field
		if (!document.getElementsByTagName)	return;		// funzione non supportata
	
		// se non e' gia' stato fatto, costruisco lista dei select della pagina
		if (!selectFieldList)	{	
		
			// prendo array dei select presenti nella pagina
			var selList=document.getElementsByTagName("select");
			if (selList.length<1)	return;	// nessun select trovato

			// variabili di appoggio
			var sLeft,sRight,sTop,sBottom,mLeft,mRight,mTop,mBottom,objParent;
			
			// calcolo gli offset del menu aperto rispetto al body
			mLeft = floatMenuOpen.offsetLeft;
			mTop = floatMenuOpen.offsetTop;
			objParent = floatMenuOpen.offsetParent;
			while (objParent&&objParent.tagName.toUpperCase()!="BODY") {	// scorro tutti gli oggetti parent
				mLeft += objParent.offsetLeft;
				mTop += objParent.offsetTop;
				objParent = objParent.offsetParent;
			}
			// calcolo larghezza e aggiungo bordo di sicurezza
			mRight=mLeft+floatMenuOpen.clientWidth+10;
			mBottom=mTop+floatMenuOpen.clientHeight+10;
			mLeft-=10;
			mTop-=10;

			// creo array globale elementi
			selectFieldList=new Array();	
			
			// loop elements
			for (s=0;s<selList.length;s++)	{
			
				if (!selList[s].offsetParent)	continue;
			
				// calcolo gli offset del select rispetto al body
				sLeft = selList[s].offsetLeft;
				sTop = selList[s].offsetTop;
				objParent = selList[s].offsetParent;
				while (objParent&&objParent.tagName.toUpperCase()!="BODY") {	// scorro tutti gli oggetti parent
					sLeft += objParent.offsetLeft;
					sTop += objParent.offsetTop;
					objParent = objParent.offsetParent;
				}
				// calcolo larghezza e aggiungo bordo di sicurezza
				sRight=sLeft+selList[s].clientWidth+10;
				sBottom=sTop+selList[s].clientHeight+10;
				sLeft-=10;
				sTop-=10;
				
				//alert("menu  = "+mLeft+" , "+mTop+" x "+mRight+" , "+mBottom +"\n\n"+selList[s].name+" = "+sLeft+" , "+sTop+" x "+sRight+" , "+sBottom);				
				
				// controllo incrocio box
				if (sLeft>mRight||sTop>mBottom)	continue;
				if (sRight<mLeft||sBottom<mTop)	continue;

				// save
				selectFieldList[selectFieldList.length]=selList[s];
			}
		}
		if (!selectFieldList||selectFieldList.length<1)	return;			// no element found
		
		// hide fields
		for (s=0;s<selectFieldList.length;s++)		selectFieldList[s].style.visibility="hidden";
	}
}


// switch float menu
var floatMenuOpen=null;
function  switchCatMenu(autoTogle,openMenu)	{
	if (!document.getElementById)	return;	// not compatible
	
	// get object (first time only)
	if (!floatMenuOpen)		floatMenuOpen=getObj("floatCatMenuOpenDiv");
	if (!floatMenuOpen)		return;
	
	// check new state
	var currentShow=isVisible(floatMenuOpen);
	if (autoTogle)	openMenu=!currentShow;
	else			if (openMenu==currentShow)	return;
		
	if (!openMenu)	{		// close menu
		switchBox(floatMenuOpen,false);
		// visualizzo select nascosti precedentemente
		if (InternetExplorer)	switchSelectField(true);
		
	} else { 	// open menu
		switchBox(floatMenuOpen,true);
		// nascondo select della pagina
		if (InternetExplorer)	switchSelectField(false);
	}
}



// select: autosubmit
function autoSubmitSelect (selObj,emptyAlso)	{
	var v=selObj[selObj.selectedIndex].value;
	if (v!=""||emptyAlso)	selObj.form.submit();
}


// open a custom windows (full parameters)
function customPopUp(url,x,y,name,param,focusOnPopUp,focusOnWindow)		{

	// dimensioni
	var winSize="";
	if (x>0)	winSize+="width="+x+",";
	if (y>0)	winSize+="height="+y+",";
	
	// new windows
	var siteWindowVar=open(url,name,winSize+"directories=no,"+param);
	
	if (siteWindowVar&&focusOnPopUp)	siteWindowVar.focus();	// popup in primo piano
	if (siteWindowVar&&focusOnWindow)	self.focus();			// finestra corrente in primo piano
	
	// il return serve per controllare eventuali popup bloccati o javascript non abilitato
	return (siteWindowVar==null);
}

// open a windows (custom parameters)
function newWinCust(url,wname,wparam)		{
	var siteWindowVar=open(url,wname,"directories=no,"+wparam);
	siteWindowVar.focus();
}

// open a new popup window
function apri(url)		{
	// new windows
	newWinCust(url,"popupwin","toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,location=no,width=550,height=320");
}

// open a new window (+ size)
function apriXY(url,x,y)		{
	// new windows
	newWinCust(url,"popupwin"+x+"X"+y,"toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,location=no,width="+x+",height="+y);
}


// open a new popup window
function help(what,basePath)		{
	// open popup
	var poplink=basePath+"products/help/help.php?what="+what;
	customPopUp(poplink,550,320,"help","toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,location=no",true,false);
}



// funzione di appoggio per form di ricerca
function changeProdSearch(obj,on)	{
	if (!obj)	return;
	
	// onFocus
	if (on&&obj.value == obj.defaultValue)	obj.value="";
	
	// onBLur
	if (!on&&obj.value == "")	obj.value=obj.defaultValue;
}

// funzione di appoggio per form di ricerca
function checkProdSearch(theForm)	{
	var obj=theForm.searchText;
	if (!obj||obj.value==obj.defaultValue||obj.value=="")	return false;
	return true;
}

// aggiungo un prodotto al carello
function addToCart(idProd,basePath)	{
	// open popup
	var poplink=basePath+"products/cart/add.php?idprod="+idProd;
	return customPopUp(poplink,550,370,"addToCart","toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,location=no",true,false);
}

