﻿
// ****** Code for the Auction pages ****** //
// Personal Sale Ajax Variables
var TryPrice = -1;
var SelectedPrice = -1;
var SelectedPayments = 1;

var refreshTimeoutID  = 0;
var timeLeftTimeoutID = 0;
var attemptTimeLeftTimeoutID = 0;

var flagRefreshOverride = 0;

var RefreshHoldPersonal = false;

var xmlHttpPersonal  = null;
var ajaxHoldPersonal = false;
var AttempLeftMinutes, AttempLeftSeconds;


var timeOut

function RefreshAttemps() 
{
	if (!ajaxHoldPersonal && !RefreshHoldPersonal) 
	{
		var tryPrice = $("PricesBuilder1_txtPrice").value;
		if(tryPrice.length == 0 || isNaN(tryPrice)) tryPrice = 0;
		
		var tryPayments = $("PricesBuilder1_ddlPayments").value;
		if(tryPayments.length == 0 || isNaN(tryPayments)) tryPayments = 0;
	    
		//alert("RefreshAttemptData -> RAD  AiD="+AuctionId + "&AP=0&PY=0&came=" + pageCame);
		if (!ajaxHoldPersonal && !RefreshHoldPersonal) 
		{
			if (flagRefreshOverride == 1)
				PersonalAjaxSendData ("RT", "AiD="+AuctionId + "&PiD="+ProductId + "&TP=0&TPY=0");
			else
				PersonalAjaxSendData ("RT", "AiD="+AuctionId + "&PiD="+ProductId + "&TP="+TryPrice+"&TPY="+tryPayments);
						
			flagRefreshOverride = 0;
		}		
		
		window.clearTimeout(refreshTimeoutID);
		refreshTimeoutID = window.setTimeout("RefreshAttemps()", RefreshTime);
	}
}
function RefreshAttemps_REDIRECT() 
{
	if (!ajaxHoldPersonal && !RefreshHoldPersonal) 
	{
		var tryPrice = $("PricesBuilder1_txtPrice").value;
		if(tryPrice.length == 0 || isNaN(tryPrice)) tryPrice = 0;
		
		var tryPayments = $("PricesBuilder1_ddlPayments").value;
		if(tryPayments.length == 0 || isNaN(tryPayments)) tryPayments = 0;
	    
		//alert("RefreshAttemptData -> RAD  AiD="+AuctionId + "&AP=0&PY=0&came=" + pageCame);
		if (!ajaxHoldPersonal && !RefreshHoldPersonal) 
		{
			if (flagRefreshOverride == 1)
				PersonalAjaxSendData ("RT", "AiD="+AuctionId + "&PiD="+ProductId + "&TP=0&TPY=0");
			else
				PersonalAjaxSendData ("RT", "AiD="+AuctionId + "&PiD="+ProductId + "&TP="+TryPrice+"&TPY="+tryPayments);
						
			flagRefreshOverride = 0;
		}		
		
		window.clearTimeout(refreshTimeoutID);
		refreshTimeoutID = window.setTimeout("RefreshAttemps()", RefreshTime);
	}
}
function SendTryOffer() 
{
	if (RefreshHoldPersonal) return;

	RefreshHoldPersonal = true;
	BlockAttepts();
	SendTryToServer();

	return false;
}

function SendTryToServer()
{
	if (!ajaxHoldPersonal ) 
	{		
		var tryPrice = $("PricesBuilder1_txtPrice").value;
		if(tryPrice.length == 0 || isNaN(tryPrice)) tryPrice = 0;
		
		var tryPayments = $("PricesBuilder1_ddlPayments").value;
		if(tryPayments.length == 0 || isNaN(tryPayments)) tryPayments = 0;
		
		urlData = "AiD="+AuctionId + "&PiD="+ProductId + "&TP="+tryPrice + "&TPY="+tryPayments;
		//alert(urlData);
		DisableAttemptMessage('','');
 		PersonalAjaxSendData ("PT", urlData);
 	
 		RefreshHoldPersonal = false;
 	}
 	else 
 	{
 		window.setTimeout("SendTryToServer()", 100);
 	}
}
function SendBuyOffer() 
{
	if (RefreshHoldPersonal) return;

	RefreshHoldPersonal = true;
	BlockAttepts();
	SendBuyToServer();

	return false;
}
function SendBuyToServer()
{
	if (!ajaxHoldPersonal ) 
	{		
		urlData = "AiD="+AuctionId + "&PiD="+ProductId;
		DisableAttemptMessage('','');
 		PersonalAjaxSendData ("BUY", urlData);
 	
 		RefreshHoldPersonal = false;
 	}
 	else 
 	{
 		window.setTimeout("SendTryToServer()", 100);
 	}
}

//----------//----------------//---------

function UpdateTimeLeft (fullCloseDate, tlH, tlM, tlS)
{	
	try {SetInnerHTML ("OrderConditions1_lblEndDate", fullCloseDate);} 
	catch (e) {}
	
	TimeLeftHours	= tlH;
	TimeLeftMinutes	= tlM;
	TimeLeftSeconds	= tlS;
	
	ShowTimeLeft ();
}

function ShowTimeLeft ()
{
	if(TimeLeftHours > 0 || TimeLeftMinutes > 0 || TimeLeftSeconds > 0) {
		if(TimeLeftSeconds-- == 0) {
			flagRefreshOverride = 1;
			TimeLeftSeconds = 59;
			if(TimeLeftMinutes-- == 0) {
				TimeLeftMinutes = 59;
				TimeLeftHours--;
			}
		}
	} else {
		TimeLeftHours	= 0;
		TimeLeftMinutes = 0;
		TimeLeftSeconds = 0;
	}
	
	if(TimeLeftHours < 10) SetInnerHTML ("lblHours", "0" + TimeLeftHours);
	else SetInnerHTML ("lblHours", TimeLeftHours);
			
	if(TimeLeftMinutes < 10) SetInnerHTML ("lblMinutes", "0" + TimeLeftMinutes);
	else SetInnerHTML ("lblMinutes", TimeLeftMinutes);
	
	if(TimeLeftSeconds < 10) SetInnerHTML ("lblSeconds", "0" + TimeLeftSeconds);
	else SetInnerHTML ("lblSeconds", TimeLeftSeconds);
	
	window.clearTimeout(timeLeftTimeoutID);
	timeLeftTimeoutID = window.setTimeout("ShowTimeLeft()", 1000);
}

function UpdateAttemptTimeLeft (tlM, tlS)
{	
//alert(tlM + ":" + tlS)
	window.clearTimeout(attemptTimeLeftTimeoutID);
	AttempLeftMinutes	= tlM;
	AttempLeftSeconds	= tlS;
	
	ShowAttemptTimeLeft ();
}
function ShowAttemptTimeLeft ()
{
	if(AttempLeftMinutes > 0 || AttempLeftSeconds > 0)
	{
		if(AttempLeftSeconds-- == 0) 
		{
			flagRefreshOverride = 1;
			AttempLeftMinutes--;
			AttempLeftSeconds = 59;
		}
	}
	else 
	{
		AttempLeftMinutes = 0;
		AttempLeftSeconds = 0;
		RefreshAttemps();
	}
	
	var time = "<br>נותרו: ";
	
	if(AttempLeftMinutes < 10) time = time + "0" + AttempLeftMinutes;
	else time = time + AttempLeftMinutes;
	
	time = time + ":";
	
	if(AttempLeftSeconds < 10) time = time + "0" + AttempLeftSeconds;
	else time = time + AttempLeftSeconds;
	
	SetInnerHTML("attemptTime", time);
	var h = $("PricesBuilder1_divMessage").style.height;
	if ((h*1) < 55)
		$("PricesBuilder1_divMessage").style.height = "55px";
	
	window.clearTimeout(attemptTimeLeftTimeoutID);
	attemptTimeLeftTimeoutID = window.setTimeout("ShowAttemptTimeLeft()", 1000);
}


function UpdatePrices (currentPrice, currentPayments)
{
	//alert("currentPrice = " + currentPrice + " currentPayments = " + currentPayments);
	$("PricesBuilder1_txtPrice").value = "";
	try	{ $("PricesBuilder1_txtPrice").focus();}
	catch (e) {}
    $("PricesBuilder1_ddlPayments").selectedIndex = currentPayments - 1;
}

function BlockAttepts()
{
	$("btnOrder2").disabled = true;
	$("btnOrder2").style.cursor = "Auto";
	$("PricesBuilder1_btnOrderSB").disabled = true;
	$("PricesBuilder1_btnOrderSB").style.cursor = "Auto";
	$("PricesBuilder1_txtPrice").disabled = true;
	$("PricesBuilder1_ddlPayments").disabled = true;
}
function UpdateAttemptMessage (mess, image, button)
{
	//alert("mess = \n" + mess + " \nimage = \n" + image);
	$("mesResult").style.display = "block";
    SetInnerHTML ("messageText", mess);
    SetInnerHTML ("messageImage", image);
    if (button != '')
    {
		$("divLastChance").style.display = "block";
		SetInnerHTML ("divLastChance", button);
		$("btnOrder2").disabled = true;
		$("btnOrder2").style.cursor = "Auto";
		$("PricesBuilder1_btnOrderSB").disabled = true;
		$("PricesBuilder1_btnOrderSB").style.cursor = "Auto"
		$("PricesBuilder1_txtPrice").disabled = true;
		$("PricesBuilder1_ddlPayments").disabled = true;
		$("PricesBuilder1_divMessage").style.height = "95px";
	}
	else 
	{
		$("divLastChance").style.display = "none";
		$("divLastChance").style.cursor = "hand";
		SetInnerHTML ("divLastChance", '');
		$("btnOrder2").disabled = false;
		$("btnOrder2").style.cursor = "hand";
		$("PricesBuilder1_btnOrderSB").disabled = false;
		$("PricesBuilder1_btnOrderSB").style.cursor = "hand"
		$("PricesBuilder1_txtPrice").disabled = false;
		$("PricesBuilder1_ddlPayments").disabled = false;
		//$("PricesBuilder1_divMessage").style.height = "45px";
	} 
}
function DisableAttemptMessage (mess, image)
{
	//alert("mess = \n" + mess + " \nimage = \n" + image);
	$("mesResult").style.display = "none";
    SetInnerHTML ("messageText", mess);
    SetInnerHTML ("messageImage", image);
}
function UpdateAttempsTable (dataTable)
{
	var ctrl = document.getElementById("PricesBuilder1_divWinnersTable");
	if(ctrl != null) 
	{
		SetInnerHTML ("PricesBuilder1_divWinnersTable","");
		SetInnerHTML ("PricesBuilder1_divWinnersTable",  "<div height='100%'>"+dataTable+"</div>");
	}
}

function ShowMessage (engineMessage, flagRefresh)
{
	alert (engineMessage);
	flagRefreshOverride = 1;
	
	if (flagRefresh) RefreshAttemps();
}
function CheckPriceOnEnter(theText, e){
	
	var characterCode = "";
	if (e.which != null)  characterCode = e.which;
	else characterCode = e.keyCode;
	
    if(characterCode == 13) 
    {
		if (objText.value.length == 0 || isNaN(objText.value))
		{
			alert($("hidTryPriceError").value);			
		}
		else 
		{
			objText.value = objText.value * 1;
			if ($("PricesBuilder1_btnOrder2"))
				$("PricesBuilder1_btnOrder2").click();	
			else SendTryOffer();
		}
		return false;
	}
	else return true;
}
function CheckPrice(theText, e)
{
	objText = $(theText);
	objText.value = Trim(objText.value);
	
	if(objText.value.length == 0 || isNaN(objText.value))
		objText.className="InvalidFormField";
	else objText.className="FormField";
	
}