﻿function init(ids, pageval, main_auc_ids)
{
	//alert(pageval);
	//setAuctionsvalues(ids, pageval, main_auc_ids);
	//refreshID=
	setInterval("setAuctionsvalues('"+ids+"','"+pageval+"','"+main_auc_ids+"')",1000);
	//setAuctionsvalues('"+ids+"','"+pageval+"','"+main_auc_ids+"')

}

function formatNumber(num)
{
    var n = num.toString();
    var nums = n.split('.');
    var newNum = "";
    if (nums.length > 1)
    {
        var dec = nums[1].substring(0,2);
        newNum = nums[0] + "," + dec;
    }
    else
    {
    newNum = num;
    }

    return newNum
}


	  function setAuctionsvalues(index_auctions, page_name, main_auc_ids)
	  {

	  	//alert(page_name)
	  	//alert(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();
				//auctions = auctions + auctionId + '#' + auctionTitle + '|';
				auctions = auctions + auctionId + '#' + start_time + '#' + end_time + '|';
			});

		//alert(auctions);
		 //alert($('#item_price_'+$.trim(main_auc_ids)).html());

		 var main_auc_price=$('#item_price_'+$.trim(main_auc_ids)).html();


		//var auctions=index_auctions;
		  $.post("function/crediti.php", {} , function(data)
	      {
	      		if(document.getElementById('quanti_klik')){
				$('#quanti_klik').html('Klik : <span class="bold">' + data + '</span>')
				}
	      })

		  //var auctions=index_auctions;
		  $.post("function/ajax_auction_data.php", { all_auctions: auctions , mainAucId: main_auc_ids, mainAucPrice: main_auc_price, pg_name: page_name} , function(data)
	      {
		  	//alert(data);
		  	//$('#test').html(data);
             //splitting the values

			 auctions=data.split('|');
			 var dominio = document.domain

			 if(dominio == "www.kinger.sk" || dominio == "kinger.sk"){
			 	valuta = "&euro;"
			 	str_valuta = "€"
			 }else{
			 	valuta = "CZK";
			 	str_valuta = "CZK";
			 }

			 for(i=0;i<=auctions.length;i++)
			 {
				 if(auctions[i] != undefined)
				 {

				  	auc_det=auctions[i].split("#");
					// check there is update data,
					//alert(auc_det[0])
					if(auc_det[0]=="no_update")
					{
						//alert(auc_det[6]);
						//alert(auc_det[1]);

						$('#item_price_'+$.trim(auc_det[1])).html(auc_det[2] + valuta);
						$('#bidder_name_'+$.trim(auc_det[1])).html(auc_det[3]);
						$('#item_timer_'+$.trim(auc_det[1])).html(auc_det[4]);

						if(document.getElementById("saving")){

							$('#saving').html(auc_det[2] + " " + valuta);
							var prezzo =document.getElementById("prezzoprodotto").innerHTML
							prezzo = prezzo.split(str_valuta)
							prezzo = prezzo[0]
							$('#prezzoscontato').html( formatNumber((prezzo - auc_det[2])) + " " + valuta)
						}

						if(auc_det[5])
						{
							if($.trim($('#'+$.trim(auc_det[1])).html())!=$.trim(auc_det[5]))
							{
							//	$('#item_timer_'+$.trim(auc_det[1])).animate({backgroundColor:"#CC0000"},200).animate({backgroundColor:"#FFFFFF"},500);
							}
							$('#'+$.trim(auc_det[1])).html(auc_det[5]);
						}

						if(auc_det[4]=="Ended" && page_name=="product")
						{
							//document.location.reload();
							document.location.href='close-auction-details.php?pid='+auc_det[1];
						}
						if($.trim(auc_det[6])=="yes")
						{

							$('#auc_bid_now_'+auc_det[1]).hide();
							$('#remind_'+auc_det[1]).show();
						}
						else
						{
							$('#auc_bid_now_'+auc_det[1]).show();
							$('#remind_'+auc_det[1]).hide();
						}



					}
					else
					{

						if(auc_det[11]=="Ended" && page_name=="product")
						{
							//document.location.reload();
							document.location.href='close-auction-details.php?pid='+auc_det[0];
						}

					   $('#item_timer_'+$.trim(auc_det[0])).html(auc_det[3]);

					   if(page_name=="product"){
						$('#item_price_'+$.trim(auc_det[0])).html(auc_det[1] + valuta);
						if(document.getElementById("saving")){

							$('#saving').html(auc_det[1] + " " + valuta);
							var prezzo =document.getElementById("prezzoprodotto").innerHTML
							prezzo = prezzo.split(str_valuta)
							prezzo = prezzo[0]
							$('#prezzoscontato').html( formatNumber(prezzo - auc_det[1]) + " " + valuta)
						}

						}else{
						$('#item_price_'+$.trim(auc_det[0])).html(auc_det[1]);
						}
						$('#bidder_name_'+$.trim(auc_det[0])).html(auc_det[2]);
						if(auc_det[3]=="Ended")
						{
						$('#bid_now_bttn_'+$.trim(auc_det[0])).html("SOLD");
						}

						if(auc_det[4])
							{
								//alert(auc_det[4]);
								$('#latest_bid_list').html(auc_det[4]);
							}
						if($.trim(auc_det[0])==main_auc_ids)
						{
							//alert(auc_det[0]);
							var rrp=$('#rrp').html();
							$('#item_price2_'+$.trim(auc_det[0])).html(auc_det[1]);
							var item_price=auc_det[1];
							var total_saving=parseFloat(rrp)-parseFloat(item_price);
							$('#saving').html(total_saving.toFixed(2));

						}
					}// end else for data not update

				 }
			 }
          });
	  }

<!--             help page  -->
function hidedisplay_helpheaders(div_id) {
  for (var i = 0; i < 15; i++) {
    el = document.getElementById('help_header_' + i);
    if (el) {
      el.style.display = 'none';
    }
  }
  $('#'+div_id).show();
}
function display_help(hqaid)
{


	$('#help_disp').html('<div style="padding-left:40px; padding-top:20px; padding-bottom:20px; width:500px;" align="center"><img src="images/ajax-loader.gif" /><div>');
	$.post("function/ajax_help_data.php", {question_id: hqaid} , function(data)
		{
			   if (data != '' || data != undefined || data != null)
			   {
				  $('#help_disp').html(data);
			   }
          });
}
function checkUserName(un)
{
	$('#un').html('<img src="images/ajax_loader_small.gif" />');
	$.post("function/ajax_check_username.php", {user_name: un} , function(data)
		{
			   //alert(data);
			   if (data != '' || data != undefined || data != null)
			   {
				  $('#un').html(data);
			   }
          });
}
function hideDive5Sec(divid)
{
	$('#'+divid).hide();
	//alert(divid);
}
function place_bid(auc_id,user_id,bid_type,page_name)
{
//	alert("a:"+auc_id+",u"+user_id+",bt:"+bid_type+",p:"+page_name);
	if(user_id == undefined || user_id == null || user_id == '')
	{
	//document.location.href='index.php';
	window.alert('Najprv sa prihlás!')
	}
	else
	{
			$.post("function/ajax_bidding_process.php", {aid: auc_id, bidType: bid_type,uid:user_id, page:page_name} , function(data)
	      	{
				//alert(data);
				if($.trim(data)=="recharge")
				{
					window.alert('Not enough credits!')
					$('#bid_error_'+auc_id).show();
					$('#bid_error_'+auc_id).html("Not enough funds.");
					setTimeout("hideDive5Sec('bid_error_"+auc_id+"')", 3000);
				}
				else if($.trim(data)=="already_winner")
				{
					window.alert('Si momentálny víťaz!')
					$('#bid_error_'+auc_id).show();
					$('#bid_error_'+auc_id).html("You already have the highest bid.");
					setTimeout("hideDive5Sec('bid_error_"+auc_id+"')", 3000);
				}
				else if($.trim(data)=="first_snip")
				{
					window.alert('Only for first win users!')
					$('#bid_error_'+auc_id).show();
					$('#bid_error_'+auc_id).html("Only for first win user.");
					setTimeout("hideDive5Sec('bid_error_"+auc_id+"')", 3000);
				}
				else
				{
					//alert(data);
					if(data=="Ended")
					{
						$('#item_timer_'+$.trim(auc_id)).html("Ended");
						$('#bid_now_bttn_'+$.trim(auc_id)).html("SOLD");
					}
					else
					{

						//alert(data);
						auctions=data.split('#');
						//animate item price
						//alert($.trim($('#item_price_'+$.trim(auctions[0])).html()));
						//alert($.trim(auctions[1]));

						if($.trim($('#item_price_'+$.trim(auctions[0])).html())!= $.trim(auctions[1]))
						{
						  //var bg_color=$('#item_price_'+$.trim(auctions[0])).parents('tr').attr('bgcolor');
						  //$('#item_price_'+$.trim(auctions[0])).animate({backgroundColor:"#CC0000"},200).animate({backgroundColor:"#FFFFFF"},500);
						  //$('#item_timer_'+$.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]);

						// if(auctions[4]){$('#no_bid_placed_'+$.trim(auctions[0])).html(auctions[4]);}
						// if(auctions[5]){$('#bid_placed_amt_'+$.trim(auctions[0])).html(auctions[5]);}
					}
				}

			 });
	}
}

function  get_category_page(cat_id,jobs_id,limit)
{

	$.post("function/ajax_auction_category_data.php", { category_id: cat_id, job_id: jobs_id, lt: limit} , function(data)
	      	{
				//alert(data);
             if(data != undefined && data!="")
				 {
					var data_parts=data.split("<--second_part-->");
					$('#cat_auction_'+cat_id+'_'+jobs_id).html(data_parts[0]);
					if(data_parts[1] != undefined && data_parts[1]!="")
					{
						//alert(data_parts[1]);
						init(data_parts[1],"all_category_page","");
					}
				 }
          });
}
function browse_category_page(cat_id,jobs_id,limit,page_no)
{

	$.post("function/ajax_auction_category_data.php", {category_id: cat_id, job_id: jobs_id, page: page_no, lt: limit} , function(data)
	      	{
             if(data != undefined && data!="")
				 {

					var data_parts=data.split("<--second_part-->");
					$('#cat_auction_'+cat_id+'_'+jobs_id).html(data_parts[0]);
					init(data_parts[1],"all_category_page","");

				 }
          });
}
function book_bidbutler(form)
{

	var uid=form.uid.value;
	var aid=form.aid.value;
	var no_bid=form.no_bids.value;
	var job=form.job.value;

	var bidButler_id=form.bidbuttler_id.value;

	//alert('uid='+uid+'aid='+aid+'b_from='+b_from+'b_to='+b_to);
	//alert('uid'+uid+'aid'+aid+'b_from'+b_from+'b_to'+b_to+"no_bid"+no_bid+"job"+job+"bidButler_id"+bidButler_id);

	 if (uid == '' || uid == undefined || uid == null)
	 {return false;}
	 else if(aid == '' || aid == undefined || aid == null)
	 {return false;}


	$.post("function/book_bidbutlerd.php", { user_id: uid, auc_id: aid, no_bids: no_bid, action:job,bidButtlerId:bidButler_id } , function(data)
	      {
			 //alert(data);
			  butler_msg=data.split('#');

				if($.trim(butler_msg[0])=="error")
				{
					// alert(data);
					$('#bidbutller_msg').html(butler_msg[1]);
					$('#bid_butler_erro').show();
				}
				if($.trim(butler_msg[0])=="success")
				{
					$('#bidbutller_msg').html(butler_msg[1]);
					$('#bid_butler_erro').show();
					bidbutlerHistory(aid,uid);
					/* $('#bidbutlerhome').hide(); */
					 $('#bidbutleroverview').show();
				}
				if($.trim(butler_msg[0])=="edit_success")
				{
					$('#bidbutller_msg').html(butler_msg[1]);
					$('#bid_butler_erro').show();
					bidbutlerHistory(aid,uid);
				}
          });

		form.no_bids.value="";

	return false;
}
function bidbutlerHistory(aid,uid)
{

		$.post("function/ajax_bidbutler_overview.php", {user_id: uid, auc_id: aid} , function(data)
		{
			 //alert(data);
			 if (data != '' || data != undefined || data != null)
			   {
				  $('#bidbutlerov').html(data);
			   }
          });

}
function deleteBidButller(bidButtlerId)
{

		if(confirm("Are you sure to delete?"))
		{

		$.post("function/ajax_bidbutler_overview.php", {buttlerId: bidButtlerId, job:'delete'} , function(data)
		{
			   if (data != '' || data != undefined || data != null)
			   {
				  $('#bidbutlerov').html(data);
			   }
          });
	}
	else{return false}

}
function editBidButller(bidButtlerId)
{

		$.post("function/ajax_bidbutler_overview.php", {buttlerId: bidButtlerId, job:'edit'} , function(data)
		{
			   if (data != '' || data != undefined || data != null)
			   {
				  $('#bidbutlerov').html(data);
			   }
          });

}
function my_offers(frm_o)
{

	var code=frm_o.code.value;
	$.post("function/ajax_promotion_offer_process.php", {offer: code} , function(data)
		{
			   if (data != '' || data != undefined || data != null)
			   {
				  $('#offer_code').html(data);
			   }
          });
	return false;
}
function refer_friend(frm_o)
{

	var uemail=frm_o.email.value;
	$('#refer_pop_msg').show();
	$('#refer_pop_msg').html('<img src="images/ajax-loader.gif" />');

	$.post("function/ajax_refer_friend_process.php", {user_email: uemail} , function(data)
		{
			   if (data != '' || data != undefined || data != null)
			   {
				  $('#refer_pop_msg').show();
				  $('#refer_pop_msg').html(data);
				  	frm_o.email.value='';
				  setTimeout("hideDive5Sec('refer_pop_msg')", 3000);
			   }
          });
	return false;
}
function voteNow(aucID,userID)
{
	//alert(aucID);
	if(userID == undefined || userID == null || userID == '')
	{document.location.href='login.php';}
	else
	{
			$.post("function/ajax_vote_bidding_process.php", {aid: aucID, uid:userID} , function(data)
	      	{
				//alert(data);
				 var vote_bid_info=data.split('#');
				 var status=vote_bid_info[0];
				  var msg=vote_bid_info[1];
				 //alert(msg);
				if($.trim(status)=="no_paid")
				{
					$('#vote_bid_error_'+aucID).show();
					$('#vote_bid_error_'+aucID).html(msg);
					setTimeout("hideDive5Sec('vote_bid_error_"+aucID+"')", 3000);
				}
				else if($.trim(status)=="already_bid")
				{
					$('#vote_bid_error_'+aucID).show();
					$('#vote_bid_error_'+aucID).html(msg);
					setTimeout("hideDive5Sec('vote_bid_error_"+aucID+"')", 3000);
				}
				else
				{
					//alert(data);
					if(status=="Ended")
					{
						$('#vote_timer_'+$.trim(aucID)).html(msg);
						$('#vote_bttn_'+$.trim(aucID)).html("ENDED");
					}
					else
					{
						//alert(aucID)
						$('#vote_bid_error_'+aucID).show();
						$('#vote_bid_error_'+aucID).html(msg);
						setTimeout("hideDive5Sec('vote_bid_error_"+aucID+"')", 3000);
					}
				}

			 });
	}
}