function init_auc(pageval, main_auc_ids)
{		
	//setAuctionsvalues(pageval,main_auc_ids);
	setInterval("setAuctionsvalues('"+pageval+"','"+main_auc_ids+"')",1000);
}

	function setAuctionsvalues(pageval, main_auc_ids)
	{		
		var auctions = '';
	  	$('.timer_info').each(function(){
        		var auctionId       = $(this).attr('id');
				var start_time      = $(this).attr('title');
				var end_time        = $('#'+auctionId).html();
				var reservedAmount  = $('#auc_reservedprice_'+auctionId).val();
				var resetTime       = $('#auc_resettime_'+auctionId).val();
				
				auctions = auctions + auctionId + '#' + start_time + '#' + end_time+ '#' + reservedAmount+ '#' + resetTime + '|';
			});
				 
		 var main_auc_price = $('#auc_price_'+$.trim(main_auc_ids)).html();
		 
		  
		  $.post("ajax/ajax_auction_data.php", {auc_data: auctions} , function(data)
	      {
				//alert(data);
				var aucresponse=data.split('|');
			 
			 for(i=0; i<aucresponse.length; i++)
			 {
				 if($.trim(aucresponse[i]) != undefined && $.trim(aucresponse[i]) !='')
				 { 
					 var auc_data=aucresponse[i].split("#");
					 var auc_id=$.trim(auc_data[0]);
					 var auc_price=$.trim(auc_data[1]);
					 var biider_name=$.trim(auc_data[2]);
					 var timer_countdown=$.trim(auc_data[3]);
					 var end_date=$.trim(auc_data[4]);
					 var remaind=$.trim(auc_data[5]);
					 					 					 
					 if($.trim($('#auc_price_'+auc_id).html()) != auc_price)
					 {$('#auc_price_'+auc_id).animate({backgroundColor:"#CC0000"},200).animate({backgroundColor:"#FFFFFF"},500);}
					
					 $('#auc_price_'+auc_id).html(auc_price);						
					 $('#bidder_name_'+auc_id).html(biider_name);
					 
					 var front_tm=$('#auc_timer_'+auc_id).html();
					 if(front_tm != "Ended")
					 {
					 	$('#auc_timer_'+auc_id).html(timer_countdown);
					 }
										 					 
					 					 
					 if(end_date)
					 {$('#'+auc_id).html(end_date);}
					 
					 if(remaind=="yes")
					 {
						 $('#bid_now_bttn_'+auc_id).hide();
						 $('#remind_'+auc_id).html("Coming soon...");
						 $('#remind_'+auc_id).show();						 
						 $('#auc_timer_'+auc_id).attr("style",'margin:0px;font-size:14px;color:#666;');						 
					 }					
					 else
					 {
					 	$('#remind_'+auc_id).hide();
						$('#bid_now_bttn_'+auc_id).show();
						
						//if(timer_countdown <= "00:10" && timer_countdown > "00:01")
						//if ( (timer_countdown == '00:00') && (reservedAmount < auc_price)){
						if ( (timer_countdown == '00:00')){
							if(pageval=="product"){$('#auc_timer_'+auc_id).attr("class",'time_big_red');}
							else {$('#auc_timer_'+auc_id).attr("class",'time_left_red');}
						}
						else
						{
							if(pageval=="product"){$('#auc_timer_'+auc_id).attr("class",'time_big');}
							else {$('#auc_timer_'+auc_id).attr("class",'time_left');}
						}
					}
					//if (timer_countdown == '00:01' || timer_countdown == '00:00'){
					if ( timer_countdown == '00:00' ){
							//alert(timer_countdown);
							$('#auc_timer_'+auc_id).html("Going").animate({ backgroundColor: "red" }, 600);
					 }
					 else if(timer_countdown == 'Closed')
					 {					
						if(pageval=="product" && auc_id==main_auc_ids){
							$('#auc_timer_'+auc_id).html("Ended");
							document.location.href='product/winner_details/'+auc_id;
						}	
						else{
							//alert(timer_countdown);
							$('#bid_now_bttn_'+auc_id).html('<img src="images/sold.gif" width="116" height="28" />');		
							$('#auc_timer_'+auc_id).attr("style",'margin:0px;font-size:16px;color:#CC0000;');	
							$('#auc_timer_'+auc_id).html("Ended");
						}
					 }
					 
					 if(pageval=="product")
					 {
						
						var rrp=$('#rrp').html();
						var saving=rrp-auc_price;
						$("#auc_price").html(auc_price);
						$("#saving").html(saving);
					 }
					 //bidhistory(main_auc_ids);
					 if(auc_id==main_auc_ids && pageval=="product" && main_auc_price!=auc_price)
					 {	 
						
						bidhistory(main_auc_ids);
					 }
					 
					 
				 }//end if
			 }//end for
          });
}

function bidhistory(main_auc_ids)
{
	$.post("ajaxbidhistory/index", {aid: main_auc_ids} , function(data)
	{		
		$("#bidhistory").html(data);
	});
}

function place_bid(id,uid)
{	
	
	if(uid == undefined || uid == null || uid == '')
	{document.location.href='signup/index';}
	else
	{
		
		$.post("ajaxbidding/index", {aid: id, user_id:uid} , function(data)
	    {
			//alert(data);
				if($.trim(data)=="showdown")
				{
					$('#bid_now_bttn_'+id).hide();
					$('#last_bids_'+id).show();
					$('#bid_error_'+id).show();
					$('#bid_error_'+id).html("Enter your last no of bids.");
					setTimeout("hideDive5Sec('bid_error_"+id+"')", 3000);
				}
				else if($.trim(data)=="recharge")
				{
					$('#bid_error_'+id).show();
					$('#bid_error_'+id).html("Not enough funds.");
					setTimeout("hideDive5Sec('bid_error_"+id+"')", 3000);
				}
				else if($.trim(data)=="already_winner")
				{
					$('#bid_error_'+id).show();
					$('#bid_error_'+id).html("You already have the highest bid.");					
					setTimeout("hideDive5Sec('bid_error_"+id+"')", 3000);
				}
				else if($.trim(data)=="perweeklimitation")
				{
					$('#bid_error_'+id).show();
					$('#bid_error_'+id).html("You have exceed per week won auction limitation.");
					setTimeout("hideDive5Sec('bid_error_"+id+"')", 3000);
				}
				else if($.trim(data)=="permonthlimitation")
				{
					$('#bid_error_'+id).show();
					$('#bid_error_'+id).html("You have exceed per month won auction limitation.");
					setTimeout("hideDive5Sec('bid_error_"+id+"')", 3000);
				}
				else
				{
					//alert(data);
					if(data=="Ended")
					{
						//$('#item_timer_'+$.trim(id)).html("Ended");
						//$('#bid_now_bttn_'+$.trim(id)).html("SOLD");
					}
					else
					{
						
						//alert(data);
						auctions=data.split('#');
						
						/* if($.trim($('#auc_price_'+$.trim(auctions[0])).html())!= $.trim(auctions[1]))
						{							
						 	$('#auc_price_'+$.trim(auctions[0])).animate({backgroundColor:"#CC0000"},200).animate({backgroundColor:"#FFFFFF"},500);
						}*/
						 //$('#item_price_'+$.trim(auctions[0])).html(auctions[1]);
						 $('#bidder_name_'+$.trim(auctions[0])).html(auctions[2]);
						 $('#user_bid_balance').html(auctions[3]);
						
					}
				}
				 
		});
	}
}

function showdown_bids(frm,id,uid)
{
	//alert(uid);
	var lbid=frm.last_bid.value;	
 	var objRegExp  = /(^-?\d\d*$)/;//Validates that a string contains only valid integer number.
	if(objRegExp.test(lbid))
	{
		//alert('yes');
		$.post("showdownbidding/index", {aid: id, mid: uid, last_bids: lbid} , function(data)
		{
			//alert(data);
			 	if($.trim(data)=="recharge")
				{
					$('#bid_error_'+id).show();
					$('#bid_error_'+id).html("Not enough funds.");
					setTimeout("hideDive5Sec('bid_error_"+id+"')", 3000);
				}
				else if(data=="Ended")
					{
						$('#item_timer_'+$.trim(id)).html("Ended");
						$('#bid_now_bttn_'+$.trim(id)).html("SOLD");
					}
					else
					{
						$('#bid_error_'+id).show();
						$('#bid_error_'+id).html(data);
						setTimeout("hideDive5Sec('bid_error_"+id+"')", 3000);
						
					}
				
		});
	}
	else
	{		
		//alert('no');
		$('#bid_error_'+id).show();
		$('#bid_error_'+id).html("Please enter integer number");
		setTimeout("hideDive5Sec('bid_error_"+id+"')", 3000);
	}

	return false;
}
function hideDive5Sec(divid)
{
	$('#'+divid).hide(); 		
}

function bidbutler_overview(aucid)
{
	$('#bidbutlerhome').hide();
	$('#bidbutleroverview').show();
	bidbutler_overview_data(aucid);
}
function book_bidbutler_home()
{
	$('#bidbutleroverview').hide();
	$('#bidbutlerhome').show();	
}

function book_bidbutler()
{
	//alert(frmbutler.uid.value);
	var usid=document.frmbutler.uid.value;
	var aucid=document.frmbutler.aid.value;
	var b_from=document.frmbutler.bid_from.value;
	var b_to=document.frmbutler.bid_to.value;
	var no_bid=document.frmbutler.bids.value;
		
	 if (usid == '' || usid == undefined || usid == null) 
	 {return false;} 
	 else if(aucid == '' || aucid == undefined || aucid == null)
	 {return false;}
	//alert(no_bid);
	
	$.post("ajaxbookbidbutler/index", {aid: aucid, bid_from: b_from, bid_to: b_to, bids: no_bid, uid: usid} , function(data)
	{	
		var results=data.split('#');
		
		if(results[0]=="error")
		{
			$('#bidbutler_error').show();
			$('#bidbutler_error').html(results[1]);
			setTimeout("hideDive5Sec('bidbutler_error')", 3000);
		}
		else if(results[0]=="success")
		{
			//$('#bidbutler_error').show();
			//$('#bidbutler_error').html(results[1]);
			//setTimeout("hideDive5Sec('bidbutler_error')", 3000);
			
			bidbutler_overview(aucid);
			document.frmbutler.bid_from.value="";
			document.frmbutler.bid_to.value="";
			document.frmbutler.bids.value="";
		}
	});
	
}
function bidbutler_overview_data(aucid)
{
	$.post("ajax_bidbutler_overview/index",{aid:aucid},function(data)
	{
		$("#butler_overview").html(data);
	});
}

