window.onload = updateTime;

function updateTime() {

 if (document.all) {
		auctNum = document.all["TotAuctions"].className;
		aucTotNum = parseInt(auctNum);
    } else {
		auctNum = document.getElementById("TotAuctions").className;
		aucTotNum = parseInt(auctNum);
    }

  for (x=1; x<=aucTotNum; x++) {
	if (document.all) {
			dateLimit = document.all["dateEnd"+x].className;
			var costoLast5 = document.all["costoOffer_"+x].value;
			var idAsta = document.all["idSingleOffer_"+x].value;
	   } else {
			dateLimit = document.getElementById("dateEnd"+x).className;
			var costoLast5 = document.getElementById("costoOffer_"+x).value;
			var idAsta = document.getElementById("idSingleOffer_"+x).value;
	   }

		date1 = new Date();
		date2 = new Date();
		diff  = new Date();

		date1temp = new Date();
		date1.setTime(date1temp.getTime());

		date2temp = new Date(dateLimit);
		date2.setTime(date2temp.getTime());

	if (date1.getTime() <= date2.getTime()) {
		diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

		timediff = diff.getTime();

		days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
		timediff -= days * (1000 * 60 * 60 * 24);

		hours = Math.floor(timediff / (1000 * 60 * 60)); 
		timediff -= hours * (1000 * 60 * 60);

		mins = Math.floor(timediff / (1000 * 60)); 
		timediff -= mins * (1000 * 60);

		secs = Math.floor(timediff / 1000); 
		timediff -= secs * 1000;

		timediff = "<span class='timeRes'>" + days + "</span>" + "gg &nbsp;" + "<span class='timeRes'>" + hours + "</span>" + "h " + "<span class='timeRes'>" + mins + "</span>" + "m " + "<span class='timeRes'>" + secs + "</span>" + "s";
	} else {
		timediff = "0g 0h 0m 0s";
	}

	   if (timediff==="0g 0h 0m 0s") {
			this.location = 'http://www.biddestit.com/functions/update_auctions.php';
	   } else {
			if (document.all) {
				document.all["asta"+x].innerHTML = timediff;
				if (mins<5 && document.all["costoOfferVis_"+x]) document.all["costoOfferVis_"+x].innerHTML = costoLast5;
	   		} else {
				document.getElementById("asta"+x).innerHTML= timediff;
				if (mins<5 && document.getElementById("costoOfferVis_"+x)) document.getElementById("costoOfferVis_"+x).innerHTML = costoLast5;
	   		}
	  }
	
	//offerWin(idAsta);
  }
	setTimeout("updateTime()",500); //Settimeout rivaluta una funzione/espressione ogni X millisecondi.
}


// Ajax Request
function ajaxRequest() {
	var xmlHttp;
	try {  // Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();  
	}
	catch (e) {  // Internet Explorer  
		try {   
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}
  		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
    		catch (e) {
				alert("Il tuo browser non supporta AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

// Login submit
function offerWin(idAsta) {
	//alert(idAsta);
	var xmlHttp = ajaxRequest();
	var rnd = Math.floor(Math.random()*932485);
	xmlHttp.open("GET","./functions/winner.php?id="+idAsta+"&rnd="+rnd,true);
  	xmlHttp.send(null);
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			if (xmlHttp.responseText=='' || xmlHttp.responseText==null) {
				//alert("Errore!");
			} else {
				if (document.all) {
					document.all["offerWin_"+idAsta].style.background = "url(images/bollino_over.png) no-repeat";
					document.all["offerWin_"+idAsta].innerHTML = xmlHttp.responseText;
				} else {
					document.getElementById("offerWin_"+idAsta).style.background = "url(images/bollino_over.png) no-repeat";
					document.getElementById("offerWin_"+idAsta).innerHTML= xmlHttp.responseText;
				}
			}
		}
	}
	return;
}

function offerWinCover(idAsta) {
	if (document.all) {
		document.all["offerWin_"+idAsta].innerHTML = '';
		document.all["offerWin_"+idAsta].style.background = "url(images/bollino.png) no-repeat";
	} else {
		document.getElementById("offerWin_"+idAsta).innerHTML= '';
		document.getElementById("offerWin_"+idAsta).style.background = "url(images/bollino.png) no-repeat";
	}
	return;
}
