function doLogin()
{
	document.getElementById("loginbox").submit();
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function cartSubmitMulti()
{
	if(onCartSubmitMulti())
		document.getElementById('form_carrello').submit();
}

function onCartSubmitMulti()
{
	var pre=canSubmit;
	var ret = false;
	if(pre)
	{
		var vsErrore="";
		var vnChecked  =0;
		for(i in gaRigheProdotti)
		{
			if(document.getElementById('check'+i).checked)
			{
				vnChecked++;
				if(!cartValidateRow('qta'+i,'qta_min'+i))
				{
					//alert('errore');
					vsErrore+= 'il prodotto '+gaRigheProdotti[i]+' è acquistabile in quantità multiple di '+
								document.getElementById('qta_min'+i).value+"\n";
						
					//alert(vsErrore);
					//return false;
				}
			}
		}
		if(vnChecked==0)
			alert("E'necessario selezionare almeno un prodotto");
		else if(vsErrore!="")
		{
			alert("Alcuni prodotti non rispettano le quantità  minime di acquisto:\n"+vsErrore);
		}
		else
		{
			canSubmit=false;
			ret = true;
		}
	}
	
	return ret;
}


function onCartSubmitSingle()
{
	cartSubmit('form_carrello', 'qta', 'qta_min');
	return false;
	//pre=canSubmit;
	//canSubmit=false;
	//return pre;
}

function cartSubmitLink(postfix)
{
	var pre=canSubmit;
	if(pre)
	{
		if(cartValidateRow('qta'+postfix,'qta_min'+postfix))
		{
			canSubmit=false;
			vsUrl = "index.php?module=site&method=redirect&redirect:method=carrello&0:module=shop&0:method=aggiungiProdotto";
			vsUrl += "&0:riga:id_prodotto="+escape(document.getElementById('prodotto'+postfix).value);
			vsUrl += "&0:riga:qta_ordinata="+escape(document.getElementById('qta'+postfix).value);
			document.location.href=vsUrl;
		}
		else
		{
			alert ("E' necessario inserire una quantità multipla di "+document.getElementById('qta_min'+postfix).value+"!");
		}
	}
}

function cartValidateRow(qta, qta_min)
{
	return ((document.getElementById(qta).value % document.getElementById(qta_min).value) == 0);
	
}

function cartSubmit(form, qta, qta_min)
{
	pre=canSubmit;
	if(pre)
	{
		if(cartValidateRow(qta, qta_min))
		{
			canSubmit=false;
			document.getElementById(form).submit();	
		}
		else
		{
			alert ("E' necessario inserire una quantità multipla di "+document.getElementById(qta_min).value+"!");
		}
	}	
}

function enterSubmit(theForm,e)
{
	if(window.event)
		keyPressed = window.event.keyCode; // IE hack
	else
		keyPressed = e.which; // standard method
	if (keyPressed == 13)
		theForm.submit();
}

function enterDoSubmit(theForm,e)
{
	if(window.event)
		keyPressed = window.event.keyCode; // IE hack
	else
		keyPressed = e.which; // standard method
	if (keyPressed == 13)
		doSubmit();
}


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

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



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) 
	{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // all but Explorer Mac
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} 
	else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function resizeDivs()
{
	vaPage = getPageSize();
	voDiv = document.getElementById("fade");
	if (voDiv)
	{
		voDiv.style.height=(vaPage[1]+'px');
	}
	toggleSelect();
}

//nasconde le select che "bucano" i div, solo su IE 6
function toggleSelect()
{
	if (vbIE6)
	{
		//inserire qui gli ID delle select che possono dare problemi al layer dei filtri
		var vaSelect = new Array;
		vaSelect[0] = "idbrand";
		vaSelect[1] = "idcat";
		vaSelect[2] = "idprinter";
		vaSelect[3] = "0:filters:id_macrocategoria";
		vaSelect[4] = "n_prod";
		
		for (i=0;i<vaSelect.length;i++)
		{
			voSel = document.getElementById(vaSelect[i]);
			if (voSel)
			{
				if (voSel.style.visibility == "hidden")
					voSel.style.visibility = "visible";
				else
					voSel.style.visibility = "hidden";	
			}
		}
	}	
}

function showWaiting()
{
	vaPageSize = getPageSize();
	document.getElementById("statediv").style.visibility="visible";
	document.getElementById("statediv").style.display="block";
	document.getElementById("statediv").style.height=vaPageSize[1]+"px";
}

function hideWaiting()
{
	document.getElementById("statediv").style.visibility="hidden";
	document.getElementById("statediv").style.display="none";
}

function validaEmail()
{
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;	
    if(!emailPattern.test(document.getElementById("guestEmail").value))
    	return false;
    else
       return true;	
}
