/*  AShop
 *  Copyright 2002-2009 - All Rights Reserved Worldwide
 *  http://www.ashopsoftware.com
 *  This software is licensed per individual site.
 *  By installing or using this software, you agree to the licensing terms,
 *  which are located at http://www.ashopsoftware.com/license.htm
 *  Unauthorized use or distribution of this software 
 *  is a violation U.S. and international copyright laws.
 *--------------------------------------------------------------------------*/

function updatebid(ajaxRequest) {
	var time = new Date().getTime();
	time = time/1000;
	time = time-timediff;
	time = Math.round(time);
	parameters = ajaxRequest.responseText;
	parametersarray = parameters.split('|');
	productid = parseInt(parametersarray[0]);
	newstarttime = parseInt(parametersarray[1]);
	if (!newstarttime) newstarttime = 0;
	newprice = parametersarray[3];
	newscreenname = parametersarray[5];
	if (newstarttime == 0 || newstarttime != starttime[productid]) starttime[productid] = newstarttime;
	$('price'+productid).update('$'+newprice);
	$('screenname'+productid).update(newscreenname);
}

function isIE()
{
  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function countdown() {
	var time = new Date().getTime();
	time = time/1000;
	time = time-timediff;
	time = Math.round(time);
	IDs.each(function(productid) {
		if (activated[productid] == 1) {
			var seconds = time - starttime[productid];
			var secondsleft = fplength[productid] - seconds;
			if (secondsleft > fplength[productid]) secondsleft = fplength[productid];
			if (starttime[productid] <= 0) secondsleft = fplength[productid];
			if (secondsleft <= 0) {
				$('countdown'+productid).update('SOLD!');
				$('bidbutton'+productid).update('');
			} else {
				if (secondsleft < 10) pad = '0000';
				else if (secondsleft < 100) pad = '000';
				else if (secondsleft < 1000) pad = '00';
				else if (secondsleft < 10000) pad = '0';
				if (secondsleft <= 10) $('countdown'+productid).update('<font color=red>'+pad+secondsleft+'</font>');
				else $('countdown'+productid).update(pad+secondsleft);
			}
			var myAjax = new Ajax.Request(
				'admin/bidengine.php', 
				{
					method: 'get', 
					parameters: 'productid='+productid+'&dummy='+ new Date().getTime(), 
					onSuccess: updatebid
				}
			);
		} else {
			if (time >= activatetime[productid]) {
				activated[productid] = 1;
				$('bidbutton'+productid).style.display='block';
				$('price'+productid).style.display='block';
				$('screenname'+productid).style.display='block';
			}
		}
	});
}

function placebid(productid,bidder) {
	var myAjax = new Ajax.Request(
		'admin/bidengine.php', 
		{
			method: 'get', 
			parameters: 'placebid='+productid+'&bidder='+bidder+'&dummy='+ new Date().getTime(),
			onSuccess: updatebid
		}
	);
	var bids = parseInt($('bidsinfo').innerHTML);
	if (bids > 0) bids--;
	$('bidsinfo').update(bids);
}