function OSP_Shop_RefreshCart()
{
	var url = OSP_PUBLIC_PATH + '/index.php5?plugin=eshop&action=refresh_cart';

	dojo.xhrGet({
		url: url,
		load: function(response){ OSP_Shop_RefreshCart_OK(response); },
		error: function(response){ OSP_Shop_RefreshCart_Error(response); }
	});
}

function OSP_Shop_RefreshCart_OK(response)
{
	if ( byId('eshop_cart').parentNode )
	{
		byId('eshop_cart').parentNode.innerHTML = response;
	}
}

function OSP_Shop_RefreshCart_Error(response)
{
	alert('OSP_Shop_RefreshCart_Error ' + response);
}


function OSP_Shop_GetHTMLAttributeOptions(product_id, product_html_id, attribute_id, child_attribute_id, parent_attribute_id)
{
	var url = OSP_PUBLIC_PATH + '/index.php5?plugin=eshop&action=get_html_attribute_options&product_id=' + product_id + '&product_html_id=' + product_html_id + '&attribute_id=' + attribute_id + '&child_attribute_id=' + child_attribute_id + '&parent_attribute_id=' + parent_attribute_id;

	var src_select = byId(product_html_id + '_attribute_' + attribute_id);
	var dst_select = byId(product_html_id + '_attribute_' + child_attribute_id);

	if ( src_select && src_select.selectedIndex >= 0 )
	{
		OSP_SetFormDisabled(byId(product_html_id + '_form'), true);

		eval('var attribute_relation_data = ' + unescape(src_select.options[src_select.selectedIndex].value));
		url += '&parent_relation_id=' + parseInt(attribute_relation_data['id']);

		// Clear previous options
		while ( dst_select.options.length )
		{
			dst_select.remove(0);
		}

		dojo.xhrGet({
			url: url,
			load: function(response){ OSP_Shop_GetHTMLAttributeOptions_OK(response); },
			error: function(response){ OSP_Shop_GetHTMLAttributeOptions_Error(response); }
		});
	}
}

function OSP_Shop_GetHTMLAttributeOptions_OK(response)
{
	eval('var data = ' + response);

	var src_select = byId(data['product_html_id'] + '_attribute_' + data['attribute_id']);
	var dst_select = byId(data['product_html_id'] + '_attribute_' + data['child_attribute_id']);

	// Add new options
	for ( var i = 0; i < data['option_arr'].length; i++ )
	{
		if ( BrowserDetect.browser == 'Explorer' )
		{
			dst_select.options[i] = new Option(i, i);
			dst_select.options[i].value = data['option_arr'][i]['value'];
			dst_select.options[i].innerHTML = data['option_arr'][i]['caption'];
		}
		else
		{
			var option = document.createElement('option');
			option.value = data['option_arr'][i]['value'];
			option.innerHTML = data['option_arr'][i]['caption'];
			dst_select.options.add(option, null);
		}
	}

	// Update price
	dst_select.onchange();

	OSP_SetFormDisabled(byId(data['product_html_id'] + '_form'), false);
}

function OSP_Shop_GetHTMLAttributeOptions_Error(response)
{
	alert('OSP_Shop_GetHTMLAttributeOptions_Error ' + response);
}


function OSP_Shop_UpdateProductPrice(price_value_html_id, price_symbol, price_precision, product_html_id, attribute_arr)
{
	var form = byId(product_html_id + '_form');

	// Base price is defined
	if ( byId(product_html_id + '_base_price_value') )
	{
		var price = parseFloat(byId(product_html_id + '_base_price_value').value);
	}
	// Base price is NOT defined
	else
	{
		var price = parseFloat(byId(price_value_html_id).innerHTML);
		var hidden_field = document.createElement('input');
		hidden_field.type = 'hidden';
		hidden_field.name = hidden_field.id = product_html_id + '_base_price_value';
		hidden_field.value = price;
        form.appendChild(hidden_field);
	}

	for ( var i = 0; i < attribute_arr.length; i++ )
	{
		var input = byId(product_html_id + '_attribute_' + attribute_arr[i]);

		if ( input )
		{
			// Select list
			if ( input.length )
			{
				eval('var attribute_relation_data = ' + unescape(input.options[input.selectedIndex].value));
			}
			// Radio button
			else
			{
				var elements = form[input.name];
				// Get checked element
				for ( var j = 0; j < elements.length; j++ )
				{
					if ( elements[j].checked )
					{
						eval('var attribute_relation_data = ' + unescape(elements[j].value));
					}
				}
			}

			price += parseFloat(attribute_relation_data['price']);
		}
	}

	byId(price_value_html_id).innerHTML = sprintf('%.' + price_precision + 'f', price) + ' ' + price_symbol;
}


function OSP_Shop_SubmitAndGoTo(form_id, action_textid)
{
	// Disable form buttons
	for ( var i = 0; i < byId(form_id).elements.length; i++ )
	{
		if ( byId(form_id).elements[i].tagName == 'INPUT' && (byId(form_id).elements[i].type == 'submit' || byId(form_id).elements[i].type == 'button') )
		{
			byId(form_id).elements[i].disabled = true;
		}
	}

	byId('osp_goto_action').value = action_textid;
	byId(form_id).submit();
}


function OSP_Shop_SwitchImage(container_id, url)
{
	var container = byId(container_id);
	if ( !container.osp_cache )
	{
		container.osp_cache = new Object();
	}

	url += '&container_id=' + container_id;

	// Cache exists
	var content = container.osp_cache[escape(url)];
	if ( content )
	{
		container.innerHTML = content;
	}
	// NO cache exists
	else
	{
		dojo.xhrGet({
			url: url,
			load: function(response){ OSP_Shop_SwitchImage_OK(response); },
			error: function(response){ OSP_Shop_SwitchImage_Error(response); }
		});
	}
}

function OSP_Shop_SwitchImage_OK(response)
{
	eval('var data = ' + response);

	var container = byId(data['container_id']);

	container.innerHTML = data['content'];

	// Cache the content
	container.osp_cache[escape(data['url'])] = data['content'];
}

function OSP_Shop_SwitchImage_Error(response)
{
	alert('OSP_Shop_SwitchImage_Error ' + response);
}
