
var oAuctionTimer;
var aTimersToMonitor;
var nSecondsPerDay = 86400;
var nSecondsPerHour = 3600;
var nSecondsPerMinute = 60;
var nAuctionCounter = 0;
var nSecondsBetweenAJAXRequests = 30;
var xmlHttp;
var sReservePriceStatus_StyleColor = '#267b26';
var sReservePriceStatus_StyleFontStyle = 'italic';
var sReservePriceStatus_StyleFontWeight = 'bold';
var sCurrentPrice_StyleFontSize = '11pt';
var sURL = new String(window.location);

var sTimeRemainingText = "";
var sBiddingOpensText = "";
var bListPage = false;
var sBidNowText = "";
var sEventsToCheckForOnPage = "";

var arMonths = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

sURL = sURL.toLowerCase();

function GetSecondsRemainingFromDiv(oElem) {

    var nSecRemain = 0;

    if (!isNaN(oElem.innerHTML)) {

        nSecRemain = parseInt(oElem.innerHTML);        

    } else {

        var reg = />(\d+)</i;
        var arrReg = reg.exec(oElem.innerHTML);        

        if (arrReg) {
            nSecRemain = parseInt(arrReg[1]);
        }
    }

    return nSecRemain;
}

function ChangeViewNowLinks(sEventID) {
    var arrDetailsLinks = document.getElementsByName("lnkDetails" + sEventID);
    var iLnk = 0;

    while (iLnk < arrDetailsLinks.length) {
        arrDetailsLinks[iLnk].innerHTML = sBidNowText;
        iLnk++;
    }
}


///Method that will make the time remaining text red and show the clock if there is less than an hour left in the auction
function DisplayClock(nSecondsLeft, sEventID) {
    if (nSecondsLeft < (nSecondsPerHour + nSecondsPerMinute)) {
        if (bListPage == true) {
            if (document.getElementById('auction' + sEventID + 'clockDescr') != null) {
                document.getElementById('auction' + sEventID + 'clockDescr').style.display = '';
            }
        } else {
            if (document.getElementById('auction' + sEventID + 'clock') != null) {
                document.getElementById('auction' + sEventID + 'clock').style.display = '';
            }
        }        
    }
}

///<summary>
///Method that will fire when an auction has ended - it will either change the text to auction ended or reload the window
///</summary>
function AuctionEnded(sEventID) {
    var URL = new String(window.location);
    URL = URL.toLowerCase();

    clearInterval(oAuctionTimer);

    if (URL.indexOf('details.aspx', 0) > 0) {
        RefreshPage();
    }
    else {
        if (bListPage == true) {
            //document.getElementById('auction' + sEventID + 'Descr').innerHTML = 'Auction Has Ended.';
            $('#auction' + sEventID + 'Descr').html('Auction Has Ended.');
            //document.getElementById('auction' + sEventID + 'Header').innerHTML = 'Auction Has Ended.';
            $('#auction' + sEventID + 'Header').html('Auction Has Ended.');
        } else {
            //document.getElementById('auction' + sEventID).innerHTML = 'Auction Has Ended.';
            $('#auction' + sEventID).html('Auction Has Ended.');
        }
        
        MakeTextBlack(sEventID);

        //on list page, so we need to remove the OAL that just ended and start the timer again.
        var sNewEventsOnPage;
        var sRegExpReplace;

        sNewEventsOnPage = sEventsToCheckForOnPage.toUpperCase();

        sEventID = sEventID.toUpperCase();

        sRegExpReplace = new RegExp(sEventID, '');

        //Remove current OAL that just ended
        sNewEventsOnPage = sNewEventsOnPage.replace(sRegExpReplace, '');

        //Replace double commas left behind with a single comma
        sNewEventsOnPage = sNewEventsOnPage.replace(/\,\,/g, ",");

        sEventsToCheckForOnPage = sNewEventsOnPage;

        if (sEventsToCheckForOnPage.length > 1) {
            //Reset aTimersToMonitor list.
            aTimersToMonitor = sEventsToCheckForOnPage.split(',');

            //Start up timer again
            oAuctionTimer = setInterval("CheckCountdown()", 1000);
        }

    }
}

///Method that will make the time remaining text black and hide the clock if there is more than an hour left or the auction has ended
function MakeTextBlack(sEventID) {
    if (bListPage == true) {
        document.getElementById('auction' + sEventID + 'Descr').style.color = 'black';
        document.getElementById('auction' + sEventID + 'RemainingDescr').style.color = 'black';
        document.getElementById('auction' + sEventID + 'clockDescr').style.display = 'none';
        
    } else {
        document.getElementById('auction' + sEventID).style.color = 'black';
        document.getElementById('auction' + sEventID + 'Remaining').style.color = 'black';
        document.getElementById('auction' + sEventID + 'clock').style.display = 'none';
    }
}


//<summary>
//Method that will get the new bidding opens display text
//</summary>
function GetBiddingOpensTime(sDate, bIncludeTimeZone) {
    var dCurrentDate = new Date(sDate);

    //var dCurrentDate = Date.parse(sDate);            

    //Subtract a days worth of seconds since bidding opens 1 day before the auction ends
    //dCurrentDate = addSecs(dCurrentDate,(nSecondsRemaining - nSecondsPerDay));                                 

    return GetFormattedTimeString(dCurrentDate, bIncludeTimeZone);
}

//<summary>
//Method that will get the new bidding opens display text
//</summary>
function GetBiddingEndsTime(sDate, bIncludeTimeZone, nSecondsLeft, bMakeTextRedWhenApplicable) {
    var dCurrentDate = new Date(sDate);
    var sFormattedTimeString = ''

    //var dCurrentDate = Date.parse(sDate);            

    //Get end date
    //dCurrentDate = addSecs(dCurrentDate,(nSecondsRemaining));

    if (nSecondsLeft >= nSecondsPerDay) {
        sFormattedTimeString = GetFormattedTimeString(dCurrentDate, bIncludeTimeZone);
    }
    else {
        sFormattedTimeString = GetNewTimeRemaining(nSecondsLeft, bMakeTextRedWhenApplicable);
    }

    return sFormattedTimeString;

}

//<summary>
//Method that will get the new bidding opens display text
//</summary>
function GetFormattedTimeString(dDate, bIncludeTimeZone) {
    var day;
    var nMonthIdx;
    var month;
    var year;
    var time;

    year = dDate.getFullYear();

    time = getBiddingOpenTime(dDate, bIncludeTimeZone);

    day = dDate.getDate();

    nMonthIdx = dDate.getMonth();

    month = arMonths[nMonthIdx];

    return month + ' ' + day + ', ' + year + ' ' + time;

}

function addSecs(d, s) {
    return new Date(d.valueOf() + s * 1000);
}

function getBiddingOpenTime(dEndDate, bIncludeTimeZone) {
    var nHour;
    var nMinutes;
    var sAMPM;
    var jsHours = dEndDate.getHours();
    var sReturnTime;

    if (jsHours > 12) {
        nHour = jsHours - 12;
        sAMPM = 'PM';
    }
    else {
        nHour = jsHours;
        sAMPM = 'AM';
    }

    //getHourValueAndAMorPM(nHours, sAMPM, dEndDate.getHours());

    if (dEndDate.getMinutes() < 10) {
        nMinutes = '0' + dEndDate.getMinutes();
    }
    else {
        nMinutes = dEndDate.getMinutes();
    }

    sReturnTime = nHour + ":" + nMinutes + ' ' + sAMPM;

    if (bIncludeTimeZone == true) {
        sReturnTime += ' (Central Time)';
    }

    return sReturnTime;

}



//<summary>
//Method that will get the new time to display to the user
//</summary>
function GetNewTimeRemaining(nSecondsRemaining, bMakeTextRedWhenApplicable) {
    var nDays = GetDaysRemaining(nSecondsRemaining);
    var nHours = GetHoursRemaining(nSecondsRemaining, nDays);
    var nMinutes = GetMinutesRemaining(nSecondsRemaining, nDays, nHours);
    var nSeconds = GetSecondsRemaining(nSecondsRemaining, nDays, nHours, nMinutes);

    if (nSeconds > 0 && nMinutes > 5) {
        nMinutes = nMinutes + 1;
    }

    if (nMinutes == 60) {
        nMinutes = 0;
        nHours = nHours + 1;
    }

    if (nHours == 24) {
        nHours = 0;
        nDays = nDays + 1;
    }

    return ConvertTimeToText(nSecondsRemaining, nDays, nHours, nMinutes, nSeconds, bMakeTextRedWhenApplicable);
}

//<summary>
//Method that will determine the number of days remaining based on the seconds
//</summary>
function GetDaysRemaining(nSecondsRemaining) {
    return Math.floor(nSecondsRemaining / nSecondsPerDay);
}

//<summary>
//Method that will determine the number of hours remaining based on the seconds - days
//</summary>
function GetHoursRemaining(nSecondsRemaining, nDays) {
    return Math.floor((nSecondsRemaining - (nDays * nSecondsPerDay)) / nSecondsPerHour);
}

//<summary>
//Method that will determine the number of minutes remaining based on seconds - days - hours
//</summary>
function GetMinutesRemaining(nSecondsRemaining, nDays, nHours) {
    return Math.floor((nSecondsRemaining - (nDays * nSecondsPerDay) - (nHours * nSecondsPerHour)) / nSecondsPerMinute);
}

//<summary>
//Method that will determine the number of seconds remaining based on seconds - days - hours - minutes
//</summary>
function GetSecondsRemaining(nSecondsRemaining, nDays, nHours, nMinutes) {
    return nSecondsRemaining - (nDays * nSecondsPerDay) - (nHours * nSecondsPerHour) - (nMinutes * nSecondsPerMinute);
}

//<summary>
//Method that will convert the days, hours, minutes, seconds to text IE 1 Day 3 hours 5 Minutes
//</summary>
function ConvertTimeToText(nSecondsRemaining, nDays, nHours, nMinutes, nSeconds, bMakeTextRedWhenApplicable) {
    var sText = '';
    var nDaysDisplayText = '';
    var nHoursDisplayText = '';
    var nMinutesDisplayText = '';
    var sClass;
    
    if (nSecondsRemaining < (nSecondsPerHour + nSecondsPerMinute)) {
        if (bListPage == true) {
            if (bMakeTextRedWhenApplicable == true) {
                sClass = 'class=redtext';
            } else {
                sClass = '';
            }            
        } else {
            //Under 1 hour left for auction, so make time text red
            sClass = 'class=redtext';
        }
        
        nDaysDisplayText = '<span ' + sClass + '>' + nDays + '</span>';
        nHoursDisplayText = '<span ' + sClass + '>' + nHours + '</span>';
        nMinutesDisplayText = '<span ' + sClass + '>' + nMinutes + '</span>';
    }
    else {

        nDaysDisplayText = "<strong>" + nDays + "</strong>";
        nHoursDisplayText = "<strong>" + nHours + "</strong>";
        nMinutesDisplayText = "<strong>" + nMinutes + "</strong>";
    }

    if (nDays > 0) {
        sText = sText + GetText(nDays, ' <span class=style1>days</span> ', ' <span class=style1>day</span> ', nDaysDisplayText);
    }

    if (nHours > 0) {
        sText = sText + GetText(nHours, ' <span class=style1>hours</span> ', ' <span class=style1>hour</span> ', nHoursDisplayText);
    }

    if (nMinutes > 0) {
        sText = sText + nMinutesDisplayText + ' <span class=style1>min</span> ';
    }
    else if (nMinutes == 0 && nDays > 0 && nHours == 0) {
        if (nSecondsRemaining < (nSecondsPerHour + nSecondsPerMinute) && bListPage == false) {
            sText = sText + "<span class=redtext>1</span>" + ' min ';
        }
        else {
            sText = sText + "<span class=style1>1</span>" + ' min ';
        }
    }

    if (nSeconds > 0 && nMinutes <= 5 && nHours == 0 && nDays == 0) {
        sText = sText + GetText(nSeconds, ' <span class=style1>seconds</span> ', ' <span class=style1>second</span> ', '<span ' + sClass + '>' + nSeconds + '</span>');
    }

    return sText;
}

//<summary>
//Method that will get the actual text for the value, it will determine if the plural text or the singular text should be shown after the value
//</summary>
function GetText(nValue, sPluralText, sSingularText, sValueDisplayText) {
    var sValue = '';

    if (nValue == 1) {
        sValue = sValueDisplayText + sSingularText;
    }
    else {
        sValue = sValueDisplayText + sPluralText;
    }

    return sValue;
}



//<summary>
//Method that will get the HTTP request to perform the AJAX call back
//</summary>
function getNewXmlHttpRequestObject() {

    var newObj

    if (window.XMLHttpRequest) {

        newObj = new XMLHttpRequest();      //Mozilla Browsers        

    } else if (window.ActiveXObject) {        
        newObj = new ActiveXObject("Microsoft.XMLHTTP");      //IE

    }
    
    return newObj;
}

///Method that will set the price div tags and turn the text red if the price changes
function SetCurrentBidText(sEventID, sOpeningPrice, sPrice) {
    if (sOpeningPrice != sPrice) {
        //Need to find translation and display it.
        var oCurrentBidTranslation = document.getElementById('currentbidtranslation' + sEventID);
        var oCurrentBidDisplay = document.getElementById('currentbid' + sEventID);

        if (oCurrentBidTranslation != null && oCurrentBidDisplay != null) {
            //Don't make text red if on details.aspx page		    			 
            if (sURL.indexOf("details.aspx") == -1) {
                oCurrentBidDisplay.style.color = 'red';
                oCurrentBidDisplay.style.fontSize = sCurrentPrice_StyleFontSize;
            }            
            oCurrentBidDisplay.style.fontWeight = sReservePriceStatus_StyleFontWeight;
            oCurrentBidDisplay.innerHTML = oCurrentBidTranslation.innerHTML;
        }
    }

}

///Method that will set the price div tags and turn the text red if the price changes
function SetPrice(sEventID, sPrice, sOpeningPrice) {
    var spanPrice = document.getElementById('auction' + sEventID + 'price');
    var sPriceSpanID = '#auction' + sEventID + 'price';
        
    if (spanPrice != null) {
        if (sOpeningPrice != sPrice) {
            if (sURL.indexOf("details.aspx") != -1) {
                //They don't want the text to be red on the details page
                if ($(sPriceSpanID).text() != sPrice) {
                    spanPrice.style.color = 'red';
                    //spanPrice.innerHTML = sPrice;
                    $(sPriceSpanID).text(sPrice);
                    document.getElementById('currentbid' + sEventID).style.color = 'red';
                } else {
                    spanPrice.style.color = 'black';
                    //spanPrice.innerHTML = sPrice;
                    $(sPriceSpanID).text(sPrice);
                    document.getElementById('currentbid' + sEventID).style.color = 'black';
                }
            }
            else {
                spanPrice.style.color = 'red';
                spanPrice.style.fontWeight = sReservePriceStatus_StyleFontWeight;
                spanPrice.style.fontSize = sCurrentPrice_StyleFontSize;
                //spanPrice.innerHTML = sPrice;
                $(sPriceSpanID).text(sPrice);
            }
        }
        else {
            if ($(sPriceSpanID).text() != sPrice) {
                //spanPrice.style.color = 'red';
                //spanPrice.innerHTML = sPrice;
                $(sPriceSpanID).text(sPrice);
            }
            else {
                //They want text to be black
                //spanPrice.style.color = 'black';
            }
        }

    }
}

function CheckHideHighestBidderTable(nHighBid) {

    //Check if displaying the "You Are Currently The Highest Bidder" table			
    if (document.getElementById('tblHighBidder') != null) {

        if (document.getElementById('tblHighBidder').style.display == 'block') {
            //Remove all non-numeric characters
            nHighBid = nHighBid.replace(/[^\d]/g, '');

            //nUsersHighBid should have been set on SubmitBid control
            if (nHighBid > nUsersHighBid) {
                document.getElementById('tblHighBidder').style.display = 'none';
            }
        }
    }

}

function CheckTimeExtended(nNewSecondsLeft, sEndDate, sCurrentTimeDivID, sEventID) {

    var nOldSecondsLeft = $(sCurrentTimeDivID).text();
    var bDisplayTimeExtendedMessage = false;
    var oTimeExtendedDiv = document.getElementById('dvAuctionExtended');
    var nTimeDiff = (nNewSecondsLeft - nOldSecondsLeft);
    var dEndDate = new Date(sEndDate);
    var dBiddingOpensDate = new Date(dEndDate.getFullYear(), dEndDate.getMonth(), dEndDate.getDate() - 1, dEndDate.getHours(), dEndDate.getMinutes(), dEndDate.getSeconds());


    //Check to see if time differs by more than 30 seconds either way.
    if (Math.abs(nNewSecondsLeft - nOldSecondsLeft) >= 30) {
        if (nNewSecondsLeft > nOldSecondsLeft) {
            //Less than 5 minutes left in auction 					
            if (nTimeDiff <= 330) {
                //List page does not have the auction extended div so need to check if div is on current page.
                bDisplayTimeExtendedMessage = true;
            }

            if (oTimeExtendedDiv != null) {
                if (bDisplayTimeExtendedMessage == true) {
                    oTimeExtendedDiv.style.display = 'block';
                }
                else {
                    oTimeExtendedDiv.style.display = 'none';
                }
            }
        }

        if (bListPage == true) {
            //Update list page
            //var oListPageTimeLeftDisplayDescr = document.getElementById('auction' + sEventID + 'Descr');
            //var oListPageTimeLeftDisplayHeader = document.getElementById('auction' + sEventID + 'Header');
            var sListPageTimeLeftDisplayHeaderID = '#auction' + sEventID + 'Header';
            var sListPageTimeLeftDisplayDescrID = '#auction' + sEventID + 'Descr';

            //var oListPageTimeRemainingDescr = document.getElementById('auction' + sEventID + 'RemainingDescr');
            var sListPageTimeRemainingDescr = '#auction' + sEventID + 'RemainingDescr'
            var bMakeTextRedWhenApplicable;

            if (nNewSecondsLeft > nSecondsPerDay) {

                if (bListPage == true) {
                    var sBiddingOpensTime = GetBiddingOpensTime(dBiddingOpensDate, bListPage);
                    
                    if ($(sListPageTimeLeftDisplayDescrID)) {
                        //oListPageTimeLeftDisplayDescr.innerHTML = sBiddingOpensTime
                        $(sListPageTimeLeftDisplayDescrID).html(sBiddingOpensTime);
                        $(sListPageTimeRemainingDescr).html(sBiddingOpensText + ": ");
                    }

                    if ($(sListPageTimeLeftDisplayHeaderID)) {
                        $(sListPageTimeLeftDisplayHeaderID).html= sBiddingOpensTime
                    }
                } else {
                    //document.getElementById('auction' + sEventID).innerHTML = GetBiddingOpensTime(dBiddingOpensDate, bListPage);
                    $('#auction' + sEventID).html(GetBiddingOpensTime(dBiddingOpensDate, bListPage));
                    //document.getElementById('auction' + sEventID + 'Remaining').innerHTML = sBiddingOpensText + ": ";
                    $('#auction' + sEventID + 'Remaining').html(sBiddingOpensText + ': ');
                }                 

            }
            else {
                if (bListPage == true) {
                    var sBiddingEndsTime = GetBiddingEndsTime(sEndDate, bListPage, nNewSecondsLeft, bMakeTextRedWhenApplicable);

                    if ($(sListPageTimeLeftDisplayDescrID)) {
                        bMakeTextRedWhenApplicable = true;
                        $(sListPageTimeLeftDisplayDescrID).html = sBiddingEndsTime;
                        //oListPageTimeRemainingDescr.innerHTML = sTimeRemainingText + ": ";
                        $(sListPageTimeRemainingDescr).html(sTimeRemainingText + ": ");
                    }

                    if ($(sListPageTimeLeftDisplayHeaderID)) {
                        $(sListPageTimeLeftDisplayHeaderID).html= sBiddingEndsTime;
                    }

                } else {
                    bMakeTextRedWhenApplicable = false;
                    //document.getElementById('auction' + sEventID).innerHTML = GetBiddingEndsTime(sEndDate, bListPage, nNewSecondsLeft, bMakeTextRedWhenApplicable);
                    $('#auction' + sEventID).html(GetBiddingEndsTime(sEndDate, bListPage, nNewSecondsLeft, bMakeTextRedWhenApplicable));
                    //document.getElementById('auction' + sEventID + 'Remaining').innerHTML = sTimeRemainingText + ": ";
                    $('#auction' + sEventID + 'Remaining').html(sTimeRemainingText + ': ');
                }                
            }

        }
        else {
            
            bMakeTextRedWhenApplicable = false;
            
            //Update time left
            //oCurrentAuctionTimeLeftDisplayText.innerHTML = GetNewTimeRemaining(nNewSecondsLeft, bMakeTextRedWhenApplicable);
            $('#auction' + sEventID).html(GetNewTimeRemaining(nNewSecondsLeft, bMakeTextRedWhenApplicable));

            //Update "Sale Ends" text
            if (document.getElementById('spanSaleEnds' + sEventID) != null) {
                //document.getElementById('spanSaleEnds' + sEventID).innerHTML = document.getElementById('spanSaleEndsTranslation' + sEventID).innerHTML + GetBiddingEndsTime(sEndDate, bListPage, nNewSecondsLeft, bMakeTextRedWhenApplicable);
                //$('#spanSaleEnds' + sEventID).html($('#spanSaleEndsTranslation' + sEventID).html() + GetBiddingEndsTime(sEndDate, bListPage, nNewSecondsLeft, bMakeTextRedWhenApplicable));
                $('#spanSaleEnds' + sEventID).html($('#spanSaleEndsTranslation' + sEventID).html() + GetBiddingOpensTime(sEndDate, false));
            }

            //Update "Bidding Opens" text
            if (document.getElementById('spanBiddingOpensTime' + sEventID) != null) {
                //document.getElementById('spanBiddingOpensTime' + sEventID).innerHTML = GetBiddingOpensTime(dBiddingOpensDate, true);
                $('#spanBiddingOpensTime' + sEventID).html(GetBiddingOpensTime(dBiddingOpensDate, true));
            }
        }
        $(sCurrentTimeDivID).text(nNewSecondsLeft);
    }
}


function CheckHideReserveMetText(nNewHighBid, sEventID) {
    var oReserveMetDisplay = document.getElementById('dvAuctionReserveMetDisplay' + sEventID);

    //Check if the reserve met div is on page.  (DIV is on Details.aspx page, but not list page)
    if (oReserveMetDisplay != null) {
        var nCurrentReservePrice = document.getElementById('dvAuctionReservePrice' + sEventID).innerHTML;
        var sCurrentReserveText = document.getElementById('dvAuctionReserveMetDisplay' + sEventID);
        var sReserveMetText = document.getElementById('dvAuctionReserveMetText').innerHTML;
        var sReserveNotMetText = document.getElementById('dvAuctionReserveNotMetText').innerHTML;
        var sNoReserveText = document.getElementById('dvAuctionNoReserveText').innerHTML;
        var nCurrentReservePrice;

        nCurrentReservePrice = nCurrentReservePrice.replace(/[^\d]/g, '');

        nNewHighBid = nNewHighBid.replace(/[^\d]/g, '');

        if (parseFloat(nCurrentReservePrice) > 0) {

            if (parseFloat(nNewHighBid) >= parseFloat(nCurrentReservePrice)) {

                oReserveMetDisplay.innerHTML = sReserveMetText;
                //oReserveMetDisplay.style.color = sReservePriceStatus_StyleColor;
                //oReserveMetDisplay.style.fontStyle = sReservePriceStatus_StyleFontStyle;
                oReserveMetDisplay.style.fontWeight = sReservePriceStatus_StyleFontWeight;

            }
            else {
                oReserveMetDisplay.innerHTML = sReserveNotMetText;
                oReserveMetDisplay.style.color = "";
                oReserveMetDisplay.style.fontStyle = "";
                oReserveMetDisplay.style.fontWeight = "";
            }
        }
        //This should not be necessary
        else {
            oReserveMetDisplay.innerHTML = sNoReserveText;
            //oReserveMetDisplay.style.color = sReservePriceStatus_StyleColor;
            //oReserveMetDisplay.style.fontStyle = sReservePriceStatus_StyleFontStyle;
            oReserveMetDisplay.style.fontWeight = sReservePriceStatus_StyleFontWeight;
        }

    }

}

function CheckDisplayOpeningBid(dblOpeningBid, dblCurrentBid, sEventID) {
    var spanOpeningBid = document.getElementById('openingbid' + sEventID);
    var oOpenedAtTranslation = document.getElementById('openedattranslation' + sEventID);
    var oOpeningBidMeetsReserveTranslation = document.getElementById('dvAuctionNoReserveText');
    var sLeadingBreak = '<br />';
    var sTrailingBreak = '<br />';
    var sOpeningBidMeetsReserveTranslation = '';

    if (oOpenedAtTranslation != null) {

        if (oOpeningBidMeetsReserveTranslation != null && sURL.indexOf("details.aspx") == -1) {
            sOpeningBidMeetsReserveTranslation = oOpeningBidMeetsReserveTranslation.innerHTML;
        }

        //Check if opening bid div is on page.
        if (spanOpeningBid != null) {

            if ((dblOpeningBid != dblCurrentBid) && (dblOpeningBid.toString() != '$0')) {
                if (sURL.indexOf("userbids.aspx") != -1) {
                    sLeadingBreak = '';
                    sTrailingBreak = '';
                    sOpeningBidMeetsReserveTranslation = '';
                }

                if (sURL.indexOf("details.aspx") != -1) {
                    sTrailingBreak = '';
                }

                spanOpeningBid.innerHTML = sLeadingBreak + oOpenedAtTranslation.innerHTML + " " + dblOpeningBid + sOpeningBidMeetsReserveTranslation + sTrailingBreak;
            }

        }
    }

}

function CheckDisplayNumberOfBids(nNumberOfBids, sEventID) {
    var spanNumberOfBids = document.getElementById('NumBids' + sEventID);

    //Check if opening bid div is on page.
    if (spanNumberOfBids != null) {
        if (nNumberOfBids > 0) {

            //if (location.href.indexOf("details.aspx") != -1)
            if (sURL.indexOf("details.aspx") != -1) {
                if (nNumberOfBids == 1) {
                    var oBidTranslation = document.getElementById('bidtranslation' + sEventID);
                    if (oBidTranslation != null) {
                        spanNumberOfBids.innerHTML = nNumberOfBids + " " + oBidTranslation.innerHTML + "<br>"
                    }
                }
                else {
                    var oBidsTranslation = document.getElementById('bidstranslation' + sEventID);

                    if (oBidsTranslation != null) {
                        spanNumberOfBids.innerHTML = nNumberOfBids + " " + oBidsTranslation.innerHTML + "<br>"
                    }
                }

            }
            else {
                var oNumberOfBidsTranslation = document.getElementById('numberofbidstranslation' + sEventID);

                if (oNumberOfBidsTranslation != null) {
                    spanNumberOfBids.innerHTML = "<br>" + oNumberOfBidsTranslation.innerHTML.replace(/##!!/i, nNumberOfBids);
                }

            }
        }

    }

}

function CheckDisplayBidWillExtendAuction(nSecondsRemaining) {
    var spanBidWillExtendAuction = document.getElementById('spanBidWillExtendAuction');

    if (spanBidWillExtendAuction != null) {
        if (nSecondsRemaining < nSecondsPerHour) {
            spanBidWillExtendAuction.style.display = 'block';

        }
    }
}

function ReloadPageIfProxyHasJustClosed(nNewSecondsRemaining, sCurrentTimeDivID) {
    if ($(sCurrentTimeDivID).length != 0) {
        var URL = new String(window.location);
        var nOldSecondsLeft = $(sCurrentTimeDivID).text();

        //Check if proxy bidding has just closed
        if (nNewSecondsRemaining < nSecondsPerDay && nOldSecondsLeft > nSecondsPerDay) {

            URL = URL.toLowerCase();

            if (URL.indexOf('details.aspx', 0) > 0) {
                RefreshPage();
            }
        }
    }
}

function RefreshPage() {
    window.location = window.location;
}

function StartEventTimer(sEventsOnPage) {
    sEventsToCheckForOnPage = sEventsOnPage;

    aTimersToMonitor = sEventsToCheckForOnPage.split(',');
    oAuctionTimer = setInterval("CheckCountdown()", 1000);
}

function InitializeListPage(sTimeRemaining, sBiddingOpens, sBidNow) {
    sTimeRemainingText = sTimeRemaining;
    sBiddingOpensText = sBiddingOpens;
    sBidNowText = sBidNow;
    bListPage = true;
}

//<summary>
//Method that will decrement all the auction countdown values by 1
//</summary>
function CheckCountdown() {
    nAuctionCounter = nAuctionCounter + 1;

    if (nAuctionCounter == nSecondsBetweenAJAXRequests) {
        CheckForUpdatedAuctions();        

        nAuctionCounter = 0;
    }

    for (i = 0; i < aTimersToMonitor.length; i++) {
        if (aTimersToMonitor[i].length > 0) {
            DecrementSecondsLeft(aTimersToMonitor[i].toString().toUpperCase());
        }
    }
}

//<summary>
//Method that will do a AJAX request back to the server to get the updated end times and prices
//</summary>
function CheckForUpdatedAuctions() {
    var sEventIDs = '';

    for (i = 0; i < aTimersToMonitor.length; i++) {
        sEventIDs = sEventIDs + aTimersToMonitor[i] + ',';
    }

    var sAJAXURL = "/onlineauctions/auctioninformation/auctioninformation.aspx?CurrentTime=" + new Date().getTime() + "&eventids=" + sEventIDs;

    $.ajax({
        url: sAJAXURL,

        type: 'GET',

        success: function(data) {
            NewProcessRequest(data);
        },

        error: function(data) {
            //Since there was an error and we don't have the latest OAL info, refresh the page.            
            refreshPageDueToBadDataFromAJAX();
        }
    });

}

function NewProcessRequest(data) {

    if (data != null && data != '') {
        ParseResults(data);
    } else {
        refreshPageDueToBadDataFromAJAX();
    }
}

function ProcessRequest() {

    if (xmlHttp.readyState == 4) {
        ParseResults(xmlHttp.responseText);
    }
}

///<summary>
///Method that will decrement the current eventIDs timer
///</summary>
function DecrementSecondsLeft(sEventID) {

    //Set the current element to the element on the page
    var oCurrentElement;
    var oTimeRemainingElement;
    var bUpdateTimeRemaining = true;
    var nSecondsRemaining;
    var sElementID;

    if (bListPage == true) {
        oCurrentElement = document.getElementById('dvAuction' + sEventID + 'Headerhdn');
        sElementID = '#dvAuction' + sEventID + 'Headerhdn';
    } else {
        oCurrentElement = document.getElementById('dvAuction' + sEventID + 'hdn');
        sElementID = '#dvAuction' + sEventID + 'hdn';
    }

    if (oCurrentElement != null) {
        nSecondsRemaining = GetSecondsRemainingFromDiv(oCurrentElement);

        if (nSecondsRemaining > 0) {

            if (bListPage) {
                if (nSecondsRemaining == nSecondsPerDay) {
                    document.getElementById('auction' + sEventID + 'RemainingDescr').innerHTML = sTimeRemainingText + ": ";
                    ChangeViewNowLinks(sEventID);
                }

                if (nSecondsRemaining > nSecondsPerDay) {
                    bUpdateTimeRemaining = false;
                }
            } else {

                //Once live bidding opens for an OAL, we want to check for new information every 5 seconds.
                if (nSecondsRemaining <= nSecondsPerDay) {
                    nSecondsBetweenAJAXRequests = 5;
                }

                if (nSecondsRemaining == (nSecondsPerDay - 1)) {
                    window.location.reload();
                }
            }

            $(sElementID).text((nSecondsRemaining - 1));

            DisplayClock(nSecondsRemaining, sEventID);

            if (bUpdateTimeRemaining) {
                var bMakeTextRedWhenApplicable;

                if (bListPage == true) {
                    if (document.getElementById('auction' + sEventID + 'Descr')) {
                        bMakeTextRedWhenApplicable = true;
                        //document.getElementById('auction' + sEventID + 'Descr').innerHTML = GetNewTimeRemaining(nSecondsRemaining, bMakeTextRedWhenApplicable);
                        $('#auction' + sEventID + 'Descr').html(GetNewTimeRemaining(nSecondsRemaining, bMakeTextRedWhenApplicable));
                    }

                    if (document.getElementById('auction' + sEventID + 'Header')) {
                        bMakeTextRedWhenApplicable = false;
                        //document.getElementById('auction' + sEventID + 'Header').innerHTML = GetNewTimeRemaining(nSecondsRemaining, bMakeTextRedWhenApplicable);
                        $('#auction' + sEventID + 'Header').html(GetNewTimeRemaining(nSecondsRemaining, bMakeTextRedWhenApplicable));
                    }

                } else {
                    bMakeTextRedWhenApplicable = true;
                    //document.getElementById('auction' + sEventID).innerHTML = GetNewTimeRemaining(nSecondsRemaining, bMakeTextRedWhenApplicable);
                    $('#auction' + sEventID).html(GetNewTimeRemaining(nSecondsRemaining, bMakeTextRedWhenApplicable));
                }
            }
        }
        else {
            AuctionEnded(sEventID);
        }
    }
}

//<summary>
//Method that will parse the results returned from the server
//</summary>
function ParseResults(sResults) {

    if (sResults.indexOf('good') >= 0) {
        var sNewResults = sResults.substring(5);
        var arrResults = sNewResults.split('!');
        var sEventID;
        var arrAuction;
        var sTimeRemainingID;

        for (i = 0; i < arrResults.length; i++) {

            arrAuction = arrResults[i].split('|');

            if (arrAuction.length >= 3) {
                sEventID = arrAuction[0];

                if (bListPage == true) {
                    sTimeRemainingID = '#dvAuction' + sEventID + 'Headerhdn'
                } else {
                    sTimeRemainingID = '#dvAuction' + sEventID + 'hdn'
                }

                if ($(sTimeRemainingID) != null) {

                    ReloadPageIfProxyHasJustClosed(arrAuction[1], sTimeRemainingID);

                    CheckTimeExtended(arrAuction[1], arrAuction[5], sTimeRemainingID, sEventID);

                    CheckDisplayBidWillExtendAuction(arrAuction[1]);

                    if (arrAuction[2].length > 2) {
                        SetCurrentBidText(sEventID, arrAuction[4], arrAuction[2])

                        SetPrice(sEventID, arrAuction[2], arrAuction[4]);

                        CheckHideHighestBidderTable(arrAuction[2]);

                        CheckHideReserveMetText(arrAuction[2], sEventID);

                        CheckDisplayNumberOfBids(arrAuction[3], sEventID);

                        CheckDisplayOpeningBid(arrAuction[4], arrAuction[2], sEventID);
                    }

                }
            }
        }
    }
}

function getParameterByName(sName) {
    sName = sName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    
    var regexS = "[\\?&]" + sName + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href.toLowerCase());
    
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function refreshPageDueToBadDataFromAJAX() {
    var nCurrentRefreshCount;
    var bRefreshPage = false;
    var nNewRefreshCount;

    nCurrentRefreshCount = getParameterByName('refreshcnt');

    if (nCurrentRefreshCount.length != 0) {
        //Check how many times the page has refreshed
        if (isNaN(nCurrentRefreshCount)) {
            bRefreshPage = true;
            nNewRefreshCount = 1;
        } else {
            if (parseInt(nCurrentRefreshCount) < 3) {
                bRefreshPage = true;
                nNewRefreshCount = parseInt(nCurrentRefreshCount) + 1;
            }
        }
    } else {
        bRefreshPage = true;
        nNewRefreshCount = 1;
    }

    if (bRefreshPage == true) {
        if (nNewRefreshCount > 1) {
            //RefreshCnt is already in querystring so we can just update the value
            sURL = sURL.replace('refreshcnt=' + nCurrentRefreshCount, 'refreshcnt=' + nNewRefreshCount);
        } else {
            //Check to see if there are query string values.
            if (sURL.indexOf("?") != -1) {
                sURL += '&';
            } else {
                sURL += '?';
            }

            sURL += 'refreshcnt=' + nNewRefreshCount;
        }       

        window.location = sURL;
    }
    
}
