
/**************************************************
* Name: shop_ajax.js
* Author: 
* Modified: 2009-06-25 15:42:46
* Overview:
* Copyright: WLD
* Notes: 
**************************************************/

// Gets a message from an XML fragment
function hijaxGetMessage (httpTransport) {
	if (!httpTransport) return false;

	// The XML response
	var responseXML = httpTransport.responseXML;
	if (!responseXML) return false;

	// The response container
	var responseTag = responseXML.getElementsByTagName ("response")[0];
	if (!responseTag) return false;

	// The message container
	var responseMessage = responseTag.getElementsByTagName ("message")[0];
	return (responseMessage ? responseMessage.firstChild.nodeValue : false);
}

// Add hooks to basket addition forms
function hijaxBasketAdd () {
	// Get all the forms in the document
	var basketInputs = document.getElementsByTagName ("input");
	var basketAnchors = document.getElementsByTagName ("a");

	// Look for that form elements that contain the magic class name
	for (var i = 0; i < basketInputs.length; i++) {
		if (basketInputs[i].className.toLowerCase ().indexOf ("ajax_intercept") > -1) {
			var classExt = '';
			if (basketInputs[i].className.toLowerCase ().indexOf ("ajax_intercept_small") > -1) {
				classExt = '_small';
			}

			// Add our hooks to it
			var insertLocation = basketInputs[i].parentNode;
			basketInputs[i].parentNode.parentNode.onsubmit = function () {
				return hijaxInterceptAdd (this, insertLocation, classExt);
			}
		}
	}

	// Look for that form elements that contain the magic class name
	for (var i = 0; i < basketAnchors.length; i++) {
		if (basketAnchors[i].className.toLowerCase ().indexOf ("ajax_intercept") > -1) {
			var classExt = '';
			if (basketAnchors[i].className.toLowerCase ().indexOf ("ajax_intercept_small") > -1) {
				classExt = '_small';
			}
			// Add our hooks to it
			basketAnchors[i].onclick = function () {
				var insertLocation = this.parentNode.parentNode.parentNode.parentNode;
				return hijaxInterceptAdd (this.parentNode.parentNode, insertLocation, classExt);
			}
		}
	}
}


function hijaxInterceptAdd (what, insertLocation, classaffix) {
	// NOTE: Function always returns true to enable failover default form submission
	// Return false is AJAXing was successful and the traditional form submission is to be aborted

	// Ensure we have the form host objects
	var formAjax = what;
	var formAjaxInputs = what.getElementsByTagName ("input");
	var formAjaxAnchors = what.getElementsByTagName ("a");

	// Disable the button
	for (var i = 0; i < formAjaxInputs.length; i++) {
		if (formAjaxInputs[i].getAttribute ("type").toLowerCase () == "submit" && formAjaxInputs[i].className.toLowerCase ().indexOf ("ajax_intercept") > -1) {
			var submitButton = formAjaxInputs[i];
			submitButton.disabled=true;
			break;
		}
	}

	// Disable the anchor
	for (var i = 0; i < formAjaxAnchors.length; i++) {
		if (formAjaxAnchors[i].className.toLowerCase ().indexOf ("ajax_intercept") > -1) {
			var submitAnchor = formAjaxAnchors[i];
			submitAnchor.style.visibility = "hidden";
			break;
		}
	}

	// Remove any user messages already displayed
	var userMessage = document.getElementById ("ajaxresponsemsg");
	if (userMessage) {
		userMessage.parentNode.removeChild (userMessage);
		userMessage = null;
	}

	// Create and show the loader image
	var loaderDiv = document.createElement ("p");
	loaderDiv.setAttribute ("class", "ajax_loader_div"+classaffix);
	loaderDiv.setAttribute ("id", "ajaxresponsemsg");
	loaderDiv.className = loaderDiv.getAttribute ("class");
	loaderDiv.style.height='0px';
	loaderDiv.style.overflow='hidden';

	var loaderMsg = document.createTextNode ("Amending your basket, please wait");
	loaderDiv.appendChild (loaderMsg);
	loaderDiv = insertLocation.insertBefore (loaderDiv, insertLocation.childNodes[0]);

	expand(1,loaderDiv);

	// Get all the input fields and loop through them
	var formAjaxRequest = formAjax.getAttribute ("action") + "?ajax=1&";

	for (i = 0; i < formAjax.elements.length; i++) {
		// Had to use this obscure logic structure because of several weird browser errors
		// Probably some throwback from the forms host object with regards to the new DOM methods
		if (formAjax.elements[i].name && formAjax.elements[i].name.length > 0) {
			if ((formAjax.elements[i].type.toLowerCase () == "checkbox" && formAjax.elements[i].checked == false) || (formAjax.elements[i].type.toLowerCase () == "radio" && formAjax.elements[i].checked == false)) {
				 // Pwnt
			} else {
				formAjaxRequest += formAjax.elements[i].name + "=" + escape (formAjax.elements[i].value) + "&";
			}
		}
	}

	// Prepare the callback with closures

	var hijaxInterceptAddCallBack = function (httpTransport) {
		var callBackDelay = function() {
			hijaxInterceptAddDone (httpTransport, what, submitButton, submitAnchor, insertLocation, classaffix);
		}

		window.setTimeout(callBackDelay,2000);
	}

	// Make the request
	var formAjaxSuccess = new httpDo ('GET', formAjaxRequest, hijaxInterceptAddCallBack);

	return !formAjaxSuccess;
}

// Inform the user the action is executed
function hijaxInterceptAddDone (httpTransport, what, submitButton, submitAnchor, insertLocation, classaffix) {

	// Check we have our parameters
	if (!httpTransport || !what) return false;

	// Remover the loader and re-enable the submit button
	//loaderDiv.parentNode.removeChild (loaderDiv);

	// Remove any user messages already displayed
	var userMessage = document.getElementById ("ajaxresponsemsg");
	if (userMessage) {
		userMessage.parentNode.removeChild (userMessage);
		userMessage = null;
	}

	// Show the response as a user message
	var responseMessage = hijaxGetMessage (httpTransport);
	if (!responseMessage)
		responseMessage = "Completed";
	var addedDiv = document.createElement ("p");
	addedDiv.setAttribute ("id", "ajaxresponsemsg");
	addedDiv.id = addedDiv.getAttribute ("id");
	addedDiv.setAttribute ("class", "message"+classaffix);
	addedDiv.className = addedDiv.getAttribute ("class");
	var addedMsg = document.createTextNode (responseMessage + " ");
	addedDiv.appendChild (addedMsg);
	var addedLink = document.createElement ("a");
	addedLink.setAttribute ("href", "/basket.php");
	addedLinkMsg = document.createTextNode ("View your basket");
	addedLink.appendChild (addedLinkMsg);
	addedDiv.appendChild (addedLink);

	addedDiv = insertLocation.insertBefore (addedDiv, insertLocation.childNodes[0]);

	if (submitButton)
		submitButton.disabled=false;

	if (submitAnchor)
		submitAnchor.style.visibility = "";


	// Tell screen readers that part of the page has changed
	addedDiv.tabindex = -1;

	// Tell the humans that part of the page has been changed
	//what.parentNode.parentNode.style.borderColor = '#FF0000';

	fadeUp(what.parentNode, 241, 241, 241, 100, 246, 69, 77);

	// Reset the form
	var doFormReset = function () { formHardReset (what.parentNode); }
	setTimeout (doFormReset, 500);
}


// Function to make columns the same height
function sameHeight(columns) {
	var divs = document.getElementsByTagName("DIV"); // Get all div elements
	if (divs.length > 0) { // Check we have some div's to work with (who doesn't? :-)
		for (var i=0; i < divs.length; i++) { // Loop through each div
			if (divs[i].className.toLowerCase ().indexOf ("productrow") > -1) { // If this div is a product row, work on its children
				var prodItems = divs[i].childNodes; // Get all of the div's child nodes
				var maxheight = 0;
				var newheight = 0;
				for (var j = 0; j < prodItems.length; j++) { // Loop through its child nodes
					if (prodItems[j].nodeType == 1 && prodItems[j].className.toLowerCase ().indexOf ("product") > -1 && prodItems[j].offsetHeight > maxheight)
						maxheight = prodItems[j].offsetHeight; // Set maxheight to the height of this div, if it is taller than the last one
				}

				newheight = maxheight - 2 - 8 - 30; // Account for padding and border (2 + 8 + 30)

				for (var j = 0; j < prodItems.length; j++) { // Loop through its child nodes again and set the height of each one to maxheight
					if (prodItems[j].nodeType == 1 && prodItems[j].className.toLowerCase ().indexOf ("product") > -1) {
						prodItems[j].style.minHeight = (newheight) + 'px';
						/* IE6 is broken, so we have to apply height instead of min-height */
						if (prodItems[j].offsetHeight != maxheight)
							prodItems[j].style.height = (newheight) + 'px';
					}
				}
			}
		}
	}
}


function expandBuyColumn() {
	var scroller = document.getElementById('scroller');
	var expander = document.getElementById('expand');
	if (expander) {
		expander.onclick = function () {
			expander.style.visibility='hidden';
			scroller.style.overflow='hidden'; // Remove scroll bar
			return !expand(1,scroller);
		}
	}
}


function expand(increment,scroller) {
	increment += increment;
	var myHeight = scroller.offsetHeight + increment;

	if (scroller.expanding)
		clearTimeout(scroller.expanding);

	if (myHeight >= scroller.scrollHeight) {
		scroller.style.height=scroller.scrollHeight+"px";
	}
	else {
		scroller.style.height=(myHeight)+'px';
		scroller.expanding = setTimeout(function(){expand(increment,scroller)},20);
	}

	return true;
}

// Add load events
winAddLoadEvent (sameHeight);
winAddReadyEvent (hijaxBasketAdd);
winAddReadyEvent (expandBuyColumn);


