/////////////////////functions for basket
/////builds a communication object between page and server
function httpRequest(url) {

    var httpObj = false;

    if (typeof XMLHttpRequest != 'undefined') {

        httpObj = new XMLHttpRequest();

    } else if (window.ActiveXObject) {

        try{

            httpObj = new ActiveXObject('Msxml2.XMLHTTP');

        } catch(e) {

            try{

                httpObj = new ActiveXObject('iMicrosoft.XMLHTTP');

            } catch(e) {}

        }

    }

    if (!httpObj) return;


//////handles response from server
    httpObj.onreadystatechange = function() {

        if (httpObj.readyState == 4) { 
        
        // when request is complete

            //callback(httpObj.responseText);
				var response = httpObj.responseText;
				///alert(response);
				
				
				if (response >= 0) {
				/////update basket items count
				document.getElementById('numberBasketItems').innerHTML=response;
				////show checkout butt
				showCheckOut (response);
				}
        }

    };

/////////sends the info to server
    httpObj.open('GET', url, true);

    httpObj.send(null);

}


//////add items to basket function - pass=  prod code, prod id, qty, type(sub or main)

function AddToBasket (basketedit, prodcode, prodid, qty, type) {


/////get basket id
var basketid = document.getElementById('add_basket').innerHTML;	


httpRequest('javascript/ajax.php?basketedit=' + basketedit + '&basketid=' + basketid + '&prodcode=' + prodcode + '&prodid=' + prodid + '&quantity=' + qty + '&prodtype=' + type + '&timestamp=' + Date() );

/////DEBUG
//location.href = 'javascript/ajax.php?basketedit=' + basketedit + '&basketid=' + basketid + '&prodcode=' + prodcode + '&prodid=' + prodid + '&quantity=' + qty + '&prodtype=' + type + '&timestamp=' + Date();

////update basket items count
getNumBaskItems();

alert('The Product ' + prodcode + ' has been added to your basket.');

//////DEBUG
//alert ('basketedit=' + basketedit + '&basketid=' + basketid + '&prodcode=' + prodcode + '&prodid=' + prodid + '&quantity=' + qty + '&prodtype=' + type + '&timestamp=' + Date());


}



/////get number of basket items on page load
function getNumBaskItems() {

////get basket id
var basketid = document.getElementById('add_basket').innerHTML;
var basketedit = 'number';

////alert('javascript/ajax.php?basketedit=' + basketedit + '&basketid=' + basketid);

///perform a http request to get num items
httpRequest('javascript/ajax.php?basketedit=' + basketedit + '&basketid=' + basketid);
}

