function updateQuantity(products_id, quantity, setId)
{
    if(!setId) {
        setId = 0;
    }
    $('cartLoadingImg').style.display = 'block';
    new Ajax.Request
	(
		'/cart/update/id/' + products_id + '/quantity/' + quantity + '/setId/' + setId + '/ajax/true',
		{
			method: 'get',
			onSuccess: function(transport) { updateQuantitySuccess(transport, products_id, setId) },
			onLoading: cartLoading
		}
	);
}

function updateQuantitySuccess(transport, products_id, setId)
{
    eval('Result = ' + transport.responseText);
    
    if(Result['success'] == 1) {
        if(setId) {
            $('quantitySet_' + setId).value = Result['quantity'];
        } else {
            $('quantity_' + products_id).value = Result['quantity'];   
        }
        updateTotal();
	    Effect.Fade('cartLoadingImg');
    } else {
        displayErrorDiv('Nu exista cantitatea dorita in stocul nostru!');
        if(setId) {
            $('quantitySet_' + setId).value = Result['quantity'];
        } else {
            $('quantity_' + products_id).value = Result['quantity'];   
        }
        Effect.Fade('cartLoadingImg');
    }
}


function deleteProduct(products_id, setId)
{
    if(!setId) {
       setId = 0;
    }
    $('cartLoadingImg').style.display = 'block';
    new Ajax.Request
    (
    	'/cart/delete/id/' + products_id + '/setId/' + setId + '/ajax/true',
    	{
    		method: 'get',
    		onSuccess: function(transport) { deleteProductSuccess(transport, products_id, setId) },
    		onLoading: cartLoading
    	}
    );
}

function deleteProductSuccess(transport, products_id, setId)
{
    eval('Result = ' + transport.responseText);
    
    if(Result['success'] == 1) {
        updateTotal();
        if(setId) {
            Effect.BlindUp('cartTableSet_' + setId)
        } else {
            Effect.BlindUp('cartTableProduct_' + products_id)   
        }
	    Effect.Fade('cartLoadingImg');
    } else {
        displayErrorDiv('A fost o eroare in prelucrarea datelor, va rugam incercati din nou');
    }
}

function updateTotal()
{
    new Ajax.Request
	(
		'/cart/get-total/ajax/true',
		{
			method: 'get',
			onSuccess: function(transport) { updateTotalSuccess(transport) }
		}
	);
}

function updatePriceModificationsInfo()
{
    new Ajax.Request
	(
		'/cart/price-modifications/ajax/true',
		{
			method: 'get',
			onSuccess: function(transport) { updatePriceModificationsInfoSuccess(transport) }
		}
	);
}

function updateTotalSuccess(transport)
{
    eval('Result = ' + transport.responseText);
    $('totalCartPrice').innerHTML = Result['total'] + ' LEI';
    
    updatePriceModificationsInfo();
}

function updatePriceModificationsInfoSuccess(transport)
{
     $('priceModificationsDiv').innerHTML = transport.responseText;
}

function cartLoading(transport)
{
    $('cartLoadingImg').style.display = 'block';
}

function preCheckout()
{
    new Ajax.Request
	(
		'/cart/has-products/ajax/true',
		{
			method: 'get',
			onSuccess: preCheckoutResult,
			onLoading: fullScreenLoading
		}
	); 
}

function preCheckoutResult(transport)
{  
   eval('Result = ' + transport.responseText);
   if(Result['has_products'] == true) {
       preCheckoutLogin();
   } else {
       endFullScreenLoading();
	   displayErrorDiv('Nu aveti nici un produs in cos');
   }
}

function preCheckoutLogin()
{
    new Ajax.Request
	(
		'/login/is-logged',
		{
			method: 'get',
			onSuccess: preCheckoutLoginResult,
			onLoading: fullScreenLoading
		}
	); 
}

function preCheckoutLoginResult(transport)
{  
   eval('Result = ' + transport.responseText);
   
   if(Result['success'] == 1) {
       window.location = "/checkout"
   } else {
       fullScreenLogin('checkout');
   }
}
