var curID;

function GotoPage(initUrl, maxPage)
{
	var page;
	var url;
	page=document.pages.gotopage.value;
	url=initUrl + "&intpage=" + page;
	if (isNaN(page))
		alert("Devi inserire un valore numerico");
	else if (page <= 0 || page > maxPage)
		alert("Inserisci un numero di pagina valido");
	else
		document.location.href=url
}

function GotoPage2(initUrl, maxPage)
{
	var page;
	var url;
	page=document.pages_bottom.gotopage_bottom.value;
	url=initUrl + "&intpage=" + page;
	if (isNaN(page))
		alert("Devi inserire un valore numerico");
	else if (page <= 0 || page > maxPage)
		alert("Inserisci un numero di pagina valido");
	else
		document.location.href=url
}

function checkContatti(form)
{
	if (form.nome.value=='' || form.mail.value=='' || form.oggetto.value=='' || form.testo.value=='')
		alert('Tutti i campi devono essere compilati\nImpossibile inviare il messaggio.');
	else
		form.submit();
	
	return false;
}

function showDiv(id,stima,lotto,asta)
{
	curID=id;
	var obj_start = document.getElementById("cart_" + id + "_start");
	var obj_end = document.getElementById("cart_" + id + "_end");
	var obj_start_x=findPosX(obj_start);
	var obj_start_y=findPosY(obj_start);
	var obj_end_x=findPosX(obj_end) + getElementWidth("cart_" + id + "_end");
//	var obj_end_y=findPosY(obj_end);
	divOffer.style.height=getElementHeight("cart_" + id + "_start");
	
	document.getElementById("myOffer").value=stima;
	document.getElementById("offertaMinima").value=stima;
	document.getElementById("numLotto").value=id;
	document.getElementById("lotto").value=lotto;
	document.getElementById("asta").value=asta;

	divOffer.style.display='block';
	divOffer.style.left=obj_start_x + 1;
	divOffer.style.top=obj_start_y;
	divOffer.style.width=obj_end_x - obj_start_x;
	divOffer.style.height=getElementHeight("cart_" + id + "_start");
	document.getElementById("myOffer").select();
}

function closeDiv()
{
	divOffer.style.display='none';
}

function submitOffer()
{
	if (isNaN(document.getElementById("myOffer").value)){
		alert("Inserisci un importo valido nella casella dell'offerta.");
		return false;
	}
	
	var myOffer=parseInt(document.getElementById("myOffer").value);
	var offMinima=parseInt(document.getElementById("offertaMinima").value);
	var numLotto=parseInt(document.getElementById("numLotto").value);
	var lotto=parseInt(document.getElementById("lotto").value);
	var asta=parseInt(document.getElementById("asta").value);
	
	if (myOffer < offMinima){
		alert("La tua offerta deve essere maggiore o uguale all'offerta minima (" + offMinima + " €)");
		return false;
	}
		
	if(confirm("Confermi l'offerta di " + myOffer + " € per il lotto numero n° " + lotto + " dell'asta n°" + asta + " ?")){
		document.location.href='logica.asp?page=cart&action=addoffer&id=' + curID + '&offer=' + myOffer;
	}	
}

function confirmOffer(frm)
{
	if (frm.condizioni.checked==false){
		alert("Prima di confermare l'ordine devi accettare le condizioni delle offerte.");
		return false;
	}
		
	if (confirm('Sei sicuro di voler confermare l\'offerta?')){
		frm.subm.disabled=true;
		frm.submit();
	}
}

function delConfirm(ordine,lotto,idlotto)
{
	if(confirm("Sei sicuro di voler eliminare dall'ordine l'offerta del lotto n° " + lotto + "?"))
	{
		document.location.href='logica.asp?page=cart&action=del&orderid=' + ordine + '&id=' + idlotto;
	}
}

function shopDelConfirm(id)
{
	if(confirm("Sei sicuro di voler eliminare dal carrello l'articolo n° " + id + "?"))
	{
		document.location.href='logica.asp?page=shopcart&action=del&id=' + id;
	}
}

function confirmOrder(frm)
{
	if (frm.pagamento.value==3 && frm.pagaltro.value.length==0) {
		alert("Specificare la forma di pagamento");
		return false;
	}

	if (frm.condizioni.checked==false){
		alert("Prima di confermare l'ordine devi accettare le condizioni di vendita");
		return false;
	}
		
	if (confirm('Sei sicuro di voler confermare l\'ordine?\n'))
		frm.submit();
}

function deleteOrder(idord)
{
	if (confirm('Sei sicuro di voler annullare l\'ordine?\nProseguendo le tue offerte verranno perse.'))
		document.location.href='logica.asp?page=cart&action=clear&orderid=' + idord;
}

function clearCart()
{
	if (confirm('Sei sicuro di voler cancellare tutto il contenuto del tuo carrello?.'))
		document.location.href='logica.asp?page=shopcart&action=clear';
}

function updateCart()
{
	document.location.href='logica.asp?page=shopcart&action=update';
}

function findLotto(frm)
{
	var lotto = frm.lotto.value;
	var asta = frm.asta.value;
	
	alert(lotto);
	alert(asta);
/*	if ((isNaN(lotto)==true || isNaN(asta)==true){
		alert("Inserire il numero del lotto e dell'asta in cui ricercare");
		return false;
	}
	*/
	openPopup('schedalotto.asp?find=true&lotto=' + lotto + "&asta=" + asta,800,600,1,0);
}

function chkCart()
{
	if (document.forms[1].pagamento.selectedIndex==-1) {
		alert("Devi specificare la forma di pagamento");
		return false;
	}

	if (document.forms[1].spedizione.selectedIndex==-1) {
		alert("Devi specificare il tipo di spedizione");
		return false;
	}

	if (document.forms[1].condizioni.checked==false){
		alert("Prima di confermare l'ordine devi accettare le condizioni di vendita");
		return false;
	}
	document.forms[1].submit();
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function getElementHeight(Elem) {
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	xPos = elem.offsetHeight;
	return xPos;
}

function getElementWidth(Elem) {
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	xPos = elem.offsetWidth;
	return xPos;
}

function changeImg(obj,image)
{
	obj.src=image;
}

function openModal(url,width,height)
{	
	var arg='';
	var ret = window.showModalDialog(url,arg,"dialogHeight: " + height + "px; dialogWidth: " + width + "px; dialogTop: px; dialogLeft: px; edge: Raised; center: Yes; help: No; resizable: No; status: No; scroll: No");
}

function openPopup(url,width,height,scroll,resize){
	var rs='';
	var sc='';
	if (scroll==1)
		sc='yes';
	else
		sc='no';
	
	if (resize==1)
		rs='yes';
	else
		rs='no';
		
	var obj = window.open(url,'Popup','width=' + width + ',height=' + height + ',status=no,scrollbars=' + sc + ',resizable=' + rs + ',top=0,left=0,dependent=yes,alwaysRaised=yes');
	obj.opener = window;
	obj.focus();
}