/**
* this function adds product to the cart
* @param ProductID string identifier of the product
*/
function add_to_cart(ProductID) 
{
	if (!locked)
	{
		$.blockUI();
		locked = true;
		$.ajax({
			url:site_path + 'products/add_basket.php',
			type:'post', 
			data:$('form').serialize()+'&Product='+ProductID, 
			dataType:'json',
			success:onSuccessUpdateList
		});    	
	}
}
/**
 * Parse result for ajax request to add_basket.php
 * JSON data format:
 * 1) result (string) = ok | error
 * 2) errors (array of string) - if result == error
 * 3) users (array) - - if result == ok :
 */
function onSuccessUpdateList(data)
{
	$.unblockUI();
	locked = false;

	$('#note').html('');
	
	if (data.result == 'ok')
	{
		//var order = $('#order_list');
		//order.remove('tr');

		var list = '';
		var records = data.records;
		for (i in records) {
			if ( (records[i].Product == undefined)
				 || (records[i].Size == undefined)
				 || (records[i].ColourLabel == undefined)
				 || (records[i].Quantity == undefined) ) {
					 continue;
			}
			list +=
			  '<tr> ' +
			     '<td>&nbsp;' + records[i].Product + '&nbsp;</td> ' +
			     '<td>&nbsp;' + records[i].Size + '&nbsp;</td> ' +
			     '<td>&nbsp;' + records[i].ColourLabel + '&nbsp;</td> ' +
			     '<td>&nbsp;' + records[i].Quantity + '&nbsp;</td> ' +
			  '</tr>';			
		}
		
		$('#order_list').html(list);
		
		if (data.success) {
			$('#note').html(data.success);
			$('#note').addClass('success');
		
		
			$('#Quantity').val('1');
				
			$('#Size').val('');
				
			$('#Colour').val('');
				
			if (data.Size) {
				set_standard_border('Size_' + selected_size, basic_size_class);
			}
			
			if (data.Colour) {
				set_standard_border('Colour_' + selected_colour, basic_colour_class);
			}
				
			selected_size = '0';
			selected_colour = '0';
		}
	}
	
	if (data.validate.length > 0)
	{
		$('#note').addClass('error');
		for (i in data.validate) {
			$('#note').append(data.validate[i]+'<br />');
		}
	}
	
}

/**
* Generage record for controllers table
* @param record hash of records
*/
function generateTableRecord(record)
{
	/*return new Template
			("<tr> " +
				'<td>&nbsp;#{Product}&nbsp;</td> ' +
				'<td>&nbsp;#{Size}&nbsp;</td> ' +
				'<td>&nbsp;#{ColourLabel}&nbsp;</td> ' +
				'<td>&nbsp;#{Quantity}&nbsp;</td> ' +
				"</tr>"
			).evaluate(record);*/
	return '';
}


/**
* retrieve form value 
*/
function getFormValues()
{
	/*var pars = new Hash();
	pars.set('Quantity', $F('Quantity'));
	pars.set('Size', $F('Size'));
	pars.set('Colour', $F('Colour'));
	return pars;*/
}
