function ajaxFunction(div, string, prefix, refresh_screen) {
	var xmlHttp;
	cursor_wait();
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
	
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
	
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
			  alert("Your browser does not support AJAX!");
			  return false;
			}
		}
	
	}
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) {
			cursor_clear();
			document.getElementById(div).innerHTML = xmlHttp.responseText;
			if (refresh_screen) {
				/*
				window.scrollTo(0,0);
				document.getElementById('basket_confirm').style.display = 'block';
				document.getElementById('overlay').style.display = 'block';
				*/
				window.location = prefix+'Basket.htm';
			}
		}
	}
	xmlHttp.open("GET", string, true);
	xmlHttp.send(null);

}

function addToCart(productId, quantity, price, uri, prefix) {

	var optionString = "";
	var quantity = parseFloat(quantity);
	var delivery = '';
	var i = 0;
	var time_val = '';
	
	if (!document.getElementById('accept_terms').checked) {
		document.getElementById('basketError').innerHTML = '<p style="color: #cc0000;">You must accept our terms and conditions before purchasing tickets from us. Please tick the tickbox above the ADD TO BASKET button, thank you</p>';
		return false;
	} 
	
	if (document.getElementById('delivery_0').checked) {
		delivery = 'UK';
	}
	
	if (document.getElementById('fl')) {
		//time selection
		len = document.fl.time_sel.length
		for (i = 0; i <len; i++) {
			if (document.fl.time_sel[i].checked) {
				time_val = document.fl.time_sel[i].value
			}
		}
	
	//if ( (quantity == '0') ) { // we're looping through quantities and prices
		
		var i = 0;
		do {
			i++;
			var quantity = document.getElementById('quantity_'+i).value;
			var price = document.getElementById('price_'+i).value;

			//var true_false = i == document.getElementById('counter').value ? true : false;
			
			if ( (!isNaN(quantity) && quantity != '0') && (!isNaN(price) && price != '0') ) {
				ajaxFunction('basket_contents', prefix+'functions/addToCart.php?id='+productId+'&quantity='+quantity+'&price='+price+'&delivery='+delivery+'&time_val='+time_val, prefix, false);
			}
			ajaxFunction('basket_popup_contents', prefix+'includes/basketWhole.php?uri='+uri, prefix, true);
		
		} while (i < document.getElementById('counter').value);
		
	} else if ( (!isNaN(quantity) && quantity != '0') && (!isNaN(price) && price != '0') ) {
		
		ajaxFunction('basket_contents', prefix+'functions/addToCart.php?id='+productId+'&quantity='+quantity+'&price='+price+'&delivery='+delivery+'&time_val='+time_val, prefix, false);
		
		ajaxFunction('basket_popup_contents', prefix+'includes/basketWhole.php?uri='+uri, prefix, true);
		/*
		window.scrollTo(0,0);
		document.getElementById('basket_confirm').style.display = 'block';
		document.getElementById('overlay').style.display = 'block';
		*/
		//document.getElementById('basketError').innerHTML = '';
	
	} else {
		
		document.getElementById('basketError').innerHTML = '<p style="color: #cc0000;">Please select a ticket option and enter a quantity.</p>';
		return false;
	
	}
}


function updtAddress(clientId, Email, DName, DTitle, DAddress1, DAddress2, DTown, DCounty, DPostcode, DPhone, comments, prefix, sessID) {
	if (DName != '' && DAddress1 != '' && DTown != '' && DCounty != '' && DPostcode != '' && DPhone != '' && Email != '') {
		ajaxFunction('addressError','/functions/updtClient.php?id='+clientId+'&Email='+Email+'&DName='+DName+'&DTitle='+DTitle+'&DAddress1='+DAddress1+'&DAddress2='+DAddress2+'&DTown='+DTown+'&DCounty='+DCounty+'&DPostcode='+DPostcode+'&DPhone='+DPhone+'&comments='+comments);
		//window.location = 'http://localhost/fyh/secure/'
		//window.location = prefix
		
		//window.location = 'https://secure.furnishyourhome.co.uk/secure/index.php?sessID='+sessID;
		//window.location = 'http://fyh/secure/index.php?sessID='+sessID;
		
		window.location = prefix+'index.php?sessID='+sessID;
	} else {
		document.getElementById('addressError').innerHTML = '<p style="color: #cc0000;">Please complete the address.</p>';
		return false;
	}
}

function confirmDelete(pageID, prefixURL, type){
	var divWidth = '235';
	var divHeight = '200';
	var content = '<div class="shadow" style="padding-top: 10px; padding-left: 10px; padding-bottom: 10px; background-color: #ffffff; border: solid 1px #666666; border-top:solid 30px #661C3F; height: 85px; width: 300px; "><h2>Delete item from basket</h2><p>Are you sure you want to delete this item?</p><div class="buttonWrap"><a onclick="return deleteConfirmed('+pageID+',\''+prefixURL+'\',\''+type+'\');" class="button">OK</a> <a onclick="return displayNone(\'popupDiv\');" class="button">Cancel</a></div></div>';
	
	popupDiv(content, divWidth, divHeight);
}

function deleteConfirmed(pageID, prefixURL, type){
//	window.location.href=prefixURL+'functions/cartDel.php?recordID='+pageID;
	var optionString = "";
	var quantity = "";
	var typeURL = "?type="+type;
	ajaxFunction('',prefixURL+'functions/cartDel.php?recordID='+pageID+'&quantity='+quantity+optionString);
	window.location.href=prefixURL+'Basket.htm'+typeURL;
}

function displayNone(div){
document.getElementById(div).style.display = 'none';
}

function displayBlock(div){
document.getElementById(div).style.display = 'block';
}

function popupDiv(content, divWidth, divHeight){
	var divID = 'popupDiv';
	var box;
	
	if(document.getElementById(divID)){
		box = document.getElementById(divID);
	} else {
		box = document.createElement("div");
		box.setAttribute("id",divID);
		document.body.appendChild(box);
	}

	var scrollingLeft = Math.round((document.documentElement.clientWidth/2)-(box.style.width/2)) - (divWidth / 2);

	var scrollingTop = Math.round((document.documentElement.clientHeight/2)-(box.style.height/2)+document.documentElement.scrollTop) - (divHeight / 2);

	box.style.width = divWidth+'px';
	box.innerHTML = content;
	box.style.display = 'block';
	box.style.position = 'absolute';
	box.style.top = scrollingTop+'px';

	box.style.left = scrollingLeft+'px';
	box.style.zIndex = '1';

	//return hideShowSelects('hidden');
	return
}

function onlyNumbers(e) {

	var iKeyCode;

	if (!e) {
		var e = window.event;
	}

	if (e.keyCode) {
		iKeyCode = e.keyCode;
	} else {
		if (e.which) {
			iKeyCode = e.which;
		}
	}

	switch(iKeyCode) {

		case 8:
		case 9:
		case 35:
			break;
		case 36:
			break;
		case 37:
		case 38:
		case 39:
		case 40:
		case 46:
			break;
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			if (e.shiftKey || e.altKey){
				return false;
			}
			break;
		case 96:
		case 97:
		case 98:
		case 99:
		case 100:
		case 101:
		case 102:
		case 103:
		case 104:
		case 105:
			//return correct numeric from keypad
			return iKeyCode - 48; break;
		case 110:
		//case 190:
			//if you are supporting decimal points
		//	return '.';	break;
		default: return false;
	}
}

// Changes the cursor to an hourglass
function cursor_wait() {
	document.body.style.cursor = 'wait';
}

// Returns the cursor to the default pointer
function cursor_clear() {
	document.body.style.cursor = 'default';
}


