function perform_web_store_action(action,param1,param2,param3)

{
	if (action == 'CHANGE_SEARCH_CATEGORY')
	{
	var a_params=new Array();
		var v_follow_on_function="perform_cms_action('CHANGE_SEARCH_GROUP')";
  		a_params[0]='ajax_root^cms_web_store';
		a_params[1]='target_id^search_group';
		a_params[2]='with_all_key^ALL';
		a_params[3]='with_all_value^--ALL--';
		a_params[4]='no_values_key^ALL';
		a_params[5]='no_values_value^--ALL--';
		a_params[6]='follow_on_function^' + v_follow_on_function;
		a_params[7]='object_name^product_groups';
		a_params[8]='category^' + document.getElementById('search_category').value;
		ajax_get_data(a_params);
		return;
	}
	
	if (action == 'CHANGE_SEARCH_GROUP')
	{
		var a_params=new Array();
		var v_follow_on_function="perform_cms_action('CHANGE_SEARCH_BRAND')";
  		a_params[0]='ajax_root^cms_web_store';
		a_params[1]='target_id^search_brand';
		a_params[2]='with_all_key^ALL';
		a_params[3]='with_all_value^--ALL--';
		a_params[4]='no_values_key^ALL';
		a_params[5]='no_values_value^--ALL--';
		a_params[6]='follow_on_function^' + v_follow_on_function;
		a_params[7]='object_name^product_brands';
		a_params[8]='group^' + document.getElementById('search_group').value;
 		
		ajax_get_data(a_params);
		return;
	}
	
	if ( action == 'DO_PRODUCT_SEARCH' )
	{
		path = param1;
		category = document.getElementById('search_category').value;
		group = document.getElementById('search_group').value;
		brand = document.getElementById('search_brand').value;
		keyword = document.getElementById('search_keyword').value;
		
		top.location.href = path + '/' + category + '/' + group + '/' + brand + '/' + keyword;	
		return;
	}
	
	if (action == 'BUY')
	{
		var a_params = new Array();
		var product_id = param1;
		var amount = param2;
		var button_id = param3;
		 
		a_params[0] = 'ajax_root^cms_web_store';
		a_params[1] = 'product_id^' + product_id;
		a_params[2] = 'amount^' + amount;
		a_params[3] = 'object_name^add_to_order';
		a_params[4] = 'button_id^' + button_id;

		disable_buttons(); 
		ajax_get_data(a_params,'web_store_ajax_response');
		return;
	}
	
	if (action == 'COMPLETE_ORDER')
	{
		disable_buttons();
		top.location.href = _DBSVARS.request.machine_root + '/checkout';
		return;
	}
	
	if (action == 'UPDATE_ORDER_SUMMARY')
	{

		div_order_state = document.getElementById('order_status');
		div_order_count = document.getElementById('order_count');
		div_order_state = document.getElementById('order_state');
		div_order_total = document.getElementById('order_total');
		number_products = param1;
		total_amount = param2;
		stack_height = number_products;
		
		if ( stack_height > 5 )
		{
			stack_height = 5;
		}
		if ( number_products == 0 )
		{
		
			div_order_count.innerHTML = 'Your cart is empty';
			div_order_state.innerHTML = '';
			div_order_total.inneHTML = '';
			document.getElementById('complete_order_button').style.display='none';
		}
		else
		{
			items = 'item';
			if (number_products > 1)
			{
				items = 'items';	
			}
			div_order_count.innerHTML = 'Your cart has ' + number_products + ' ' + items ;
			div_order_state.innerHTML = '';
			div_order_total.innerHTML = total_amount;
			document.getElementById('complete_order_button').style.display='';
		}

		stack_icon = _DBSVARS.request.imagesroot + '/' + 'order_' + stack_height + '.jpg';
		div_order_state.style.backgroundRepeat="no-repeat";
		div_order_state.style.backgroundImage="url('" + stack_icon + "')";
		   
		return;
	}
	
	if (action == 'CHANGE_ORDER_QUANTITY')
	{
		var a_params = new Array();
		var product_id = param1;
		var quantity = document.getElementById('quantity_' + product_id).value;
		 
		if ( isNaN(quantity) )
		{
			alert('Please enter quantity as a number without commas or formatting');
			return;
		}
		
		if ( Math.round(quantity) != quantity )
		{
			alert('Please enter quantity as a round number without decimal places');
			return;
		}
		
		if ( quantity < 0 )
		{
			alert('Please enter quantity as positive number ');
			return;
		}
		
		if ( quantity == 0 )
		{
			if ( !confirm('Making the quantity 0 will remove this item from your order.  Are you sure?') )
			{
				return;
			}
		}
		
		
		a_params[0] = 'ajax_root^cms_web_store';
		a_params[1] = 'product_id^' + product_id;
		a_params[2] = 'quantity^' + quantity;
		a_params[3] = 'object_name^change_order_quantity';
		
		disable_buttons();
		ajax_get_data(a_params,'web_store_ajax_response');
		
		return;
		
	}

	if (action == 'DELETE_ORDER_LINE')
	{
		var a_params = new Array();
		var product_id = param1;
		
		if ( !confirm('Are you sure you want to delete this line?') )
		{
			return;
		}
		 
		a_params[0] = 'ajax_root^cms_web_store';
		a_params[1] = 'product_id^' + product_id;
		a_params[2] = 'quantity^0';
		a_params[3] = 'object_name^change_order_quantity';
		
		disable_buttons();
		ajax_get_data(a_params,'web_store_ajax_response');
		
		return;
	}
	
	if (action == 'BACK_TO_STORE')
	{
		disable_buttons();
		top.location.href = _DBSVARS.request.machine_root + '/store';
		return;
	}

/*	if (action.length > 5 && action.substring(0,5) == 'STEP_')
	{
		document.getElementById('next_action').value = action.toLowerCase();
		disable_buttons();
		document.f_checkout.submit();
		
		return;
	}
*/	
	if (action == 'UPDATE_REGISTRATION')
	{
		 perform_cms_action(action,param1,param2,param3);
		 return;
	}
	
	if (action == 'SUBMIT_REGISTRATION')
	{
		 perform_cms_action(action,param1,param2,param3);
		 return;
	}
	
	
	if (action == 'DOCUMENT_DETAILS')
	{
		document.getElementById('document_id').value = param1;
		disable_buttons();
		document.f_main.submit();
		return;
	}

	if (action == 'SUBMIT_PAYMENT_DETAILS')
	{
		
		if ( trim_string( document.getElementById('card_type').value) == '' )
			{ 	
				alert('Please select the card type.');
				return;
			}
		
		if ( trim_string( document.getElementById('card_number').value)== '' )
			{ 	
				alert('Please enter the card number.');
				return;
			}		
		
		if ( isNaN(document.getElementById('card_number').value) )
		{
			alert('Please enter the card number without any spaces or dashes etc.');
			return;
		}
		
		if ( trim_string( document.getElementById('cardholder_name').value)== '' )
			{ 	
				alert('Please enter the cardholder name.');
				return;
			}
			
		if ( trim_string( document.getElementById('expiry_month').value)== '' )
			{ 	
				alert('Please select a month.');
				return;
			}	
			
		if ( trim_string( document.getElementById('expiry_year').value)== '' )
			{ 	
				alert('Please select a year.');
				return;
			}
		
				
		var a_params = new Array();
		a_params[0] = 'ajax_root^cms_web_store';
		a_params[1] = 'cardholder_name^' + document.getElementById('cardholder_name').value;
		a_params[2] = 'card_number^' + document.getElementById('card_number').value;
		a_params[3] = 'card_type^' + document.getElementById('card_type').value;
		a_params[4] = 'expiry_month^' + document.getElementById('expiry_month').value;
		a_params[5] = 'expiry_year^' + document.getElementById('expiry_year').value;
		a_params[6] = 'payment_amount^' + document.getElementById('payment_amount').value;
		a_params[7] = 'object_name^payment_details';
		a_params[8] = 'next_action^' + param1;

		disable_buttons();
		ajax_get_data(a_params,'web_store_ajax_response');
		
		return;
	}
	
	if (action == 'ORDER_CF2SAP')
	{
		var a_params = new Array();
		a_params[0] = 'ajax_root^cms_web_store';
		a_params[1] = 'product_id^' + document.getElementById('product_id').value;
		a_params[2] = 'annual_maintenance_product_id^' + document.getElementById('annual_maintenance').value;
		a_params[3] = 'annual_maintenance_checked^' + document.getElementById('annual_maintenance').checked;
		a_params[4] = 'sap_host^' + document.getElementById('sap_host').value;
		a_params[5] = 'repeat_sap_host^' + document.getElementById('repeat_sap_host').value;
		a_params[6] = 'org_name^' + document.getElementById('org_name').value;
		a_params[7] = 'org_address^' + document.getElementById('org_address').value;
		a_params[8] = 'org_country_id^' + document.getElementById('org_country_id').value;
		a_params[9] = 'accept_conditions^' + document.getElementById('accept_conditions').checked;
		a_params[10] = 'object_name^order_cf2sap';
		a_params[11] = 'license_start_yyyy^' + document.getElementById('license_start_yyyy').value;
		a_params[12] = 'license_start_mm^' + document.getElementById('license_start_mm').value;
		a_params[13] = 'license_start_dd^' + document.getElementById('license_start_dd').value;
		disable_buttons();
		
		ajax_get_data(a_params,'web_store_ajax_response');
		return;
	}

}

function web_store_ajax_response(r)
{		
	enable_buttons();
	
	if (report_ajax_errors(r)==false)
	{
		var status = '';
		var message = '';
		var object_name = '';
		var product_id = "";
		var next_action = '';
	
		status = get_value_from_ajax_response(r,'status');
		message = get_value_from_ajax_response(r,'message');
		object_name = get_value_from_ajax_response(r,'object_name');
		next_action = get_value_from_ajax_response(r,'next_action');
		
		if (status == 'ER')
		{
			try{display_message(message)}catch(e){};
			/*alert(message);*/
			
			switch(object_name)
			{
				case 'payment_details':	
					var receipt_text = get_value_from_ajax_response(r,'receipt');
					document.getElementById('receipt').innerHTML = receipt_text;	
				break;
			}
			
			return;
		}
		
		switch(object_name)
		{
		
		case 'add_to_order':
			number_products = get_value_from_ajax_response(r,'number_products');
			total_amount = get_value_from_ajax_response(r,'total_amount');
			product_id = get_value_from_ajax_response(r,'product_id');
			button_id = get_value_from_ajax_response(r,'button_id');
			try {document.getElementById('button_' + button_id).onclick = '';}catch(e){};
			document.getElementById('button_span2_' + button_id).innerHTML = 'Selected';
			document.getElementById('button_span1_' + button_id).className = 'button_2_disabled_span1';
			document.getElementById('button_span2_' + button_id).className = 'button_2_disabled_span2';
			
			perform_cms_action('UPDATE_ORDER_SUMMARY',number_products,total_amount);
			
		break; 
		  
		case 'change_order_quantity':
		 	
			total_amount = get_value_from_ajax_response(r,'total_amount');
			line_total = get_value_from_ajax_response(r,'line_total');
			product_id = get_value_from_ajax_response(r,'product_id');
		 	document.getElementById('order_total_amount').innerHTML = total_amount;
			document.getElementById('line_total_' + product_id).innerHTML = line_total;
			
		break;
		
		case 'delete_order_line':
		 	
		 	total_amount = get_value_from_ajax_response(r,'total_amount');
			product_id = get_value_from_ajax_response(r,'product_id');
		 	document.getElementById('order_total_amount').innerHTML = total_amount;
			document.getElementById('order_line_' + product_id).style.display = 'none';
		
		break;
		
		case 'delivery_details':
		 	
		 	perform_cms_action('STEP_3');
		
		break;

		case 'order_cf2sap':
		 	perform_cms_action('STEP_3');		
		break;		
		
		case 'payment_details':		
	 		perform_cms_action(next_action);
		break;
					
		}
		
	}		
}