var flipflop=1;
var storedata;
var checktime = new Date().getTime() / 1000;
var timecheck = checktime;
var checktime2 = checktime;
var joinvar = 0;
var str= document.location.href;
var dpage = 0;
function OnloadPage() {
    if ($.browser.msie) {
        $.ajaxSetup({
            cache: false
        });	//Configuring ajax
    }

    var auctionUpdateTime = 1000;
    var counterUpdateTime = 1000;


    var auctions = '';

    $('.auction-item').each(function() {
        var auctionId    = $(this).attr('id');
        var auctionTitle = $(this).attr('title');        
        auctions = auctions + auctionId + '=' + auctionTitle + '&';
    });
    if(str.match(".html"))
    {
    	var explod = str.split('-');
    	var tempauid = explod[explod.length-1].replace('.html','');
    	dpage = 1;
    }

    getStatusUrl = 'update_info.php?flp=' + flipflop + '&dpage=' + dpage + '&aucsid=' + tempauid;;

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

    if($('#display_avatar').length){
        getStatusUrl+='&avatar='+$('#display_avatar').html();
    }
//alert(getStatusUrl);
    var GlobalVar = 0;

    setInterval(function() {
        if (auctions.length>0) {
            
            //alert(auctions);
            $.ajax({
                url: getStatusUrl,
                dataType: 'json',
                type: 'get',
                cache:false,
                timeout: 3000,
                data: auctions,
                global: false,
                success: function(response) {
                    if(response.message!='ok') return;

                    var data=response.data;
                    storedata = response.data;
					
                    //alert(data);                  

                    $.each(data, function(i, item) {
                        //alert(item.auc_id);
                        auction_id = item.auc_id;
                        auction_price = item.newprice;
                        auction_bidder_name = item.username;

                        
						
                        //                        value=item.auction.value;
                        //                        if (auction_bidder_name=="") {
                        //                            auction_bidder_name="---"
                        //                        }
                        //                        if(value==""){
                        //                            value="---";
                        //                        }
						
                        //var pip=document.getElementById('price_index_page_' + auction_id);
                        //alert($("#price_index_page_"+auction_id).html()+'--'+auction_price);
                        var options = {
                            color:'#FF6666'
                        };
                        if(item.uniqueauction==false){

                            if ($("#price_index_page_"+auction_id).length>0 && $("#price_index_page_"+auction_id).html() != (CurrencySymbol+auction_price)) {
                                
                                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,1000);
                                            //$('#product_bidder_' + auction_id).effect('highlight',options,1000);
                                            //$('#counter_index_page_' + auction_id).effect('highlight',options,1000);
                                        //$('#currencysymbol_' + auction_id).effect('highlight',options,500);
                                      
                                        } else {
                                            $('#price_index_page_' + auction_id).effect('highlight',options,1000);
                                            //$('#product_bidder_' + auction_id).effect('highlight',options,1000);
                                            //$('#counter_index_page_' + auction_id).effect('highlight',options,1000);
                                        //$('#currencysymbol_' + auction_id).effect('highlight',options,500);
                                     
                                        }
                                    } else {
                                        $('#price_index_page_' + auction_id).effect('highlight',options,1000);
                                        //$('#product_bidder_' + auction_id).effect('highlight',options,1000);
                                        //$('#counter_index_page_' + auction_id).effect('highlight',options,1000);
                                    //$('#currencysymbol_' + auction_id).effect('highlight',options,500);
                           
                                    }
                                
                                }


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

                                $('#price_index_page_'+auction_id).html(CurrencySymbol+auction_price);
                                //$('#currencysymbol_' + auction_id).html(CurrencySymbol);
                                $('#acutalprice_'+auction_id).html(CurrencySymbol+auction_price);
                                
                                if($('#bhistory1_'+auction_id).length)
                                {
                                	
                                
                                $('#bhistory1_'+auction_id).html('');
                                
                                $.each(item.Histories, function(n, tRow){
                                    
                                	var bidtype = "";
                                	var bidp = "";
                                	var bidu = "";
                                	if(!tRow.bidding_price)
                                	{
                                		bidp = "&nbsp;";
                                	}
                                	else
                                	{
                                		bidp = "$"+tRow.bidding_price;
                                	}
                                	if(!tRow.username)
                                	{
                                		bidu = "&nbsp;";
                                	}
                                	else
                                	{
                                		bidu = tRow.username;
                                	}
                                	if(!tRow.bidding_type)
                                	{
                                		bidtype = "&nbsp;";
                                	}
                                	else if(tRow.bidding_type == "b")
                                	{
                                		 bidtype = "AutoBidder"; 
                                	}
                                	else if(tRow.bidding_type == "s")
                                	{
                                		 bidtype = "Single Bid";
                                	}
                                	var row = '<tr><td width="49">' + bidp + '</td><td width="130">' + bidu + '</td><td width="57">' + bidtype + '</td></tr>';
									
                                    $('#bhistory1_'+auction_id).append(row);
                                });
                                }
                                if($('#girl1').html() && item.buynow == 0)
                                {
                                	$('#girl1').hide();
                                }
								//alert(item.buynow);
                                //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);
                                }
                            
                                //                            var vip=document.getElementById('value_index_page_' + auction_id);
                                //                            if(vip){
                                //                                vip.innerHTML=value;
                                //                            }
                                //document.getElementById('value_index_page_' + auction_id).innerHTML = value;

                                if($("#product_bidder_"+auction_id).length>0){
                                	if(auction_bidder_name == "")
                                	{
                                		$("#product_bidder_"+auction_id).html("No Bids Yet");
                                	}
                                	else
                                	{
                                    $("#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.topbidder;
                                    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 {
                                //pip=document.getElementById('price_index_page_' + auction_id);
                                if ($('#history_auctionid').length>0) {
								
                                    if (auction_id==$("#history_auctionid").html()){ // document.getElementById('history_auctionid').innerHTML) {
                                        //$("#price_index_page_"+auction_id).css('backgroundColor', "#ffffff");
                                    }
                                    else {
                                        //$("#price_index_page_"+auction_id).css('backgroundColor', "#ffffff");
                                    }
                                } else {
                                    //$("#price_index_page_"+auction_id).css('backgroundColor', "#ffffff");

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

                                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,1000);
                                            //$('#product_bidder_' + auction_id).effect('highlight',options,1000);
                                            //$('#counter_index_page_' + auction_id).effect('highlight',options,1000);
                                        //$('#currencysymbol_' + auction_id).effect('highlight',options,500);

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

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

                                }


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


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

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

                            } else {
                                //pip=document.getElementById('price_index_page_' + auction_id);
                                if ($('#history_auctionid').length>0) {

                                    if (auction_id==$("#history_auctionid").html()){ // document.getElementById('history_auctionid').innerHTML) {
                                        $("#ubid_index_page_"+auction_id).css('backgroundColor', "#ffffff");
                                    }
                                    else {
                                        $("#ubid_index_page_"+auction_id).css('backgroundColor', "#ffffff");
                                    }
                                } else {
                                    $("#ubid_index_page_"+auction_id).css('backgroundColor', "#ffffff");
                                }
                            }
                        }
                    });
                    GlobalVar = 1;
                },
                error: function(XMLHttpRequest,textStatus, errorThrown) {/*alert(textStatus+"--"+errorThrown);*/}
            });
        }
        if (flipflop==1) {
            flipflop = 1;
        //ChangeCountdownData(storedata);
        } else if (flipflop==2) {
            flipflop = 1;
        //alert(storedata);
        //ChangeCountdownData(storedata);
        }
        ChangeCountdownData(storedata);
    }, auctionUpdateTime);

    $('.ubid-button-link').click(function(){
        var aname=$(this).attr('name');
        var id=$(this).attr('rel');
        var price=$('#lowestprice_'+id).val();
        if(isNaN(price)==true || Number(price)<0.01){
            alert('Invalid Price');
            return;
        }

        $.ajax({
            url: aname+"&bidprice="+price,
            dataType: 'json',
            success: function(data) {
				checktime = new Date().getTime() / 1000;
                $.each(data, function(i, item) {
                    result = item.result.split("|");
					
                    //alert(result[0]);

                    if (result[0]=="unsuccess") {
                        if (result[1]==1) {
                            alert("You don't have sufficient free points in your account!");
                            return false;
                        }else if(result[1]==2){
                            alert("Sorry, you have reached your weekly limit for won auctions!");
                            return false;
                        }else if(result[1]==3){
                            alert("Sorry, you have reached your monthly limit for won auctions!");
                            return false;
                        }else if(result[1]==4){
                            return false;
                        }else {
                            if (confirm("You'll need to buy more bids!")) {
                                window.location.href='buybids.php';
                            }
                        }
                    }
					
                    if(result[0]=='existbid'){
                        alert('You have already bid 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 {
                            //obj = document.getElementById('bids_count');
                            //objvalue = document.getElementById('bids_count').innerHTML;
                            if ($('#bids_count').html()!='0') {
                                $('#bids_count').html($('#bids_count').html()-1);
                                $('#bid_msg_'+id).html('Your bid was placed')
                                							 .show(1)
                                                             .animate({opacity: 1.0}, 2000)
                                                             .hide(1);
                                //alert(item.auc_id);
                                //alert(id);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        }
                    }
                });
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });
        
    });

    $('.bid-button-link').click(function() {
    	var tmpa=$(this).attr('name').split("=");
    	var tmpb = tmpa[2].split("&");
    	var itid = tmpb[0];
        //alert('a');
        $.ajax({            
            url: $(this).attr('name'),
            dataType: 'json',
            success: function(data) {
				checktime = new Date().getTime() / 1000;
                $.each(data, function(i, item) {
                    result = item.result.split("|");
					
                    //alert(result[0]);
					
                    if (result[0]=="unsuccess") {
                        if (result[1]==1) {
                            alert("You don't have sufficient free points in your account!");
                            return false;
                        }else if(result[1]==2){
                            alert("Sorry, you have reached your weekly limit for won auctions!");
                            return false;
                        }else if(result[1]==3){
                            alert("Sorry, you have reached your monthly limit for won auctions!");
                            return false;
                        }else if(result[1]==4){
                            return false;
                        }else {
                            if (confirm("You'll need to buy more bids!")) {
                                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()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        } else {
                            //obj = document.getElementById('bids_count');
                            //objvalue = document.getElementById('bids_count').innerHTML;
                            if ($('#bids_count').html()!='0') {
                                $('#bids_count').html($('#bids_count').html()-1);
                                $('#bid_msg_'+itid).html('Your bid was placed')
                                							.show(1)
                                                             .animate({opacity: 1.0}, 1500)
                                                             .hide(1);
                                //alert(itid);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        }
                    }
                    else if(result[0]!="unsuccess")
                    {
                    	$('#bid_msg_'+itid).html('You are already the highest bidder')
                                							.show(1)
                                                             .animate({opacity: 1.0}, 1500)
                                                             .hide(1);
                    }
                });
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });

        return false;
    });

    if ($('.productImageThumb').length) {
        setInterval(function() {
            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;

            if (oldprice!=newprice) {
                getStatusUrl3 = 'updatehistory.php?aucid_new='+auctionhisid;
                
                $.ajax({
                    url: getStatusUrl3,
                    dataType: 'json',
                    success: function(data) {

                        if(data==null) return;
                        //data1 = eval('(' + data.responseText + ')');
                        var fontweight;
                        //alert(data);
                        
                        for (i=0; i<data.histories.length; i++) {
                            biddingprice = data.histories[i].history.bprice;
                            biddingusername = data.histories[i].history.username;
                            biddingtype = data.histories[i].history.bidtype;
							
						
                            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);
                        }

                        //alert(data.myhistories.length);

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

                                biddingprice1 = data.myhistories[j].myhistory.bprice;
                                biddingusername1 = data.myhistories[j].myhistory.time;
                                biddingtype1 = data.myhistories[j].myhistory.bidtype;

                                //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.histories[0].history.bprice);

                        $("#curproductprice").html(data.histories[0].history.bprice);
                        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:'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);
                            //data.data.price - data.data.fprice
                            $("#placebidssavingdisp").html(data.data.saving);
                            $("#placebidssaving").html(data.data.saving);
                            //alert(fprice);
                            $("#newbuynowprice").html(CurrencySymbol + data.data.buynowprice);
                            //$("#acutalprice").html(CurrencySymbol + data.data.fprice);
                        }
                    },
                    error:function (XMLHttpRequest, textStatus, errorThrown) {
                    //alert(textStatus);
                    }
                });

            }
        }, counterUpdateTime);
    }


    if ($('.productUniqueAuction').length) {
        setInterval(function() {
            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 (oldbids!=newbids) {
                //alert('a');
                getStatusUrl3 = 'updatehistory_unique.php?aucid_new='+auctionhisid;

                $.ajax({
                    url: getStatusUrl3,
                    dataType: 'json',
                    success: function(data) {

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

                        for (i=0; i<data.histories.length; i++) {
                            username = data.histories[i].history.username;
                            adddate = data.histories[i].history.adddate;

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

                            $("#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);
                        }

                        //alert(data.myhistories.length);

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

                                username1 = data.myhistories[j].myhistory.username;
                                adddate1= data.myhistories[j].myhistory.adddate;
                                bidprice1= data.myhistories[j].myhistory.bidprice;

                                //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:'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);
                            //alert(data.data.saving);
                           // $("#acutalprice").html(CurrencySymbol + data.data.fprice);
                        }
                    },
                    error:function (XMLHttpRequest, textStatus, errorThrown) {
                    //alert(textStatus);
                    }
                });

            }
        }, counterUpdateTime);
    }
	
    $(".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 palce AutoBidder for more than one bid!");
                return false;
            }

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

            $.ajax({
                url: "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 greater 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("You'll need to buy more bids!");
                                }
                            }
                        } else {
                            $('#bid_form').val('');
                            $('#bid_to').val('');
                            $('#bid_bids').val('');                            
                            $('#butlermessage').show();
                            changeMessageTimer = setInterval("ChangeButlerImageSecond()",5000);
                            changedatabutler(data,"abut",totalbids);
                        }
                    });
                },
                error: function(XMLHttpRequest,textStatus, errorThrown) { }
            });

            return false;
        }
    });
}

function DeleteBidButler(id, div_id) {
    $.ajax({
        url: url = "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;
		var i = 0;
		var k = 0;
		var aucnum = 0;
		var auctionarray =new Array();
		//var tempauction2= '';
        $.each(data, function(i, item) {
            auction_id = item.auc_id;
            auction_time = item.time;
            pausestatus = item.pause;
            aucstatus = item.aucstatus;
			if(timecheck-checktime > 900 && joinvar == 0)
                        {
                        	//window.alert("Are You still here?");
                        	//window.location
                        	joinprompt();
                        	joinvar =1;
                        	//if(timecheck-checktime > 1900)
                        	//{
                        	//	window.location = "http://dev.upbids.com/users/logout";
                        	//}
                        }
                    timecheck = new Date().getTime() / 1000;
                    
                    //$('#auction_' + auction_id).css('display','none');
                    //alert("test");
            if (auction_time) {
                //alert(auction_time);
                if(auction_time==2 && enableTimerDelayer==true){
                    $('#counter_index_page_' + auction_id).css('color', '#E80000');
                    $('#counter_index_page_' + auction_id).html('GOING ONCE');
                }else if(auction_time==1 && enableTimerDelayer==true){
                    $('#counter_index_page_' + auction_id).css('color', '#E80000');
                    $('#counter_index_page_' + auction_id).html('GOING TWICE');
                }else if (auction_time==0 && aucstatus == 3 && str.match("mybuyitnow.php") == null) {
                	auctionarray[i] = auction_id;
                	if(i > 1 && (i-k) >= 2)
                	{
                		$('#auction_' + auctionarray[k]).css('display','none');
                		k = k+1;
                		checktime2 = new Date().getTime() / 1000;
                	}
                	if(timecheck-checktime2 > 600)
                	{
                		$('#auction_' + auction_id).css('display','none');
                	}
                	//if(display)
                	//{
                    $('#counter_index_page_' + auction_id).css('color', '#C00');
                    $('#counter_index_page_' + auction_id).html('&nbsp;');
                    $('#solditem_' + auction_id).css('display','');
                    //opacity:0.4;filter:alpha(opacity=40);
                    $('#picture_' + auction_id).css('opacity','0.4');
                    $('#picture_' + auction_id).css('filter','alpha(opacity=40)');
                    $('#image_main_' + auction_id).attr('onclick', '');
                    $('#image_main_' + auction_id).attr('name','');
                    $('#image_main_' + auction_id).attr('class','');
                    $('#image_main_' + auction_id).html('');
                    $('#blink_img').hide();
                    i = i+1;
                	//}
                //document.getElementById('image_main_' + auction_id).src = "img/buttons/btn-sold_92.png";
                } else if (pausestatus==1) {
                    $('#counter_index_page_' + auction_id).html('Pause');
                    //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', '#002356');
                        $('#counter_index_page_' + auction_id).html(calc_counter_from_time(auction_time));
                    }
                    $('#image_main_' + auction_id).show();

                    if(document.getElementById("blink_img")!=null){
                        if(auction_time<=15){
                            //$('#blink_img').css('display', 'block');
                        }else{
                            //$('#blink_img').css('display', 'none') ;
                        }
                        
                    }
                }
            }
            //k =  k+1;
        });
        
    }
}

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) : "");

}

