//
// Javascripts for the shoppingcart.
//


AjaxRequest.prototype.onData = function(reqObj) {
	// AJAX-event handling function, takes an XMLHttpRequest object.
	// Success:  <added qty="1" sku="1234WERT">Chocolate Wert</added>
	// Failure: <error>Human readable error message.</error>

	//alert(reqObj.responseText);	

	var xml = reqObj.responseXML;
	

	if (reqObj.responseXML.getElementsByTagName('added')) {
	
		var elem;
		if (window.XMLHttpRequest) // if not IE
			elem = reqObj.responseXML.getElementsByTagName('added')[0];
		else
			elem = reqObj.responseXML.getElementsByTagName('added').item(0);
		
		// fill in the box with good stuff.
		document.getElementById('addPopupQty').innerHTML = elem.getAttribute('qty');
		document.getElementById('addPopupItemName').innerHTML = elem.firstChild.nodeValue;
		document.getElementById('addPopupMessage').innerHTML = '<i>Item has been added to your cart.</i>';
		//alert('testing for cart update A');		


		// update the HTML of the page to swap in "full cart" image if new.
		// document.getElementById('basket').src = '/shop/servlet/Basket?pv='+(new Date()).getTime();
		var c = document.getElementById('basketCount').innerHTML;
		//alert('testing for cart update B');
		if (isInt(c))
			document.getElementById('basketCount').innerHTML = parseInt(c) + parseInt(elem.getAttribute('qty'));
		else
			document.getElementById('basketCount').innerHTML = elem.getAttribute('qty');

	} else if (reqObj.responseXML.getElementsByTagName('error')) {
		var elem;
		if (window.XMLHttpRequest) // if not IE
			elem = reqObj.responseXML.getElementsByTagName('error')[0];
		else
			elem = reqObj.responseXML.getElementsByTagName('error').item(0);

		// fill with evil.
		document.getElementById('addPopupQty').innerHTML = '';
		document.getElementById('addPopupItemName').innerHTML = elem.firstChild.nodeValue;
		document.getElementById('addPopupMessage').innerHTML = '<i>Item has <b>not</b> been added to your cart.</i>';
	} else {
		// no response.. fill with puzzlement.
		document.getElementById('addPopupQty').innerHTML = '';
		document.getElementById('addPopupItemName').innerHTML = 'Error adding item to cart.';
		document.getElementById('addPopupMessage').innerHTML = '<i>Item has <b>not</b> been added to your cart.</i>';
	}

};

function addToCart(event, sku, isQty) {
	//alert('add to cart');
	// The javascript that initiates the add-to-cart
	// param sku: the item sku ('3914STRCRM') to add
	// param isQty: true if function should look for qty field in "mainForm", false otherwise

	if (! event) event = window.event;

	var qty = 1;
	if (isQty) {
		var val = document.mainForm.elements['qty'].value;
		if (isInt(val))
			qty = parseInt(val);
	}

	// test if AJAX-capable
	if (! isAjaxCapable) {
		window.location.href = '/shop/servlet/AddItem?id='+sku+'&qty='+qty;
		return;
	}

	// show "waiting" box. requires the 'showFunction' function.
	document.getElementById('addPopupQty').innerHTML = '';
	document.getElementById('addPopupItemName').innerHTML = '';
	document.getElementById('addPopupMessage').innerHTML = '<i>Adding item....</i>';

	document.getElementById('Layer2').style.left = 
		(getMouseX(event) - 180) + 'px';
	document.getElementById('Layer2').style.top = 
		(getMouseY(event) - 110) + 'px';

	showfunction('Layer2');
	
	var r = new AjaxRequest("POST","/shop/servlet/AddItem?mode=ajax","qty=" + qty + "&id=" + sku);
	// done. the AjaxRequest.prototype.onData function handles the rest.
}


// Utility function to test if a var is an int

function isInt(str) {
	var i = parseInt(str);

	if (isNaN(i))
		return false;

	i = i.toString();
	if (i != str)
		return false;

	return true;
}


// on mouse event, give current mouse x-coord and y-coord

function getMouseX(event) {
	var posx = 0;
	if (event.pageX) {
		posx = event.pageX;
	} else {
		posx = event.clientX + document.body.scrollLeft +
			document.documentElement.scrollLeft;
	}
	return posx;
}

function getMouseY(event) {
	var posy = 0;
	if (event.pageY) {
		posy = event.pageY;
	} else {
		posy = event.clientY + document.body.scrollTop +
			document.documentElement.scrollTop;
	}
	return posy;
}

function isAjaxCapable() {
	var req = undefined;
	try {
		if (window.ActiveXObject) {
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					req = undefined;
				}
			}
		} else if (window.XMLHttpRequest) {
			try {
				req = new XMLHttpRequest();
			} catch (e) {
				req = undefined;
			}
		}
	} catch (e) {
		// pass
	}
	return (req == undefined);
}

