//Every second refresh and display bid history in product page
function all_bids_refresh(auc_id, page_in, baseURL,user_id )
{	
	//alert(baseURL);
	bidding_info(auc_id,page_in, baseURL,user_id );
	setInterval("bidding_info('"+auc_id+"','"+page_in+"','"+baseURL+"','"+user_id+"')",5000);
}
function bidding_info(auc_id,page_in, baseURL, user_id){
	
	jQuery.post(baseURL+"app/ajax/ajax_display_allbid.php", { auc_id: auc_id,paging: page_in, baseURL: baseURL,user_id: user_id} , function(data)
	      {														
			  //alert(data);
			  jQuery("#bid_status").html(jQuery.trim(data));									 
		  });
}
	
	
function checkUserName(un)
{
 
	jQuery('#un').html('<img src="images/ajax_loader_small.gif" />');
	jQuery.post("app/ajax/ajax_check_username.php", {user_name: un} , function(data)
		{			
			   //alert(data);
			   if (data != '' || data != undefined || data != null) 
			   {				   
				  jQuery('#un').html(data);	
				  jQuery('#un').attr("style",'color:#CC0000;');
			   }
          });
}

function showprice(aid,uid, lang, baseURL)
{ 
	if(uid == undefined || uid == null || uid == '')
	{document.location.href= baseURL+"login.php";return false;}
 	//jQuery(".hotbannerShow"+jQuery.trim(aid)).hide();alert(aid);
	jQuery.post(baseURL+"app/ajax/ajax_showprice.php", {usid: uid, aucid: aid,ln:lang} , function(data)
		{		//alert(data);
			
			   if (data != '' || data != undefined || data != null) 
			   {		
				  if(data=="Closed")
				  {
					  jQuery("#msg"+jQuery.trim(aid)).show();					  
					  jQuery("#msg"+jQuery.trim(aid)).html("The auction has been sold.");
					  //jQuery('#showpeep'+jQuery.trim(aid)).html('<img src="images/sold.gif" />');
					  jQuery('#bidnow_btn'+jQuery.trim(aid)).html('<img src="images/sold.gif" />');					  
					  setTimeout("hide_div('msg"+aid+"')", 3000);
				  }
				  else if(data == "PriceRevealed")
				  {					  
					  jQuery("#pricerevealed"+jQuery.trim(aid)).show();
					  secTimer("timecountdown"+jQuery.trim(aid),"pricerevealed"+jQuery.trim(aid),'');
				  }
				  else if(data == "buyNowProcessing")
				  {					  
					  jQuery("#buyNowProcessing"+jQuery.trim(aid)).show();
					  setTimeout("hide_div_class('buyNowProcessing"+aid+"')", 9000);
				  }
				  else if(data=="recharge")
				  {
					  	jQuery("#msg"+jQuery.trim(aid)).show();					  
					  	jQuery("#msg"+jQuery.trim(aid)).html("Please recharge your account.");					  	
					  	setTimeout("hide_div('msg"+aid+"')", 3000);
						document.location="account.php?job=purcahse-credit";
				  }
				  else if(data=="error")
				  {
					  jQuery("#msg"+jQuery.trim(aid)).show();					  
					  	jQuery("#msg"+jQuery.trim(aid)).html("System Error. Please try latter.");					  	
					  	setTimeout("hide_div('msg"+aid+"')", 3000);					
				  }
				  else
				  { 
				  	
					  	var auc_data=data.split("@@");
						//alert(jQuery.trim(auc_data[0]));
						if(jQuery.trim(auc_data[0])=="buynow")
						{
							var auc_price=jQuery.trim(auc_data[1]);
							var realc=jQuery.trim(auc_data[2]);
					 		var bonus=jQuery.trim(auc_data[3]);
							var credit_from=jQuery.trim(auc_data[4]);
							
							jQuery("#cprice"+jQuery.trim(aid)).html(auc_price);
							jQuery("#rcredit").html(realc);
							//jQuery("#rcreditSL").html(realc);
							jQuery("#bcredit").html(bonus);
                            //jQuery("#bcreditSL").html(bonus);
							jQuery("#credit_from"+jQuery.trim(aid)).html(credit_from);							
							//jQuery("#showpeep"+jQuery.trim(aid)).hide();
							jQuery("#bidnow_btn"+jQuery.trim(aid)).hide();
							jQuery("#buynow"+jQuery.trim(aid)).show();
							jQuery("#buynowtime"+jQuery.trim(aid)).show();
					  		secTimer("buynowTimer"+jQuery.trim(aid),"buynowtime"+jQuery.trim(aid),jQuery.trim(aid));
							
						}
				  }
			   }
          });
}


function buynow(aid,uid, lang, baseURL)
{
	
	if(uid == undefined || uid == null || uid == '')
	{document.location.href=baseURL+"login.php";return false;}
	
	if(jQuery("#credit_from"+jQuery.trim(aid)).html() == "real")
	{
		//baseURL+'account.php?job=checkout&id='+aid+'&ln='+lang; 
		document.location.href=baseURL+'checkout/'+aid+'/my-account.php';
	}
	else
	{
		 //jQuery("#msg"+jQuery.trim(aid)).show();					  
		 //jQuery("#msg"+jQuery.trim(aid)).html("You must use credits to buy this product, Click Here to proceed!");
		// var result =window.confirm("You must use credits to buy this product, Click Here to proceed!");
		 
		// if(result==true)
		//{
			jQuery.post(baseURL+"app/ajax/ajax_buynow.php", {usid: uid, aucid: aid} , function(data)
			{			
				 //alert(data);
				   if (data != '' || data != undefined || data != null) 
				   {
					   if(data=="recharge")
					  {
							jQuery("#msg"+jQuery.trim(aid)).show();					  
							jQuery("#msg"+jQuery.trim(aid)).html("Please recharge your account.");					  	
							setTimeout("hide_div('msg"+aid+"')", 3000);
							document.location=baseURL+"account.php?job=purcahse-credit";
					  }
					  else if(data == "buynow")
					  {
						  //document.location.href=baseURL+'account.php?job=checkout&id='+aid+'&ln='+lang;
						  document.location.href=baseURL+'checkout/'+aid+'/my-account.php';
					  }
				   }
				   
			  });	
		//}
		 
	}
	
}


function hide_div(divid)
{jQuery('#'+divid).hide();}

function hide_div_class(divid)
{jQuery('#'+divid).hide();}


function place_bid(bid_type,lang, baseURL,uid,aid)
{	
var single_bid_value=jQuery('#bidval_single'+aid).val();
var from_bid_value=jQuery('#from_bidval'+aid).val();
var to_bid_value=jQuery('#to_bidval'+aid).val();

if(uid == undefined || uid == null || uid == '')
	{  
		document.location.href=baseURL+"login.php";return false;    
    }
	else
	{
      jQuery.post("app/ajax/ajax_bidding_process.php", {auction_id: aid, sbid_val: single_bid_value, fbidval: from_bid_value, tbid_val:to_bid_value, bidt: bid_type,ln:lang} , function(data)
	      	{				
				//alert(data);
                //splitting the values
				auctions=data.split('@@');							
				 if(data != undefined && data != null && data != '')
				 {	
				 
				 	if(jQuery.trim(auctions[0]) == "Not allowed. Auction was Closed.")
					{	
						jQuery('#place_bid').html('<img src="images/sold.gif" />');
					}
					else
					{                        
						if(jQuery('#page').html() == "Live Auction" )
						{
                            jQuery('#bid_error'+aid).html(auctions[0]);
							jQuery('#bid_error'+aid).show();
						}
                        else
						{							
							var currency=jQuery('#currency').html();
							var convert_rate=jQuery('input#convert_rate').val();
							
							jQuery('#bid_error').html(auctions[0]);	
							jQuery('#bid_error').show();
							//if(auctions[0] != undefined || auctions[0] != null || auctions[0] != '')
							//{dis_bid(aid,uid,'',20, baseURL,lang);}					
							jQuery("#rcredit").html(auctions[1]);
							jQuery("#rcreditSL").html(auctions[1]);
							jQuery("#bcredit").html(auctions[2]);
							jQuery("#remainbids").html(auctions[3]);
							
							if(jQuery.trim(currency)=="USD")
							{ 
							var convert_totalBid =parseFloat(auctions[4])*parseFloat(convert_rate)
							var totalbid=convert_totalBid.toFixed(2);
							jQuery("#totalBidByUser"+aid).html(totalbid);	
							} else {
							convert_totalBid = auctions[4];
							jQuery("#totalBidByUser"+aid).html(convert_totalBid);
							}
							var rrpAmt=jQuery('#rrpAmt'+aid).html();
							var shipping=jQuery('#shipping').html();
							// totalamount for show
							var buyprice_show=parseFloat(rrpAmt)+parseFloat(shipping)-parseFloat(convert_totalBid);
							var buyprice_con=buyprice_show.toFixed(2);
							jQuery("#buyprice_show").html(buyprice_con);
							
							// totalamount for textbox
							var unitprice=jQuery('input#unitprice').val();
							totalprice_textbox=parseFloat(unitprice)-parseFloat(auctions[4])
							jQuery("input#buyprice").val(totalprice_textbox);
						}
					}
				 }				
          });
	}	
	return false;	
}

var seconds=11;
function secTimer(id,hideDiVid,aucid)
{	
		var time = 10;
		jQuery("#"+id).html(10);
		var tim = setInterval(function(){			
			jQuery("#"+id).html(--time);
			if(time <= 0){
				clearInterval(tim);			
				jQuery('#'+hideDiVid).hide();		
				if(aucid)
				{
					jQuery("#buynow"+jQuery.trim(aucid)).hide();
					//jQuery("#showpeep"+jQuery.trim(aucid)).show();
					jQuery("#bidnow_btn"+jQuery.trim(aucid)).show();										
					//if(jQuery("#easy").html() == "view")
					//jQuery("#cprice"+jQuery.trim(aucid)).html('????');
					//else
					jQuery("#cprice"+jQuery.trim(aucid)).html('???');					
					//if(jQuery("#hotauc"+jQuery.trim(aucid)).html() == "Yes")
					//{
						//jQuery(".hotbannerHide"+jQuery.trim(aucid)).hide();
						//jQuery(".hotbannerShow"+jQuery.trim(aucid)).show();												
					//}					
				}
			}
		}, 1000);
}

// display current winner every 3 sec
function find_lub_winner(auc_id)
{	
	check_lub_winner(auc_id);		
	setInterval("check_lub_winner('"+auc_id+"')",3000);
}

function check_lub_winner(auc_id)
{	
	  jQuery.post("app/find_current_lub_winner.php", { aid: auc_id} , function(data)
	  {    
			if(data!=undefined) 
			{ //alert(data);
				if(data!="")
				{
					jQuery("#CWUsername"+auc_id).html(data);
				}
				else 
				{
					jQuery("#CWUsername"+auc_id).html('None');
				}
				
			}
	  });

 }	 



