//
// Store Functions
//

function getKeyCode(event)
{
	var keyCode;
	if (window.event) keyCode = event.keyCode; else keyCode = event.which;
	return keyCode;
}

function getKeyPressed(event)
{
	var keyCode = getKeyCode(event);
	return String.fromCharCode(keyCode);
}

function getImageSelector(fieldId, value)
{
	var containerId = 'custom_field_container_' + fieldId;
	var container = document.getElementById(containerId);

	container.innerHTML = 'test';
}

function updateAttributeSet(attributeSetId)
{
	var containerId = 'attribute_set_container_' + attributeSetId;
	var container = document.getElementById(containerId);

	if (container.style.display == 'none')
	{
		container.style.display = 'block';
	}
	else
	{
		container.style.display = 'none';
	}
}

function updateProductImage(newUrl)
{
	var imageElement = document.getElementById('productImage');
	imageElement.src = newUrl;
	return true;
}

function showProductImages(url)
{
	var newWin = window.open(url, null, 'width=520,height=600,toolbar=0,statusbar=0,scrolling=yes');
}

function showAttributeUrl(url)
{
	var newWin = window.open(url, null, 'width=580,height=400,menubar=0,statusbar=0,scrollbars=1');
}

//////////////////////////////////////
// AJAX for AttributePanel in Admin //

function addAttributeSet(index,product)
{
    var container = document.getElementById('attribute-set-container');
    var attribute_set = document.getElementById('new-attribute-set').options[index].value;
    
    if (attribute_set == '')
    {
        return false;
    }
    else
    {
        //alert('Attribute Set: ' + attribute_set);
        Ext.Ajax.request({
            url     : 'attribute_set.render.php?action=add&attribute_set='+attribute_set+'&product='+product,
            method  : 'get',
            callback: function (opts,success,response) {
                container.innerHTML += response.responseText;
            }
        });
        return true;
    }
}

function removeAttributeSet(product_attribute_set)
{
    var container = document.getElementById('attribute-set-container');
    
    if (product_attribute_set == '')
    {
        return false;
    }
    else
    {
        if (confirm('Do you really want to remove this \nattribute set from this product?'))
        {
            //alert('Attribute Set: ' + attribute_set);
            Ext.Ajax.request({
                url     : 'attribute_set.render.php?action=remove&product_attribute_set='+product_attribute_set,
                method  : 'get',
                callback: function (opts,success,response) {
                    container.innerHTML = response.responseText;
                }
            });
            return true;
        }
        else
        {
            return false;
        }
    }
}