var flipflop=1;
var storedata;
var auctionUpdateTime = 1000;
var counterUpdateTime = 1000;
var auctiondata = '';
var getStatusUrl;
var lastsendtime;
var GlobalVar = 0;
var reloadWhenEnd=false;

var lastmovetime=new Date();

$(function(){
    $( "#timeout_dialog" ).dialog({
        autoOpen: false,
        modal: false,
        buttons: {
            Ok: function() {
                $(this).dialog( "close" );
            }
        },
        close: function(event, ui) {
            setTimeout('updateAuctionInfo();', auctionUpdateTime);
        }
    });
$( "#highest_bidder_dialog" ).dialog({
        autoOpen: false,
		closeOnEscape: false,
		draggable: false,
		resizable: false,
        modal: true,
        buttons: {
            Ok: function() {
			setTimeout("$('#highest_bidder_dialog').dialog('close')",3000);

               // $(this).dialog( "close" );
            }
        }
    });

    $('body').mousemove(function(){
        lastmovetime=new Date();
    });

    $('.buy-now').hover(
        function(){
            var pricetag=$(this).attr('ref');
            $('#'+pricetag).show();
        },
        function(){
            var pricetag=$(this).attr('ref');

            $('#'+pricetag).hide();
        }
        );
});

function OnloadPage() {
    if ($.browser.msie) {
        $.ajaxSetup({
            cache: false,
            timeout:auctionUpdateTime*2
        });	//Configuring ajax
    }

    var firstauction=true;
    $('.auction-item').each(function() {
        //var auctionId    = $(this).attr('id');
        var auctionTitle = $(this).attr('title');
        if(firstauction){
            auctiondata += auctionTitle;
            firstauction=false;
        }else{
            auctiondata += ',' + auctionTitle;
        }
        
    });

    getStatusUrl = 'update_info.php?flp=' + flipflop;

    if($('#bidder_count').length){
        getStatusUrl = 'update_info.php?flp=' + flipflop+'&biddercount='+$('#bidder_count').html();
    }

    if($('#display_avatar').length){
        getStatusUrl+='&avatar='+$('#display_avatar').html();
    }

    

    setTimeout('updateAuctionInfo();', auctionUpdateTime);


    $('.ubid-button-link').click(function(){
        var aname=$(this).attr('name');

        if(aname==''){
            return;
        }

        var id=$(this).attr('rel');
        var price=$('#lowestprice_'+id).val();
        if(isNaN(price)==true || Number(price)<0.01){
            alert('Invalid Price');
            return;
        }

        $.ajax({
            url: siteurl+aname+"&bidprice="+price,
            dataType: 'json',
            success: function(data) {

                $.each(data, function(i, item) {
                    result = item.result.split("|");

                    //alert(result[0]);

                    if (result[0]=="unsuccess") {
                        if (result[1]==1) {
                            alert("You do not have enough free points available for this selection");
                            return false;
                        }else if(result[1]==2){
                            alert("You have achieved your weekly win limit");
                            return false;
                        }else if(result[1]==3){
                            alert("You have achieved your monthly win limit");
                            return false;
                        }else if(result[1]==11){
                            alert("You have reached your monthly win limit. Congratulations!");
                            return false;
                        }else if(result[1]==12){
                            alert("You have reached your monthly Bid Bundle win limit. Congratulations!");
                            return false;
                        }else if(result[1]==13){
                            alert("You have reached your 2 Bid Bundle win limit for the day. Congratulations!");
                            return false;
                        }else if(result[1]==14){
                            alert("You have reached your item win limit for the day. Congratulations!");
                            return false;
                        }else if(result[1]==15){
                            alert("You have reached your (200-499 USD) 2 item monthly win limit. Congratulations!  Please refer to your Win Limit section to check on your available wins.");
                            return false;
                        }else if(result[1]==16){
                            alert("You have reached your (500+ USD) item monthly win limit. Congratulations!  Please refer to your Win Limit section to check on your available wins.");
                            return false;
                        }else if(result[1]==4){
                            alert("Minimum amount of seats have not been met");
                            return false;
                        }else if(result[1]==5){
                            alert("You must obtain a seat for this auction");
                            return false;
                        }else if(result[1]==6){
                            alert("This auction is locked. You must be an early bidder to avoid auction locking");
                            return false;
                        }
                        else {
                            if (confirm("Please recharge your bidaccount!")) {
                                window.location.href='buybids.php';
                            }
                        }
                    }

                    if(result[0]=='existbid'){
                        alert('You have already bidded with this price');
                        return false;
                    }

                    if (result[0]=="success") {
                        $('#lowestprice_'+id).val('');
                        if (result[1]==1) {

                            //obj = document.getElementById('free_bids_count');
                            //objvalue = document.getElementById('free_bids_count').innerHTML;
                            if ($('#free_bids_count').html()!='0') {
                                $('#free_bids_count').html($('#free_bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        } else {
						
							var bincheckbox = 0;							
							if (document.getElementById("dugmo"))
							{
								var lfckv = document.getElementById("dugmo").checked;
								if (lfckv)
								{
									bincheckbox = 1;
								}
							}
							
                            //objvalue = document.getElementById('bids_count').innerHTML;
                            if (($('#spinner_bids_count').html()!='0') && (bincheckbox == 0)) {
                                $('#spinner_bids_count').html($('#spinner_bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
							else {
								if (($('#free_bids_count').html()!='0') && (bincheckbox == 0)) {
                                $('#free_bids_count').html($('#free_bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
								}
							else
							{
								if ($('#bids_count').html() =='1') {
								if (confirm("You just used your last bid. Click OK to recharge your bid account")) {
                                window.location.href='5mindeal.php';
                            }
								
								}
								if ($('#bids_count').html()!='0') {
                                $('#bids_count').html($('#bids_count').html()-1);
								}
							}
							}
                        }
                    }
                });
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });
        
    });

    $('.bid-button-link').click(function() {
        //alert('a');
        var url=$(this).attr('name');
        if(url=='')
            return;
        $.ajax({            
            url: siteurl+url,
            dataType: 'json',
            success: function(data) {
				
                $.each(data, function(i, item) {
                    result = item.result.split("|");
					
                    //alert(result[0]);
					
                    if (result[0]=="unsuccess") {
                        if (result[1]==1) {
                            alert("You do not have enough free points available for this selection");
                            return false;
                        }else if(result[1]==2){
                            alert("You have achieved your weekly win limit");
                            return false;
                        }else if(result[1]==3){
                            alert("You have achieved your monthly win limit");
                            return false;
                        }else if(result[1]==11){
                            alert("You have reached your monthly win limit. Congratulations!");
                            return false;
                        }else if(result[1]==12){
                            alert("You have reached your monthly Bid Bundle win limit. Congratulations!");
                            return false;
                        }else if(result[1]==13){
                            alert("You have reached your 2 Bid Bundle win limit for the day. Congratulations!");
                            return false;
                        }else if(result[1]==14){
                            alert("You have reached your item win limit for the day. Congratulations!");
                            return false;
                        }else if(result[1]==15){
                            alert("You have reached your (200-499 USD) 2 item monthly win limit. Congratulations!  Please refer to your Win Limit section to check on your available wins.");
                            return false;
                        }else if(result[1]==16){
                            alert("You have reached your (500+ USD) item monthly win limit. Congratulations!  Please refer to your Win Limit section to check on your available wins.");
                            return false;							
                        }else if(result[1]==4){
                            alert("Minimum amount of seats have not been met");
                            return false;
                        }else if(result[1]==5){
                            alert("You must obtain a seat for this auction");
                            return false;
                        }else if(result[1]==6){
                            alert("This auction is locked. You must be an early bidder to avoid auction locking");
                            return false;
                        }else if(result[1]==7){
                            alert("This auction has ended");
                            return false;
                        }else {
                            if (confirm("Please recharge your bidaccount!")) {
                                window.location.href='buybids.php';
                            }
                        }
                    }
                    if(result[0]=="topbid"){
                        //alert("You are the top bidder");
						$("#highest_bidder_dialog").dialog('open');
                        return false;
                    }
                    if (result[0]=="success") {
                        if (result[1]==1) {

                            //obj = document.getElementById('free_bids_count');
                            //objvalue = document.getElementById('free_bids_count').innerHTML;
                            if ($('#free_bids_count').html()!='0') {
                                $('#free_bids_count').html($('#free_bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        } else {
							
							var bincheckbox1 = 0;							
							if (document.getElementById("dugmo"))
							{
								var lfckv1 = document.getElementById("dugmo").checked;
								if (lfckv1)
								{
									bincheckbox1 = 1;
								}
							}
                            //obj = document.getElementById('bids_count');
                            //objvalue = document.getElementById('bids_count').innerHTML;
                            if (($('#spinner_bids_count').html()!='0') && (bincheckbox1 == 0)) {
                                $('#spinner_bids_count').html($('#spinner_bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
							else {
								if (($('#free_bids_count').html()!='0') && (bincheckbox1 == 0)) {
                                $('#free_bids_count').html($('#free_bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
								}
							else
							{
								if ($('#bids_count').html() =='1') {
								if (confirm("You just used your last bid. Click OK to recharge your bid account")) {
                                window.location.href='5mindeal.php';
                            }
								
								}
								if ($('#bids_count').html()!='0') {
                                $('#bids_count').html($('#bids_count').html()-1);
								}
							}
							}
                        }
                    }
                });
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });

        return false;
    });

    $('.butseat-button-link').click(function() {
        //alert('a');
        $.ajax({
            url: siteurl+$(this).attr('name'),
            dataType: 'json',
            success: function(data) {

                $.each(data, function(i, item) {
                    result = item.result.split("|");

                    //alert(result[0]);

                    if (result[0]=="unsuccess") {
                        if (result[1]==1) {
                            alert("You do not have enough free points available for this selection");
                            return false;
                        }else if(result[1]==2){
                            alert("This auction is not seated auction");
                            return false;
                        }else if(result[1]==3){
                            alert("Minimum amount of seats have not been met");
                            return false;
                        }else if(result[1]==4){
                            alert("Maximum amount of seats have been met");
                            return false;
                        }else if(result[1]==5){
                            alert("You already have a seat for this auction");
                            return false;
                        }else if(result[1]==7){
                            alert("This auction has ended");
                            return false;
                        }else {
                            if (confirm("Please recharge your bidaccount!")) {
                                window.location.href='buybids.php';
                            }
                        }
                    }

                    if (result[0]=="success") {
                        if (result[1]==1) {

                            //obj = document.getElementById('free_bids_count');
                            //objvalue = document.getElementById('free_bids_count').innerHTML;
                            if ($('#free_bids_count').html()!='0') {
                                $('#free_bids_count').html($('#free_bids_count').html()-result[2]);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        } else {
							
							var bincheckbox2 = 0;							
							if (document.getElementById("dugmo"))
							{
								var lfckv2 = document.getElementById("dugmo").checked;
								if (lfckv2)
								{
									bincheckbox2 = 1;
								}
							}
                            //obj = document.getElementById('bids_count');
                            //objvalue = document.getElementById('bids_count').innerHTML;
                            if (($('#spinner_bids_count').html()!='0') && (bincheckbox2 == 0)) {
                                $('#spinner_bids_count').html($('#spinner_bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
							else {
								if (($('#free_bids_count').html()!='0') && (bincheckbox2 == 0)) {
                                $('#free_bids_count').html($('#free_bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
								}
							else
							{
								if ($('#bids_count').html() =='1') {
								if (confirm("You just used your last bid. Click OK to recharge your bid account")) {
                                window.location.href='5mindeal.php';
                            }
								
								}
								if ($('#bids_count').html()!='0') {
                                $('#bids_count').html($('#bids_count').html()-1);
								}
							}
							}
                        }
                    }
                });
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });

        return false;
    });
   

    $(".bookbidbutlerbutton").click(function() {
        //alert(document.getElementById('bookbidbutlerbutton').name);
        if ($('#bookbidbutlerbutton').attr('name')!="") {
			
            var bidbutstartprice = Number($('#bid_form').val());
            var bidbutendprice = Number($('#bid_to').val());
            var totalbids = $('#bid_bids').val();
		
            if (bidbutstartprice=="") {
                alert("Please enter AutoBidder start price!");
                return false;
            }
            if (bidbutendprice=="") {
                alert("Please enter AutoBidder end price!");
                return false;
            }
            if (totalbids=="") {
                alert("Please enter AutoBidder bids!");
                return false;
            }
            if (totalbids<=1) {
                alert("You must book AutoBidder with more than just one bid!");
                return false;
            }

            if($('#isreverseauction').val()=='0'){
                if (bidbutstartprice >= bidbutendprice) {
                    alert("AutoBidder start price must be higher than end price!");
                    return false;
                }
            }
            else{
                if (bidbutstartprice <= bidbutendprice) {
                    alert("AutoBidder start price must be higher than end price for reverse auction!");
                    return false;
                }
            }

            $.ajax({
                url: siteurl+"addbidbutler.php?aid="+$(this).attr('name')+"&bidsp="+bidbutstartprice+"&bidep="+bidbutendprice+"&totb="+totalbids,
                dataType: 'json',
                success: function(data) {
                    $.each(data, function(i, item) {
                        if (item.result) {
                            result = item.result.split("|");
                            if (result=="unsuccessprice") {
                                alert("BID FROM Value needs to be higher than the Current Auction Price!");
                            } else if (result[0]=="unsuccess") {
                                if (result[1]==1) {
                                    alert("You don't have sufficient free points in your account!");
                                }
                                else {
                                    alert(result[1]);
                                }
                            }
                        } else {
                            $('#bid_form').val('');
                            $('#bid_to').val('');
                            $('#bid_bids').val('');                            
                            $('#butlermessage').show();
                            changeMessageTimer = setInterval("ChangeButlerImageSecond()",5000);
                            
//                            if(data.use_free=='1'){
//                                $('#free_bids_count').html(data.bids);
//                            }else{
//                                $('#bids_count').html(data.bids);
//                            }
                            changedatabutler(data,"abut",totalbids);
                        }
                    });
                },
                error: function(XMLHttpRequest,textStatus, errorThrown) { }
            });

            return false;
        }
    });
}

function updateUniqueHistory(){
    if($('.productUniqueAuction').length<=0){
        return;
    }

    auctionhisid = $('#history_auctionid').html();//document.getElementById('history_auctionid').innerHTML;

    oldbids = $('#curproductbids').html();//document.getElementById('curproductprice').innerHTML;
    newbids = $('#ubid_index_page_' + auctionhisid).html();//document.getElementById('price_index_page_' + auctionhisid).innerHTML;

    if (true) {
        //alert('a');
        getStatusUrl3 = siteurl+'updatehistory_unique.php?aucid_new='+auctionhisid;

        $.ajax({
            url: getStatusUrl3,
            dataType: 'json',
            success: function(data) {
                var lastPos,lastName,currentName,currentPos;

                if(data==null || data.message=='failed') return;
                //data1 = eval('(' + data.responseText + ')');
                var fontweight;
                //alert(data);

                for (var i=0; i<data.hiss.length; i++) {
                    username = data.hiss[i].his.un;
                    adddate = data.hiss[i].his.ad;

                    if(i==0){
                        fontweight="bold";
                    }else{
                        fontweight="normal";
                    }

                    if(i==0){
                        currentName=data.hiss[i].his.un;
                        currentPos=data.hiss[i].his.latlng;
                    }

                    if(i==1){
                        lastName=data.hiss[i].his.un;
                        lastPos=data.hiss[i].his.latlng;
                    }

                    $("#bid_user_name_"+i).html(username);
                    $("#bid_user_name_"+i).css("font-weight", fontweight);

                    $("#bid_date_"+i).html(adddate);
                    $("#bid_date_"+i).css("font-weight", fontweight);
                }

                if(typeof(updateMarker)=='function'){
                    updateMarker(currentPos,currentName,lastPos,lastName);
                }

                //alert(data.myhistories.length);

                if (data.mhiss.length>0) {
                    for (j=0; j<data.mhiss.length; j++) {
                        if(j==0){
                            fontweight="bold";
                        }else{
                            fontweight="normal";
                        }

                        username1 = data.mhiss[j].mhis.un;
                        adddate1= data.mhiss[j].mhis.ad;
                        bidprice1= data.mhiss[j].mhis.bp;

                        //document.getElementById('my_bid_price_' + j).innerHTML = "$" +  biddingprice1;
                        $("#my_bid_username_"+j).html(username1);
                        $("#my_bid_username_"+j).css("font-weight", fontweight);

                        $("#my_bid_price_"+j).html(bidprice1);
                        $("#my_bid_price_"+j).css("font-weight", fontweight);

                        //document.getElementById('my_bid_time_' + j).innerHTML = biddingusername1;
                        $("#my_bid_date_"+j).html(adddate1);
                        $("#my_bid_date_"+j).css("font-weight", fontweight);
                    }
                }

                $("#curproductbids").html(newbids);
            //changedatabutler(data,"abut",data.butlerslength.length);
            //document.getElementById('curproductprice').innerHTML = data.histories[0].history.bprice;
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });

        //update saving
        var onlineperbidvalue=$("#onlineperbidvalue_text").val();
        var price=$("#price_text").val();
        var fprice=$("#fprice_text").val();
        var aucid=$("#aucid_text").val();

        //alert(price+"_"+fprice+"_"+aucid+"_"+onlineperbidvalue);

        $.ajax({
            type:'POST',
            url:siteurl+'update_savingprice.php',
            dataType:'json',
            cache:false,
            data:{
                onlineperbidvalue:onlineperbidvalue,
                aucid:aucid,
                price:price,
                fprice:fprice
            },
            success:function(data){
                //alert(data);
                if(data.msg=='ok'){
                    $(".placebidscount").html(data.data.totbid);
                    $(".placebidsamount").html(data.data.totbidprice);
                    $("#placebidssavingdisp").html(data.data.saving);
                    $("#placebidssaving").html(data.data.saving);
                    $(".newbuynowprice").html(CurrencySymbol + data.data.buynowprice);
                }
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
            //alert(textStatus);
            }
        });

    }
}

function updateHistory(){
    
    if($('.productImageThumb').length<=0){
        return;
    }
    auctionhisid = $('#history_auctionid').html();//document.getElementById('history_auctionid').innerHTML;

    oldprice = $('#curproductprice').html();//document.getElementById('curproductprice').innerHTML;
    newprice = $('#price_index_page_' + auctionhisid).html();//document.getElementById('price_index_page_' + auctionhisid).innerHTML;

    //console.log(oldprice+' '+newprice);

    if (true) {
        getStatusUrl3 = siteurl+'updatehistory.php?aucid_new='+auctionhisid;

        $.ajax({
            url: getStatusUrl3,
            dataType: 'json',
            success: function(data) {
                var lastPos,lastName,currentName,currentPos;

                if(data==null || data.message=='failed') return;
                //data1 = eval('(' + data.responseText + ')');
                var fontweight;
                //alert(data);

                for (var i=0; i<data.hiss.length; i++) {
                    biddingprice = data.hiss[i].his.bp;
                    biddingusername = data.hiss[i].his.un;
                    biddingtype = data.hiss[i].his.bt;

                    if(i==0){
                        currentName=data.hiss[i].his.un;
                        currentPos=data.hiss[i].his.latlng;
                    }

                    if(i==1){
                        lastName=data.hiss[i].his.un;
                        lastPos=data.hiss[i].his.latlng;
                    }


                    if(i==0){
                        fontweight="bold";
                    }else{
                        fontweight="normal";
                    }

                    $("#bid_price_"+i).html(CurrencySymbol + biddingprice);
                    $("#bid_price_"+i).css("font-weight", fontweight);

                    $("#bid_user_name_"+i).html(biddingusername);
                    $("#bid_user_name_"+i).css("font-weight", fontweight);

                    if (biddingtype=='s') {
                        $("#bid_type_"+i).html("Single Bid");
                    //document.getElementById('bid_type_' + i).innerHTML = "Single Bid";
                    } else if (biddingtype=='b') {
                        //document.getElementById('bid_type_' + i).innerHTML = "AutoBidder";
                        $("#bid_type_"+i).html("AutoBidder");
                    } else if (bidding_type=='m') {
                        //document.getElementById('bid_type_' + i).innerHTML = "SMS Bid";
                        $("#bid_type_"+i).html("SMS Bid");
                    }
                    $("#bid_type_"+i).css("font-weight", fontweight);

                }
                if(typeof(updateMarker)=='function'){
                    updateMarker(currentPos,currentName,lastPos,lastName);
                }

                //alert(data.myhistories.length);

                if (data.mhiss.length>0) {
                    for (j=0; j<data.mhiss.length; j++) {
                        if(j==0){
                            fontweight="bold";
                        }else{
                            fontweight="normal";
                        }

                        biddingprice1 = data.mhiss[j].mhis.bp;
                        biddingusername1 = data.mhiss[j].mhis.t;
                        biddingtype1 = data.mhiss[j].mhis.bt;

                        //document.getElementById('my_bid_price_' + j).innerHTML = "$" +  biddingprice1;
                        $("#my_bid_price_"+j).html(CurrencySymbol +  biddingprice1);

                        $("#my_bid_price_"+j).css("font-weight", fontweight);

                        //document.getElementById('my_bid_time_' + j).innerHTML = biddingusername1;
                        $("#my_bid_time_"+j).html(biddingusername1);
                        $("#my_bid_time_"+j).css("font-weight", fontweight);

                        if (biddingtype1=='s') {
                            $("#my_bid_type_"+j).html("Single Bid");
                        //document.getElementById('my_bid_type_' + j).innerHTML = "Single Bid";
                        } else if (biddingtype1=='b') {
                            $("#my_bid_type_"+j).html("AutoBidder");
                        //document.getElementById('my_bid_type_' + j).innerHTML = "AutoBidder";
                        } else if (biddingtype1=='m') {
                            //document.getElementById('my_bid_type_' + j).innerHTML = "SMS Bid";
                            $("#my_bid_type_"+j).html("SMS Bid");
                        }
                        $("#my_bid_type_"+j).css("font-weight", fontweight);
                    }
                }

                if($('#product_auctionprice').length>0)
                    $("#product_auctionprice").html(CurrencySymbol + data.hiss[0].his.bp);

                $("#curproductprice").html(data.hiss[0].his.bp);
                changedatabutler(data,"",data.butlerslength.length);
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });

        //update saving
        var onlineperbidvalue=$("#onlineperbidvalue_text").val();
        var price=$("#price_text").val();
        var fprice=$("#fprice_text").val();
        var aucid=$("#aucid_text").val();

        //alert(price+"_"+fprice+"_"+aucid+"_"+onlineperbidvalue);

        $.ajax({
            type:'POST',
            url:siteurl+'update_savingprice.php',
            dataType:'json',
            cache:false,
            data:{
                onlineperbidvalue:onlineperbidvalue,
                aucid:aucid,
                price:price,
                fprice:fprice
            },
            success:function(data){
                //alert(data);
                if(data.msg=='ok'){
                    //console.log(data.data.buynowprice);
                    $(".placebidscount").html(data.data.totbid);
                    $(".placebidsamount").html(data.data.totbidprice);
                    $("#placebidssavingdisp").html(data.data.saving);
                    $("#placebidssaving").html(data.data.saving);
                    $(".newbuynowprice").html(CurrencySymbol + data.data.buynowprice);
                }
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
            //alert(textStatus);
            }
        });

    }
}

function updateAuctionInfo() {
    if (auctiondata.length>0) {
        $.ajax({
            url: siteurl+getStatusUrl,
            dataType: 'json',
            type: 'get',
            cache:false,
            timeout: 3000,
            data: {
                auctionlist:auctiondata
            },
            global: false,
            success: function(response) {
                if(response.message!='ok') return;

                var data=response.data;
                storedata = response.data;


                //console.log((new Date()-lastsendtime)/1000-response.time+' '+response.time);
                log((new Date()-lastsendtime)/1000-response.time+' '+response.time);

                //alert(data);

                $.each(data, function(i, item) {
                    //alert(item.auc_id);
                    auction_id = item.id;
                    auction_price = item.np;
                    auction_bidder_name = item.hu==null?'---':item.hu;

                    if(reloadWhenEnd && item.lt==0){
                        //console.log('reload');
                        window.location.reload();
                    }

                    if(typeof(updateAuction)=='function'){
                        updateAuction(auction_id,CurrencySymbol+auction_price,item.lt);
                    }
              
                    var options = {
                        color:'#f79909'
                    };

                    if(item.sa==true){
                        if(item.san==true){
                            if($('#seat_count_'+auction_id).html()!=item.sc){
                                $('#seat_count_'+auction_id).html(item.sc);
                                var bpos=(item.sc / item.ms-1)*120;
                                $('#seat_bar_'+auction_id).css('background-position',bpos+'px 0px');
                                if (GlobalVar == 1) {
                                    //$('#seat_count_' + auction_id).effect('highlight',options,500);
                                    flashEffect('seat_bar_',auction_id);
                                }
                            }
                            return;
                        }else{
                            if($('#seat_panel_'+ auction_id).length>0 && $('#seat_panel_'+ auction_id).css('display')=='block'){
                                $('#seat_panel_'+ auction_id).css('display', 'none');
                                $('#normal_panel_'+ auction_id).css('display', 'block');
                                // the button
                                $('#seat_button_'+ auction_id).css('display', 'none');
                                $('#normal_button_'+ auction_id).css('display', 'block');
                            }
                        }
                    }

                    if(item.ua==false){
                        //console.log($("#price_index_page_"+auction_id).html()+' ' +auction_price);

                        if ($("#price_index_page_"+auction_id).length>0 && $("#price_index_page_"+auction_id).html() != auction_price) {
                            updateHistory();
                            
                            if (GlobalVar == 1) {
                                if ($('#history_auctionid').length>0) {

                                    if (auction_id==$('#history_auctionid').html()){// document.getElementById('history_auctionid').innerHTML) {
                                        //$('#price_index_page_' + auction_id).effect('highlight',options,500);
                                        //$('#currencysymbol_' + auction_id).effect('highlight',options,500);
                                        flashDetailEffect('price_index_page_',auction_id);
                                    } else {
                                        //$('#price_index_page_' + auction_id).effect('highlight',options,500);
                                        //$('#currencysymbol_' + auction_id).effect('highlight',options,500);
                                        flashEffect('price_index_page_',auction_id);
                                    }
                                } else {
                                    //$('#price_index_page_' + auction_id).effect('highlight',options,500);
                                    flashEffect('price_index_page_',auction_id);
                                //$('#currencysymbol_' + auction_id).effect('highlight',options,500);

                                }

                            }

                            


                            if($('#product_avatarimage_'+auction_id && item.avatar!="").length){
                                $('#product_avatarimage_'+auction_id).attr('src',item.avatar);
                            }


                            $('#price_index_page_'+auction_id).html(auction_price);
                            $('#currencysymbol_' + auction_id).html(CurrencySymbol);

                            //set the tax amount

                            if($('#product_taxamount_'+auction_id).length>0){
                                var tax1=$('#product_tax1_'+auction_id).val();
                                var tax2=$('#product_tax2_'+auction_id).val();
                                var taxamount=0;
                                if(tax1!=0){
                                    taxamount+=auction_price*tax1/100;
                                }
                                if(tax2!=0){
                                    taxamount+=auction_price*tax2/100;
                                }
                                $('#product_taxamount_'+auction_id).html(CurrencySymbol + Math.round(taxamount*100)/100);
                            }


                            if($("#product_bidder_"+auction_id).length>0){
                                $("#product_bidder_"+auction_id).html(auction_bidder_name);
                            }

                            //alert($('#topbider_index_page_' + auction_id).length);

                            if($('#topbider_index_page_' + auction_id).length>0){
                                //PennyAuctionSoft add for top bidder
                                topbidder=item.tb;
                                acls=$('#topbider_index_page_' + auction_id).attr('class');
                                totalcount=0;
                                if(acls.indexOf('i4')>0){
                                    totalcount=4;
                                }else if(acls.indexOf('i3')>0){
                                    totalcount=3;
                                }
                                if(totalcount>0){
                                    bidderhtml="";
                                    $.each(topbidder,function(i,bitem){
                                        bidderhtml+='<li><a>'+bitem+'</a></li>';
                                        totalcount--;
                                        if(totalcount==0) return false;
                                    });
                                    for(i=totalcount-1;i>=0;i--){
                                        bidderhtml+='<li><a>---</a></li>';
                                    }
                                    $('#topbider_index_page_' + auction_id).html(bidderhtml);
                                }
                            }
                        //PennyAuctionSoft add for top bidder
                            

                        }
                    }else{
                        if ($("#ubid_index_page_"+auction_id).length>0 && $("#ubid_index_page_"+auction_id).html() != item.lbc) {
                            updateUniqueHistory();

                            if (GlobalVar == 1) {
                                if ($('#history_auctionid').length>0) {

                                    if (auction_id==$('#history_auctionid').html()){// document.getElementById('history_auctionid').innerHTML) {
                                        //$('#ubid_index_page_' + auction_id).effect('highlight',options,500);
                                        //$('#currencysymbol_' + auction_id).effect('highlight',options,500);
                                        flashDetailEffect('ubid_index_page_',auction_id);

                                    } else {
                                        flashEffect('ubid_index_page_',auction_id);
                                    //$('#currencysymbol_' + auction_id).effect('highlight',options,500);

                                    }
                                }else {
                                    //$('#ubid_index_page_' + auction_id).effect('highlight',options,500);
                                    flashEffect('ubid_index_page_',auction_id);
                                //$('#currencysymbol_' + auction_id).effect('highlight',options,500);
                                }

                            }


                            if($('#product_avatarimage_'+auction_id && item.avatar!="").length){
                                $('#product_avatarimage_'+auction_id).attr('src', item.av);
                            }


                            $('#ubid_index_page_'+auction_id).html(item.lbc);

                            if($("#product_bidder_"+auction_id).length>0){
                                $("#product_bidder_"+auction_id).html(auction_bidder_name);
                            }
                            
                        }
                    }
                });
                GlobalVar = 1;
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) {
            //runUpdateTimer();
            },
            complete:function(){
                runUpdateTimer();
            },
            beforeSend:function(XMLHttpRequest){
                lastsendtime=new Date();
            }
        });
    }
    if (flipflop==1) {
        flipflop = 1;
    //ChangeCountdownData(storedata);
    } else if (flipflop==2) {
        flipflop = 1;
    //alert(storedata);
    //ChangeCountdownData(storedata);
    }
    ChangeCountdownData(storedata);
}

function flashDetailEffect(itemname,itemid){
   
    var strFontsize=$('#'+itemname + itemid).css('font-size');

    var iFontsize=Number(strFontsize.substr(0, strFontsize.length-2));
    if(iFontsize>30){
        iFontsize=30;
    }

    var fsize=(iFontsize+3)+'px';
    var fsoption={
        fontSize:fsize
    };

    $('#'+itemname + itemid).animate(fsoption,300,'linear',function(){
        $('#flash-bg-'+itemid+' .flash-highlight').fadeTo(300, 0.9,function(){
            $(this).fadeTo(300, 0.1,function(){
                $(this).hide();
            });
        });
        $('#cross-flash'+itemid).css({
            backgroundPosition:'-300px 0px'
        }).animate({
            backgroundPosition:350
        },300,'linear');
        $('#'+itemname + itemid).animate({
            fontSize:iFontsize+'px'
        },300);
    });
}

function flashEffect(itemname,itemid){
    var strFontsize=$('#'+itemname + itemid).css('font-size');
    var iFontsize=Number(strFontsize.substr(0, strFontsize.length-2));
    if(iFontsize>16){
        iFontsize=16;
    }
    var fsize=(iFontsize+3)+'px';
    var fsoption={
        fontSize:fsize
    };

    $('#'+itemname + itemid).animate(fsoption,200,'linear',function(){
        $('#flash-bg-'+itemid+' .flash-highlight').fadeTo(200, 0.9,function(){
            $(this).fadeTo(200, 0.1,function(){
                $(this).hide();
            });
        });
        $('#'+itemname + itemid).animate({
            fontSize:iFontsize+'px'
        },200);
    });
}

function runUpdateTimer(){
    if((new Date()-lastmovetime)/1000>timeoutvalue && timeoutvalue!=0){
        try{
            $("#timeout_dialog").dialog('open');
            return;
        }catch(e){
            
        }
    }

    var lefttime=(auctionUpdateTime-(new Date()-lastsendtime));
    if(lefttime<=0)
        lefttime=0;
    //console.log(lefttime);
    setTimeout('updateAuctionInfo();', lefttime);
}

function DeleteBidButler(id, div_id) {
    $.ajax({
        url: url = siteurl+"deletebutler.php?delid=" + id,
        dataType: 'json',
        success: function(data) {
            $.each(data, function(i, item) {
                result = item.result;
                if (result=="unsuccess") {
                    alert("Your BidBuddy is running you can't delete it!");
                } else {
                    placebids = document.getElementById('butlerbids_' + div_id).innerHTML;
                    if ($('.usefreebids').length && document.getElementById('useonlyfree').innerHTML == '1') {
                        objbids = document.getElementById('free_bids_count');
                        objbidsvalue = document.getElementById('free_bids_count').innerHTML;

                        if (objbids.innerHTML!='0') {
                            objbids.innerHTML = Number(objbidsvalue) + Number(placebids);
                        }
                    } else {
                        objbids = document.getElementById('bids_count');
                        objbidsvalue = document.getElementById('bids_count').innerHTML;
                        if (objbids.innerHTML!='0') {
                            objbids.innerHTML = Number(objbidsvalue) + Number(placebids);
                        }
                    }
                    changedatabutler(data,"dbut","");
                }
            });
        },
        error: function(XMLHttpRequest,textStatus, errorThrown) { }
    });
    return false;
}

function ChangeCountdownData(resdata) {
	
    if (resdata && resdata!="") {
        data = resdata;

        $.each(data, function(i, item) {
            auction_id = item.id;
            auction_time = item.lt;
            pausestatus = item.p;

            if (auction_time) {
                //alert(auction_time);
                var oncearr=['ON','CE','!'];
                var twicearr=['TW','IC','E!'];
                var soldarr=['SO','LD','!'];
                var pausearr=['PA','US','E!'];
                if(auction_time==2 && enableTimerDelayer==true){
                    $('#counter_index_page_' + auction_id).css('color', '#E80000');
                    $('#counter_index_page_' + auction_id).html(buildtimerlist(oncearr));
                }else if(auction_time==1 && enableTimerDelayer==true){
                    $('#counter_index_page_' + auction_id).css('color', '#E80000');
                    $('#counter_index_page_' + auction_id).html(buildtimerlist(twicearr));
                }else if (auction_time==0) {
                    $('#counter_index_page_' + auction_id).css('color', '#E80000');
                    $('#counter_index_page_' + auction_id).html(buildtimerlist(soldarr));
                    $('#image_main_' + auction_id).attr('onclick', '');
                    $('#image_main_' + auction_id).attr('name','');
                    $('#image_main_' + auction_id).text('SOLD');
                //document.getElementById('image_main_' + auction_id).src = "img/buttons/btn-sold_92.png";
                } else if (pausestatus==1) {
                    $('#counter_index_page_' + auction_id).html(buildtimerlist(pausearr));
                    //document.getElementById('image_main_' + auction_id).src = "img/buttons/btn_placebid_92.png";
                    $('#image_main_' + auction_id).attr('onclick', '');
                    $('#image_main_' + auction_id).attr('name','');
                    $('#image_main_' + auction_id).text('PAUSE');
                } else {
                    if(enableTimerDelayer==true){
                        auction_time-=2;
                    }
                    
                    if (auction_time<10) {
                        $('#counter_index_page_' + auction_id).css('color', '#E80000');
                        $('#counter_index_page_' + auction_id).html(calc_counter_from_time(auction_time));
                    } else {
                        $('#counter_index_page_' + auction_id).css('color', '#ffffff');
                        $('#counter_index_page_' + auction_id).html(calc_counter_from_time(auction_time));
                    }
                    $('#image_main_' + auction_id).show();
                }

                if($('#blink_img_'+auction_id).length){
                    if(auction_time>0 && auction_time<=15){
                        $('#blink_img_'+auction_id).css('display', 'block');
                    }else{
                        $('#blink_img_'+auction_id).css('display', 'none');
                    }
                }
               
            }
        });
    }
}

function showhide_auctype(value,type){    
    if(type=='over'){
        $('#auction_type'+value).show();

    }else if(type=='out'){
        $('#auction_type'+value).hide();
    }
}


//callback function to bring a hidden box back
function callback(){
    setTimeout(function(){
        $("#effect:hidden").removeAttr('style').hide().fadeIn();
    }, 1000);
}





function number_format( number, decimals, dec_point, thousands_sep ) {

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;

    var d = dec_point == undefined ? "." : dec_point;

    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";

    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;



    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");

}

function log(msg){
//console.log(msg);
}
