// Javascript functions used in the Kencart software


// make a popup window for viewing images
function image_popup(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, 'Image', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=450,left = 112,top = 84');");
}

// used to toggle the shopping list feature on and off on the show_cart page
function toggleLayer(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display=="none" ? "block":"none";
	}
}

// alert the user if he is trying to buy more than we have in stock
function showStockWarning()
{
	return confirm('Your order includes some product quantities which are not currently avaiable in stock.\n '
		+ 'If you proceed with the order, these products will be put on a back-order and dispatched\n when they come back into stock.'
		+ ' See our Shipping Policy for more information.\n\n'
		+ 'Press \'OK\' to proceed, or \'Cancel\' to amend your order');
}

// function to add a cool slide effect to the shipping info page
function toggle_shipping_layer(show_me)
{
	var divs = new Array();
	divs[0] = "info_mainland";
	divs[1] = "info_highlands";
	divs[2] = "info_channel";
	divs[3] = "info_eec";
	divs[4] = "info_world";
		
	var i;
	
	for (i in divs)
	{
		if (i==0 || i==1 || i==2 || i==3 || i==4)
		{
			if(document.getElementById(divs[i]).style.display != "none")
			{
				Effect.BlindUp(divs[i], {duration: 0.5});
			}
			if (divs[i] == show_me)
				Effect.toggle(show_me, 'Blind', {duration: 0.5}); 
		}
	}
}


// the following functions are used to display the AJAX dynamic search suggestions ////////////

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('search_box').value);
		searchReq.open("GET", 'search_suggest.php?search=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
		
		// set focus onto suearch_suggest div
		//document.getElementById('search_suggest').focus(0);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest')
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			/*var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + str[i] + '</div>';*/
			
			var suggest = '<div ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}

//Click function
function setSearch(value) {
	document.getElementById('search_box').value = value.replace(/&amp;/g,"&");
	document.getElementById('search_suggest').innerHTML = '';
}

// function to empty the search_suggest div when the focus goes off the div
function emptySuggest() {
		//alert(document.activeElement.id);
		if (document.activeElement.id != "search_suggest")
			document.getElementById('search_suggest').innerHTML = '';
		
}
