currentpage = "";
number = 0;

function loaded(page) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req != undefined) {
        req.onreadystatechange = function() {
            if (req.readyState == 4) { // only if req is "loaded"
                if (req.status == 200) { // only if "OK"
                    document.getElementById("ads").innerHTML = req.responseText;              
                    changepage(page);
                }
            }
        };
        req.open("GET", "/eds", true);
        req.send("");
    }
}

function clicked(id) {
    
}

function changepage(page) {
    if ((page != currentpage)||(page=="reports")) {
        if ((page!="")||(page!=null)) {
            load(page+"?num="+number++);
            if (page != currentpage) {
            temp = currentpage;
            currentpage = page;
            if (document.getElementById(page)!=null) {
                document.getElementById(page).style.backgroundColor = "#99CCFF";
            }
            if (document.getElementById(temp)!=null) {
                document.getElementById(temp).style.backgroundColor = "#DDCCFF";
            }
            }
        }
    }
}

function updatebuyorder(id) {
    if (document.getElementById("row"+id).style.visibility == "hidden") {
        document.getElementById("row"+id).innerHTML = "<td colspan=5><label><span>Amount:</span>"+
        "<input id='request"+id+"' type='text' value='0'/></label><label><span>PED Returned:</span>"+
        "<input id='ped"+id+"' type='text' value='0'/></label><a href='javascript:updatebuyord("+id+");'"+
        "class='button'>Add</a> <a href='javascript:endbuyord("+id+");' class='button'>End Order</a></td>";
        document.getElementById("row"+id).style.visibility = "visible";
    } else {
        document.getElementById("row"+id).style.visibility = "hidden";
        document.getElementById("row"+id).innerHTML = "";
    }
}

function updateauction(id) {
    if (document.getElementById("auctionrow"+id).style.visibility == "hidden") {
        document.getElementById("auctionrow"+id).innerHTML = "<td colspan=5><a href='javascript:auctionsold("+id+");'"+
        "class='button'>Sold</a> <a href='javascript:auctionnotsold("+id+");' class='button'>Not Sold</a></td>";
        document.getElementById("auctionrow"+id).style.visibility = "visible";
    } else {
        document.getElementById("auctionrow"+id).innerHTML = "";
        document.getElementById("auctionrow"+id).style.visibility = "hidden";
    }
}

function auctionsold(id) {
    saleprice = parseFloat(document.getElementById("saleprice"+id).innerHTML);
    inp = prompt('Please enter the finishing bid of the auction:',saleprice);
    if (inp != '' && inp != null) {
        salep = parseFloat(inp);
        var params = "ped=" + encodeURIComponent(salep) +
            "&id=" + encodeURIComponent(id);
        download("/main", params);
        document.getElementById("auction"+id).innerHTML = "";
        document.getElementById("auction"+id).style.visibility = "hidden";
        updateauction(id);
        load("current");
    }
}

function auctionnotsold(id) {
    if (confirm("You are about to end this auction without a buyer, is this OK?")) {
        var params = "end=" + encodeURIComponent("True") +
                "&id=" + encodeURIComponent(id);
        download("/main", params)
        document.getElementById("auction"+id).innerHTML = "";
        document.getElementById("auction"+id).style.visibility = "hidden";
        updateauction(id);
        load("current");
    }
}

function changebalance() {
    inp = prompt('Please enter the amount you wish to add or subtract to your balance:','0.00');
    if (inp != '' && inp != null) {
        inp = parseFloat(inp);
        if (inp) {
            var params = "amount=" + encodeURIComponent(inp);
            download("/", params);
            if (inp > 0) {
                alert("You added "+inp+" PED to your balance. Refresh your page to see the changes");
            } else if (inp < 0) {
                inp = inp * -1;
                alert("You subtracted "+inp+" PED from your balance. Refresh your page to see the changes");
            }
        }
    }
}

function updatebuyord(id) {
    amount = document.getElementById("amount"+id).innerHTML;
    matched = document.getElementById("matched"+id).innerHTML;
    avail = amount-matched;
    reqested = document.getElementById("request"+id).value;
    if (reqested>0) {
        if (reqested<avail) {
            var params = "amount=" + encodeURIComponent(reqested)  +
                        "&ped=" + encodeURIComponent( document.getElementById("ped"+id).value )+
                        "&id=" + encodeURIComponent(id);
            updatebuyorder(id);
            download("/current", params);
            load("current");
        } else if (reqested == avail) {
            var params = "amount=" + encodeURIComponent(reqested)  +
                        "&ped=" + encodeURIComponent( document.getElementById("ped"+id).value )+
                        "&id=" + encodeURIComponent(id);
            download("/current", params)
            document.getElementById("results"+id).innerHTML = "";
            document.getElementById("results"+id).style.visibility = "hidden";
            updatebuyorder(id);
            load("current");
        } else {
            alert("You have entered more then there is unmatched on the buy order");
        }
    }
}

function endbuyord(id) {
    if (confirm("Are you sure you want to end this Buy Order? Any values entered will not be counted.")) {
        var params = "end=" + encodeURIComponent("True") +
                "&id=" + encodeURIComponent(id);
        download("/current", params)
        document.getElementById("results"+id).innerHTML = "";
        document.getElementById("results"+id).style.visibility = "hidden";
        updatebuyorder(id);
        load("current");
    }
}

function updateitems() {
    var val = document.getElementById("cat").value
    if (val != "none") {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req != undefined) {
        req.onreadystatechange = function() {
            if (req.readyState == 4) { // only if req is "loaded"
                if (req.status == 200) { // only if "OK"
                    document.getElementById("value").innerHTML = req.responseText;
                    document.getElementById("value").disabled = false;
                }
            }
        };
        req.open("POST", "/iteminfo");
        var contentType = "application/x-www-form-urlencoded";
        req.setRequestHeader("Content-Type", contentType);
        var params = "type=" + encodeURIComponent(val);
        req.send(params);
    }
    } else {
        document.getElementById("item").disabled = true;
    }
}

function load(page) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req != undefined) {
        req.onreadystatechange = function() {
            if (req.readyState == 4) { // only if req is "loaded"
                if (req.status == 200) { // only if "OK"
                    document.getElementById("container").innerHTML = req.responseText;
                }
            }
        };
        req.open("GET", "/"+page, true);
        req.send("");
    }
}

function changemonth(month) {
    load("reports?month="+month+"&num="+number++);
}


function download(url, opt_options) {
  var options = opt_options || {};
  var request;

  if (typeof ActiveXObject != 'undefined') {
    request = new ActiveXObject('Microsoft.XMLHTTP');
  } else if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  } else {
    throw new Error("XMLHttpRequest not supported");
  }
  var requestType = "GET";
  if (options) {
    requestType = "POST";
  }
  request.open(requestType, url);

  if (requestType == "POST") {
    var contentType = "application/x-www-form-urlencoded";
    request.setRequestHeader("Content-Type", contentType);
  }
  request.send(options);

}

function isorder() {
    var checked = document.getElementById("order").checked;
    if (checked) {
        document.getElementById("days").style.visibility = "visible";
        document.getElementById("price").disabled = true;
        document.getElementById("markup").disabled = false;
    } else {
        document.getElementById("days").style.visibility = "hidden";
        document.getElementById("price").disabled = false;
        document.getElementById("markup").disabled = true;
    }
}

function isauction() {
    var checked = document.getElementById("auction").checked;
    if (checked) {
        document.getElementById("fee").style.visibility = "visible";
    } else {
        document.getElementById("fee").style.visibility = "hidden";
    }
    updatebuyMarkup()
}

var currentTTvalue  = 0.0;

function getTT() {
    var currentitem = document.getElementById("item").value;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req != undefined) {
        req.onreadystatechange = function() {
            if (req.readyState == 4) { // only if req is "loaded"
                if (req.status == 200) { // only if "OK"
                    currentTTvalue = req.responseText;
                    document.getElementById("ttval").value = req.responseText;
                    updateTT();
                }
            }
        };
        req.open("POST", "/gettt");
        var contentType = "application/x-www-form-urlencoded";
        req.setRequestHeader("Content-Type", contentType);
        var params = "type=" + encodeURIComponent(currentitem);
        req.send(params);
    }
    currentitem = document.getElementById("item").value;
    if (window.XMLHttpRequest) {
        req1 = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req1 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req1 != undefined) {
        req1.onreadystatechange = function() {
            if (req1.readyState == 4) { // only if req is "loaded"
                if (req1.status == 200) { // only if "OK"
                    costperitem = req1.responseText;
                }
            }
        };
        req1.open("POST", "/getcost");
        var contentType = "application/x-www-form-urlencoded";
        req1.setRequestHeader("Content-Type", contentType);
        var params = "type=" + encodeURIComponent(currentitem);
        req1.send(params);
    }
}
var costperitem = 0;

function setTT() {
    currentTTvalue = document.getElementById("value").value;
    document.getElementById("item").value = document.getElementById("value").options[document.getElementById("value").selectedIndex].text;
    updateTT()
}

function updateTT() {
    var val = document.getElementById("amount").value;
    if (val>0) {
        document.getElementById("ttval").value = currentTTvalue * val;
    } else {
        document.getElementById("ttval").value = currentTTvalue;
    }
}

function updateMarkup() {
    var checked = document.getElementById("order").checked;
    var tt = document.getElementById("ttval").value;
    if (tt>0) {
        if (checked) {
            var markup = document.getElementById("markup").value;
            if (markup>0) {
                document.getElementById("price").value = (markup/100) * tt;
            }
        } else {
            var price = document.getElementById("price").value;
            if (price>0) {
                document.getElementById("markup").value = Math.round((price/tt)*10000)/100;
            }
        }
    }
}

function updatebuyMarkup() {
    var tt = document.getElementById("ttval").value;
    var checked = document.getElementById("auction").checked;
    if (tt>0) {
        var price = document.getElementById("price").value;
        if (price>0) {
            if (checked) {
                var fees = Math.round((((price-tt)*100 +995)/(1990+(price-tt)))*100)/100;
                var amount = document.getElementById("amount").value;
                var item = document.getElementById("item").value;
                document.getElementById("markup").value = Math.round(((price-fees)/tt)*10000)/100;
                document.getElementById("fees").value = fees;
                document.getElementById("finalprice").value = Math.round((price - fees - costperitem*amount)*100)/100;
            } else {
                var amount = document.getElementById("amount").value;
                var item = document.getElementById("item").value;
                document.getElementById("markup").value = Math.round(((price)/tt)*10000)/100;
                document.getElementById("fees").value = 0;
                document.getElementById("finalprice").value = Math.round((price - costperitem*amount)*100)/100;
            }
        }
    }
}

function submit() {
    document.getElementById("item").value = document.getElementById("value").options[document.getElementById("value").selectedIndex].text;
    document.getElementById("price").disabled = false;
    document.getElementById("markup").disabled = false;
    document.getElementById("ttval").disabled = false;
    document.buyform.submit();
}

