function orderProduct(id) {
	document.getElementById('product_'+id).checked = false;
	document.getElementById('content').style.display = 'none';
	document.getElementById('ajax_adding').style.display = 'block';
	location.hash = "ajax_adding_box"
	//alert('http://www.richard-group.com/perl/cms/add_product.pl?product_id='+id+'&site_id=35&session_id='+readCookie('sessionid'));
	makeXMLRequestProduct('/perl/add_product.pl?product_id='+id+'&site_id=35&session_id='+readCookie('sessionid'), '');
}

var xml_request_product = false;

function makeXMLRequestProduct(url, parameters) {
	xml_request_product = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		xml_request_product = new XMLHttpRequest();
		if (xml_request_product.overrideMimeType) {
			xml_request_product.overrideMimeType('text/plain');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			xml_request_product = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xml_request_product = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!xml_request_product) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	xml_request_product.onreadystatechange = xml_handler_product;
	xml_request_product.open('GET', url + parameters);
	xml_request_product.send(null);
}

function xml_handler_product() {
	if(xml_request_product.readyState != 4) return false;
	if(xml_request_product.status != 200) {
		document.getElementById('ajax_message').innerHTML = 
			'We could not add the product to your cart as it returned a '+xml_request_product.status+' error!<br/>'+
			'<a href="Javascript:void(0);" onClick="Javascript:closeMessage();">Return to product list</a>';
		return false;
	}
	var text = xml_request_product.responseText;
	if (!text) {
		document.getElementById('ajax_message').innerHTML = 
			'An unexpected error happened while trying to add the product to your cart, please try again!<br/>'+
			'<a href="Javascript:void(0);" onClick="Javascript:closeMessage();">Return to product list</a>';
		return false;
	}
	
	var lines = text.split("\n");
	if (lines[0].match(/^(\w+);(\d+);(.*)$/)) {
		if (RegExp.$2 == 1) {
			document.getElementById('ajax_message').innerHTML = 
				RegExp.$3+'<br/>'+
				'<a href="Javascript:void(0);" onClick="Javascript:closeMessage();">Return to product list</a> or '+
				'<a href="/cart/" onClick="Javascript:closeMessage();">Go to the cart</a><br/>';
			return false;
		} else {
			document.getElementById('ajax_message').innerHTML = 
				'An error happened while trying to add the product to your cart ('+RegExp.$3+'), please try again!<br/>'+
				'<a href="Javascript:void(0);" onClick="Javascript:closeMessage();">Return to product list</a>';
			return false;
		}
	} else {
		document.getElementById('ajax_message').innerHTML = 
			'An unexpected error (#1) happened while trying to add the product to your cart, please try again!<br/>'+
			'<a href="Javascript:void(0);" onClick="Javascript:closeMessage();">Return to product list</a>';
		return false;
	}
}

function closeMessage() {
	document.getElementById('ajax_adding').style.display = 'none';
	document.getElementById('content').style.display = 'block';
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
