function AjaxRequest(target_div,file) {var MyHttpRequest = false;var MyHttpLoading = '<p>Updating...</p>'; // or use an animated gif instead: var MyHttpLoading = '<img src="loading.gif" border="0" alt="running" />';
	var ErrorMSG = 'Sorry - No XMLHTTP support in your browser, buy a newspaper instead';
	var cart
	
	cart = document.getElementById('cart')
	if (cart != null) {cart.style.display = 'block';}
	
	if(window.XMLHttpRequest) { // client use Firefox, Opera etc - Non Microsoft product
		try {
			MyHttpRequest = new XMLHttpRequest();
		}
		catch(e) {
			MyHttpRequest = false;
		}
	}
	else if(window.ActiveXObject) { // client use Internet Explorer
		try {
			MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
			MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				MyHttpRequest = false;
			}
		}
	}
	else {
		MyHttpRequest = false;
	}
	
	
	
	if(MyHttpRequest) { // browser supports httprequest
		var random = Math.random() * Date.parse(new Date()); // make a random string to prevent caching
	
		var file_array = file.split('?'); // prepare to check if we have a query string or not
		if(file_array[1] != '') { // there is a query string
		var query_string = '&rand=' + random;
		}
		else {// there is no query string
			var query_string = '?rand=' + random;
		}
	
		MyHttpRequest.open("get", url_encode(file + query_string), true); // <-- run the httprequest using GET
	
		// handle the httprequest
		MyHttpRequest.onreadystatechange = function () {
			if(MyHttpRequest.readyState == 4) { // done and responded
				document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result
			}
			else {
				document.getElementById(target_div).innerHTML = MyHttpLoading; // still working
			}
		}
	
		MyHttpRequest.send(null);
	}
	else {
		document.getElementById(target_div).innerHTML = ErrorMSG; // the browser was unable to create a httprequest
	}
}
	// end of "AJAX" function
	
	
	
function ShowCart() {
	showcartdiv();
	AjaxRequest('sb-packagelist','/wp-content/plugins/package-download/page-addon/cart.php?action=show');
}

function EmptyCart() {
	showcartdiv();
	AjaxRequest('sb-packagelist','/wp-content/plugins/package-download/page-addon/cart.php?action=empty');
}

function DownloadCart() {
	var random = Math.random() * Date.parse(new Date()); // make a random string to prevent caching
	showcartdiv();
	AjaxRequest('sb-packagelist','/wp-content/plugins/package-download/page-addon/updatecart.php?uid=' + random);
	AjaxRequest('sb-packagelist','/wp-content/plugins/package-download/page-addon/packfiles.php?uid=' + random);
	// window.open('/wp-content/plugins/package-download/page-addon/packfiles.php?uid=' + random,'','width=300,height=200');
	// AjaxRequest('sb-packagelist','/wp-content/plugins/package-download/page-addon/cart.php?action=show');
}

function AddFile(id,type) {
	showcartdiv();
	AjaxRequest('sb-packagelist','/wp-content/plugins/package-download/page-addon/cart.php?action=add&id='+id+'&type='+type);
}

function RemoveFile(id) {
	showcartdiv();
	AjaxRequest('sb-packagelist','/wp-content/plugins/package-download/page-addon/cart.php?action=remove&id='+id);
}
	
	// Here follows a function to urlencode the string we run through our httprequest, it has nothing to do with AJAX itself
	// If you look carefully in the above httprequest you se that we use this url_encode function around the file and query_string
	// This is very handy since we are using GET in our httprequest and for instance
	// any occurrance of the char # (from textboxes etc) will brake the string we are sending to our file - we don't want that to brake!
	// It will also convert spaces to +
	
	function url_encode(string) {
		var string;
		var safechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?=:";
		var hex = "0123456789ABCDEF";
		var encoded_string = "";
		for(var i = 0; i < string.length; i++) {
			var character = string.charAt(i);
			if(character == " ") {
				encoded_string += "+";
			}
			else if(safechars.indexOf(character) != -1) {
				encoded_string += character;
			}
			else {
				var hexchar = character.charCodeAt(0);
				if(hexchar > 255) {
					encoded_string += "+";
				}
				else {
					encoded_string += "%";
					encoded_string += hex.charAt((hexchar >> 4) & 0xF);
					encoded_string += hex.charAt(hexchar & 0xF);
				}
			}
		}
		return encoded_string;
	}
	
function setDisplay(id, display) 
{ 
    try 
    { 
        var elem; 
        elem = document.getElementById(id); 
        if (elem != null) 
        {        
            elem.style.display = display; 
            return display; 
        } 
    } 
    catch(e) 
    { 
    } 
}

function hidecartdiv() {
	setDisplay('cart','none');
}

function showcartdiv() {
	setDisplay('cart','block');
}

function updatepack(message) {
	if (message) {
		showcartdiv();
		document.getElementById('sb-packagelist').innerHTML = message;
	}
}

