﻿var LotTimeRem = new Array();
var SecPast;
var TheTimeout;
var TimeOn = true;
SecPast = 0;

function TimePast(){
	SecPast++;
	FinalCountdown();
}

function FinalCountdown(){
	var InfoArray, Output, LotID;
	var DayCount, HourCount, MinCount, SecCount, SecRem;
	var Unknown = 0;
	var CheckForClosedLots = 1;
	
	if(TimeOn){
		if(LotTimeRem.length <= 0){
			if(window.HasStarted){
				clearInterval(intervalIDForLots);  //Turn off timer while we check to see if any lots are extended or staggered

				if (!(document.getElementById("isLotDetail"))){
					var EmbedParms = "";
					var AuctionID = -1;
					var HasStaggeredLots = false;
					
					if (document.getElementById("embedParams")){
						EmbedParams = document.getElementById('embedParams').value;
					}
					if (document.getElementById("hdnAuctionID")){
						AuctionID = document.getElementById('hdnAuctionID').value;
					}
					if (document.getElementById("hdnHasStaggeredLots")){
						HasStaggeredLots = document.getElementById('hdnHasStaggeredLots').value;
					}
					
					document.getElementById('hdnSomethingJustGotExtended').value = "0";
					RequestURL = "/asp/xml/AuctionTimeWithStaggeredInfoXML.asp?aid=" + AuctionID + "&stg=" + HasStaggeredLots + EmbedParams  + "&date=" + Date();
					wRequest = new Sys.Net.WebRequest();
					wRequest.set_url(RequestURL);
					wRequest.set_httpVerb("GET");
					wRequest.add_completed(ExtendedLotAuctionClosed);
					executor = wRequest.get_executor();
					wRequest.invoke();
				}
			}
		}

		for(i = 0; i < LotTimeRem.length; i++){
			InfoArray = LotTimeRem[i];
			InfoArray = InfoArray.split(",");
			LotID = "TRem:" + InfoArray[0];
		
			DayCount = 0;
			HourCount = 0;
			MinCount = 0;
			SecCount = 0;
			Output = "";
			
			SecCount = InfoArray[1];
			SecCount = SecCount - SecPast;
			if(SecCount >= -30){
				SecRem = SecCount
				if(SecCount < 0){
					SecCount = 0;
				}
				if(SecCount >= 86400){
					DayCount = Math.floor(SecCount/86400);
					SecCount -= (DayCount * 86400);
				}
				if(SecCount >= 3600){
					HourCount = Math.floor(SecCount/3600);
					SecCount -= (HourCount * 3600);
				}
				if(SecCount >= 60){
					MinCount = Math.floor(SecCount/60);
				}

				SecCount -= (MinCount * 60);

				if((MinCount <= 0 && SecCount <= 0) && (SecRem % 3) == 0){
					refreshLotTimer(InfoArray[0], CheckForClosedLots);
					if (CheckForClosedLots == 1){
						CheckForClosedLots = 0;
					}
				}
			}else{
				LotTimeRem.splice(i,1);
				Unknown = 1;
			}

			Output = DayCount + HourCount + MinCount + SecCount

			if (document.getElementById(LotID)){
				document.getElementById(LotID).style.display = "inline";
			}
			if(DayCount + HourCount + MinCount + SecCount > 0){
				
				if (DayCount != undefined && HourCount != undefined && MinCount != undefined && SecCount != undefined){
					if(document.getElementById(LotID)){
						document.getElementById(LotID).innerHTML = DisplayString(DayCount, HourCount, MinCount, SecCount); ;
					}
				}
			}
			else{
				if(document.getElementById(LotID)){
					document.getElementById(LotID).innerHTML = ":00";
				}
			}	
		}
	}
}

function refreshLotTimer(lotId, checkForClosedLots){
	var RequestURL = "/asp/xml/LotTimeRemXML.asp?lid=" + lotId + "&date=" + Date() + "&checkForClosedLots=" + checkForClosedLots;
	var wRequest = new Sys.Net.WebRequest();
	wRequest.set_url(RequestURL);
	wRequest.set_httpVerb("GET");
	wRequest.add_completed(ResetTime);
	var executor = wRequest.get_executor(); 
	wRequest.invoke();
}

function ResetTime(executor) {
	var XMLOutput, BidButton;
	var LotSecRem, LotID, NextBid;
	var LotStatus, HighBid, Bidder;
	var ReserveMet;
	var Eject = 0, AuctionID, LotNumber;
	var ApplicationSSLHome, AuctionHouseID, isExtended;
	if(executor.get_responseAvailable()){
		XMLOutput = executor.get_xml();
	
		
		LotID = XMLOutput.getElementsByTagName("LotID")[0].childNodes[0].nodeValue;
		
		if(document.getElementById("placeBid" + LotID)){

			if (document.getElementById("placeBid" + LotID).style.display != "none" || 
			    document.getElementById("placeInstaBid" + LotID).style.display != "none" || 
				document.getElementById("confirmBid" + LotID).style.display != "none"|| 
				document.getElementById("incrementChoice" + LotID).style.display != "none") {
	
				LotSecRem = XMLOutput.getElementsByTagName("LotSecRem")[0].childNodes[0].nodeValue;
				AuctionID = XMLOutput.getElementsByTagName("AuctionID")[0].childNodes[0].nodeValue;
				AuctionHouseID = XMLOutput.getElementsByTagName("AuctionHouseID")[0].childNodes[0].nodeValue;
				LotNumber = XMLOutput.getElementsByTagName("LotNumber")[0].childNodes[0].nodeValue;
				ApplicationSSLHome = XMLOutput.getElementsByTagName("ApplicationSSLHome")[0].childNodes[0].nodeValue;
				LotStatus = XMLOutput.getElementsByTagName("LotStatusCd")[0].childNodes[0].nodeValue;
				Bidder = XMLOutput.getElementsByTagName("SoldToBidderID")[0].childNodes[0].nodeValue;
				HighBid = XMLOutput.getElementsByTagName("HammerPrice")[0].childNodes[0].nodeValue;
				isExtended = XMLOutput.getElementsByTagName("Extended")[0].childNodes[0].nodeValue;
				NextBid = XMLOutput.getElementsByTagName("NextBid")[0].childNodes[0].nodeValue;
				ReserveMet = XMLOutput.getElementsByTagName("ReserveMet")[0].childNodes[0].nodeValue;

				if (LotSecRem <= 0 || LotStatus != '0'){
					if(document.getElementById('LotTimer:' + LotID)){
						document.getElementById('LotTimer:' + LotID).style.display = 'none';
						document.getElementById('LotStatus:' + LotID).value = LotStatus;
					}
				}

				if (LotSecRem > 0) {

					for (i = 0; i < LotTimeRem.length; i++) {
						InfoArray = LotTimeRem[i].split(",");
						if (InfoArray[0] == LotID) {
							if(isExtended == '1'){
								// turn extended bidding icon on
								if(!document.getElementById('extBidIcon:' + LotID)){
									if(document.getElementById('TRemLbl:' + LotID)){
										var el = document.getElementById('TRemLbl:' + LotID);
										if (document.getElementById("TimeRemaining:" + LotID).innerHTML.indexOf('Time Remaining:') > -1) {
											el.innerHTML = '<span id="extBidIcon:' + LotID + '" class="extBidIcon"></span> Bidding Extended: ';
										} else {
											el.innerHTML = '<span id="extBidIcon:' + LotID + '" class="extBidIcon"></span> Bidding Extended: <br />';
										}
										jQuery(el).addClass('extended');
										el = document.getElementById('TRem:' + LotID);
										jQuery(el).addClass('extended');
									}
								}
							}
							MakeUrgent(LotSecRem, LotID, isExtended);
							LotSecRem = LotSecRem - 0 + SecPast;
							LotTimeRem[i] = InfoArray[0] + "," + LotSecRem;
						}
					}
					if (document.getElementById("BidButton:" + LotID) && !document.getElementById("MultiBid:" + LotID)) {
						document.getElementById("BidButton:" + LotID).style.display = "block";
					}

				} else {

					if (LotStatus == 3 || LotStatus == 4) {
						document.getElementById("placeBid" + LotID).style.display = "none";
						document.getElementById("placeInstaBid" + LotID).style.display = "none";
						if (document.getElementById("confirmBid" + LotID)){
							document.getElementById("confirmBid" + LotID).style.display = "none";
						}
						if (document.getElementById("slideDown")){
							document.getElementById("slideDown").style.display = "none";
						}
						document.getElementById("Bidder:" + LotID).style.display = "none";

						Eject = 1;
					} else {
						//Do not update the values.  Leave previous values visible.
						//document.getElementById("Bid:" + LotID).innerHTML = "updating...";
						//document.getElementById("Bidder:" + LotID).innerHTML = "By: updating..."; 
					}
				}

				setLotStatus(LotID, HighBid, Bidder, ReserveMet, "~dnd~", LotStatus, LotSecRem, NextBid);

			}
		}

		if (Eject == 1) {

			for(i = 0; i < LotTimeRem.length; i++){
				InfoArray = LotTimeRem[i].split(",");
				if(InfoArray[0] == LotID){
					LotTimeRem.splice(i, 1);
					/*document.getElementById("TWarn:" + LotID).style.display = "none";*/
					document.getElementById("TRem:" + LotID).innerHTML = "0 min, 0 sec left to bid on this lot";
					document.getElementById("TRem:" + LotID).style.display = "none";
				}
			}	
		}
		
	}
}

function RefreshAllTime() {
	var Output = document.getElementById("AuctionID").value;
	
	var RequestURL = "/asp/xml/LotTimeRemAllXML.asp?aid=" + Output + "&date=" + Date();
	var wRequest = new Sys.Net.WebRequest();
	wRequest.set_url(RequestURL);
	wRequest.set_httpVerb("GET");
	wRequest.add_completed(ResetAllTime);
	executor = wRequest.get_executor();
	wRequest.invoke();
}

function ResetAllTime(executor){
	var XMLOutput, Lots, i, j;
	var LotSecRem, LotID, NextBid;
	var LotStatus, Bidder, HighBid;
	var ReserveMet;
	var InArray, BidButton;
	var AuctionID, AuctionHouseID, LotNumber, ApplicationSSLHome, isExtended;
	if(executor.get_responseAvailable()){
		XMLOutput = executor.get_xml();
	
		Lots = XMLOutput.getElementsByTagName("Lot");
		
		for(j = 0; j < Lots.length; j++){
		
			LotID = Lots.item(j).childNodes[1].childNodes[0].nodeValue; //LotID
			if(document.getElementById("placeBid" + LotID)){
				if(document.getElementById("placeBid" + LotID).style.display != "none" || document.getElementById("placeInstaBid" + LotID).style.display != "none"){ // added AND condition to prevent ajax that runs every 30 seconds from screwing up an in progress bid
					LotSecRem = Lots.item(j).childNodes[0].childNodes[0].nodeValue; //LotSecRem
					LotStatus =	Lots.item(j).childNodes[2].childNodes[0].nodeValue; //LotStatusCd
					Bidder = Lots.item(j).childNodes[3].childNodes[0].nodeValue; //SoldToBidderID
					HighBid = Lots.item(j).childNodes[4].childNodes[0].nodeValue; //HammerPrice
					AuctionID = Lots.item(j).childNodes[5].childNodes[0].nodeValue; //AuctionID
					LotNumber = Lots.item(j).childNodes[6].childNodes[0].nodeValue; //LotNumber
					AuctionHouseID = Lots.item(j).childNodes[7].childNodes[0].nodeValue; //AuctionHouseID
					ApplicationSSLHome = Lots.item(j).childNodes[8].childNodes[0].nodeValue; //ApplicationSSLHome
					isExtended = Lots.item(j).childNodes[9].childNodes[0].nodeValue; //ExtendedBidding
					NextBid = Lots.item(j).childNodes[10].childNodes[0].nodeValue; //NextBid (minimum bid)
					ReserveMet = Lots.item(j).childNodes[11].childNodes[0].nodeValue; //ReserveMet

					if(LotSecRem > 0){
						if(document.getElementById("TRem:" + LotID)){
							InArray = 0;
							for(i = 0; i < LotTimeRem.length; i++){
								InfoArray = LotTimeRem[i];
								InfoArray = InfoArray.split(",");
								if(InfoArray[0] == LotID){
									if (isExtended == '1') {
										UrgentType = 'Extended';
										// turn extended bidding icon on
										if (!document.getElementById('extBidIcon:' + LotID)) {
											if (document.getElementById('TRemLbl:' + LotID)) {
												var el = document.getElementById('TRemLbl:' + LotID);
												if (document.getElementById("TimeRemaining:" + LotID).innerHTML.indexOf('Time Remaining:') > -1) {
													el.innerHTML = '<span id="extBidIcon:' + LotID + '" class="extBidIcon"></span> Bidding Extended: ';
												} else {
													el.innerHTML = '<span id="extBidIcon:' + LotID + '" class="extBidIcon"></span> Bidding Extended: <br />';
												}
												jQuery(el).addClass('extended');
												el = document.getElementById('TRem:' + LotID);
												jQuery(el).addClass('extended');
											}
										}
										try { document.getElementById('hdnSomethingJustGotExtended').value = "1"; } catch (e) { };
									}
									MakeUrgent(LotSecRem, LotID, isExtended);
									LotSecRem = LotSecRem - 0 + SecPast;
									LotTimeRem[i] = InfoArray[0] + "," + LotSecRem;
									InArray = 1;
								}
							}
							if(InArray == 0){
								LotSecRem = LotSecRem - 0 + SecPast;
								LotTimeRem.push(LotID +","+ LotSecRem)
							}
							
							setLotStatus(LotID, HighBid, Bidder, ReserveMet, "~dnd~", LotStatus, LotSecRem, NextBid);
						}
					}
					
					if(LotStatus == 0){
						
						setLotStatus(LotID, HighBid, Bidder, "", "~dnd~", LotStatus, LotSecRem, NextBid);
						if(document.getElementById("BidButton:" + LotID) && !document.getElementById("MultiBid:" + LotID)){
							document.getElementById("BidButton:" + LotID).style.display = "block";
						}
					}
				}
			}
		}
		TimeOn = true;
		clearTimeout(TheTimeout);
		if(Lots.length > 0){
			TheTimeout = setTimeout("RefreshAllTime();",5000);
		}
	}else{
		TimeOn = true;
		clearTimeout(TheTimeout);
		if(Lots.length > 0){
			//alert('setting RefreshAllTime() to 1 seconds');
			TheTimeout = setTimeout("RefreshAllTime();",1000);
		}
	}
}
function MakeUrgent(LotSecRem, LotID, isExtended) {
	if (isExtended == '1') {
		document.getElementById("urgentIcon:" + LotID).className = "urgentTimedExtended medium";
		document.getElementById('extBidIcon:' + LotID).style.display = "none";
	}
	if (LotSecRem <= 60) {
		if (jQuery("TRem:" + LotID).hasClass('one') == false) {
			if (isExtended != '1') { document.getElementById("urgentIcon:" + LotID).className = "urgentTimedOne medium"; };
			document.getElementById("TimeRemaining:" + LotID).className = "topContentCenter urgent one";
			if (document.getElementById("TimeRemaining:" + LotID).innerHTML.indexOf('Time Remaining:') > -1) {
				document.getElementById("TRem:" + LotID).className = "lotTimeRemaining urgent one large";
				document.getElementById("TRemLbl:" + LotID).className = "lotTimeRemainingLabel extended urgent one";
			} else {
				document.getElementById("TRem:" + LotID).className = "lotTimeRemaining urgent one";
			}
		}
	} else {
		if (LotSecRem < 600) {
			if (jQuery("TRem:" + LotID).hasClass('urgent') == false) {
				if (isExtended != '1') { document.getElementById("urgentIcon:" + LotID).className = "urgentTimedTen medium"; };
				document.getElementById("TimeRemaining:" + LotID).className = "topContentCenter urgent";
				if (document.getElementById("TimeRemaining:" + LotID).innerHTML.indexOf('Time Remaining:') > -1) {
					document.getElementById("TRem:" + LotID).className = "lotTimeRemaining urgent large";
				} else {
					document.getElementById("TRem:" + LotID).className = "lotTimeRemaining urgent";
				}
			}
		}
	}
}


function DisplayString(day, hour, minute, second) {

	var sDayText = "Day";
	var sHourText = "Hour"
	var sMinuteText = "Minute";
	var sSecondText = "Second";
	var sTimerLeftText = "";
	var sTimerRightText = "";
	var bShortFormat = true;
	var sSpace;
	if (day > 0 || hour > 0) {
		bShortFormat = false;
		sSpace = "&nbsp;"
	} else {
		sSpace = "";
	};

	if (day > 1)
		sDayText = "Days"

	if (hour > 1)
		sHourText = "Hours"

	if (minute > 1)
		sMinuteText = "Minutes"

	if (second > 1 || second == 0)
		sSecondText = "Seconds"

	var iTextCount = 0;

	if (day > 0) {
		sTimerLeftText = day + " " + sDayText;
		iTextCount++;
	}

	if (hour > 0) {
		if (iTextCount == 1) {
			sTimerRightText = hour + " " + sHourText;
		} else {
			sTimerLeftText = hour + " " + sHourText;
		}
		iTextCount++;
	}

	if (iTextCount < 2) {
		if (minute > 0) {
			if (iTextCount == 1) {
				bShortFormat ? sTimerRightText = minute : sTimerRightText = minute + " " + sMinuteText;
			} else {
				bShortFormat ? sTimerLeftText = minute : sTimerLeftText = minute + " " + sMinuteText;
			}
			iTextCount++;
		}
	}

	if (iTextCount < 2) {
		//if (second > 0) {
			if (bShortFormat == true && second < 10) { second = "0" + "" + second; };
			if (iTextCount == 1) {
				bShortFormat ? sTimerRightText = ":" + second : sTimerRightText = second + " " + sSecondText;
			} else {
				bShortFormat ? sTimerLeftText = ":" + second : sTimerLeftText = second + " " + sSecondText;
			}
		//}
	}

	return sTimerLeftText + sSpace + sTimerRightText;

}

