var bidXmlHttpObj;
var auction_id_selected = "";
var isMulti = false;

function CreateAjaxBidXmlHttpObj()
{
	try
	{
		bidXmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	catch(e)
	{
		try
		{
			bidXmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			bidXmlHttpObj = null;
		}
	}
	if(!bidXmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		bidXmlHttpObj = new XMLHttpRequest();
	}
}

function isNumberKey(evt)
{
		var charCode = (evt.which) ? evt.which : event.keyCode
		if (charCode > 31 && (charCode < 48 || charCode > 57))
		if (charCode!=13)		return false;	
		return true;
}

function reloadStatus(auction_id)
{
  auction_id_selected = auction_id;
		var requestUrl;
	
		requestUrl = "./auctions_ajax.php" + "?action=reloadStatus&auction_id="+auction_id;
		CreateAjaxBidXmlHttpObj();
		
		if(bidXmlHttpObj)
		{
			showLoadingGen("loadLayer",50);			
			bidXmlHttpObj.open( "GET", requestUrl, true );
			bidXmlHttpObj.onreadystatechange = StateChangeReloadStatusHandler;
			bidXmlHttpObj.send(null);	
		}		
}
function deleteBid(auction_id,id)
{
	 auction_id_selected = auction_id;
		var requestUrl;
	
		requestUrl = "./auctions_ajax.php" + "?action=deletebid&auction_id="+auction_id+"&id=" + id;
	
		CreateAjaxBidXmlHttpObj();
		
		if(bidXmlHttpObj)
		{
			showLoadingGen("loadLayer",50);			
			bidXmlHttpObj.open( "GET", requestUrl, true );
			bidXmlHttpObj.onreadystatechange = StateChangeBidsHandler;
			bidXmlHttpObj.send(null);	
		}	
}
  
function addBookmark(title,url) 
{
		if (window.sidebar) { 
				window.sidebar.addPanel(title, url,""); 
		} else if( document.all ) {
				window.external.AddFavorite( url, title);
		} else if( window.opera && window.print ) {
				return true;
		}
}

function doBidMulti(id)
{
	 isMulti = true;
	 auction_id_selected = id;	
		var input_min = document.getElementById("bid_value_min_"+id);
		var input_max = document.getElementById("bid_value_max_"+id);

  if (input_min != null && input_max != null)
		{
			var bid_value_min  = input_min.value;
			var bid_value_max  = input_max.value;
			if (bid_value_min == "" || bid_value_max == "")
			{
				 alert ("Debes indicar tu puja.");
					return;
			}
			
			var requestUrl;
		
			requestUrl = "./auctions_ajax.php" + "?action=dobidMulti&auction_id="+id+"&valueMin=" + encodeURIComponent(bid_value_min)+"&valueMax=" + encodeURIComponent(bid_value_max);
		
			CreateAjaxBidXmlHttpObj();
			
			if(bidXmlHttpObj)
			{
 			showLoadingGen("loadLayer",50);				
				bidXmlHttpObj.open( "GET", requestUrl, true );
				bidXmlHttpObj.onreadystatechange = StateChangeBidsHandler;
				bidXmlHttpObj.send(null);	
			}	
			
			
		}
}

function doBidUser(id)
{
	 isMulti = false;
	 auction_id_selected = id;
	 var input = document.getElementById("bid_value_"+id);
		if (input != null)
		{
			var bid_value  = input.value;
			if (bid_value == "")
			{
				 alert ("Debes indicar tu puja.");
					return;
			}
			var requestUrl;
		
			requestUrl = "./auctions_ajax.php" + "?action=dobid&auction_id="+id+"&value=" + encodeURIComponent(bid_value);
		
			CreateAjaxBidXmlHttpObj();
			
			if(bidXmlHttpObj)
			{
 			showLoadingGen("loadLayer",50);				
				bidXmlHttpObj.open( "GET", requestUrl, true );
				bidXmlHttpObj.onreadystatechange = StateChangeBidsHandler;
				bidXmlHttpObj.send(null);	
			}	
		}	
}
function doBid(id)
{
	 isMulti = false;
	 auction_id_selected = id;
	 var input = document.getElementById("bid_value_"+id);
		if (input != null)
		{
			var bid_value  = input.value;
			if (bid_value == "")
			{
				 alert ("Debes indicar tu puja.");
					return;
			}
			var requestUrl;
		
			requestUrl = "./auctions_ajax.php" + "?action=dobid&auction_id="+id+"&value=" + encodeURIComponent(bid_value);
		
			CreateAjaxBidXmlHttpObj();
			
			if(bidXmlHttpObj)
			{
 			showLoadingGen("loadLayer",50);				
				bidXmlHttpObj.open( "GET", requestUrl, true );
				bidXmlHttpObj.onreadystatechange = StateChangeBidsHandler;
				bidXmlHttpObj.send(null);	
			}	
		}
}

function StateChangeReloadStatusHandler()
{
 if(bidXmlHttpObj.readyState == 4)
	{
		if(bidXmlHttpObj.status == 200)
		{			
		   var response = bidXmlHttpObj.responseText;
					
		   if (response == "ERROR_CODE_LOGIN")
					{
						 location.href = "./create_account.php";
					}
					else
					{
						 document.getElementById("bids_details_"+auction_id_selected).innerHTML = bidXmlHttpObj.responseText;
					}
					
		   hideLoadingGen("loadLayer",50);					
		}
		else
		{
			alert("Código de error: "  + bidXmlHttpObj.status);
		}		
	}
}

function StateChangeBidsHandler()
{
	if(bidXmlHttpObj.readyState == 4)
	{
		if(bidXmlHttpObj.status == 200)
		{			
		   var response = bidXmlHttpObj.responseText;
					
		   if (response == "ERROR_CODE_LOGIN")
					{
						 location.href = "./create_account.php";
					}
					else if (response == 'ERROR_CODE_MAX_BIDS')
					{
							alert ("Puedes hacer como maximo 5 ofertas individuales a la vez.");
					}
					else if (response == "ERROR_CODE_BID_REPEAT")
					{
						 alert ("Ya has realizado una puja con este importe.");
					}
					else if (response == "ERROR_CODE_MIN_GREATER_MAX")
					{
					  alert ("La primera puja tiene que ser menor que la segunda.");
					}
					else if (response == "ERROR_CODE_MAX_BIDS_MULTI")
					{
						 alert ("Solo se puede hacer una oferta multiple a la vez.");
					}
					else if (response == "ERROR_CODE_NO_BID_MULTI_WITH_SIMPLE")
					{
						 alert ("No se puede realizar una oferta simple con una multiple.");
					}
					else
					{
						 document.getElementById("auction_container_"+auction_id_selected).innerHTML = bidXmlHttpObj.responseText;
					}
					
					if (isMulti)
					{
						 document.getElementById("bid_value_min_"+auction_id_selected).value = "";
							document.getElementById("bid_value_max_"+auction_id_selected).value = "";
					}
					else
					{
 						document.getElementById("bid_value_"+auction_id_selected).value = "";
					}
		   hideLoadingGen("loadLayer",50);					
		}
		else
		{
			alert("Código de error: "  + bidXmlHttpObj.status);
		}		
	}
}

function viewBids(auction_id)
{
	 if (document.getElementById("bids_details_"+auction_id).innerHTML == "") reloadStatus(auction_id);
		document.getElementById("view_bids_"+auction_id).style.display = "none";
		document.getElementById("hide_bids_"+auction_id).style.display = "block";
		document.getElementById("bids_details_"+auction_id).style.display = "block";
}

function hideBids(auction_id)
{
		document.getElementById("view_bids_"+auction_id).style.display = "block";
		document.getElementById("hide_bids_"+auction_id).style.display = "none";
		document.getElementById("bids_details_"+auction_id).style.display = "none";
}

function confirmAuction(auction_id)
{
		location.href="./confirm.php?id="+auction_id;
}

function hideLoadingGen(id)
{
	document.getElementById(id).style.visibility="hidden";
}

function showLoadingGen(id,width)
{ 
	 var loadLayer = document.getElementById(id);
	 loadLayer.style.position = "absolute";
	
	 loadLayer.style.top = f_scrollTop() + 0.5 * ( f_clientHeight()) + "px";
 	loadLayer.style.left = f_scrollLeft() + 0.5 * ( f_clientWidth() - width) + "px";
 	loadLayer.style.visibility="visible";
	
}

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}



			   
