function getCartUrl (url, id) {
    var qty = document.getElementById('qty_' + id).value;
    return window.location = url + "qty/" + qty;
}

function exibeCampoComentario (id, element) {
    document.getElementById('container_' + id).style.display = "block";
    document.getElementById('additional_data_' + id).focus();
    element.parentNode.style.display = "none";
}

function submitAddCart (produto, valor) {
    var tecla = '';
    if (window.event) {
	tecla = valor.keyCode;
    } else if (valor.which) {
	tecla = valor.which;
    }
    if (tecla == 13) {
	$('btnAddCart_'+produto).click();
	return false;
    }
    return true;
}

function updateButton (produto, acao) {
    if (isNaN(acao))
	{
		acao = 1;
    }
	if (acao == 2)
	{
		jQuery('#btnAddCart_' + produto).removeClass('ajax-added bt-adicionado');
		jQuery('#btnAddCart_' + produto).addClass('bt-comprar');
		
		if (jQuery('#btnAddCart_' + produto).html())
		{
			jQuery('#btnAddCart_' + produto).html('Comprar');
		}
	}
	else
	{
		jQuery('#btnAddCart_' + produto).addClass('ajax-added bt-adicionado');
		
		if (jQuery('#btnAddCart_' + produto).html())
		{
			jQuery('#btnAddCart_' + produto).html('Adicionado');
		}
	}
    return true;
}

var notifyCartTimeout;

function notifyCart (mensagem, time) {
    if (time == undefined)
    {
        time = 3000;
    }
    else
    {
        time = time * 1000;
    }
    var p = new Element('p');
    p.update(mensagem);
    var div = new Element('div');
    div.id = 'notifyCart';
    div.insert(p);
    $(document.body).insert(div);
	clearTimeout(notifyCartTimeout);
	notifyCartTimeout = setTimeout('deleteNotifyCart()', time);
}

function deleteNotifyCart ()
{
    clearTimeout(notifyCartTimeout);
    try
    {
        jQuery('#notifyCart').remove();
    } catch (e) {
        return;
    }
}

var __COMMENTS = {};

/*
jQuery('div.add-comment input[name=additional_data]').focus(function(event){
	var id = jQuery(this).attr('id').replace(/([^0-9]*)/g, '');
	var value = jQuery(this).val();
	__COMMENTS[id] = value;
	jQuery('#container_' + id + ' span.comment-saved img').css('display', 'none');
});

jQuery('div.add-comment input[name=additional_data]').blur(function(event){
	var id = jQuery(this).attr('id').replace(/([^0-9]*)/g, '');
	var value = jQuery(this).val();
	var matched = jQuery('#btnAddCart_' + id).hasClass('ajax-added');

	if (matched && __COMMENTS[id] != value)
	{
		jQuery.post(CART_AJAX_UPDATE_COMMENT_URL, {'additional_data': value, 'id': id}, function(data) {
			if (data.success)
			{
				jQuery('#container_' + id + ' span.comment-saved img').css('display', 'block');
				return;
			}
			notifyCart('Não foi possível atualizar o comentário. Por favor, tente novamente', 5);
		}, 'json');
	}
});
*/

jQuery('div.add-comment input[name=additional_data]').keydown(function(event){
	if (event.keyCode == 13)
	{
		event.preventDefault();
		return false;
	}
});

