
	/***
	 *
	 *	AJAX Based Predictive Search Application for LondonTown.com
	 *	Copyright (C) 2005, All Rights Reserved, Globalvision Media
	 *	Author: James Holden (jim@londontown.com)
	 *	Date: December 2005
	 *
	 *	Javascript code to implement a predictive search system for the LondonTown.com website.  This
	 *	code works with the search bar found in the upper right corner of LondonTown.com.
	 *
	 *	TODO:	Rename and manage routines so they re unique to search and do not conflict (potentially)
	 *			with other site components in the future.
	 *
	 */

	var subOldMilli 			= 0;
	var subNewMilli 			= 0;
	var subOldkey 				= '';
	var subPrev				="";
	var subSearchedAlready 	= false;
	var subFocEnabled 			= true;
	var subActiveXML 			= "";
	var subSearchSystem;


	/**
	 * @desc obtain a new xml http request
	 **/
	if (window.XMLHttpRequest) {
		subSearchSystem = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		if (subActiveXML) {
			subSearchSystem = new ActiveXObject(subActiveXML);
		} else {
			var activeversions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
			for (var i = 0; i < activeversions.length ; i++) {
				try {
					subSearchSystem = new ActiveXObject(activeversions[i]);
					if (subSearchSystem) {
						subActiveXML = activeversions[i];
						break;
					}
				} catch (objException) {
							// trap; try next one
				}
			}
		}
	}

	/**
	 * @method subProcessSearch
	 * @param url - String
	 * @desc processes a url using xml http request
	 */
	function subProcessSearch(url) {
		try {
			subSearchedAlready 	= false;
			if ( subActiveXML ) {
				subSearchSystem.onreadystatechange = doSubSearchProcess;
				subSearchSystem.open("GET", url, true);
				subSearchSystem.send(null);
			} else {
				if (subSearchSystem) {
					subSearchSystem.onreadystatechange = doSubSearchProcess;
					subSearchSystem.open("GET", url, true);
					subSearchSystem.send(null);
				}
			}
		} catch ( otherException ) {}
	}

	/**
	 * @method doSubSearchProcess
	 * @desc the ajax readystate change function. Process the DOM and then push the contents out to the browser
	 */
	function doSubSearchProcess() {
		try {
			if (subSearchSystem.readyState == 4) {
				if (subSearchSystem.status == 200) {
					var subresponse = subSearchSystem.responseXML.documentElement;
					if ( subresponse ) {
						var subs = document.getElementById('subsearchkey');
						var subdata = document.getElementById("subresultstack");
						var subcontent = "";
						var subitems = subresponse.getElementsByTagName("i");
						subcontent += '<table bgcolor="#ffffff" width="220" border="0" cellspacing="0" cellpadding="0">';
						if ( subitems.length > 0 ) {
							for ( var i = 0 ; i < subitems.length; i++ )
							{
								var subdataname = subitems[i].getAttributeNode("name").nodeValue;
								var subd = subdataname.substring(0,30);
								if ( subdataname.length > 30 ) {
									subd+="...";
								}
								subcontent += "<tr><td colspan=\"2\" bgcolor=\"#003366\"><img src=\"/LTtemplates/template_images/londontown.trans.gif\" width=\"10\" height=\"1\"></td></tr><tr bgcolor=\"#003366\"><td style=\"cursor:pointer\" onClick=\"javascript:location.href='/betasearch/index-test.php?key="+subdataname+"'\" onMouseOver=\"this.bgColor='#18558A'\" onMouseOut=\"this.bgColor='#437CB0'\" width=\"209\" bgcolor=\"#437CB0\">  <a href=\"/betasearch/index-test.php?key="+subdataname+"\" class=\"tail_white9pxbold\">"+subd+"</a>&nbsp;</td><td><img name=\"Home\" border=\"0\" src=\"/LTtemplates/template_images/arrow.unhighlight.gif\" width=\"13\" height=\"19\"></td></tr>"
							}
							if ( subitems.length < 1 ) {
								subcontent += '<tr><td colspan="2" class="resultname">No results found for this</td></tr>';
								subdata.style.display = 'none';
							}
						}
						subcontent += "<tr><td colspan=\"2\" bgcolor=\"#003366\"><img src=\"/LTtemplates/template_images/londontown.trans.gif\" width=\"10\" height=\"1\"></td></tr><tr bgcolor=\"#003366\"><td style=\"cursor:pointer\" onClick=\"javascript:location.href='/betasearch/index-test.php?key="+subs.value+"'\"  bgcolor=\"#003366\">  <a href=\"/betasearch/index-test.php?key="+subs.value+"\" class=\"tail_white9pxbold\">All results page</a>&nbsp;</td><td><img name=\"Home\" border=\"0\" src=\"/LTtemplates/template_images/arrow.unhighlight.gif\" width=\"13\" height=\"19\"></td></tr>";
						subcontent += '</table>';
						subdata.innerHTML = subcontent;
					}
				} else {
				}
			}
		} catch ( someException ) {}
	}

	var isSubValSameCookie = getSubCookieValue();


	/**
	 * @method delayedSearch
	 * @desc Do a new search IF the user hasnt pressed the key for 1/2 second, the new key is != to the old one and if a search hasnt already been done
	 */
	function subDelayedSearch() {
		subNewMilli = getSubMilliSeconds();
		var subobj = document.getElementById('subsearchkey');
		var subval = subobj.value;
		if ( ((subNewMilli-subOldMilli)>500) && (subval != subOldkey) && (subval!=isSubValSameCookie) && (subval != 'undefined')) {
			subfoc();
			document.cookie = "cookme="+subval+"; path=/";
			subProcessSearch('/searchlondon/event.php?key='+subval+"&r="+Math.random());
			subOldkey = subval;
		}

	}

	/**
	 * @method getSubCookie
	 * @desc Assigns an HTML element value to the cookie value
	 */
	function getSubCookie(){
		var subcookVal = getSubCookieValue();
		if ( subcookVal != '' && subcookVal != 'undefined' && subcookVal != null) {
			var subd = document.getElementById("subsearchkey");
			subd.value=subcookVal;
			subProcessSearch('/searchlondon/event.php?key='+subcookVal+"&r="+Math.random());			
		}
	}

	/**
	 * @method getSubCookieValue
	 * @desc Obtains the cookme cookie
	 */
	function getSubCookieValue(){
		var subcookies = document.cookie;
		var suboutseg = subcookies.split(";");
		for ( var i = 0 ; i < suboutseg.length; i++ ) {
			var subinseg = suboutseg[i].split("=");
			//alert(inseg[0]);
			if ( subinseg[0].replace(" ","")=="cooksubme" && subinseg[1] != "undefined") {
				return subinseg[1];
			}
		}
	}


	/* A group of very boring and uninteresting methods for use within this */

	function subfoc(){ if(subFocEnabled){ var v = document.getElementById('subresultstack'); v.style.display = '';} }

	function subfocoff(){ if ( subFocEnabled ) {var v =document.getElementById('subresultstack'); v.style.display = 'none';} }

	function subwait(){ subDelayedSearch(); setTimeout("subwait()",100); }

	function subStartIt(){ subFocEnabled=true; }

	function subStopIt(){ subFocEnabled=false; }

	function getSubMilliSeconds() { return Date.parse(new Date()); }

	function keySubWasPressed(){ subOldMilli = getSubMilliSeconds(); }






