﻿// ------------------------------------------------------------
//	Auction Types: S, P, SP, PM
//	Event Types: NB, FB, HB, OB, W, L, MOBP, NR, BEU, BEL, NBU, US, AP, FE, LIVE, SFA
//	NR = No Rights
//	BEU = Bid Exists for User
//	BEL = Bid Exists and Lot is Live
//	NBU = No Bids For User
//	US = Unsold
//	AP = Approved
//	FE = Feedback Expected
//	LIVE = Lot is Live
//	NORES = No Reserve
//	RESM = Reserve Met
//	RESNM = Reserve not Met
//	LBE = Looser, bid exists
//	WBE = Winner, bid exists
//	OEU = Offer exists for user
//	OA = Offer allowed
//	SFA = Sold For is Active
// ------------------------------------------------------------

function show_box(boxId) {
    var obj = document.getElementById(boxId);
    if (obj)
        obj.style.display = "block";
}
function hide_box(boxId) {
    var obj = document.getElementById(boxId);
    if (obj)
        obj.style.display = "none";
}

function changeMaximumBid(){
	processBidding(standardAuction);
}
function openHistory(){
		show_bidding_history();
}

// --------------------------------------------- Variables -------------------------------------------
var standardAuction = "S";
var purchaseNowAuction = "P";
var makeAnOfferAuction = "M";

var mainStandard = "bid_form_r";
var mainMao = "bid_form_mao";


var spanAtLeastId = "spanAtLeast";
var spanMaxBidAgent = "spanMaxBidAgent";
var spanMaoAtLeastId = "spanMaoAtLeast";

var spanRequestedBidId = "spanRequestedBid";
var spanExpectedBidId = "spanExpectedBid";

var spanConfirmMaoAmountId = "spanConfirmMaoAmount";


var tooLowStandard = "bid_form_too_low";
var bidAgentTooLow = "bid_agent_too_low";
var tooLowMao = "bid_form_mao_too_low";



var confirmStandard = "bid_form_confirmation";
var confirmStandardReserveNotMet = "bid_form_confirmation_rnm";
var confirmStandardReserveMet = "bid_form_confirmation_rm";
var confirmPurchase = "bid_form_purchase_now";
var confirmMao = "bid_form_mao_confirm";

var loginStandardId = "login_form_r";
var noRightsStandardId = "bid_form_restricted";		// HIGH ATTENTION !!!!!
var loginPurchaseId = "purchase_login_form";
var loginMaoId = "mao_login_form";
var confirmationAmount = 0;
var confirmationMaoAmount = 0;

// ---------------------------------------------------------------------------------------------------

function pushToRestoreDialog(auctionType) {
    if(typeof(AdditionalParams) != "undefined")
	    AdditionalParams.push("&bidDialog=" + auctionType);
}

function restoreDialogAfterLogin(){
	if (currentAuction != "" && location.search.indexOf("bidDialog") >= 0){
		if (!isOwner)
			processBidding(currentAuction);
	}
}

function processBidding(auctionType){
	// show first dialog
	// return if not logged in
    if (!isLoggedIn) {
		show_box(getLoginFirm(auctionType));
		return;
	}
	else
		if (isRestricted == "true"){
			show_box(noRightsStandardId);
			return;
		}
	
	// if logged in - show first dialog's div
	switch(auctionType){
		case standardAuction: show_box(mainStandard); break;
		case purchaseNowAuction: show_box(confirmPurchase); break;
		case makeAnOfferAuction: show_box(mainMao); break;
	}		
}
function continueFromMain(auctionType){
	var value = getMainValue(auctionType);
	hideAllDivs();
	isValueTooLow(auctionType, value);
}
function continueFromTooLow(auctionType){
	var value = getTooLowValue(auctionType);
	isValueTooLow(auctionType, value);
}
function showConfirmation(auctionType){
	// HIGH ATTENTION !!!!!
	switch(auctionType){
		case standardAuction:{
			// check expected value
			WebService.GetPreBidInfo(lotId,confirmationAmount, standardAuctionConfirmCallback);
		}break;
		case makeAnOfferAuction:{
			// show offer with specific amount
			//performAction(makeAnOfferAuction);
			doMakeAnOfferAuction(confirmationMaoAmount);
		}break;
	}
}

function standardAuctionConfirmCallback(arg){
	// receive arg - PreBidInfo
	hideAllDivs();
	var requestedValueAmount = arg.RequestedValueAmount;
	var expectedValueAmount = arg.ExpectedValueAmount;
	if (!arg.IsReserveExists)
		show_box(confirmStandard);
	else
		if (arg.IsReserveMetForUser)
				show_box(confirmStandardReserveMet);
			else{
				show_box(confirmStandardReserveNotMet);
				}
	setInnerTextToSpanWithAttribute(spanRequestedBidId, requestedValueAmount);
	setInnerTextToSpanWithAttribute(spanExpectedBidId, expectedValueAmount);
}

function doMakeAnOfferAuction(arg){
	// arg - the value from confirmationMaoAmount
	hideAllDivs();
	show_box(confirmMao);
	setInnerText(spanConfirmMaoAmountId, arg);
}

	
function performAction(auctionType){
	switch(auctionType){
		case standardAuction: WebService.ProcessBid(lotId, confirmationAmount, getProxyLogin(), performActionCallback); break;
		case purchaseNowAuction: WebService.PurchaseNow(lotId, performActionCallback); break;
		case makeAnOfferAuction: WebService.MakeAnOffer(lotId, confirmationMaoAmount, performActionCallback); break;
	}
}

// ------------------------------------------ Perform Callbacks --------------------------------------
function performActionCallback(arg){	
	hideAllDivs();
	refreshBidInfo();
}
// ---------------------------------------------------------------------------------------------------


function getMainValue(auctionType){
	var obj = document.getElementById(getMainValueId(auctionType));
	if (obj)
		return obj.value;
	else
		return "0";
}
function getTooLowValue(auctionType){
	var obj = document.getElementById(getTooLowValueId(auctionType));
	if (obj)
		return obj.value == "" ? 0 : obj.value;
	else
		return "0";
}

function getMainValueId(auctionType){
	switch(auctionType){
		case standardAuction: return mainStandardValueId; break;
		case makeAnOfferAuction: return mainMaoValueId; break;
	}
	return mainStandardValueId;
}
function getTooLowValueId(auctionType){
	switch(auctionType){
		case standardAuction: return tooLowStandardValueId; break;
		case makeAnOfferAuction: return tooLowMaoValueId; break;
	}
	return tooLowStandardValueId;
}	
function getTooLowDiv(auctionType){
	switch(auctionType){
		case standardAuction: return tooLowStandard; break;
		case makeAnOfferAuction: return tooLowMao; break;
	}
	return tooLowStandard;
}

function getConfirmationDiv(auctionType){
	switch(auctionType){
		case standardAuction: return confirmStandard; break;
		case purchaseNowAuction: return confirmPurchase; break;
		case makeAnOfferAuction: return confirmMao; break;
	}
	return confirmStandard;
}

// Must call only for STANDARD or MAO case
function getMainDiv(auctionType){
	switch(auctionType){
		case standardAuction: return mainStandard; break;
		case makeAnOfferAuction: return mainMao; break;
	}
	return "";
}

function getLoginFirm(auctionType){
	switch(auctionType){
		case standardAuction: return loginStandardId; break;
		case purchaseNowAuction: return loginPurchaseId; break;
		case makeAnOfferAuction: return loginMaoId; break;
	}
	return loginStandardId;
}
// ------------------------------------------ isValueTooLow ------------------------------------------
function isValueTooLow(auctionType, value){
	if (auctionType==standardAuction)
		WebService.IsBidTooLow(lotId, value, bidTooLowCallback);
	else
		if (auctionType==makeAnOfferAuction)
			WebService.IsMaoTooLow(lotId, value, maoTooLowCallback);
}
function bidTooLowCallback(arg){
	// if is not too low -- show confirmation and exit
	if (!arg.IsTooLow && !arg.IsBidAgentTooLow){
		confirmationAmount = arg.RequestedAmount;
		showConfirmation(standardAuction);
		return;
	}
	
	if (arg.IsTooLow){
		// else show too low
		show_box(tooLowStandard);
		setInnerText(spanAtLeastId, arg.ExpectedValueAmount);
	}
	else{
		// else show too low
		show_box(bidAgentTooLow);
		setInnerText(spanMaxBidAgent, arg.MaxBidAgentValueAmount);
	}
}
function maoTooLowCallback(arg){
	// if is not too low -- show confirmation and exit
	if (!arg.IsTooLow){
		confirmationMaoAmount = arg.RequestedAmount;
		showConfirmation(makeAnOfferAuction);
		return;
	}
	
	// else show too low
	show_box(tooLowMao);
	//alert(arg.ExpectedValueAmount);
	setInnerText(spanMaoAtLeastId, arg.ExpectedValueAmount);
}
// ---------------------------------------------------------------------------------------------------
function hideAllDivs(){
	hide_box(mainStandard);		
	hide_box(mainMao);		
	hide_box(tooLowStandard);		
	hide_box(tooLowMao);		
	hide_box(confirmStandard);		
	hide_box(confirmStandardReserveMet);		
	hide_box(confirmStandardReserveNotMet);		
	hide_box(confirmPurchase);		
	hide_box(confirmMao);		
	hide_box(loginStandardId);		
	hide_box(noRightsStandardId);		
	hide_box(loginPurchaseId);		
	hide_box(loginMaoId);		
	hide_box(bidAgentTooLow);		
}
function refreshBidInfo(){
	// divsProcessor.reachBidInfo();
	performReaching();
}

// check is types set contains the specific pattern
function isInType(pattern, typesSet){
	
	if (typesSet && pattern){
		var arr = typesSet.split(",");
		if (arr){
			for(var i = 0; i < arr.length; i++){
				if (arr[i].trim()==pattern.trim())
					return true;
			}
		}
	}
	return false;
}
function hideAllSpansWithAttributes(){
	var spans = document.getElementsByTagName("div");
	if (spans){
		for(var i = 0; i < spans.length; i++){
			var auctions = spans[i].getAttribute("auctions");
			var events = spans[i].getAttribute("events");
			if (auctions && events)
				spans[i].style.display="none";
		}
	}
}
function getSpansByAttributes(auctionAttributeValue, eventAttributeValue){
	var spans = document.getElementsByTagName("div");
	var result = new Array();
	if (spans){
		for(var i = 0; i < spans.length; i++){
			var auctions = spans[i].getAttribute("auctions");
			var events = spans[i].getAttribute("events");
			if (isInType(auctionAttributeValue, auctions) && isInType(eventAttributeValue, events))
				result.push(spans[i]);
		}
	}
	return result;
}
function showSpansWithAttributes(auctionAttributeValue, eventAttributeValue){
	var spans = getSpansByAttributes(auctionAttributeValue, eventAttributeValue);
	if (spans)
		for(var i = 0; i < spans.length; i++)
			spans[i].style.display = "";
}
function hideSpansWithAttributes(auctionAttributeValue, eventAttributeValue){
	var spans = getSpansByAttributes(auctionAttributeValue, eventAttributeValue);
	if (spans)
		for(var i = 0; i < spans.length; i++)
			spans[i].style.display = "none";
}
function getElementsWithAttributes(tagName, attributeName){
	var result = new Array();
	var spans = document.getElementsByTagName(tagName);
	if (spans){
		for(var i = 0; i < spans.length; i++){
			if (spans[i].getAttribute(attributeName))
				result.push(spans[i]);
		}
	}
	return result;
}
function setInnerTextToSpanWithAttribute(attributeName, value){
	var spans = getElementsWithAttributes("span", attributeName);
	if (spans)
		for(var i = 0; i < spans.length; i++)
			spans[i].innerHTML = value;
}
function setHrefToLinkWithAttribute(attributeName, value){
	var elements = getElementsWithAttributes("a", attributeName);
	if (elements)
		for(var i = 0; i < elements.length; i++){
			//elements[i].setAttribute("href", value);
			elements[i].href = value;
		}
}
function displaySoldFor(amount) {
    setInnerTextToSpanWithAttribute("soldForAmount", amount);
}
function displayBidInfo(bidAmount, purchaseNowAmount, screenName, userProfileLink, totalBids, userMaxBidAmount, userMaxOfferAmount) {
	setInnerTextToSpanWithAttribute("spanBidAmount", bidAmount);
	setInnerTextToSpanWithAttribute("spanPurchaseNowAmount", purchaseNowAmount);
	setInnerTextToSpanWithAttribute("spanUserScreenName", screenName);
	setInnerTextToSpanWithAttribute("spanTotalBids", totalBids);
	setInnerTextToSpanWithAttribute("spanUserMaxBidAmount", userMaxBidAmount);
	setInnerTextToSpanWithAttribute("spanUserMaxOfferAmount", userMaxOfferAmount);
	setHrefToLinkWithAttribute("linkBidderProfile", userProfileLink);

	// Hide Make An Offer button
		if (isOwner || userMaxOfferAmount != null && userMaxOfferAmount != 0) {
			$('#btnMakeAnOfferId').hide();
			$('#makeanofferSectionId').hide();
		}
		else if ($('#bid_form_mao').css("display") == "none" && $('#bid_form_mao_confirm').css("display") == "none") {
			$('#btnMakeAnOfferId').show();
			$('#makeanofferSectionId').show();
		}
}
function hideSpansIfNotAuthenticated(){
    if (!isLoggedIn) {
		var spans = getElementsWithAttributes("span", "requiresAuthentication");
		if (spans)
			for(var i = 0; i < spans.length; i++)
				spans[i].style.display="none";
	}
}

// ---------------------------------------------------------------------
///	DivsControl.ascx handlers
// ---------------------------------------------------------------------


function onSuspendedHandler(arg){
	// HIGH ATTENTION !!!!!
	//showSpansWithAttributes(currentAuctionType, "NB");
	hideAllSpansWithAttributes();
	showSpansWithAttributes(currentAuctionType, "NR");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.WinnerScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onFeedbackExpectedHandler(arg) {
	showSpansWithAttributes(currentAuctionType, "FE");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.WinnerScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onWinnerHandler(arg) {
	hideAllSpansWithAttributes();
	showSpansWithAttributes(currentAuctionType, "W");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.WinnerScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onWinnerBidExistsHandler(arg) {
	showSpansWithAttributes(currentAuctionType, "WBE");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.WinnerScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onLooserHandler(arg) {
	hideAllSpansWithAttributes();
	showSpansWithAttributes(currentAuctionType, "L");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.WinnerScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onLooserBidExistsHandler(arg) {
	showSpansWithAttributes(currentAuctionType, "LBE");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.WinnerScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onUnsoldHandler(arg){
	hideAllSpansWithAttributes();
	showSpansWithAttributes(currentAuctionType, "US");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.WinnerScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onLiveHandler(arg){
	showSpansWithAttributes(currentAuctionType, "LIVE");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.WinnerScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onApprovedHandler(arg){
	hideAllSpansWithAttributes();
	showSpansWithAttributes(currentAuctionType, "AP");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.WinnerScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function highBidderHandler(arg){
	hideAllSpansWithAttributes();
	showSpansWithAttributes(currentAuctionType, "HB");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function outBidderHandler(arg) {
	if(arg.BidsForUser > 0){
		//hideAllSpansWithAttributes();
		showSpansWithAttributes(currentAuctionType, "OB");
		displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
		hideSpansIfNotAuthenticated();
	}	
}
function onNoBidsPlacedHandler(arg) {
	//hideAllSpansWithAttributes();
	showSpansWithAttributes(currentAuctionType, "NB");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onNoBidsForUserHandler(arg) {
	hideAllSpansWithAttributes();
	showSpansWithAttributes(currentAuctionType, "NBU");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onOfferExistsForUserHandler(arg) {
	 //hideAllSpansWithAttributes();
    showSpansWithAttributes(currentAuctionType, "OEU");
    displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
    hideSpansIfNotAuthenticated();
}
function onBidExistsForUserHandler(arg) {
	hideAllSpansWithAttributes();
	showSpansWithAttributes(currentAuctionType, "BEU");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onBidExistsForLiveLotHandler(arg){
	showSpansWithAttributes(currentAuctionType, "BEL");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function onOfferAllowedHandler(arg) {
    showSpansWithAttributes(currentAuctionType, "OA");
    displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
    hideSpansIfNotAuthenticated();
}
function firstBidPlacedHandler(arg){
	hideAllSpansWithAttributes();
	showSpansWithAttributes(currentAuctionType, "FB");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function moreThanOneBidPlacedHandler(arg){
//	hideAllSpansWithAttributes();	
	showSpansWithAttributes(currentAuctionType, "MOBP");
	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function soldForActive(arg) {
    //	hideAllSpansWithAttributes();	
    showSpansWithAttributes(currentAuctionType, "SFA");
    displaySoldFor(arg.SoldForAmount);
    hideSpansIfNotAuthenticated();
}

function reservePriceHandler(arg){
	//addOnReservePriceListener
	
	
		if (arg.ReservePrice <= 0)
			{
			
			showSpansWithAttributes(currentAuctionType, "NORES");
			}
			else 
			if (arg.TotalBids > 0){
				if (arg.ReservePrice > arg.CurrentBid)
					showSpansWithAttributes(currentAuctionType, "RESNM");
						else
							showSpansWithAttributes(currentAuctionType, "RESM");
			}


			displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
	hideSpansIfNotAuthenticated();
}
function reachedHandler(arg){
	
//	hideAllSpansWithAttributes();	
//	showSpansWithAttributes(currentAuctionType, "MOBP");
    //	displayBidInfo(arg.CurrentBidAmount, arg.PurchaseNowAmount, arg.ScreenName, arg.ProfileUrl, arg.TotalBids, arg.CurrentUsersMaximumBidAmount, arg.CurrentUserMaxOfferAmount);
//	hideSpansIfNotAuthenticated();
}

function displayLastAlert()
{
	WebService.GetLastAlert(lotId, getLastAlertCallback, getLastAlertErrorHandler, getLastAlertErrorHandler);
}
function getLastAlertErrorHandler(arg){
}

function getLastAlertCallback(arg){
	var glaControl = document.getElementById('spanAlertMessage');
	try
	{
		if(glaControl){
			if(arg == null || arg == '')
				glaControl.style.display= 'none';
			else{
				glaControl.innerHTML = arg;
				glaControl.style.display = 'inline';
			}
		}
	}
	catch(err)
	{
		if(glaControl)
		{
			glaControl.style.display= 'none';
		}
	}
}
/// Manage divs on the LotDetail's page
function MessageDetailsProcessor(bidInfoMethod, lotId){
	// constructor
	
	// events
	
	this.onReachedEvents = new Array();
	this.onFeedbackExpectedEvents = new Array();
	this.onWinnerEvents = new Array();
	this.onWinnerBidExistsEvents = new Array();
	this.onLooserEvents = new Array();
	this.onLooserBidExistsEvents = new Array();
	this.onUnsoldEvents = new Array();
	this.onApprovedEvents = new Array();
	this.onLiveEvents = new Array();
	this.onReservePriceEvents = new Array();
	this.onOfferAllowedEvents = new Array();
	
	
	this.onSuspendedEvents = new Array();
	this.onHighBidderEvents = new Array();
	this.onOutBidderEvents = new Array();
	
	this.onNoBidsPlacedEvents = new Array();
	this.onNoBidsForUserEvents = new Array();
	this.onBidExistsForUserEvents = new Array();
	this.onBidExistsForLiveLotEvents = new Array();
	this.onFirstBidPlacedEvents = new Array();
	this.onMoreThanOneBidsEvents = new Array();
	this.onOfferExistsForUserEvents = new Array();
	this.onSoldForActiveEvents = new Array();
	
	this.displayCommonInfoDelegate = null;
	
	this.bidInfoMethod = bidInfoMethod;
	this.lotId = lotId;
	this.reachCompleteScript = new Array();
};

MessageDetailsProcessor.prototype.raiseEvents = function(events, args) {
	if (events != null && typeof (events) != "undefined") {
		var isArray = typeof (events.length) != "undefined";
		if (isArray) {
			for (var i = 0; i < events.length; i++) {
				events[i](args);
			}
		}
		else {
			events(args);
		}
	}
	latestBidInfo = args;
};

// Methods
MessageDetailsProcessor.prototype.reachError = function(bidInfo) {
	if (bidInfo) {
		//alert("Refresh bid status failed. Please refresh this page manually to resolve error. To help us resolve this issue as soon as possible please contact to Client Services if this message will be appeared again. \nThanks");
		this.raiseEvents(this.reachCompleteScript);
	}
};
MessageDetailsProcessor.prototype.reachTimeout = function(bidInfo) {
	if (bidInfo) {
		//alert("Refresh bid status failed. Please refresh this page manually to resolve error. To help us resolve this issue as soon as possible please contact to Client Services if this message will be appeared again. \nThanks");
		this.raiseEvents(this.reachCompleteScript);
	}
};
MessageDetailsProcessor.prototype.handleEvents = function(bidInfo) {
	if (bidInfo) {
		if (bidInfo.IsUserSuspended) {
			// user is suspended
			this.raiseEvents(this.onSuspendedEvents, bidInfo);
		}
		else {
			if (bidInfo.IsAuctionStoped) {
				// if auction is stoped
				if (bidInfo.StatusCode == "US") {
					// auction is unsold
					this.raiseEvents(this.onUnsoldEvents, bidInfo);
				}
				else {
					if (bidInfo.StatusCode == "AP") {
						// auction is approved
						this.raiseEvents(this.onApprovedEvents, bidInfo);
					}
					else {
						if (bidInfo.IsCurrentUserWinner) {
							// is user winner
							this.raiseEvents(this.onWinnerEvents, bidInfo);
							
							//if (bidInfo.BidsForUser > 0)
								this.raiseEvents(this.onWinnerBidExistsEvents, bidInfo);
						}
						else {
							// user is looser
							this.raiseEvents(this.onLooserEvents, bidInfo);
							if (bidInfo.BidsForUser > 0)
								this.raiseEvents(this.onLooserBidExistsEvents, bidInfo);
						}
						if (bidInfo.IsFeedbackExpected)
							this.raiseEvents(this.onFeedbackExpectedEvents, bidInfo);
					}

				}

			}
			else {
				if (bidInfo.BidsForUser > 0)
					this.raiseEvents(this.onBidExistsForUserEvents, bidInfo);
				else
					if (bidInfo.BidsForUser == 0)
					this.raiseEvents(this.onNoBidsForUserEvents, bidInfo);
				// if no any bids
				if (bidInfo.TotalBids == 0) {
					this.raiseEvents(this.onNoBidsPlacedEvents, bidInfo);
				}
				else {
					// if bid is first
					if (bidInfo.TotalBids == 1) {
						this.raiseEvents(this.onFirstBidPlacedEvents, bidInfo);
					}
					else {
						if (bidInfo.IsCurrentUserHighest) {
							// if current user is highest bidder
							this.raiseEvents(this.onHighBidderEvents, bidInfo);
						}
						else {
							// if isn't highest bidder
							this.raiseEvents(this.onOutBidderEvents, bidInfo);
						}
					}
					this.raiseEvents(this.onBidExistsForLiveLotEvents, bidInfo);
				}
				if (bidInfo.StatusCode == "LI") {
					this.raiseEvents(this.onLiveEvents, bidInfo);
				}

			}
            if (bidInfo.IsSoldForActive == true) {
                this.raiseEvents(this.onSoldForActiveEvents, bidInfo);
            }
			this.raiseEvents(this.onReservePriceEvents, bidInfo);
			if (bidInfo.TotalBids > 0)
				this.raiseEvents(this.onMoreThanOneBidsEvents, bidInfo);
			if (!bidInfo.IsCurrentUserWinner && bidInfo.CurrentUserMaxOffer != null && bidInfo.CurrentUserMaxOffer != 0)
				this.raiseEvents(this.onOfferExistsForUserEvents, bidInfo);
			if (bidInfo.OfferAllowed == true)
				this.raiseEvents(this.onOfferAllowedEvents, bidInfo);
		}
	}
	this.raiseEvents(this.onReachedEvents, bidInfo);

	//	// hardcoded area
	//	// *******************************************
	//	try{
	//		
	//		var timeLast = document.getElementById(endTimeLastSpanId);
	//		var timeEnd = document.getElementById(endTimeInfoSpanId);
	//		if (timeLast && timeEnd){
	//			timeLast.innerHTML = bidInfo.AuctionLastTimeMessage;
	//			timeEnd.innerHTML = bidInfo.AuctionEndTimeMessage;
	//		}
	//		
	//	}
	//	catch(err){
	//	}
	//	// *******************************************

	if (bidInfo) {
		this.raiseEvents(this.reachCompleteScript);
	}
};
MessageDetailsProcessor.prototype.reachBidInfo = function() {
	// call web service to reach a bidInfo
	var caller = this;
	this.bidInfoMethod(this.lotId,
	function(bidInfo) { caller.handleEvents(bidInfo); },
	function(bidInfo) { caller.reachError(bidInfo); },
	function(bidInfo) { caller.reachTimeout(bidInfo); }
	);
};


MessageDetailsProcessor.prototype.addOnReachedListener = function(obj){
	this.onReachedEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnSuspendedListener = function(obj){
	this.onSuspendedEvents.push(obj);
};

MessageDetailsProcessor.prototype.addOnFeedbackExpectedListener = function(obj){
	this.onFeedbackExpectedEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnWinnerListener = function(obj){
	this.onWinnerEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnWinnerBidExistsListener = function(obj){
	this.onWinnerBidExistsEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnBidExistsForUserListener = function(obj){
	this.onBidExistsForUserEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnOfferExistsForUserListener = function(obj) {
    this.onOfferExistsForUserEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnOfferAllowedListener = function(obj) {
    this.onOfferAllowedEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnBidExistsForLiveLotListener = function(obj){
	this.onBidExistsForLiveLotEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnNoBidsForUserListener = function(obj){
	this.onNoBidsForUserEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnReservePriceListener = function(obj){
	this.onReservePriceEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnLooserListener = function(obj){
	this.onLooserEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnLooserBidExistsListener = function(obj){
	this.onLooserBidExistsEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnUnsoldListener = function(obj){
	this.onUnsoldEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnLiveListener = function(obj){
	this.onLiveEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnApprovedListener = function(obj){
	this.onApprovedEvents.push(obj);
};
MessageDetailsProcessor.prototype.addHighBidderListener = function(obj){
	this.onHighBidderEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOutBidderListener = function(obj){
	this.onOutBidderEvents.push(obj);
};
MessageDetailsProcessor.prototype.addOnNoBidsPlacedListener = function(obj){
	this.onNoBidsPlacedEvents.push(obj);	
}
MessageDetailsProcessor.prototype.addFirstBidPlacedListener = function(obj){
	this.onFirstBidPlacedEvents.push(obj);
};
MessageDetailsProcessor.prototype.addMoreThanOneBidsListener = function(obj){
	this.onMoreThanOneBidsEvents.push(obj);
};
MessageDetailsProcessor.prototype.addReachCompleteScript = function(obj) {
	this.reachCompleteScript.push(obj);
};
MessageDetailsProcessor.prototype.addSoldForListener = function(obj) {
    this.onSoldForActiveEvents.push(obj);
};
MessageDetailsProcessor.prototype.setDisplayCommonInfoDelegate = function(customDelegate){
	this.displayCommonInfoDelegate = customDelegate;
};

MessageDetailsProcessor.prototype.displayCommonInfo = function(screenNameId, yourMaxBidId, historyId, currentBidId, bidInfo){
	if (this.displayCommonInfoDelegate != null)
		this.displayCommonInfoDelegate(screenNameId, yourMaxBidId, historyId, currentBidId, bidInfo);
};

MessageDetailsProcessor.prototype.destructor = function(){
};
// JScript File
var globalLotId;
function AddToWatchList(exist, lotId) {
	if (!exist) {
		set_display('login', 'block');
		AdditionalParams.push("&addLotIdToWatchList=" + lotId);
		return;
	}
	globalLotId = lotId;
	WebService.CheckWatchList(lotId, OnComplete, onTimeOut, onError);
}

function AddToWatchListWithCallback(isLoggedIn, lotId, callback) {
	if (!isLoggedIn) {
		set_display('login', 'block');
		if (typeof (focusLogin) != "undefined") {
			focusLogin();
		}
		AdditionalParams.push("&addLotIdToWatchList=" + lotId);
		return;
	}
	globalLotId = lotId;
	WebService.AddToWatchList(globalLotId, function(obj) {
		if (typeof (callback) != "undefined") {
			callback(obj);
		}
		if (obj.isAdded) {
			// display notification
			if (typeof (notifyAddLot) != "undefined") {
				notifyAddLot(globalLotId);
			}
		}
	});
}

function RemoveFromWatchListWithCallback(lotId, callback) {
	globalLotId = lotId;
	WebService.DelFromWatchList(lotId, function(arg) {
		if (typeof (callback) != "undefined") {
			callback(true);
		}
	});
}

function RemoveFromWatchList(lotId) {
	globalLotId = lotId;
	WebService.DelFromWatchList(lotId, OnCompleteDel, onTimeOutDel, onErrorDel);
}


function OnComplete(arg) {
	if (arg == 0) {
		WebService.AddToWatchList(globalLotId, OnCompleteIns, onTimeOutIns, onErrorIns);
	}
}

function OnCompleteIns(arg) {
	var addToWatchList = document.getElementById('btnAddToWatch' + globalLotId);
	var delFromWatchList = document.getElementById('btnRemoveFromWatch' + globalLotId);
	addToWatchList.style.display = "none";
	delFromWatchList.style.display = "block";
	if (typeof (notifyAddLot) != "undefined") {
		notifyAddLot(globalLotId);
	}
}

function onTimeOutIns(arg) {
	alert(arg);
}

function onTimeOut(arg) {
	alert(arg);
}

function onErrorIns(arg) {
	alert("Error " + arg);
}

function onError(arg) {
	alert("Error " + arg);
}

function OnCompleteDel(arg) {
	var addToWatchList = document.getElementById('btnAddToWatch' + globalLotId);
	var delFromWatchList = document.getElementById('btnRemoveFromWatch' + globalLotId);
	addToWatchList.style.display = "block";
	delFromWatchList.style.display = "none";
}

function onTimeOutDel(arg) {

}

function onErrorDel(arg) {

}

		// JScript File
		var bid_history_hide_elements;
		var bid_history_agens = "agents";
		var bid_history_bids = "bids";
		var bid_history_mode = bid_history_bids;
		function isBidHistoryActivated(){
			return bid_history_mode == bid_history_bids;
		}
		function activateBidHistory(){
			bid_history_mode = bid_history_bids;
		}
		function activateBidAgentsHistory(){
			bid_history_mode = bid_history_agens;
		}
		
		// ----------------------------- Bid Agents History -----------------------------------------
		function show_bid_agent_history_with_params(lotId, isPositionChange, isPositionChangeId)
		{
			bh_setLotId(lotId);
			activateBidAgentsHistory(); 
			changePosition = isPositionChange;
			changePositionId = isPositionChangeId;
			show_bid_agent_history();
		}
		function show_bid_agent_history()
		{		
			lotId = bh_getLotId();
			bidTypeCodes = document.getElementById('hBidTypeCodes').value;
			bidStateCodes = document.getElementById('hBidStateCodes').value;
			
			pageIndex = document.getElementById('hPageIndex').value;
			pageSize = document.getElementById('hPageSize').value;
			
			lotId = bh_getLotId();
			bidTypeCodes = document.getElementById('hBidTypeCodes').value;
			bidStateCodes = document.getElementById('hBidStateCodes').value;
			extendedView = document.getElementById('hExtendedView').value;

			WebService.ReachBidAgentHistory(lotId, pageIndex, pageSize, true, onReachBidAgentsHistoryCompleted);
		}
		
		function onReachBidAgentsHistoryCompleted(arg)
		{
			hideBeforeBiddingHistory();
			set_display('biddingHistory','block');
			obj = document.getElementById('spanContent');
			if(obj)
			{
				obj.innerHTML = arg.Data;
			}
			document.getElementById('biddingHistoryCloseLink').style.display = 'inline';
			document.getElementById('biddingHistoryCloseText').style.display = 'none';
			
			RefreshPaging(arg.RowsCount);
		}
		// ------------------------------------------------------------------------------------------
		
		
		function showHistory(){
			if (isBidHistoryActivated()) {
				show_bidding_history();
			}
			else
				show_bid_agent_history();
		}
		
		
		function show_bidding_history_with_params(lotId, isPositionChange, isPositionChangeId)
		{
			bh_setLotId(lotId);
			activateBidHistory();
			changePosition = isPositionChange;
			changePositionId = isPositionChangeId;
			
			
			showHistory();			
		}
		
		function show_bidding_history()
		{
			lotId = bh_getLotId();
			bidTypeCodes = document.getElementById('hBidTypeCodes').value;
			bidStateCodes = document.getElementById('hBidStateCodes').value;
			
			pageIndex = document.getElementById('hPageIndex').value;
			pageSize = document.getElementById('hPageSize').value;
			
			lotId = bh_getLotId();
			bidTypeCodes = document.getElementById('hBidTypeCodes').value;
			bidStateCodes = document.getElementById('hBidStateCodes').value;
			extendedView = document.getElementById('hExtendedView').value;
			WebService.ReachBiddingHistory(lotId, bidTypeCodes, bidStateCodes, pageIndex, pageSize, extendedView == 1, onReachHistoryComplite);
		}

		function onReachHistoryComplite(arg)
		{
			hideBeforeBiddingHistory();
			set_display('biddingHistory','block');
			obj = document.getElementById('spanContent');
			if(obj)
			{
				obj.innerHTML = arg.Data;
			}
			document.getElementById('biddingHistoryCloseLink').style.display = 'inline';
			document.getElementById('biddingHistoryCloseText').style.display = 'none';
			
			RefreshPaging(arg.RowsCount);
		}

		function RefreshPaging(rowsCount)
		{
			pageIndex = document.getElementById('hPageIndex').value;
			pageSize = document.getElementById('hPageSize').value;
			pagesCount = Math.ceil(rowsCount / pageSize);
			
			if(pageIndex == 1)
			{
				hide_div('imgPreviousPage');
				set_display('imgPreviousPageDisabled','block');
			}
			else
			{
				set_display('imgPreviousPage','block');
				hide_div('imgPreviousPageDisabled');
			}
			
			if(pageIndex == pagesCount)
			{
				hide_div('imgNextPage');
				set_display('imgNextPageDisabled','block');
			}
			else
			{
				set_display('imgNextPage','block');
				hide_div('imgNextPageDisabled');
			}
		}
		
		function NextPage(direction)
		{
			document.getElementById('biddingHistoryCloseLink').style.display = 'none';
			document.getElementById('biddingHistoryCloseText').style.display = 'inline';
			
			pageIndex = document.getElementById('hPageIndex').value;

			if(direction == -1)
			{
				pageIndex++;
			}
			else
			{
				pageIndex--;
			}
			
			document.getElementById('hPageIndex').value = pageIndex;
			
			showHistory();
		}
		
		function RefreshHistory()
		{
			document.getElementById('biddingHistoryCloseLink').style.display = 'none';
			document.getElementById('biddingHistoryCloseText').style.display = 'inline';
			
			$("#spanContent").hide("slow");
			showHistory();
			$("#spanContent").show("slow");
		}
		
		function CloseBiddingHistory()
		{
			document.getElementById('hPageIndex').value = 1;
			set_display('biddingHistory','none');

			NeedRefresh = document.getElementById('hNeedRefresh').value;
			onCloseEventHandler = document.getElementById('hOnDialogClosed').value;
			if(onCloseEventHandler && NeedRefresh == 1)
			{
				eval(onCloseEventHandler);
			}
			showAfterBiddingHistory();
		}
		
		function RetractBid(bidId)
		{
			WebService.RetractBid(bidId, RetractBidCompleted);
		}
		function RetractBidAgent(agentId)
		{
			WebService.RetractBidAgent(agentId, RetractBidCompleted);
		}
		
		function RetractBidCompleted()
		{
			RefreshHistory();
			
			document.getElementById('hNeedRefresh').value = 1;
		}

/***** scrolling popup *****/
function initWScroll()
{
	var _p = document.getElementById('biddingHistory');
	if(_p)
	{
		var _offset = getCurrentYPos();
		var _top = _offset;
		_p.style.marginTop = _top + "px";
	}
	window.onscroll = function()
	{
		var _p1 = document.getElementById('biddingHistory');
		if(_p1)
		{
			var _offset = getCurrentYPos();
			var _top = _offset;
			_p1.style.marginTop = _top+ "px";
		}
	}
}
function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}
function getCurrentYPos()
{
	var _offset = 0;
    if (document.body && document.body.scrollTop)
      _offset =  document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      _offset = document.documentElement.scrollTop;
    if (window.pageYOffset)
      _offset =  window.pageYOffset;
    return _offset;
 }


	if (window.addEventListener){
		window.addEventListener("load", initWScroll, false);
		window.addEventListener("resize", initWScroll, false);
	}
	else if (window.attachEvent){
		window.attachEvent("onload", initWScroll);
		window.attachEvent("onresize", initWScroll);
	}
	
function trim(arg){
    return arg.replace(/^\s*/, "").replace(/\s*$/, "");
}	
	
function getIdsFromString(arg){
	var result = new Array();
	var splitted = arg.toString().split(",");
	if (splitted)
		for(var i = 0; i < splitted.length; i++)
			if (trim(splitted[i]) != "")
				result.push(trim(splitted[i]));
	return result;
}

function showElements(arg){
	if (arg)
		for(var i = 0; i < arg.length; i++){
			var obj = document.getElementById(arg[i]);
			if (obj) {
				obj.style.display = obj.getAttribute("_attr_switch_disp");
			}
		}
}

function hideElements(arg){
	if (arg)
		for(var i = 0; i < arg.length; i++){
			var obj = document.getElementById(arg[i]);
			if (obj) {
				obj.setAttribute("_attr_switch_disp", obj.style.display);
				obj.style.display = "none";
			}
		}
}
function hideBeforeBiddingHistory(){
	if (bid_history_hide_elements)
		hideElements(getIdsFromString(bid_history_hide_elements));
}
function showAfterBiddingHistory(){
	if (bid_history_hide_elements)
		showElements(getIdsFromString(bid_history_hide_elements));
}
function bh_setLotId(lotId){
	var obj = document.getElementById("hBiddingHistoryLotId");
	if (obj)
		obj.value = lotId;
}

function bh_getLotId(){
	
	var obj = document.getElementById("hBiddingHistoryLotId");
	if (obj)
		return obj.value;
	return 0;
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();