var timer_started = false;
var post_params = '';
var LockedInputs = new Array();
var isloggedin = false;


function SetContent(element,content)
{
    document.getElementById(element).innerHTML = content;
}
function GetContent(element)
{
  return document.getElementById(element).innerHTML;
}

var placeBidcallbacks = {
    success: function (o) {
    response = YAHOO.lang.JSON.parse(o.responseText)
    if (response[0]==false)
    {
      alert(response[2]);
      }
},
failure: function (o) {
    alert("Async call failed!");
},
timeout : 3000
}

function placeBid(listing_id)
{
if (isloggedin==true)
{
 _url = '/auction_up/place/bid/'+listing_id+'/'+document.getElementById('bid_input_'+listing_id).value;
 YAHOO.util.Connect.asyncRequest("POST", _url, placeBidcallbacks);
}
else
{
   YAHOO.example.container.logindialog.show();
}
}


function placeBid1(listing_id)
{
if (isloggedin==true)
{
 _url = '/auction_down/place/bid/'+listing_id+'/0';
 YAHOO.util.Connect.asyncRequest("POST", _url, placeBidcallbacks);
}
else
{
   YAHOO.example.container.logindialog.show();
}
}

function placeBid2(listing_id)
{
if (isloggedin==true)
{
 _url = '/auction_multi_down/place/bid/'+listing_id+'/0';
 YAHOO.util.Connect.asyncRequest("POST", _url, placeBidcallbacks);
}
else
{
   YAHOO.example.container.logindialog.show();
}
}

var placeAutoBidCallBacks = {
    success: function (o) {
    alert("AutoBid Placed!");
},
failure: function (o) {
    alert("Async call failed!");
},
timeout : 3000
}
var callbacks = {
    success: function (o) {
      response = YAHOO.lang.JSON.parse(o.responseText)
      //setTimeout("GetAuctions()",1000);
      isloggedin = response.l;
      for (i in response.a)
	  {
	    if (response.a[i].T=='up')
	    {
	    	    document.getElementById('bid_' + response.a[i].l).innerHTML = response.a[i].b;
		    n_b =(parseFloat(response.a[i].b) + parseFloat(response.a[i].i)).toFixed(2);
		    bid_input = document.getElementById('bid_input_' + response.a[i].l);
		    if (bid_input.value == '')
			bid_input.value = '0';
		    if (!LockedInputs[response.a[i].l] & parseFloat(document.getElementById('bid_input_' + response.a[i].l).value)<parseFloat(n_b))
			    document.getElementById('bid_input_' + response.a[i].l).value = n_b;

		    if (response.a[i].w=='')
		     {
		      document.getElementById('winner_' + response.a[i].l).innerHTML = 'no winner';
		     }
		    else
		    {
			document.getElementById('winner_' + response.a[i].l).innerHTML = response.a[i].w;
		    }
		    if (response.a[i].c == '00:00:00')
		    {
			if (response.a[i].w=='')
			    document.getElementById('time_' + response.a[i].l).innerHTML = 'CLOSED';
			else
			    document.getElementById('time_' + response.a[i].l).innerHTML = 'SOLD';
			post_params = post_params.replace(response.a[i].l + ',', '')
		    }
		    else
			document.getElementById('time_' + response.a[i].l).innerHTML = response.a[i].c;
		   if (document.getElementById('closing_on_' + response.a[i].l)!=null)
		    document.getElementById('closing_on_' + response.a[i].l).innerHTML = response.a[i].e;
	    }

	    if (response.a[i].T=='multi_down')
	    {
	    	var e = document.getElementById('qty_wb');
	    	if (e!=null)
		{
			e.innerHTML = 'Quantity';
		}
	        document.getElementById('bid_' + response.a[i].l).innerHTML = response.a[i].pl;

		document.getElementById('winner_' + response.a[i].l).innerHTML = 'Qty: ' + (response.a[i].q - response.a[i].sq).toString();
		    if (response.a[i].tl == '00:00:00')
		    {
			    document.getElementById('time_' + response.a[i].l).innerHTML = 'CLOSED';
			    post_params = post_params.replace(response.a[i].l + ',', '')
		    }
		    else if (response.a[i].q == response.a[i].sq)
		    {
			    document.getElementById('time_' + response.a[i].l).innerHTML = 'SOLD';
			if (e!=null)
			{
				e.innerHTML = 'Winning Bidder';
			}
       			    document.getElementById('winner_' + response.a[i].l).innerHTML = response.a[i].w;
			    post_params = post_params.replace(response.a[i].l + ',', '')
		    }
		    else
			document.getElementById('time_' + response.a[i].l).innerHTML = response.a[i].tl;
		   if (document.getElementById('closing_on_' + response.a[i].l)!=null)
		    document.getElementById('closing_on_' + response.a[i].l).innerHTML = response.a[i].t2;
	    }
	  }
      setTimeout("GetAuctions()",1000);
 },
    failure: function (o) {
    //alert("Async call failed!");
      setTimeout("GetAuctions()",10000);
},
timeout : 3000
}

function GetAuctions()
{
  if (post_params!='')
    YAHOO.util.Connect.asyncRequest("GET", "/listings/" + new Date().getTime() +"?n="+post_params, callbacks);
}

function addListing(listing_id)
{
  post_params = post_params+listing_id+',';
  //if (!timer_started)
  //{
//    setTimeout("GetAuctions()",1000);
//    timer_started = true;
//  }
  LockedInputs[listing_id] = false;
}

function lockInputField(listing_id)
{
  LockedInputs[listing_id] = true;
}

function unlockInputField(listing_id)
{
  LockedInputs[listing_id] = false;
}
