var handleSuccess = function(o){
    if(o.responseText !== undefined){
        document.getElementById('title').innerHTML = o.responseText;
    }
};

var handleFailure = function(o){
    if(o.responseText !== undefined){
        selectToChange.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
    }
};

function changeSelectedCategories(inField, outField) { 
    selectCatValue = document.getElementById('type').value;
    selectAuthValue = document.getElementById('author').value;
    /*
    var callback =
    {
        success:handleSuccess,
        failure:handleFailure
    };
    if ((selectAuthValue!='')||(selectCatValue!='')) {
        var sUrl = "postSelect.php";
        var postData = "type="+selectCatValue+"&&author="+selectAuthValue;
        var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
        //var sUrl = "postSelect.php?section="+document.getElementById(from).id+"&&value="+selectValue;
        //var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
    }
    */
    selectTitle = document.getElementById('title').value;
    if ((selectAuthValue!='')||(selectCatValue!='')||(selectTitle!='')) {
        if (inField == "type") {
            selectAuthValue = "";
            selectTitle = "";
        } else if (inField == "author") {
            selectTitle = "";   
        }
        var sUrl = "catalogo.php";
        var postData = "type="+selectCatValue+"&&author="+selectAuthValue+"&&title="+selectTitle;
        document.location=sUrl+'?'+postData;
    }
}

var handleSuccessAddCart = function(o){
    if(o.responseText !== undefined){
        var id = o.argument[0];
        document.getElementById('countCart').innerHTML = o.responseText;
        document.getElementById('shopElement' + id).setAttribute("class", "contenutiSelected");
        document.getElementById('shopImg' + id).setAttribute("src", "inc/css/remove.png");
        document.getElementById('shopA' + id).setAttribute("onclick", "modToCart('rem', " + id + ")");
    }
};

var handleFailureAddCart = function(o){
    if(o.responseText !== undefined){
        document.getElementById('countCart').innerHTML = o.responseText;
    }
};

function addToCart(id) { 
    var callback =
    {
        success:handleSuccessAddCart,
        failure:handleFailureAddCart,
        argument: [id]
    };
    var sUrl = "addInCart.php";
    var postData = "id="+id;
    if (confirm("Vuoi aggiungere il prodotto al tuo carrello?"))
    {
        var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
    };
}

var handleSuccessModCart = function(o){
    if(o.responseText !== undefined){
        var id = o.argument[0];
        document.getElementById('countCart').innerHTML = o.responseText;
        document.getElementById('shopElement' + id).setAttribute("class", "contenuti");
        document.getElementById('shopImg' + id).setAttribute("src", "inc/css/add.png");
        document.getElementById('shopA' + id).setAttribute("onclick", "addToCart(" + id + ")");
    }
};

var handleSuccessModCartIn = function(o){
    if(o.responseText !== undefined){
        document.getElementById('myCart').innerHTML = o.responseText;
    }
};

var handleFailureModCartIn = function(o){
    if(o.responseText !== undefined){
        document.getElementById('myCart').innerHTML = o.responseText;
    }
};

var handleFailureModCart = function(o){
    if(o.responseText !== undefined){
        document.getElementById('countCart').innerHTML = o.responseText;
    }
};

function modToCart(type, id) { 
    var callback =
    {
        success:handleSuccessModCart,
        failure:handleFailureModCart,
        argument: [id]
    };
    var sUrl = "modToCart.php";
    var postData = "type="+type+"&id="+id;
    if (confirm("Vuoi rimuovere il prodotto dal tuo carrello?"))
    {
        var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
    };
}

function modToCartIn(type, id) { 
    var callback =
    {
        success:handleSuccessModCartIn,
        failure:handleFailureModCartIn,
        argument: [id]
    };
    var sUrl = "modToCart.php";
    var postData = "type="+type+"&id="+id+"&ret=Yes";
    var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}

