function xmlhttpPost(strURL,act,targ) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText,act,targ);
        }
    }
    self.xmlHttpReq.send(getquerystring(act,targ));
}

function getquerystring(act,targ) {
	switch(act){
	 case 'callback':
	    var form     = document.forms['callback'];
	    var word = form.phone.value;
	    qstr = 'phone=' + escape(word);  // NOTE: no '?' before querystring
	    return qstr;
	 break;
	 case 'add_item':
		return "id="+ targ.value;
		document.getElementById("feedback").innerHTML = "Item added to basket<br/><a class='link' href='?page=trolley'>view items in basket</a>";
		document.getElementById("feedback").style.visibility="visible";
	 break;
		return;
	}
}

function updatepage(str,act,targ){
	switch(act){
	 case 'callback':
		document.getElementById("instructions").parentNode.innerHTML = str;
	 break;
	 case 'add_item':
		document.getElementById("trolley_count").innerHTML = str;
		document.getElementById("feedback").innerHTML = "Item added to basket<br/><a class='link' href='?page=trolley'>view items in basket</a>";
		document.getElementById("feedback").style.visibility="visible";
		feedback_timer = setTimeout("document.getElementById('feedback').style.visibility='hidden'",5000);
		if(document.all){x = targ.parentElement.children[2]; }else{x = targ.parentNode.childNodes[5];}
		x.style.color = 'white';
		x.innerHTML = 'Add';
	 break;
	 case 'simple_add':
		document.getElementById("trolley_count").innerHTML = str;
		//targ.style.color = 'white';
		targ.innerHTML = 'add another';
	 break;
	 case 'simple_remove':
		document.getElementById("trolley_count").innerHTML = str;
		targ.innerHTML = 'remove';
	 break;
	 default:targ.parentNode.innerHTML = str;
	}
}

function add_to_basket(t){
if(document.all){
targ = t.parentElement.children[0];
}else{
targ = t.parentNode.childNodes[1];
}
xmlhttpPost("ajax/trolley.php?action=add","add_item",targ);
t.innerHTML="Adding...";
t.style.color="#ec8a49";
}
function simple_add(t,id){
xmlhttpPost("ajax/trolley.php?action=add&id="+id,"simple_add",t);
t.innerHTML="adding...";
}
function simple_remove(t,id){
xmlhttpPost("ajax/trolley.php?action=remove&id="+id,"simple_remove",t);
t.innerHTML="removing...";
}
