function checkReferral(element) {
	//var ajax = new Ajax2();
	
	if (element.value != '') {
		//ajax.request('general.php?module=details&action=checkreferral&code=' + element.value, 'GET', {}, function(result, xml, text) {
		new Ajax2({url: 'general.php?module=details&action=checkreferral&code=' + element.value,
				   method: 'GET',
				   resultMode: Ajax2.RESULT_TEXT,
				   callback: function(text) {
		
			if (text == '1') document.getElementById('referral').style.display = 'block';
			else document.getElementById('referral').style.display = 'none';
		}
		});
	}
	
}

/*
	Following JavaScript assumes the usage of Prototype JavaScript framework, version 1.6.0.3
*/


function addItems(product_id){
	var max_prices = 10;
	var price_name, quantity_name;
	var json_values, json_string = '{ ';
	var eval_JSON = false;

	for(var i = 0; i < max_prices; i++){
		code_name = 'pc_' + i + '_' + product_id;
		size_name = 'ps_' + i + '_' + product_id;
		quantity_name = 'pq_' + i + '_' + product_id;
		if($(size_name) && $(quantity_name)){
			if($(quantity_name).value != ''){
				eval_JSON = true;
				json_string += '"' + quantity_name + '" : "' + product_id + ',' + $(size_name).value + ',' + $(quantity_name).value + ',' + ',' + $(code_name).value + '",';
			}
		} else {
			break;
		}
	}
	if(eval_JSON){
		json_string = json_string.substr(0, json_string.length - 1);
		json_string += '}';
		json_values =  json_string.evalJSON();
		new Ajax.Request('_add_to_basket.php',
			{
				method: 'get',
				parameters: json_values,
				onSuccess: function(transport){
			/*		var values = transport.responseJSON;
					var fields = values.keys.split(',');

					if(fields.length > 0){
						for(var i = 0; i < fields.length; i++){
							$(fields[i]).value = '';
						}
					}
					$('basket_order').innerHTML = values.prods;
					$('basket_price').innerHTML = '£' + values.money;
					$('basket_msg').innerHTML = values.basket_msg;
					*/

					window.location.href = 'basket.php'
				}
			}
		);
	}
}

function setTask(task, transfer){
	if($('task')){
		$('task').value = task;
		if(transfer){
			$('symply_frm').submit();
		}
	}
}

function deleteItem(id, size){
	name = 'q_' + id + '_' + size;
	if($(name)){
		$(name).value = 0;
		$('task').value = 'update';
		$('symply_frm').submit();
	}
}

function check_redeem(){
	var order_total = parseFloat($('basket_total').firstChild.nodeValue);
	
	if ($('credit_amount').value > redeem_max) $('credit_amount').value = redeem_max.toFixed(2);
	if ($('credit_amount').value > order_total) $('credit_amount').value = order_total.toFixed(2);
	
	$('invoice_total').firstChild.nodeValue = parseFloat(order_total - parseFloat($('credit_amount').value)).toFixed(2);
	
	
	new Ajax.Request('_change_redeem_credit.php',
			{
				method: 'get',
				parameters: {'credit_amount': $('credit_amount').value},
				onSuccess: function(transport){
				
				}
			}
		);	
	
	return;
	var num_value;
	var json_string, json_values;

	if(Number($('credit_amount').value) > redeem_max){
		$('credit_amount').value = redeem_max;
	}

	json_string = '{ "credit_amount" : "' + $('credit_amount').value + '" }';
	json_values = json_string.evalJSON();
	
}

function check_postcode(){
	var postcode;
	if($('dpcode').value == ''){
		if($('pcode').value == ''){
			$('postcode_message').style.display = 'none';
		} else {
			postcode = trim($('pcode').value);
			if(postcode.substr(0, 2).toUpperCase() == 'HS' || postcode.substr(0, 2).toUpperCase() == 'IM' || postcode.substr(0, 2).toUpperCase() == 'BT'){
				$('postcode_message').style.display = 'block';
			} else {
				$('postcode_message').style.display = 'none';
			}
		}
	} else {
			postcode = trim($('dpcode').value);
			if(postcode.substr(0, 2).toUpperCase() == 'HS' || postcode.substr(0, 2).toUpperCase() == 'IM' || postcode.substr(0, 2).toUpperCase() == 'BT'){
				$('postcode_message').style.display = 'block';
			} else {
				$('postcode_message').style.display = 'none';
			}
	}
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}