// JavaScript Document

// part of functions.js
var GlobalVar = 0;
var GlobalStat = 0;

function changeimage(id) {
    var idnew = 1;
    for (idnew=1; idnew<=4; idnew++) {
        if ( idnew==id ) {
            document.getElementById('mainimage'+idnew).style.display='block';
        } else {
            document.getElementById('mainimage'+idnew).style.display='none';
        }
    }
}

function setLogin(id) {
    document.getElementById(id).src='images/bid_click_login_hover.jpg';
}

function setBid(id) {
    document.getElementById(id).src='images/bid_click_white_bid.jpg';
}

function changeImage(image) {
    document.getElementById(image).src='images/bid_click_bid_hover.jpg';
}

function changeImageout(image) {
    document.getElementById(image).src='images/bid_click_white_bid.jpg';
}
//end of part functions.js

function changeData(responseText, stat_us) {
    var text = responseText;
    if ( text!="" ) {
        counter = text.split('#');
        for (i=0; i<counter.length; i++) {
            counter_data1 = counter[i].split(':');
            auction_id = counter_data1[0];
            auction_price = counter_data1[1];
            auction_bidder_name = counter_data1[2];
            if ( auction_bidder_name=="" ) {
                auction_bidder_name="---"
            }
            if (document.getElementById('price_index_page_' + auction_id).innerHTML != CurrencySymbol + auction_price) {
                if ( GlobalVar == 1 ) {
                    document.getElementById('price_index_page_' + auction_id).style.backgroundColor = "#f79909";
                }
                document.getElementById('price_index_page_' + auction_id).innerHTML = CurrencySymbol + auction_price;
                document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;
            } else {
                document.getElementById('price_index_page_' + auction_id).style.backgroundColor = "white";
            }
        }
    }
    GlobalVar = 1;
}

function hidedisplayzoom(div_id) {
    document.getElementById(div_id).style.display = 'block';
    if ( document.getElementById('zoomimagename').innerHTML!="" && document.getElementById('zoomimagename').innerHTML!=div_id ) {
        document.getElementById(document.getElementById('zoomimagename').innerHTML).style.display	= 'none';
    }
    document.getElementById('zoomimagename').innerHTML = div_id;
}

function closezoomimage(div_id) {
    document.getElementById(div_id).style.display='none';
}

function calc_counter_from_time(diff) {
    if (diff > 0) {
        hours=Math.floor(diff / 3600)
        minutes=Math.floor((diff / 3600 - hours) * 60)
        seconds=Math.round((((diff / 3600 - hours) * 60) - minutes) * 60)
    } else {
        hours = 0;
        minutes = 0;
        seconds = 0;
    }

    if (seconds == 60) {
        seconds = 0;
    }

    if (minutes < 10) {
        if (minutes < 0) {
            minutes = 0;
        }
        minutes = '0' + minutes;
    }

    if (seconds < 10) {
        if (seconds < 0) {
            seconds = 0;
        }
        seconds = '0' + seconds;
    }

    if (hours < 10) {
        if (hours < 0) {
            hours = 0;
        }
        hours = '0' + hours;
    }
    return hours + ":" + minutes + ":" + seconds;
}

var xmlhttp = false;
//Check if we are using IE.
try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
    try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
        xmlhttp = false;
    }
}

//If we are using a non-IE browser, create a JavaScript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    xmlhttp = new XMLHttpRequest();
}

function CheckSaveProperty(auction_ids) {
    xmlhttp.open("GET", "update_time.php?aids=" + auction_ids);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            mainarray = xmlhttp.responseText.split('/');
            counters = mainarray[0].split('#');

            for (i=0; i<counters.length; i++) {
                counter_data = counters[i].split(':');
                auction_id = counter_data[0];
                pausestatus = counter_data[2];

                if (counter_data[1]=='0') {
                    document.getElementById('counter_index_page_' + auction_id).innerHTML = 'Ended';
                    document.getElementById('image_main_' + auction_id).src = "images/but_sold2.jpg";
                    document.getElementById('image_main_' + auction_id).onclick="";
                    document.getElementById('image_main_' + auction_id).onmouseover="";
                    document.getElementById('image_main_' + auction_id).onmouseout="";
                } else if (pausestatus==1) {
                    document.getElementById('counter_index_page_' + auction_id).innerHTML = 'Pause';
                    document.getElementById('image_main_' + auction_id).src = "images/bid_click_white_bid_hover.jpg";
                    document.getElementById('image_main_' + auction_id).onclick="";
                    document.getElementById('image_main_' + auction_id).onmouseover="";
                    document.getElementById('image_main_' + auction_id).onmouseout="";
                } else {
                    auction_time = counter_data[1];

                    if (auction_time<10) {
                        document.getElementById('counter_index_page_' + auction_id).style.color = '#E80000';
                        document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
                    } else {
                        document.getElementById('counter_index_page_' + auction_id).style.color = '#6e6d6d';
                        document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
                    }
                }
            }
        }
    }
    xmlhttp.send(null);
}

function CheckSaveProperty1(auction_ids, auc_his_id, butlerbuttonid) {
    var ServerPage = "update_time.php?aids=" + auction_ids + "&auc_his_id=" + auc_his_id;

    xmlhttp.open("GET",ServerPage);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            mainarray = xmlhttp.responseText.split('/');
            counters = mainarray[0].split('#');

            for (i=0; i<counters.length; i++) {
                counter_data = counters[i].split(':');
                auction_id = counter_data[0];
                pausestatus = counter_data[2];

                if (counter_data[1]=='0') {
                    document.getElementById('counter_index_page_' + auction_id).innerHTML = 'Ended';
                    document.getElementById('image_main_' + auction_id).src = "images/but_sold2.jpg";
                    document.getElementById('image_main_' + auction_id).onclick="";
                    document.getElementById('image_main_' + auction_id).onmouseover="";
                    document.getElementById('image_main_' + auction_id).onmouseout="";
                    if (butlerbuttonid==auction_id) {
                        document.getElementById('bookbidbutlerbutton').disabled = true;
                    }
                } else if (pausestatus==1) {
                    document.getElementById('counter_index_page_' + auction_id).innerHTML = 'Pause';
                    document.getElementById('image_main_' + auction_id).src = "images/bid_click_white_bid_hover.jpg";
                    document.getElementById('image_main_' + auction_id).onclick="";
                    document.getElementById('image_main_' + auction_id).onmouseover="";
                    document.getElementById('image_main_' + auction_id).onmouseout="";
                } else {
                    auction_time = counter_data[1];

                    if (auction_time<10) {
                        document.getElementById('counter_index_page_' + auction_id).style.color = '#E80000';
                        document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
                    } else {
                        document.getElementById('counter_index_page_' + auction_id).style.color = '#6e6d6d';
                        document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
                    }
                }
            }
            CheckProHistory(mainarray[1]);
        }
    }
    xmlhttp.send(null);
}

function setbidding(prid, aid, uid) {
    var url="getbid.php?prid="+prid+"&aid="+aid+"&uid="+uid;

    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            if (transport.status==200) {
                var temp=transport.responseText;
                if (temp=="unsuccess") {
                    if (confirm("Please recharge your bid account")) {
                        window.location.href='buybids.php';
                    }
                }

                if (temp=="success") {
                    obj = document.getElementById('bids_count');
                    objvalue = document.getElementById('bids_count').innerHTML;
                    if (obj.innerHTML!='0') {
                        obj.innerHTML = Number(objvalue) - 1;
                    }
                }
            }
        }
    });
}

function CheckProHistory(aucid_new) {
    var history__data = "";
    var history____data;
    history__data = aucid_new;

    if (history__data!="" && history__data!="|") {
        history____data = history__data.split('|');
        counter_history = history____data[0].split('#');
        counter_history_mybid = history____data[1].split('#');

        if (counter_history!="") {
            for (i=0; i<counter_history.length; i++) {
                history_data = counter_history[i].split(':');
                bidding_price = history_data[0];
                bidding_username = history_data[1];

                if (bidding_username=="" && bidding_price!="") {
                    bidding_username="user removed";
                }

                bidding_type = history_data[2];
                document.getElementById('bid_price_' + i).innerHTML = CurrencySymbol + bidding_price;
                document.getElementById('bid_user_name_' + i).innerHTML = bidding_username;

                if (bidding_type=='s') {
                    document.getElementById('bid_type_' + i).innerHTML = "Single Bid";
                } else if(bidding_type=='b') {
                    document.getElementById('bid_type_' + i).innerHTML = "AutoBidder";
                }
            }
        }
        if (counter_history_mybid!="") {
            for (i=0; i<counter_history_mybid.length; i++) {
                history_data1 = counter_history_mybid[i].split('!');
                bidding_price1 = history_data1[0];
                bidding_time1 = history_data1[1];
                bidding_type1 = history_data1[2];
                document.getElementById('my_bid_price_' + i).innerHTML = CurrencySymbol + bidding_price1;
                document.getElementById('my_bid_time_' + i).innerHTML = bidding_time1;

                if (bidding_type1=='s') {
                    document.getElementById('my_bid_type_' + i).innerHTML = "Single Bid";
                } else if(bidding_type1=='b') {
                    document.getElementById('my_bid_type_' + i).innerHTML = "AutoBidder";
                }
            }
        }
    }
}

function PressImageDownBid(id) {
    document.getElementById(id).src = 'images/bid_click_press.jpg';
}

function PressImageUpBid(id) {
    document.getElementById(id).src = 'images/bid_click_bid_hover.jpg';
}

function PressImageDownLogin(id) {
    document.getElementById(id).src = 'images/login_click_press.jpg';
}

function PressImageUpLogin(id) {
    document.getElementById(id).src = 'images/bid_click_login_hover.jpg';
}
var changeMessageTimer;
function changedatabutler(data, page, butlerpbids) {
    
    var blockst = 0;
    //alert(data.butlerslength.length);
    data1 = eval('(' + data.responseText + ')');
    for (j=0; j<data.butlerslength.length; j++) {
        if (data.butlerslength[j].bidbutler.startprice!="") {
            if (Number(j)<Number(data.butlerslength.length)) {
                butlerstartprice =  CurrencySymbol +  data.butlerslength[j].bidbutler.startprice;
                butlerendprice =  CurrencySymbol +  data.butlerslength[j].bidbutler.endprice;
                butlerbid = data.butlerslength[j].bidbutler.bids;
                but_id = data.butlerslength[j].bidbutler.id;
                blockst = 1;
            } else {
                butlerstartprice = "&nbsp;";
                butlerendprice = "&nbsp;";
                butlerbid ="&nbsp;";
                but_id = "";
                blockst = 0;
            }
        } else {
            butlerstartprice = "&nbsp;";
            butlerendprice = "&nbsp;";
            butlerbid ="&nbsp;";
            but_id = "";
            blockst = 0;
        }

        var k = j+1;

        $('#mainbutlerbody_' + k).css('display', 'table-row');
        $('#butlerstartprice_' + k).html(butlerstartprice);
        $('#butlerendprice_' + k).html(butlerendprice);
        $('#butlerbids_' + k).html(butlerbid);

        if (blockst==1) {
            $('#deletebidbutler_' + k).css('display', 'table-cell');
            $('#deletebidbutler_' + k).html("<img src='images/btn_closezoom.png' style='cursor: pointer;' onclick='DeleteBidButler(\""+but_id+"\",\"" + k + "\");' id='butler_image_" + k +"' />");


        } else {
            $('#deletebidbutler_' + k).css('display', 'none');
        }
    }


    for (p=data.butlerslength.length+1; p<=20; p++) {
        $('#mainbutlerbody_' + p).css('display','none');
    }

    if(data.butlerslength.length > 0){
        $('#live_no_bidbutler').css('display','none');
    }else{
        $('#live_no_bidbutler').css('display','block');
    }

    changeMessageTimer = setInterval("ChangeButlerImageSecond()",3000);
	
    if (page=="abut") {
        if (butlerpbids!="&nbsp;") {
            if (document.getElementById("useonlyfree").innerHTML == "1") {
                objbids = document.getElementById('free_bids_count');
                if(objbids!=null){
                    objbidsvalue = document.getElementById('free_bids_count').innerHTML;
                }
            } else {
                objbids = document.getElementById('bids_count');
                if(objbids!=null){
                    objbidsvalue = document.getElementById('bids_count').innerHTML;
                }
            }
	
            if (objbids!=null && objbids.innerHTML!='0') {
                objbids.innerHTML = Number(objbidsvalue) - Number(butlerpbids);
            }
        }
    }
    
}

function ChangeButlerImageSecond() {
    if(document.getElementById('butlermessage')!=null){
        //document.getElementById('butlermessage').style.display='none';
    }
    if(changeMessageTimer){
        clearInterval(changeMessageTimer);
    }
}

function calc_counter_from_time_new(TimerID) {
    var tmp=TimerID;
    var days=Math.floor(tmp/(24*60*60));
    tmp=tmp-(24*60*60)*days;
    var hours=Math.floor(tmp/(60*60));
    tmp=tmp-(60*60)*hours;
    var minutes=Math.floor(tmp/(60));
    tmp=tmp-(60)*minutes;
    var secs=Math.floor(tmp);
	
    if (hours<=9 && hours>0) {
        hours = "0" + hours;
    }
    if (minutes<=9 && minutes>0) {
        minutes = "0" + minutes;
    }
    if (secs<=9) {
        secs = "0" + secs;
    }
    if (days>0) {
        return days + "d " + hours + "h " + minutes + "m " + secs + "s";
    } else {
        return hours + "h " + minutes + "m " + secs + "s";
    }
}

