var flipflop=1;
var storedata;
var pauseopenflag;
var counterobj = new Array();

function OnloadPage()
{
    if($.browser.msie){
        //Configuring ajax
        $.ajaxSetup({
            cache: false
        });
    }

    var auctionUpdateTime = 0.5;
	var counterUpdateTime = 1000;

    auctionUpdateTime = auctionUpdateTime * 1000;

    var auctions = '';
	var prices = '';
	var auctions1 = '';

    $('.auction-item').each(function(){
        var auctionId    = $(this).attr('id');
        var auctionTitle = $(this).attr('title');
		auctions = auctions + auctionId + '=' + auctionTitle + '&';
    });

	var firstimage_bold;

	var GlobalVar = 0;
	
	if($('.index-page-class').length)
	{
		var indexpennypanel = $('.auction-penny-item').length?$('.auction-penny-item').attr('title'):'';
		var indexzeropanel = $('.auction-zero-item').length?$('.auction-zero-item').attr('title'):'';
		var indexexpresspanel = $('.auction-express-item').length?$('.auction-express-item').attr('title'):'';
		var indexsilentpanel = $('.auction-silent-item').length?$('.auction-silent-item').attr('title'):'';

		auctions = auctions + "auctionpenny_"+indexpennypanel + '=' + indexpennypanel + '&';
		auctions = auctions + "auctionzero_"+indexzeropanel + '=' + indexzeropanel + '&';
		auctions = auctions + "auctionexpress_"+indexexpresspanel + '=' + indexexpresspanel + '&';
		auctions = auctions + "auctionsilent_"+indexsilentpanel + '=' + indexsilentpanel + '&';

		setInterval(function(){
				if(auctions){
						getStatusUrl = 'updateinfo.php?flp=' + flipflop + '&tp='+displayauctiontype;
					$.ajax({
						url: getStatusUrl,
						dataType: 'json',
						type: 'POST',
						timeout: 2000,
						data: auctions,
						global: false,
						success: function(data){
								if(displayauctiontype==1)
								{
									storedata = data;
									$.each(data, function(i, item){
									auction_id = item.auction.id;
									auction_price = item.auction.price;
									auction_bidder_name = item.auction.username;
									auction_status = item.auction.time;
									
									if(auction_id==indexzeropanel || auction_id==indexexpresspanel)
									{
										if(auction_status==3)
										{
											document.getElementById('image_main_now_' + auction_id).src = allimagepath + "sold.png";
											document.getElementById('image_main_now_' + auction_id).onclick="";
											document.getElementById('image_main_now_' + auction_id).name="";
											document.getElementById('image_main_now_' + auction_id).onmouseover="";
											document.getElementById('image_main_now_' + auction_id).onmouseout="";
										}
									}
									else if(auction_id==indexsilentpanel)
									{
										if(document.getElementById('auction_left_bids_'+auction_id))
										{
											remainingbids = Number(document.getElementById('auction_left_bids_'+auction_id).innerHTML) - 1;
											if(GlobalVar==0)
											{
												document.getElementById('auction_left_bids_'+auction_id).className = Number(item.auction.bids);
											}
											else
											{
												if(Number(item.auction.bids)!=document.getElementById('auction_left_bids_'+auction_id).className)
												{
													document.getElementById('auction_left_bids_'+auction_id).className = Number(item.auction.bids);
													document.getElementById('auction_left_bids_'+auction_id).innerHTML = remainingbids;
												}
											}
										}
										if(auction_status==3)
										{
											document.getElementById('image_main_now_' + auction_id).src = allimagepath + "sold2.png";
											document.getElementById('image_main_now_' + auction_id).onclick="";
											document.getElementById('image_main_now_' + auction_id).name="";
											document.getElementById('image_main_now_' + auction_id).onmouseover="";
											document.getElementById('image_main_now_' + auction_id).onmouseout="";
										}
									}
									else
									{
										if(auction_bidder_name=="")
										{
										   auction_bidder_name="---" 
										}
										if(document.getElementById('price_index_page_' + auction_id).innerHTML != auction_price)
										{
											if(GlobalVar == 1)	
											{	
												$(document.getElementById('price_index_page_main_' + auction_id)).highlightFade({start:"#FFFFFF",speed:400,end:"#727272",final:""});
											}
											document.getElementById('price_index_page_' + auction_id).innerHTML = auction_price;
											document.getElementById('currencysymbol_' + auction_id).innerHTML = "$";
											document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;
										}
									}
									});
									GlobalVar = 1;
								}
								else if(displayauctiontype==2 || displayauctiontype==3)
								{
									storedata = data;
									$.each(data, function(i, item){
									auction_id = item.auction.id;
									auction_price = item.auction.price;
									auction_bidder_name = item.auction.username;
									auction_status = item.auction.time;
										if(auction_id==indexpennypanel)
										{
											if(auction_bidder_name=="")
											{
											   auction_bidder_name="---" 
											}
											if(document.getElementById('price_index_page_' + auction_id).innerHTML != auction_price)
											{
												if(GlobalVar == 1)	
												{	
													$(document.getElementById('price_index_page_main_' + auction_id)).highlightFade({start:"#FFFFFF",speed:400,end:"#727272",final:""});
												}
												document.getElementById('price_index_page_' + auction_id).innerHTML = auction_price;
												document.getElementById('currencysymbol_' + auction_id).innerHTML = "$";
												document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;
											}
										}
										else if(auction_id==indexsilentpanel)
										{
											if(document.getElementById('auction_left_bids_'+auction_id))
											{
												remainingbids = Number(document.getElementById('auction_left_bids_'+auction_id).innerHTML) - 1;
												if(GlobalVar==0)
												{
													document.getElementById('auction_left_bids_'+auction_id).className = Number(item.auction.bids);
												}
												else
												{
													if(Number(item.auction.bids)!=document.getElementById('auction_left_bids_'+auction_id).className)
													{
														document.getElementById('auction_left_bids_'+auction_id).className = Number(item.auction.bids);
														document.getElementById('auction_left_bids_'+auction_id).innerHTML = remainingbids;
													}
												}
											}
											if(auction_status==3)
											{
												document.getElementById('image_main_now_' + auction_id).src = allimagepath + "sold2.png";
												document.getElementById('image_main_now_' + auction_id).onclick="";
												document.getElementById('image_main_now_' + auction_id).name="";
												document.getElementById('image_main_now_' + auction_id).onmouseover="";
												document.getElementById('image_main_now_' + auction_id).onmouseout="";
											}
										}
										else
										{
											if(auction_status==3)
											{
												document.getElementById('image_main_now_' + auction_id).src = allimagepath + "sold.png"; 
												document.getElementById('image_main_now_' + auction_id).onclick="";
												document.getElementById('image_main_now_' + auction_id).name="";
												document.getElementById('image_main_now_' + auction_id).onmouseover="";
												document.getElementById('image_main_now_' + auction_id).onmouseout="";
											}											
										}
									});
									GlobalVar = 1;
								}
								else if(displayauctiontype==4)
								{
									storedata = data;
									$.each(data, function(i, item){
									auction_id = item.auction.id;
									auction_price = item.auction.price;
									auction_bidder_name = item.auction.username;
									auction_status = item.auction.time;
									
									if(auction_id==indexpennypanel)
									{
										if(auction_bidder_name=="")
										{
										   auction_bidder_name="---" 
										}
										if(document.getElementById('price_index_page_' + auction_id).innerHTML != auction_price)
										{
											if(GlobalVar == 1)	
											{	
												$(document.getElementById('price_index_page_main_' + auction_id)).highlightFade({start:"#FFFFFF",speed:400,end:"#727272",final:""});
											}
											document.getElementById('price_index_page_' + auction_id).innerHTML = auction_price;
											document.getElementById('currencysymbol_' + auction_id).innerHTML = "$";
											document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;
										}
									}
									else if(auction_id==indexzeropanel || auction_id==indexexpresspanel)
									{
										if(auction_status==3)
										{
											document.getElementById('image_main_now_' + auction_id).src = allimagepath + "sold.png";
											document.getElementById('image_main_now_' + auction_id).onclick="";
											document.getElementById('image_main_now_' + auction_id).name="";
											document.getElementById('image_main_now_' + auction_id).onmouseover="";
											document.getElementById('image_main_now_' + auction_id).onmouseout="";
										}
									}
									else
									{
										if(document.getElementById('auction_left_bids_'+auction_id))
										{
											remainingbids = Number(document.getElementById('auction_left_bids_'+auction_id).innerHTML) - 1;
											if(GlobalVar==0)
											{
												document.getElementById('auction_left_bids_'+auction_id).className = Number(item.auction.bids);
											}
											else
											{
												if(flipflop==2 && Number(item.auction.bids)!=document.getElementById('auction_left_bids_'+auction_id).className)
												{
													document.getElementById('auction_left_bids_'+auction_id).className = Number(item.auction.bids);
													document.getElementById('auction_left_bids_'+auction_id).innerHTML = remainingbids;
												}
											}
										}
										if(auction_status==3)
										{
											document.getElementById('image_main_now_' + auction_id).src = allimagepath + "sold2.png";
											document.getElementById('image_main_now_' + auction_id).onclick="";
											document.getElementById('image_main_now_' + auction_id).name="";
											document.getElementById('image_main_now_' + auction_id).onmouseover="";
											document.getElementById('image_main_now_' + auction_id).onmouseout="";
										}
									}
									});
									GlobalVar = 1;
								}
							},
							error: function(XMLHttpRequest,textStatus, errorThrown){
						}
					});
				}
				if(flipflop==1) { flipflop = 2; }
				else if(flipflop==2) { flipflop = 1; }
				ChangeCountdownData_Index(storedata,indexpennypanel);
			}, auctionUpdateTime);
	}
	else
	{
		if(displayauctiontype=="1")
		{
			setInterval(function(){
				if(auctions){
						getStatusUrl = 'update_info.php?flp=' + flipflop;
					$.ajax({
						url: getStatusUrl,
						dataType: 'json',
						type: 'POST',
						timeout: 2000,
						data: auctions,
						global: false,
						success: function(data){
							storedata = data;
							$.each(data, function(i, item){
							auction_id = item.auction.id;
							auction_price = item.auction.price;
							auction_bidder_name = item.auction.username;
	
							if(auction_bidder_name=="")
							{
							   auction_bidder_name="---" 
							}
							if(document.getElementById('prod_price_index_page_' + auction_id))
							{
								if(document.getElementById('prod_price_index_page_' + auction_id).innerHTML != auction_price)
								{
									if(GlobalVar == 1)	
									{	
										if($('.history_auctionid').length)
										{
											if(auction_id==document.getElementById('history_auctionid').innerHTML)
											{
												$(document.getElementById('price_index_page_main_' + auction_id)).highlightFade({start:"#727272",speed:400,end:"#FFFFFF",final:""});
											}
											else
											{
												$(document.getElementById('price_index_page_main_' + auction_id)).highlightFade({start:"#727272",speed:400,end:"#F9F9F9",final:""});
											}
										}
										else
										{
											$(document.getElementById('price_index_page_main_' + auction_id)).highlightFade({start:"#727272",speed:400,end:"#F9F9F9",final:""});
										}
									}
									document.getElementById('prod_price_index_page_' + auction_id).innerHTML = auction_price;
									document.getElementById('prod_currencysymbol_' + auction_id).innerHTML = "$";
									document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;
								}
							}
							else
							{
								if(document.getElementById('price_index_page_' + auction_id).innerHTML != auction_price)
								{
									if(GlobalVar == 1)	
									{	
										$(document.getElementById('price_index_page_main_' + auction_id)).highlightFade({start:"#FFFFFF",speed:400,end:"#727272",final:""});
									}
									document.getElementById('price_index_page_' + auction_id).innerHTML = auction_price;
									document.getElementById('currencysymbol_' + auction_id).innerHTML = "$";
									document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;
								}
							}
							});
							GlobalVar = 1;
							},
							error: function(XMLHttpRequest,textStatus, errorThrown){
							}
					});
				}
				if(flipflop==1) { flipflop = 2; }
				else if(flipflop==2) { flipflop = 1; }
				ChangeCountdownData(storedata);
			}, auctionUpdateTime);
		}
	
		else if(displayauctiontype=="2" || displayauctiontype=="3")
		{
			setInterval(function(){
				if(auctions){
						getStatusUrl = 'update_status.php';
					$.ajax({
						url: getStatusUrl,
						dataType: 'json',
						type: 'POST',
						timeout: 2000,
						data: auctions,
						global: false,
						success: function(data){
							imagecounter = 1;
							$.each(data, function(i, item){
								auction_id = item.auction.id;
								auction_status = item.auction.status;
								if(auction_status==3)
								{
									if($('.indexpage').length)
									{
										if(imagecounter<=8)
										{
											document.getElementById('image_main_now_' + auction_id).src = allimagepath + "bigsold.png";
										}
										else
										{
											document.getElementById('image_main_now_' + auction_id).src = allimagepath + "bigsold1.png"; 
										}
									}
									else{ document.getElementById('image_main_now_' + auction_id).src = allimagepath + "bigsold1.png"; }
									document.getElementById('image_main_now_' + auction_id).onclick="";
									document.getElementById('image_main_now_' + auction_id).name="";
									document.getElementById('image_main_now_' + auction_id).onmouseover="";
									document.getElementById('image_main_now_' + auction_id).onmouseout="";
								}
								imagecounter++;
							});
							GlobalVar = 1;
						},
					   error: function(XMLHttpRequest,textStatus, errorThrown){
					  }
					});
				}
			}, auctionUpdateTime);
		}
		else if(displayauctiontype=="4")
		{
			setInterval(function(){
				if(auctions){
						getStatusUrl = 'silent_status.php';
					$.ajax({
						url: getStatusUrl,
						dataType: 'json',
						type: 'POST',
						timeout: 2000,
						data: auctions,
						global: false,
						success: function(data){
							imagecounter = 1;
							$.each(data, function(i, item){
								auction_id = item.auction.id;
								auction_status = item.auction.status;
								if(document.getElementById('auction_left_bids_'+auction_id))
								{
									remainingbids = Number(document.getElementById('auction_left_bids_'+auction_id).innerHTML) - 1;
									if(GlobalVar==0)
									{
										document.getElementById('auction_left_bids_'+auction_id).className = Number(item.auction.bids);
									}
									else
									{
										if(Number(item.auction.bids)!=document.getElementById('auction_left_bids_'+auction_id).className)
										{
											document.getElementById('auction_left_bids_'+auction_id).className = Number(item.auction.bids);
											document.getElementById('auction_left_bids_'+auction_id).innerHTML = remainingbids;
										}
									}
								}
								if(auction_status==3)
								{
									if($('.indexpage').length)
									{
										if(imagecounter<=8){
											document.getElementById('image_main_now_' + auction_id).src = allimagepath + "sold.png";
										}else {
											document.getElementById('image_main_now_' + auction_id).src = allimagepath + "sold2.png";
										}
									}
									else if($('.history_auctionid').length)
									{
										document.getElementById('image_main_now_' + auction_id).src = allimagepath + "sold.png";
									}
									else
									{ 
										document.getElementById('image_main_now_' + auction_id).src = allimagepath + "sold2.png";
									}
									document.getElementById('image_main_now_' + auction_id).onclick="";
									document.getElementById('image_main_now_' + auction_id).name="";
									document.getElementById('image_main_now_' + auction_id).onmouseover="";
									document.getElementById('image_main_now_' + auction_id).onmouseout="";
								}
								imagecounter++;
							});
							GlobalVar = 1;
						},
					   error: function(XMLHttpRequest,textStatus, errorThrown){
					  }
					});
				}
			}, auctionUpdateTime);
		}
		}

    $('.bid-button-link').click(function(){
		urldata = $(this).attr('name').split("&");
		bidaucid = urldata[1].substr(4);
		if(document.getElementById('silentaucprice_'+bidaucid))
		{
			biddingprice = Number(document.getElementById('silentaucprice_'+bidaucid).value);
			if(biddingprice=="")
			{
				alert(pleaseenterbidprice);	
				document.getElementById('silentaucprice_'+bidaucid).focus();
				return false;
			}
			if(biddingprice<=0)
			{
				alert(biddingpricemustgreater);	
				document.getElementById('silentaucprice_'+bidaucid).focus();
				return false;
			}
			bidurl = $(this).attr('name')+"&bpr="+biddingprice;
		}
		else
		{
			bidurl = $(this).attr('name');	
		}
		$.ajax({
            url: bidurl,
            dataType: 'json',
            success: function(data){
				$.each(data, function(i, item){
				result = item.result.split("|");
				if(result[0]=="unsuccess")
				{
					if(confirm(plsrechargebid))
					{
						window.location.href='buybids.php';
					}
				}
				else if(result[0]=="loginnotavailable")
				{
					alert(sessiontimedoutmsg);
					return false;
				}
				else if(result=="auclimitreach")
				{
					alert(limitreached);
					return false;
				}
				else if(result[0]=="alreadybuynow")
				{
					alert(alreadybuynow);	
				}
				else if(result[0]=="auctioncancelled")
				{
					alert("You can't bid. This auction is cancelled by admin!");
				}
				else if(result[0]=="alreadywon")
				{
					alert(youalreadywonauc);
				}
				else if(result[0]=="bidononerbiginner")
				{
					alert(youcanbidonlyone);
				}
				else if(result=="topbid")
				{
					alert(youaretopbidder);
					return false;
				}
				else if(result[0]=="success")
				{
					if($('.history_auctionid').length)
					{
						if(document.getElementById('history_auctionid').innerHTML == bidaucid && (result[1]=="silent" || result[1]=="penny"))
						{
							ChangePlaceBids();	
						}
					}
					/* Change for all auction types start here */
					obj = document.getElementById('bids_count');
					objvalue = document.getElementById('bids_count').innerHTML;
					if(obj.innerHTML!='0')
					{
						if(result[1]=="silent")
						{
							obj.innerHTML = Number(objvalue) - Number(result[2]);
						}
						else
						{
							obj.innerHTML = Number(Number(objvalue) - Number(1));
						}
					}
					if(result[1]=="zero")
					{
						var definecounterarray = counterobj.length+1;
						document.getElementById("price_index_page_"+result[3]).innerHTML = "&nbsp;"+result[2]+"&nbsp;";
						document.getElementById('express_counter_'+result[3]).style.display = 'block';
						document.getElementById("image_main_now_"+result[3]).style.display = "none";
						if(result[2]=="0.00")
						{
							document.getElementById("image_main_buynow_"+result[3]).style.display = "block";
							document.getElementById("price_left_"+result[3]).style.backgroundImage = "none";
							document.getElementById("price_right_"+result[3]).style.backgroundImage = "none";
						}
						else
						{
							document.getElementById('image_main_press_'+result[3]).style.display = 'block';	
						}
						if(document.getElementById("history_auctionid") && document.getElementById("history_auctionid").innerHTML==result[3])
						{
								document.getElementById("detail_price_"+result[3]).innerHTML = "&nbsp;"+result[2];
								placebids = document.getElementById('placebidscount').innerHTML;
								placeamount = document.getElementById('placebidsamounthid').innerHTML;
								document.getElementById('placebidscount').innerHTML = Number(placebids) + Number(1);
								document.getElementById('placebidsamount').innerHTML = 	number_format(Number(placeamount) + Number(front_min_price),2,'.','');
								document.getElementById('placebidsamounthid').innerHTML = 	number_format(Number(placeamount) + Number(front_min_price),2,'.','');
								newsavingvalue =  Number(document.getElementById('placebidssaving').innerHTML) - Number(front_min_price) - Number(result[2].substr(1));
								document.getElementById('placebidssaving').innerHTML = number_format(newsavingvalue,'2','.','');
								document.getElementById('placebidssavingdisp').innerHTML = number_format(newsavingvalue,'2','.','');
						}
						
						SetTimerIntervalforAuction(result[3],definecounterarray);
						
						ChangeBiddingTimer = setTimeout("ChangePriceLabel('"+result[3]+"','"+definecounterarray+"','"+result[1]+"')",(express_zero_time_limit*1000));
					}
					else if(result[1]=="express")
					{
						var definecounterarray = counterobj.length+1;
						document.getElementById("price_index_page_"+result[3]).innerHTML = "&nbsp;"+result[2]+"&nbsp;";
						if(document.getElementById("history_auctionid") && document.getElementById("history_auctionid").innerHTML==result[3])
						{
							document.getElementById("price_index_page_"+result[3]).style.color = "#000000";
							document.getElementById("price_index_page_"+result[3]).style.fontSize = "22";
						}
						else
						{
							document.getElementById("price_index_page_"+result[3]).style.color = "#11B9FF";
						}
						document.getElementById("image_main_now_"+result[3]).style.display = "none";
						document.getElementById("image_main_buynow_"+result[3]).style.display = "block";
						document.getElementById("price_left_"+result[3]).style.backgroundImage = "none";
						document.getElementById("price_right_"+result[3]).style.backgroundImage = "none";
						document.getElementById("price_index_page_"+result[3]).style.backgroundImage = "none";
						document.getElementById("price_index_page_"+result[3]).style.backgroundImage = "none";
						document.getElementById('express_counter_'+result[3]).style.display = 'block';
						if(document.getElementById("history_auctionid") && document.getElementById("history_auctionid").innerHTML==result[3])
						{
							document.getElementById("detail_price_"+result[3]).innerHTML = "&nbsp;"+result[2];
							placebids = document.getElementById('placebidscount').innerHTML;
							placeamount = document.getElementById('placebidsamounthid').innerHTML;
							document.getElementById('placebidscount').innerHTML = Number(placebids) + Number(1);
							document.getElementById('placebidsamount').innerHTML = 	number_format(Number(placeamount) + Number(front_min_price),2,'.','');
							document.getElementById('placebidsamounthid').innerHTML = 	number_format(Number(placeamount) + Number(front_min_price),2,'.','');
							newsavingvalue =  Number(Number(document.getElementById('placebidssaving').innerHTML) - Number(front_min_price) - Number(result[2].substr(1)));
							document.getElementById('placebidssaving').innerHTML = number_format(newsavingvalue,'2','.','');
							document.getElementById('placebidssavingdisp').innerHTML = number_format(newsavingvalue,'2','.','');
						}
						
						SetTimerIntervalforAuction(result[3],definecounterarray);
						
						ChangeBiddingTimer = setTimeout("ChangePriceLabel('"+result[3]+"','"+definecounterarray+"','"+result[1]+"')",(express_zero_time_limit*1000));
					}
					else if(result[1]=="silent")
					{
						document.getElementById('silentaucprice_'+result[3]).value = '';	
						//document.getElementById('auction_left_bids_'+result[3]).innerHTML = Number(document.getElementById('auction_left_bids_'+result[3]).innerHTML) - 1;
						alert(yourbidplacedsuccess);
						return false;
					}
					/* Chagne for all auction type end here */
				}
			});
            },
			error: function(XMLHttpRequest,textStatus, errorThrown){
			}
        });

        return false;
    });

	if($('.productImageThumb').length){
    setInterval(function(){
		auctionhisid = document.getElementById('history_auctionid').innerHTML;
		oldprice = document.getElementById('curproductprice').innerHTML;
		newprice = document.getElementById('prod_price_index_page_' + auctionhisid).innerHTML;
		if(oldprice!=newprice)
		{
			getStatusUrl3 = 'updatehistory.php?aucid_new='+auctionhisid;
		$.ajax({
			url: getStatusUrl3,
			dataType: 'json',
			success: function(data){
	
				data1 = eval('(' + data.responseText + ')');
				for(i=0;i<data.histories.length;i++)
				{
					biddingprice = data.histories[i].history.bprice;
					biddingusername = data.histories[i].history.username;
					biddingtype = data.histories[i].history.bidtype;
	
					document.getElementById('bid_price_' + i).innerHTML = "$" + biddingprice;
					document.getElementById('bid_user_name_' + i).innerHTML = biddingusername;
	
					if(biddingtype=='s')
					{
						document.getElementById('bid_type_' + i).innerHTML = placesinglebid;
					}
					else if(biddingtype=='b')
					{
						document.getElementById('bid_type_' + i).innerHTML = placebidbuddy;
					}
					else if(bidding_type=='m')
					{
						document.getElementById('bid_type_' + i).innerHTML = placesmsbid;
					}
				}
				
				if(data.myhistories.length)
				{
					for(j=0;j<data.myhistories.length;j++)
					{
						biddingprice1 = data.myhistories[j].myhistory.bprice;
						biddingusername1 = data.myhistories[j].myhistory.time;
						biddingtype1 = data.myhistories[j].myhistory.bidtype;
	
						document.getElementById('my_bid_price_' + j).innerHTML = "$" + biddingprice1;
						document.getElementById('my_bid_time_' + j).innerHTML = biddingusername1;
		
						if(biddingtype1=='s')
						{
							document.getElementById('my_bid_type_' + j).innerHTML = placesinglebid;
						}
						else if(biddingtype1=='b')
						{
							document.getElementById('my_bid_type_' + j).innerHTML = placebidbuddy;
						}
						else if(biddingtype1=='m')
						{
							document.getElementById('my_bid_type_' + j).innerHTML = placesmsbid;
						}
					}
				}
				document.getElementById('curproductprice').innerHTML = data.histories[0].history.bprice;
			},
			error: function(XMLHttpRequest,textStatus, errorThrown){
	//			alert(textStatus);	
			}
			   });
		}
	 },counterUpdateTime);
    }

    $('.bookbidbutlerbutton').click(function(){
	if(document.getElementById('bookbidbutlerbutton').name!="")
	{
		var bidbutstartprice;
		var bidbutendprice;
		var totalbids;
		
		
		bidbutstartprice1 = Number(document.bidbutler.bidbutstartprice.value);
		bidbutendprice1 = Number(document.bidbutler.bidbutendprice.value);

		bidbutstartprice = bidbutstartprice1;
		bidbutendprice = bidbutendprice1;
		totalbids = document.bidbutler.totalbids.value;

		if(bidbutstartprice=="")
		{
			alert(entbutsprice);
			return false;

		}
		if(bidbutendprice=="")
		{
			alert(entbuteprice);
			return false;
		}
		if(totalbids=="")
		{
			alert(entbutbids);
			return false;
		}
		if(totalbids<=1)
		{
			alert(entmoreone);
			return false;
		}
		if(bidbutstartprice>bidbutendprice)
		{
			alert(spricegreat);
			return false;
		}
		if(bidbutstartprice==bidbutendprice)
		{
			alert(endpricegreat);
			return false;
		}

		$.ajax({
            url: "addbidbutler.php?aid="+$(this).attr('name')+"&bidsp="+bidbutstartprice+"&bidep="+bidbutendprice+"&totb="+totalbids,
            dataType: 'json',
            success: function(data){
				$.each(data, function(i, item){
				result = item.result;
				if(result=="unsuccessprice")
				{
					alert(bidfromvalueismust);
					return false;
				}
				else if(result=="alreadybuynow")
				{
					alert(alreadybuynow);	
					return false;
				}
				else if(result=="auctioncancelled")
				{
					alert("You can't bid. This auction is cancelled by admin!");
					return false;
				}
				else if(result=="unsuccess")
				{
					alert(plsrechargebid);
					return false;
				}
				else if(result=="bidononerbiginner")
				{
					alert(youcanbidonlyone);
					return false;
				}
				else if(result=="auclimitreach")
				{
					alert(limitreached);
					return false;
				}
				else if(result=="alreadywon")
				{
					alert(youalreadywonauc);
					return false;
				}
				else
				{
					document.bidbutler.bidbutstartprice.value="";
					document.bidbutler.bidbutendprice.value="";
					document.bidbutler.totalbids.value="";
					document.getElementById('butlermessage').style.display='block';
					changeMessageTimer = setInterval("ChangeButlerImageSecond()",3000);
					changedatabutler(data,"abut",totalbids);
				}
			  });
			},
			error: function(XMLHttpRequest,textStatus, errorThrown){
//				alert(textStatus);
			}
        });

        return false;
	 }
    });
}

function DeleteBidButler(id,div_id)
{
	$.ajax({
		url: url = "deletebutler.php?delid=" + id,
		dataType: 'json',
		success: function(data){
		$.each(data, function(i, item){
			result = item.result;
			if(result=="unsuccess")
			{
				alert(youbidisrunning);
			}
			else
			{
				placebids = document.getElementById('butlerbids_' + div_id).innerHTML;
				objbids = document.getElementById('bids_count');
				objbidsvalue = document.getElementById('bids_count').innerHTML;
				if(objbids.innerHTML!='0')
				{
					objbids.innerHTML = Number(objbidsvalue) + Number(placebids);
				}
				changedatabutler(data,"dbut","");
			}
		});
	},
		error: function(XMLHttpRequest,textStatus, errorThrown){
//				alert(textStatus);
		}
	});
	return false;
}

function ChangeCountdownData(resdata)
{
	if(resdata && resdata!="")
	{
		data = resdata;
		pt=1;
		$.each(data, function(i, item){
		auction_id = item.auction.id;
		auction_time = item.auction.time;
		pausestatus = item.auction.pause;
//		alert(auction_id + "===" + auction_time + "===" + pausestatus);
		if(auction_time)
		{
			if(auction_time=='0')
			{
				if($('.history_auctionid').length)
				{
					if(document.getElementById('history_auctionid').innerHTML==auction_id)
					{
						auctionprefix = "";
						document.getElementById('bookbidbutlerbutton').name = "";
					}
					else
					{
						auctionprefix = "";
					}
				}
				else
				{
					auctionprefix = "";	
				}
				if(auctionprefix=="")
				{
					document.getElementById(auctionprefix+'counter_index_page_' + auction_id).style.color = '#000000';	
				}
				document.getElementById(auctionprefix+'counter_index_page_' + auction_id).innerHTML = printended;					
				document.getElementById('image_main_' + auction_id).onclick="";
				document.getElementById('image_main_' + auction_id).name="";
				document.getElementById('image_main_' + auction_id).onmouseover="";
				document.getElementById('image_main_' + auction_id).onmouseout="";
				if($('.history_auctionid').length)
				{ 	
					if(document.getElementById('history_auctionid').innerHTML==auction_id)
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + "soldw.png";
					}	
					else
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + "sold.png";
					}
				}
				else
				{
					document.getElementById('image_main_' + auction_id).src = allimagepath + "sold2.png";
				}
			}
			else if(pausestatus==1)
			{
				document.getElementById('counter_index_page_' + auction_id).innerHTML = printpause;
				document.getElementById('image_main_' + auction_id).src = allimagepath + "bidbut.png";
				document.getElementById('image_main_' + auction_id).onclick="";
				document.getElementById('image_main_' + auction_id).onmouseover="";
				document.getElementById('image_main_' + auction_id).name="";							
				document.getElementById('image_main_' + auction_id).onmouseout="";
				pauseopenflag = 1;
			}
			else
			{
				if(auction_time<10)
				{
					
					if(document.getElementById('prod_counter_index_page_' + auction_id))
					{
						document.getElementById('prod_counter_index_page_' + auction_id).style.color = '#E80000';	
						document.getElementById('prod_counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
					}
					else
					{
						document.getElementById('counter_index_page_' + auction_id).style.color = '#E80000';	
						document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);	
					}
				}
				else
				{
					if(document.getElementById('prod_counter_index_page_' + auction_id))
					{
						document.getElementById('prod_counter_index_page_' + auction_id).style.color = '#000000';	
						document.getElementById('prod_counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);	
					}
					else
					{
						document.getElementById('counter_index_page_' + auction_id).style.color = '#000000';	
						document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time)
					}
				}
				if(pauseopenflag==1)
				{
					document.getElementById('image_main_' + auction_id).src = allimagepath + "bidbut.png";
					if(pt==data.length)
					{
						pauseopenflag = 0;
					}
				}
			}
		}
		pt++;
	  }
	  );
			
	}
}

function ChangeCountdownData_Index(resdata,pennyauctionid)
{
	if(resdata && resdata!="")
	{
		data = resdata;
		pt=1;
		$.each(data, function(i, item){
		auction_id = item.auction.id;
		auction_time = item.auction.time;
		pausestatus = item.auction.pause;

		if(auction_time && document.getElementById('counter_index_page_' + auction_id))
		{
			if(auction_time=='0')
			{
				if($('.history_auctionid').length)
				{
					if(document.getElementById('history_auctionid').innerHTML==auction_id)
					{
						auctionprefix = "";
						document.getElementById('bookbidbutlerbutton').name = "";
					}
					else
					{
						auctionprefix = "";
					}
				}
				else
				{
					auctionprefix = "";	
				}
				if(auctionprefix=="")
				{
					if(document.getElementById('auction_'+auction_id).className=='auction-penny-item')
					{
						document.getElementById(auctionprefix+'counter_index_page_' + auction_id).style.color = '#000000';		
					}
					else
					{
						document.getElementById(auctionprefix+'counter_index_page_' + auction_id).style.color = '#000000';			
					}
				}
				document.getElementById(auctionprefix+'counter_index_page_' + auction_id).innerHTML = printended;					
				document.getElementById('image_main_' + auction_id).onclick="";
				document.getElementById('image_main_' + auction_id).name="";
				document.getElementById('image_main_' + auction_id).onmouseover="";
				document.getElementById('image_main_' + auction_id).onmouseout="";
				if($('.history_auctionid').length)
				{ 	
					if(document.getElementById('history_auctionid').innerHTML==auction_id)
					{
						if(document.getElementById('auction_'+auction_id).className=='auction-penny-item')
						{	
							document.getElementById('image_main_' + auction_id).src = allimagepath + "bigsold.png";
						}
						else
						{
							document.getElementById('image_main_' + auction_id).src = allimagepath + "soldw.png";	
						}
					}	
					else
					{
						if(document.getElementById('auction_'+auction_id).className=='auction-penny-item')
						{	
							document.getElementById('image_main_' + auction_id).src = allimagepath + "bigsold.png";
						}
						else
						{
							document.getElementById('image_main_' + auction_id).src = allimagepath + "sold.png";		
						}
					}
				}
				else
				{
					if(document.getElementById('auction_'+auction_id).className=='auction-penny-item')
					{	
						document.getElementById('image_main_' + auction_id).src = allimagepath + "bigsold.png";
					}
					else
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + "sold2.png";	
					}
				}
			}
			else if(pausestatus==1)
			{
				document.getElementById('counter_index_page_' + auction_id).innerHTML = printpause;
				document.getElementById('image_main_' + auction_id).src = allimagepath + "bidbut.png";
				document.getElementById('image_main_' + auction_id).onclick="";
				document.getElementById('image_main_' + auction_id).onmouseover="";
				document.getElementById('image_main_' + auction_id).name="";							
				document.getElementById('image_main_' + auction_id).onmouseout="";
				pauseopenflag = 1;
			}
			else
			{
				if(auction_time<10)
				{
					
					if(document.getElementById('prod_counter_index_page_' + auction_id))
					{
						document.getElementById('prod_counter_index_page_' + auction_id).style.color = '#E80000';	
						document.getElementById('prod_counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
					}
					else
					{
						document.getElementById('counter_index_page_' + auction_id).style.color = '#E80000';	
						document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);	
					}
				}
				else
				{
					if(auction_id==pennyauctionid)
					{
						document.getElementById('counter_index_page_' + auction_id).style.color = '#000000';	
						document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
					}
					else
					{
						document.getElementById('counter_index_page_' + auction_id).style.color = '#000000';	
						document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
					}
				}
				if(pauseopenflag==1)
				{
					document.getElementById('image_main_' + auction_id).src = allimagepath + "bidbut.png";
					if(pt==data.length)
					{
						pauseopenflag = 0;
					}
				}
			}
		}
		pt++;
	  }
	  );
			
	}
}

function GetConfirmation(auctionid)
{
	window.location.href = "confirmation.php?aid="+auctionid;	
}

function ChangePriceLabel(auctionid,defarrayele,auctionimgtype)
{
	document.getElementById("price_index_page_"+auctionid).innerHTML = "???";
	document.getElementById("price_index_page_"+auctionid).style.color = "#000000";
	document.getElementById("image_main_now_"+auctionid).style.display = "block";
	document.getElementById("image_main_buynow_"+auctionid).style.display = "none";
	if(auctionimgtype=="zero")
	{
		document.getElementById('image_main_press_'+auctionid).style.display = 'none';
	}
	if(document.getElementById('express_counter_'+auctionid))
	{
		document.getElementById('express_counter_'+auctionid).style.display = 'none';
		document.getElementById('express_counter_'+auctionid).innerHTML = 10;
	}
	clearInterval(counterobj[defarrayele]);
	if((document.getElementById("history_auctionid") && document.getElementById("history_auctionid").innerHTML==auctionid))
	{
		document.getElementById("price_left_"+auctionid).style.backgroundImage = "url('images/priceleft21.png')";
		document.getElementById("price_right_"+auctionid).style.backgroundImage = "url('images/priceright21.png')";
		document.getElementById("price_index_page_"+auctionid).style.backgroundImage = "url('images/pricegred.png')";
		document.getElementById("price_index_page_"+result[3]).style.fontSize = "18";
	}
	else
	{
		document.getElementById("price_left_"+auctionid).style.backgroundImage = "url('images/priceleft2.png')";
		document.getElementById("price_right_"+auctionid).style.backgroundImage = "url('images/priceright2.png')";
		document.getElementById("price_index_page_"+auctionid).style.backgroundImage = "url('images/pricegred.png')";
	}

	if(document.getElementById("history_auctionid") && document.getElementById("history_auctionid").innerHTML==auctionid)
	{
			document.getElementById("detail_price_"+auctionid).innerHTML = "???";
			newsavingvalue = Number(document.getElementById('currentaucprice').innerHTML) - Number(document.getElementById('placebidsamounthid').innerHTML);
			document.getElementById('placebidssaving').innerHTML = number_format(newsavingvalue,'2','.','');
			document.getElementById('placebidssavingdisp').innerHTML = number_format(newsavingvalue,'2','.','');

	}

	$.ajax({
		url: "updatestatus.php?aid="+auctionid,
		dataType: 'json',
		success: function(data){
			$.each(data, function(i, item){
					result = item.result;
				});
			}
		});
}
function SetTimerIntervalforAuction(auctionid,definearrayele)
{
	counterobj[definearrayele] = setInterval(function()
	{
		if(Number(document.getElementById('express_counter_'+auctionid).innerHTML)<=10)
		{
			document.getElementById('express_counter_'+auctionid).innerHTML = "0"+(Number(document.getElementById('express_counter_'+auctionid).innerHTML) - 1);
		}
		else
		{
			document.getElementById('express_counter_'+auctionid).innerHTML = (Number(document.getElementById('express_counter_'+auctionid).innerHTML) - 1);
		}
	},10*100);
}