function makeRequest(url) {
   var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            return "Cannot create an XMLHTTP instance";
            //return false;
        }

        http_request.onreadystatechange = function() { alertContents(http_request); };
        http_request.open('GET', url, true);
        http_request.send(null);

    }

    function alertContents(http_request) {
		var msg;
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                //alert(http_request.responseText);
				msg = http_request.responseText;
            } else {
                msg = 'There was a problem with the request.';
            }
        }
		if (!msg) msg = "<div align=center><font color=red>processing ...</font></div>"; 
		document.getElementById('shopping_chart').innerHTML = msg;
    }
/*
function add_to_chart (field_id)
{
	var qty = document.getElementById(field_id+'_qty').value;
	var url = "/ajax/build-to-chart.php?action=add&pid="+field_id+"&qty="+qty; 
		//alert(url);
	makeRequest(url);
}
*/
function add_to_chart (field_id,type)
{
	var type,qty,c_qty;
	qty = c_qty = 0;
	if(type) {
		if (document.getElementById(field_id+'_a_qty')) {
			qty = document.getElementById(field_id+'_a_qty').value;
		}
		if (document.getElementById(field_id+'_c_qty')) {
			c_qty = document.getElementById(field_id+'_c_qty').value;
		}
		qty = parseInt(qty) + parseInt(c_qty);
		var url = "/ajax/build-to-chart.php?action=add&pid="+field_id+"&type="+type+"&qty="+qty+"&c_qty="+c_qty; 
	} else {
		var qty = document.getElementById(field_id+'_qty').value;
		var url = "/ajax/build-to-chart.php?action=add&pid="+field_id+"&qty="+qty; 
	}
	if (qty < 1)
	{
		alert('Quantity feild should be a more than 0');
		return false;
	}
		//alert(url);
		
		makeRequest(url);
		alert('Item(s) has been added to the basket (on the top right hand side).\nTo proceed with payment, just click the \"Checkout\" button.'); 
}
function remove_from_chart (field_id)
{
	//var qty = document.getElementById(field_id+'_qty').value;
	var url = "/ajax/build-to-chart.php?action=remove&pid="+field_id; 
		//alert(url);
	makeRequest(url);
}
function display_chart (val)
{
	var url = "/ajax/build-to-chart.php?action=display"; 
		//alert(url);
	makeRequest(url);
}
function CalcOptn(field_id,price,type)
{
	var type;
	if (type) var qty = document.getElementById(field_id+'_'+type+'_qty').value;
	else var qty = document.getElementById(field_id+'_qty').value;
	var new_price = qty * price;
	var result = new_price.toFixed(2);
	if (type) document.getElementById(field_id+'_'+type+'_total').value = 'RM'+result; 
	else document.getElementById(field_id+'_total').value = 'RM'+result;
}