var delay;
var inList = false;

function squeeze(){
    clearTimeout(delay);
}

function getProductsList(){
    var pattern = document.getElementById("pattern").value;
    //var category = document.getElementById("id_category").value;
    
    if( pattern != "" ){
        document.getElementById("product_list_auto_comp").innerHTML = ajax("ajax/getProductsList.php", "category=&pattern="+pattern);
        document.getElementById("product_list_auto_comp").style.display = "block";
    } else hideProductList();
}

function hideProductList(){
    if( !inList ){
        document.getElementById("product_list_auto_comp").style.display = "none";
        document.getElementById("product_list_auto_comp").innerHTML = "";
    }
}

function updateBasketPrice(target, amount, num){
    switch( target.value ){
        case "6.5": case 6.5:
            updateAmount(Math.round((amount + 6.5) * 100) / 100);
            document.getElementById("cart_amount").value = Math.round((amount + 6.5) * 100) / 100;
            document.getElementById("item_number_"+num).value = "DHL Ground";
            document.getElementById("amount_"+num).value = 6.5;
            updateTax();
            break;
        case "12": case 12:
            updateAmount(Math.round((amount + 12) * 100) / 100);
            document.getElementById("span_price").innerHTML = Math.round((amount + 12) * 100) / 100;
            document.getElementById("cart_amount").value = Math.round((amount + 12) * 100) / 100;
            document.getElementById("item_number_"+num).value = "DHL 2nd Day";
            document.getElementById("amount_"+num).value = 12;
            updateTax();
            break;
        case "22.5": case 22.5:
            updateAmount(Math.round((amount + 22.5) * 100) / 100);
            document.getElementById("span_price").innerHTML = Math.round((amount + 22.5) * 100) / 100;
            document.getElementById("cart_amount").value = Math.round((amount + 22.5) * 100) / 100;
            document.getElementById("item_number_"+num).value = "DHL Xpress Overnight";
            document.getElementById("amount_"+num).value = 22.5;
            updateTax();
            break;
    }
}
function updateAmount(amount){
    var round = "";
    var str_amount = "" + amount;
    var tab_price = str_amount.split(".");
    if( tab_price.length == 2 ){
        if( tab_price[1].length == 1 ) round = "0";
    } else round = ".00";
    document.getElementById("span_price").innerHTML = amount + "" + round;
}
function updateTax(){
    if( document.getElementById("pennsylvania_tax") != null ){
        var tax = document.getElementById("cart_amount").value * 0.06;
        document.getElementById("pennsylvania_tax").value = Math.round(tax * 100) / 100;
        var amount = (1.0* document.getElementById("cart_amount").value) + (1.0*tax);
        updateAmount(amount);
    }
}
function setTax(target, num){
    if( target.checked ){
        document.getElementById("pennsylvania_tax_div").innerHTML = ajax("ajax/getPennsylvaniaTax.php", "num="+num);
        updateTax();
    } else {
        document.getElementById("pennsylvania_tax_div").innerHTML = "";
        updateAmount(document.getElementById("cart_amount").value);
    }
}