var SEPARATOR_VALUES_CART = "*";
var COOKIE_NAME_SHOPPINGCART = "gwamsc";

function addToCart(productId,quantity,toGift,value) {
    if(!existInCookie(productId))
        gwaCookieManager.addCookie(COOKIE_NAME_SHOPPINGCART, formatStringCookie(productId,quantity,toGift,value));
}

function existInCookie(value)
{
    var stringCookie = gwaCookieManager.readCookie(COOKIE_NAME_SHOPPINGCART);
    if(stringCookie != null && stringCookie != "") {
        var cookieItems = stringCookie.split(gwaCookieManager.getSeparatorItens());
        for(var x = 0; x < cookieItems.length; x++)
        {
            var item = cookieItems[x];
            var cookieValues = item.split(SEPARATOR_VALUES_CART);
            if(cookieValues[0] != null && cookieValues[0]==value)
            {
                return true;
            }

        }
    }

    return false;
}

function readCookie() {
	var read = gwaCookieManager.readCookie(COOKIE_NAME_SHOPPINGCART);
	if (read == null)
		alert("Cookie Inexistente = "+read);
	else
		alert("Cookie Atual ["+ read +"]");		
}

function deleteCookie() {
	gwaCookieManager.deleteCookie(COOKIE_NAME_SHOPPINGCART);
}

function removeItensFromCart(products) {
    var i = 0;
    for(i; i<products.length; i++) {
        removeItemCart(products[i]);
    }
}

function removeItemCart(productId) {
	gwaCookieManager.removeItemCookie(COOKIE_NAME_SHOPPINGCART, productId, 0, SEPARATOR_VALUES_CART);
}

function updateItemCart(productId,quantity,toGift,value) {
    if(quantity==0) {
        removeItemCart(productId);
    } else {
        gwaCookieManager.updateItemCookie(COOKIE_NAME_SHOPPINGCART, productId, 0, SEPARATOR_VALUES_CART,formatStringCookie(productId,quantity,toGift,value));
    }
}

function formatStringCookie(productId,quantity,toGift,value) {
	//0=productId, 1=quantity, 2=toGift, 3=value
	if(toGift == false){
		quantity = 1;
	}
	return productId+SEPARATOR_VALUES_CART+quantity+SEPARATOR_VALUES_CART+toGift+SEPARATOR_VALUES_CART+value;
}

function isSupportedCookie() {
	return gwaCookieManager.isSupported();
}

function refreshItems(products) {
    for(i=0; i<products.length; i++) {
        var productId = products[i];
        var quantity = document.getElementById('rootForm:shoppingCartList:'+i+':Quantity').value;
        var toGift = document.getElementById('rootForm:shoppingCartList:'+i+':Gift').checked;
        var value = document.getElementById('rootForm:shoppingCartList:'+i+':Value').value;
        var typeProduct = document.getElementById('rootForm:shoppingCartList:'+i+':TypeProduct').value;


        //Teste abaixo, quando tu colocava um produto em matricula em curso em pomoÃ§Ã£o
        //tu pode modificar a quantidade, so que se desmarcasse a opÃ§Ã£o para presente
        //estava ficando o a quantidade que tinha sido digitada e isso nÃ£o pode, pois
        //um curso so poder ser comprado um por pessoa fÃ­sica.
        if(typeProduct != null && typeProduct == 1 && !toGift)
        {
            quantity = 1;
        }

        updateItemCart(productId, quantity, toGift, value);
    }
}

function refreshQuantityProductGift(products) {
    for(i=0; i<products.length; i++) {
        var productId = products[i];
        var quantity = document.getElementById('rootForm:shoppingCartList:'+i+':Quantity').value;
        var toGift = document.getElementById('rootForm:shoppingCartList:'+i+':Gift').checked;
        var value = document.getElementById('rootForm:shoppingCartList:'+i+':Value').value;
          
        updateItemCart(productId, quantity, toGift, value);
    }
}


