﻿sprintfWrapper={init:function()
	{
	if(typeof arguments=="undefined")
		{
			return null
		}
	if(arguments.length<1)
		{
			return null
		}
	if(typeof arguments[0]!="string")
		{
			return null
		}
		if(typeof RegExp=="undefined")
			{
				return null
			}
		var I=arguments[0];
		var C=new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
		var G=new Array();
		var K=new Array();
		var A=0;
		var H=0;
		var L=0;
		var D=0;
		var J="";
		var F=null;
			while(F=C.exec(I))
				{
					if(F[9])
						{
							A+=1
						}
					H=D;
					L=C.lastIndex-F[0].length;
					K[K.length]=I.substring(H,L);
					D=C.lastIndex;
					G[G.length]={match:F[0],left:F[3]?true:false,sign:F[4]||"",pad:F[5]||" ",min:F[6]||0,precision:F[8],code:F[9]||"%",negative:parseInt(arguments[A])<0?true:false,argument:String(arguments[A])}
				}
			K[K.length]=I.substring(D);
			if(G.length==0)
				{
					return I
				}
			if((arguments.length-1)<A)
				{
					return null
				}
		var B=null;
		var F=null;
		var E=null;
				
		for(E=0;E<G.length;E++)
			{
				if(G[E].code=="%")
					{
						substitution="%"
					}
				else
					{
						if(G[E].code=="b")
							{
								G[E].argument=String(Math.abs(parseInt(G[E].argument)).toString(2));substitution=sprintfWrapper.convert(G[E],true)
							}
						else
							{
								if(G[E].code=="c")
									{
										G[E].argument=String(String.fromCharCode(parseInt(Math.abs(parseInt(G[E].argument)))));
										substitution=sprintfWrapper.convert(G[E],true)
									}
								else
									{
										if(G[E].code=="d")
											{
												G[E].argument=String(Math.abs(parseInt(G[E].argument)));
												substitution=sprintfWrapper.convert(G[E])
											}
										else
											{
												if(G[E].code=="f")
													{
														G[E].argument=String(Math.abs(parseFloat(G[E].argument)).toFixed(G[E].precision?G[E].precision:6));
														substitution=sprintfWrapper.convert(G[E])
													}
												else
													{
														if(G[E].code=="o")
															{
																G[E].argument=String(Math.abs(parseInt(G[E].argument)).toString(8));
																substitution=sprintfWrapper.convert(G[E])
															}
														else
															{
																if(G[E].code=="s")
																	{
																		G[E].argument=G[E].argument.substring(0,G[E].precision?G[E].precision:G[E].argument.length);
																		substitution=sprintfWrapper.convert(G[E],true)
																	}
																else
																	{
																		if(G[E].code=="x")
																			{
																				G[E].argument=String(Math.abs(parseInt(G[E].argument)).toString(16));
																				substitution=sprintfWrapper.convert(G[E])
																			}
																		else
																			{
																				if(G[E].code=="X")
																					{
																						G[E].argument=String(Math.abs(parseInt(G[E].argument)).toString(16));
																						substitution=sprintfWrapper.convert(G[E]).toUpperCase()
																					}
																				else
																					{
																						substitution=G[E].match
																					}
																			}
																	}
															}
													}
											}
									}
							}
					}
				J+=K[E];
				J+=substitution
			}
		J+=K[E];
		return J
	},convert:function(B,D)
		{
			if(D)
				{
					B.sign=""
				}
			else
				{
					B.sign=B.negative?"-":B.sign
				}
			var A=B.min-B.argument.length+1-B.sign.length;
			var C=new Array(A<0?0:A).join(B.pad);
			if(!B.left)
				{
					if(B.pad=="0"||D)
						{
							return B.sign+C+B.argument
						}
					else
						{
							return C+B.sign+B.argument
						}
				}
			else
				{
					if(B.pad=="0"||D)
						{
							return B.sign+B.argument+C.replace(/0/g," ")
						}
					else
						{
							return B.sign+B.argument+C
						}
				}
		}
};
sprintf=sprintfWrapper.init;
var pageTimer=0;

function showTime(C)
	{
		if(C>0)
			{
				var D=parseInt(C/86400);
				C=C-(D*86400);
				var B=parseInt(C/3600);
				C=C-(B*3600);
				var A=parseInt(C/60);
				C=C-(A*60);
				if(D>=1){return sprintf("%d",D)+"<font class=bidtitle10> days </font>"+sprintf("%02d",B)+":"+sprintf("%02d",A)+":"+sprintf("%02d",C)}
				if(B>=1){return sprintf("%02d",B)+"<font class=bidtitle10> hrs </font>"+sprintf("%02d",A)+"<font class=bidtitle10> mins </font>"+sprintf("%02d",C)+"<font class=bidtitle10> secs</font>"}
				if(A>1){return sprintf("%02d",A)+"<font class=bidtitle10> minutes </font>"+sprintf("%02d",C)+"<font class=bidtitle10> seconds</font>"}
				if(A==1){return sprintf("%02d",A)+"<font class=bidtitle10> minute </font>"+sprintf("%02d",C)+"<font class=bidtitle10> seconds</font>"}
				if(C>=10){return sprintf("%02d",C)+"<font class=bidtitle10> seconds</font>"}
				else{return "<font color=#ff0000>"+sprintf("%02d",C)+"</font><font class=bidtitle11> seconds</font>"}
			}
		else
			{
				return""
			}
	}

function showCur(A)
	{
		return sprintf("%.2f",A)
	}

function updatePage()
	{
		for(var C=0;C<data.length;C++)
			{
				document.getElementById("time"+data[C][0]).innerHTML=((data[C][8]==3)?"ENDED":(data[C][8]==4)?"PAUSED":showTime(data[C][1]));
				document.getElementById("price"+data[C][0]).innerHTML=showCur(data[C][2]*currRate);
				if(document.getElementById("basePrice"+data[C][0]))
					{
						document.getElementById("basePrice"+data[C][0]).innerHTML=showCur(data[C][2])
					}
				document.getElementById("bidder"+data[C][0]).innerHTML=data[C][4];
				if(document.getElementById("bidderB"+data[C][0]))
					{
						document.getElementById("bidderB"+data[C][0]).innerHTML=data[C][11];
						document.getElementById("bidderC"+data[C][0]).innerHTML=data[C][12];
					}
				if (document.getElementById("bid_btn_"+data[C][0])!=null)
				document.getElementById("bid_btn_"+data[C][0]).innerHTML=((data[C][8]==3)?"<IMG SRC='images/sold.gif' BORDER='0'>":(data[C][8]==4)?"<IMG SRC='images/paused.gif' BORDER='0'>":"<IMG SRC='images/placebid2.gif' BORDER='0'>");
				
				if (document.getElementById("bid_sbtn_"+data[C][0])!=null)
				document.getElementById("bid_sbtn_"+data[C][0]).innerHTML=((data[C][8]==3)?"<IMG SRC='images/sold2.gif' BORDER='0'>":(data[C][8]==4)?"<IMG SRC='images/paused2.gif' BORDER='0'>":"<IMG SRC='images/placebid3.gif' BORDER='0'>");

					if(currCode!="GBP")
						{
							var A=document.getElementById("savingsPrice"+data[C][0]);
							if(A)
								{
									A.innerHTML=showCur(((fixedPrice==-1)?data[C][2]:fixedPrice)*currRate);
									var E=rrp-((fixedPrice==-1)?data[C][2]:fixedPrice);
									if(E<=0)
										{
											E=0
										}
									document.getElementById("savingsFinal"+data[C][0]).innerHTML=showCur(E*currRate)
								}
						}
					var A=document.getElementById("savingsBasePrice"+data[C][0]);
					if(A)
						{
							A.innerHTML=showCur((fixedPrice==-1)?data[C][2]:fixedPrice);
							var E=rrp-((fixedPrice==-1)?data[C][2]:fixedPrice);
							if(E<=0)
								{
									E=0
								}
							document.getElementById("savingsBaseFinal"+data[C][0]).innerHTML=showCur(E)
						}
					bidsLeftEl=document.getElementById("bidsRemaining");
					if(bidsLeftEl)
						{
							bidsLeftEl.innerHTML=bidsLeft;
							if(document.getElementById("BINoffbase"))
								{
								var bidvalue=0.40;
								var inviewBids=myBids-bidsLeft-buddyBids;
								document.getElementById("BINoffbase").innerHTML=showCur(inviewBids*bidvalue);
								document.getElementById("BINpricebase").innerHTML=showCur(rrp-(inviewBids*bidvalue));
								document.getElementById("IndBids").innerHTML=inviewBids;
								if(rrp-(inviewBids*bidvalue)<0)
									{
										document.getElementById("BINpricebase").innerHTML="0.00";
									}
								if(document.getElementById("BINoff"))
									{
									document.getElementById("BINoff").innerHTML=showCur((inviewBids*bidvalue)*currRate);
									document.getElementById("BINprice").innerHTML=showCur((rrp-(inviewBids*bidvalue))*currRate);
									if(((rrp-(inviewBids*bidvalue))*currRate)<0)
										{
											document.getElementById("BINprice").innerHTML="0.00";
										}
									}
								}
						}
					if(data[C][5]!=0)
						{
							if(data[C][5]==1)
								{
									messageEl=document.getElementById("messageBox"+data[C][0]);
									if(messageEl)
										{
											var D=data[C][9]+" Bid(s)";
											D=D+" + "+showTime(data[C][10]);
											D=D+" + £"+showCur(data[C][2]-data[C][13]);
											messageEl.innerHTML=D;
											highlight("messageBox"+data[C][0])
										}
									highlight("bidder"+data[C][0]);

									highlight("time"+data[C][0])
								}
							updateBidHistoryTable(C);
							if(data[C][5]==1)
								{
								var B=getElementsByClass("autoBidRow"+data[C][0]);
								var totalBdyBids=0;
									for(j=0;j<B.length;j++)
										{
											B[j].innerHTML=""
										}
									for(j=0;j<B.length;j++)
										{
											for(k=0;k<data[C][7].length;k++)
												{
													if(B[j].getAttribute("id")==data[C][7][k][0])
														{
															var quicknum=0;
															B[j].innerHTML=data[C][7][k][1];
															quicknum=data[C][7][k][1];
															var quicknum=parseFloat(quicknum);
															totalBdyBids+=quicknum;
														}
												}
										}
									buddyBids=totalBdyBids;
									for(j=0;j<B.length;j++)
										{
											if(B[j].innerHTML=="")
												{
													B[j].parentNode.parentNode.removeChild(B[j].parentNode)
												}
										}
								}
							data[C][5]=0
						}
			}
		updateHappening=false;
		if(refreshInterval>0)
			{
				window.clearTimeout(pageTimer);
				pageTimer=window.setTimeout("getUpdate()",refreshInterval)
			}
	}

function updateBidHistoryTable(A)
	{
		bidHistoryEl=document.getElementById("bidHistoryTable"+data[A][0]);
		if(bidHistoryEl)
			{
				for(j=(data[A][6].length-1);(j>=0);j--)
					{
						var B=document.createElement("tr");
						B.className="bidRecord";
						var C=document.createElement("td");
						C.appendChild(document.createTextNode(showCur(data[A][6][j][3])));
						B.appendChild(C);
						if(currCode!="GBP")
							{
								var C=document.createElement("td");
								C.appendChild(document.createTextNode(showCur(data[A][6][j][3]*currRate)));
								B.appendChild(C)
							}
						var C=document.createElement("td");
						C.appendChild(document.createTextNode(data[A][6][j][1]));
						B.appendChild(C);
						var C=document.createElement("td");
						if(data[A][6][j][2]==2)
							{
								C.appendChild(document.createTextNode("Bid Buddy"))
							}
						else
							{
								C.appendChild(document.createTextNode("Single Bid"))
							}
						B.appendChild(C);
						insertAfter(B,bidHistoryEl)
					}
				while(bidHistoryEl.parentNode.childNodes.length>10)
					{
						bidHistoryEl.parentNode.removeChild(bidHistoryEl.parentNode.lastChild)
					}
			}
	}
var updateHappening=false;

function getUpdate()
	{
		if(updateHappening)
			{
				return 
			}
		updateHappening=true;
		var B='{"d":[';
		for(var A=0;A<data.length;A++)
			{
				if(data[A][8]==3)
					{
						continue
					}
				B=B+'{"i":"'+data[A][0]+'","h":"'+data[A][3]+'","t":"'+data[A][1]+'","p":"'+data[A][2]+'"},'
			}
			if(B.substring(B.length-1)==",")
				{
					B=B.substring(0,B.length-1)
				}
			B=B+"]}";
			if(B.length==8)
				{
					return 
				}
			new Ajax.Request("refresher.php",{method:"POST",parameters:{data:B},onSuccess:processUpdate})
	}

function processUpdate(C)
	{
		var A=C.responseText.evalJSON();
		bidsLeft=A[1];
		for(i=0;i<A[0].length;i++)
			{
				for(var B=0;B<data.length;B++)
					{
						if(data[B][0]==A[0][i][0])
							{
								if((typeof listingId)!="undefined")
									{
										if((A[0][i][0]==listingId)&&(A[0][i][1]==3))
											{
												window.location.reload()
											}
									}
								data[B][13]=data[B][2];
								data[B][1]=A[0][i][2];
								data[B][2]=A[0][i][3];
								data[B][8]=A[0][i][1];
								if(A[0][i][1]==1)
									{
										data[B][5]=A[0][i][1];
										data[B][6]=A[0][i][4];
										data[B][7]=A[0][i][5];
										data[B][9]=A[0][i][6];
										data[B][10]=A[0][i][7];
										if(A[0][i][4].length!==0)
											{
												data[B][3]=A[0][i][4][0][0];
												data[B][4]=A[0][i][4][0][1];
												data[B][11]=A[0][i][8];
												data[B][12]=A[0][i][9]
											}
									}
								if(A[0][i][1]==3)
									{
										data[B][4]=A[0][i][4]
									}
							}
					}
			}
			updatePage()
	}

function bid(C,A,B)
	{
		if(A==0)
			{
				alert("You must be signed in to bid!");
				return 
			}
		new Ajax.Request("makebid.php",{method:"POST",parameters:{i:C,l:B},onSuccess:afterBid})
	}

function afterBid(B)
	{
		switch(B.responseText)
		{
			case "1":
				getUpdate();
			break;
			case "0":
				messageEl=document.getElementById("messageBox"+listingId);
				if(messageEl)
					{
						var A="Bid NOT placed. Your login has probably expired. You must log in again!";
						messageEl.innerHTML=A;
						highlight("messageBox"+listingId)
					}
				break;
			case "2":
				messageEl=document.getElementById("messageBox"+listingId);
				if(messageEl)
					{
						var A="You need to recharge your bid account! ";
						messageEl.innerHTML=A;
						highlight("messageBox"+listingId)
					}
			break;
			case "3":
				messageEl=document.getElementById("messageBox"+listingId);
			if(messageEl)
				{
					var A="You are already the high bidder!";
					messageEl.innerHTML=A;
					highlight("messageBox"+listingId)
				}
			break;
			case "4":
				messageEl=document.getElementById("messageBox"+listingId);
			if(messageEl)
				{
					var A="No bids are accepted while the auction is paused!";
					messageEl.innerHTML=A;
					highlight("messageBox"+listingId)
				}
			break;
			default:
		}
	}

function highlight(C)
	{
		var B=600;
		var A="#fff600";
		var H="#e81f1a";
			if(document.getElementById)
				{
					var D=document.getElementById(C);
					var F=D.style.color;
					var E=D.style.fontWeight;
					var G=D.style.backgroundColor;
					D.style.color=A;
					D.style.backgroundColor=H;
					D.style.fontWeight="bolder";
					setTimeout("highlightRevert('"+C+"', '"+F+"', '"+E+"', '"+G+"')",B)
				}
	}

function highlightRevert(B,A,D,G)
	{
		if(document.getElementById)
			{
				var C=document.getElementById(B);
				C.style.color=A;
				C.style.fontWeight=D;
				C.style.backgroundColor=G;
			}
	}

function insertAfter(C,A)
	{
		var B=A.parentNode;
		if(B.lastchild==A)
			{
				B.appendChild(C)
			}
		else
			{
				B.insertBefore(C,A.nextSibling)
			}
	}

function getElementsByClass(G,E,A)
	{
		var D=new Array();
		if(E==null)
			{
				E=document
			}
		if(A==null)
			{
				A="*"
			}
		var C=E.getElementsByTagName(A);
		var B=C.length;
		var F=new RegExp("(^|\\s)"+G+"(\\s|$)");
		for(i=0,j=0;i<B;i++)
			{
				if(F.test(C[i].className))
					{
						D[j]=C[i];
						j++
					}
			}
		return D
	}

function preProcessAutoBid()
	{
		function B(G)
			{
				return G.replace(/^\s+|\s+$/g,"")
			}

		function E(G)
			{
				var H=B(G);
				return H.length>0&&!(/[^0-9]/).test(H)
			}

		function A(G)
			{
				var H=B(G);
				return !(isNaN(H))
			}
		var D=true;
		var F=document.getElementById("addAutoBid_start");
		var C=document.getElementById("addAutoBid_end");
		if(!(A(F.value)))
			{
				D=false
			}
		if(!(A(C.value)))
			{
				D=false
			}
		if(!(E(document.getElementById("addAutoBid_bids").value)))
			{
				D=false
			}
		if(!D)
			{
				alert("There is a problem with the values you entered, please try again entering: Start Price, End Price, Number of Bids to use within that range.");
				return false
			}
		if(document.getElementById("autoBidCurrency1").checked)
			{
				F.value=F.value/currRate;C.value=C.value/currRate
			}
		return true
	}

function updateABCurr(A)
	{
		var B=getElementsByClass("autoBidRowRange");
		A=((A==0)?1:currRate);
		for(j=0;j<B.length;j++)
			{
				B[j].innerHTML=showCur(B[j].getAttribute("value")*A)
			}
	}

function watchToggle(A)
	{
		new Ajax.Request("watchItem.php",{method:"POST",parameters:{id:A},onSuccess:watchToggleAfter})
	}

function watchToggleAfter(B)
	{
		var A=B.responseText.evalJSON();
		statusAEl=document.getElementById("watchStatusA");
		statusBEl=document.getElementById("watchStatusB");
		if(statusBEl)
			{
				if(A=="0")
					{
						statusAEl.innerHTML="You are not watching this item";
						statusBEl.innerHTML="Add to Watchlist"
					}
				if(A=="1")
					{
						statusAEl.innerHTML="Item is in your Watchlist";
						statusBEl.innerHTML="Remove from Watchlist"
					}
			}
	}

function bidHistoryToggle(A)
	{
		e1=document.getElementById("bidHistory");
		e2=document.getElementById("autoBids");
		if(A==1)
			{
				e1.style.display="block";
				e2.style.display="none";
				bh1=document.getElementById("bidHI");
				if(bh1)
					{
						bh1.setAttribute("src","images/bidhistory1.gif")
					}
				bh2=document.getElementById("bidHJ");
				if(bh2)
					{
						bh2.setAttribute("src","images/autobids2.gif")
					}
			}
		else
			{
				e1.style.display="none";
				e2.style.display="block";
				bh1=document.getElementById("bidHI");
				if(bh1)
					{
						bh1.setAttribute("src","images/bidhistory2.gif")
					}
				bh2=document.getElementById("bidHJ");
				if(bh2)
					{
						bh2.setAttribute("src","images/autobids1.gif")
					}
			}
	}

function helpShow(C)
	{
		var A=getElementsByClass("helpCategory");
		for(var B=0;B<A.length;B++)
			{
				if(A[B].id==("category"+C))
					{
						A[B].style.display="block"
					}
				else
					{
						A[B].style.display="none"
					}
			}
	};
