 var showTypeAhead = true;
 var resetForm = true;
 var doTypeAheadEven = true;

	function buildSearchInputDisplayHeader(wRIn) {
		if(document.getElementById('whatwhereForm2').value == "Enter ZIP or US / world city" || document.getElementById('whatwhereForm2').value == "Enter words or phrase") {	
			if(wRIn=="local") {
				document.getElementById('whatwhereForm2').value = "Enter ZIP or US / world city";
				showTypeAhead = true;
			} else if(wRIn=="site") {
				document.getElementById('whatwhereForm2').value = "Enter words or phrase";
				showTypeAhead = false;
			} else if(wRIn=="web") {
				document.getElementById('whatwhereForm2').value = "Enter words or phrase";
				showTypeAhead = false;
			}
			resetForm = true;
		} else {
			if(wRIn=="local") {
				showTypeAhead = true;
			} else if(wRIn=="site") {
				showTypeAhead = false;
			} else if(wRIn=="web") {
				showTypeAhead = false;
			}
			resetForm = true;
		}
	}
	
	
	
	
 	function preCheckForm() {
		var myForm = document.whatwhere;
		if(myForm.googleTypeSearch[0].checked) {
			doTypeAheadEven = true;
			return checkFormForLucene();
		} else if(myForm.googleTypeSearch[1].checked) {
			var inputKey = myForm.where.value;
			doTypeAheadEven = false;
			document.location = "/search/sitesearch?Keywords="+encodeKeywords(inputKey)+"&start=0&num=10&twx=on&hl=en&type=site&from=searchbox_site&channel="+ts_level1+"";
			return false;
		} else if(myForm.googleTypeSearch[2].checked) {
			var inputKey = myForm.where.value;
			doTypeAheadEven = false;
			document.location = "/search/websearch?Keywords="+encodeKeywords(inputKey)+"&start=0&num=10&twx=on&hl=en&type=web&from=searchbox_web&channel="+ts_level1+"";
			return false;
		}
		return false;
	}
	
	function encodeKeywords(keyIn) {
		keyIn = keyIn.replace("&","%26");
		keyIn = keyIn.replace("+","%2B");
		keyIn = keyIn.replace("-","%2D");
		keyIn = keyIn.replace("\"","%22");
		keyIn = keyIn.replace(" ","+");
		keyIn = stringFilterCheckerGoogle(keyIn);
		return keyIn;
	}
	
	function stringFilterCheckerGoogle(s) {
		filteredValues="*|/\"<>[]{}`\;()@&$#";
		var i;
		var returnString="";
		for(i=0;i<s.length;i++) { 
			var c=s.charAt(i);
			if(filteredValues.indexOf(c)==-1) 
			returnString+=c;
		}
		while(returnString.substring(0,1)==' ') returnString=returnString.substring(1,returnString.length);
		while(returnString.substring(returnString.length-1,returnString.length)==' ') returnString=returnString.substring(0,returnString.length-1);
		
		return returnString;
	}
	
	function preFormInputHeader() {
		if(resetForm) {
			var myForm = document.whatwhere;
			if(myForm.googleTypeSearch[0].checked)  {
				document.getElementById('whatwhereForm2').value= "";
				MyLocation();
				showRecentSearchr();
			} else {
				document.getElementById('whatwhereForm2').value= "";
			}
			resetForm = false;
		}
	}



/*********/


YAHOO.namespace("yuibook.autoComp");
		YAHOO.yuibook.autoComp.initAutoComp = function() {
			try {
			var schema = ["results", "pn", "st", "cy", "id", "lt"];
			var dataSource = new YAHOO.widget.DS_ScriptNode("http://www.weather.com/search/typeahead?tr=350&ta=", schema);
			dataSource.scriptCallbackParam = "cb";
			dataSource.scriptQueryParam = "sr";
			dataSource.maxCacheEntries = 0;
			dataSource.queryMatchSubset = true;
			var autoComp = new YAHOO.widget.AutoComplete("whatwhereForm2","suggestionContainer",dataSource);
			} catch(Exception) {}
			autoComp.formatResult = function(aResultItem, sQuery) {
			try {
				
				
				if (aResultItem[0]) {	
						document.getElementById('whatwherezip').style.display = 'none';
						searchToggle(0);
						document.getElementById('RecentResult').style.display = 'none';
						if(showTypeAhead) {
							document.getElementById('suggestionContainer').style.display = 'block';
						} else {
							document.getElementById('suggestionContainer').style.display = 'none';
						}
						if(aResultItem[4]=="1") {
							if(aResultItem[2]=="UNITED STATES") {
								return "<div class='location' id='typeAhead' style='display:block'><span>"+strTrim(aResultItem[0]+((aResultItem[1]!='')?", "+aResultItem[1]:"")+((aResultItem[2]!='')?", "+takeAwayCaps(aResultItem[2]):""))+"</span></div>";
							} else {
								/***  Added Hack to handle for non-working locations that show up in Typeahead  ***/
								if(aResultItem[2]=="Mexico" || aResultItem[2]=="MEXICO") {
									if(aResultItem[0].toLowerCase()=="san juan") {
										return "<div class='location' id='typeAhead' style='display:block'><span></span></div>";	
									} else {
										return "<div class='location' id='typeAhead' style='display:block'><span>"+strTrim(aResultItem[0]+", "+takeAwayCaps(aResultItem[2]))+"</span></div>";
									}
								} else {
									/*** End Hack ***/
									return "<div class='location' id='typeAhead' style='display:block'><span>"+strTrim(aResultItem[0]+", "+takeAwayCaps(aResultItem[2]))+"</span></div>";
								}
							}
						} else if(aResultItem[4]=="9") {
							return "<div class='location' id='typeAhead' style='display:block'><span>"+strTrim(aResultItem[3]+", "+aResultItem[0]+((aResultItem[1]!='')?", "+aResultItem[1]:((aResultItem[2]!='')?", "+aResultItem[2]:"")))+"</span></div>";
						} else if(aResultItem[4]=="11"){
							if(aResultItem[2]=="UNITED STATES") {
								return "<div class='location' id='typeAhead' style='display:block'><span>"+strTrim(aResultItem[0]+((aResultItem[1]!='')?", "+takeAwayCaps(aResultItem[1]):""))+"</span></div>";						
							} else {
								return "<div class='location' id='typeAhead' style='display:block'><span>"+strTrim(aResultItem[0]+", "+takeAwayCaps(aResultItem[2]))+"</span></div>";												
							}
						} else {
							return "<div class='location' id='typeAhead' style='display:block'><span>"+strTrim(aResultItem[0]+((aResultItem[1]!='')?", "+takeAwayCaps(aResultItem[1]):"")+((aResultItem[2]!='')?", "+takeAwayCaps(aResultItem[2]):""))+"</span></div>";											
						}
				} else {
					return "";
				
				}
			} catch(Exception){}
			};
			autoComp.allowBrowserAutocomplete = false;
			autoComp.maxResultsDisplayed = 7;
			autoComp.minQueryLength = 2;
			autoComp.queryDelay = 0.2;
			autoComp.useIFrame = true;
			autoComp.typeAhead = false;
			autoComp.autoHighlight = false;
			autoComp.animVert = false;
			//Overwritten function from Yahoo Library to fix 
			autoComp._selectItem = function(oItem) {
			   
					this._bItemSelected = true;
				    this._updateValue(oItem);
				    this._cancelIntervalDetection(this);
				    this.itemSelectEvent.fire(this, oItem, oItem._oResultData);
				    this._toggleContainer(false);
					
			
					if(oItem._oResultData[2]=="UNITED STATES") document.whatwhere.where.value = ""+oItem._oResultData[0]+", "+oItem._oResultData[1];
					else document.whatwhere.where.value = ""+oItem._oResultData[0]+", "+oItem._oResultData[2];
					
			  try {
					addLocIDLocTypeToSearch(oItem._oResultData[3], oItem._oResultData[4], oItem._oResultData[0]+", "+oItem._oResultData[1], oItem._oResultData[2]);
				} catch(Exception) {}
			};

			
			autoComp.doBeforeSendQuery = function(inCom) {
				if(showTypeAhead) {
					return inCom;
				} else {
					return "";
				}
			}  
			

autoComp._sendQuery = function(sQuery) {
    // Widget has been effectively turned off
    if(this.minQueryLength == -1) {
        this._toggleContainer(false);
        return;
    }
    // Delimiter has been enabled
    var aDelimChar = (this.delimChar) ? this.delimChar : null;
    if(aDelimChar) {
        // Loop through all possible delimiters and find the latest one
        // A " " may be a false positive if they are defined as delimiters AND
        // are used to separate delimited queries
        var nDelimIndex = -1;
        for(var i = aDelimChar.length-1; i >= 0; i--) {
            var nNewIndex = sQuery.lastIndexOf(aDelimChar[i]);
            if(nNewIndex > nDelimIndex) {
                nDelimIndex = nNewIndex;
            }
        }
        // If we think the last delimiter is a space (" "), make sure it is NOT
        // a false positive by also checking the char directly before it
        if(aDelimChar[i] == " ") {
            for (var j = aDelimChar.length-1; j >= 0; j--) {
                if(sQuery[nDelimIndex - 1] == aDelimChar[j]) {
                    nDelimIndex--;
                    break;
                }
            }
        }
        // A delimiter has been found so extract the latest query
        if(nDelimIndex > -1) {
            var nQueryStart = nDelimIndex + 1;
            // Trim any white space from the beginning...
            while(sQuery.charAt(nQueryStart) == " ") {
                nQueryStart += 1;
            }
            // ...and save the rest of the string for later
            this._sSavedQuery = sQuery.substring(0,nQueryStart);
            // Here is the query itself
            sQuery = sQuery.substr(nQueryStart);
        }
        else if(sQuery.indexOf(this._sSavedQuery) < 0){
            this._sSavedQuery = null;
        }
    }

    // Don't search queries that are too short
    if((sQuery && (sQuery.length < this.minQueryLength)) || (!sQuery && this.minQueryLength > 0)) {
        if(this._nDelayID != -1) {
            clearTimeout(this._nDelayID);
        }
        this._toggleContainer(false);
        return;
    }
		if(sQuery.indexOf('3')!=-1||sQuery.indexOf('2')!=-1||sQuery.indexOf('1')!=-1||sQuery.indexOf('0')!=-1
		||sQuery.indexOf('4')!=-1||sQuery.indexOf('5')!=-1||sQuery.indexOf('6')!=-1||sQuery.indexOf('7')!=-1||
		sQuery.indexOf('8')!=-1||sQuery.indexOf('9')!=-1){
		} else {
			sQuery = encodeURIComponent(sQuery);
		    this._nDelayID = -1;    // Reset timeout ID because request has been made
    		sQuery = this.doBeforeSendQuery(sQuery);
   			this.dataRequestEvent.fire(this, sQuery);
    		this.dataSource.getResults(this._populateList, sQuery, this);
		}
};

	
			
			//autoComp.useShadow = false;
			
			//autoComp.setHeader("Suggestions");
			//autoComp.alwaysShowContainer = true;
		};

	        YAHOO.util.Event.onDOMReady(YAHOO.yuibook.autoComp.initAutoComp);
		//Function that trims result text in site search results container
		function strTrim(input) {
			if(input.length > 30) return input.substring(0,27)+'...';
			else return input;
		}
		//Toggle with the horizontal toolbar (disable , enable) when search results container is visible
		function toolBarToggle(swVal) {
			for(var x = 2;x < 6;x++)
				document.getElementById('hdM'+x).disabled = swVal;
		}
      	//Search results container control to show or hide container
		function searchToggle(str){
			var suggestContainer = document.getElementById('suggestionContainer');
			if(str === 1){
				suggestContainer.style.display = 'none';
			} else {
				suggestContainer.style.display = 'block';
			}
		}
		
		function takeAwayCaps(input) {
			//Find out how many words the country is
			//if one word split like normal
			var splitC = input.split(" ");
			if(splitC.length == 1) {
				firstLetter = splitC[0].substring(0,1).toUpperCase();
				restWord = splitC[0].substring(1).toLowerCase();
				return firstLetter+restWord;
			} else {
				var finalWord = "";
				for(i = 0; i < splitC.length; i++){
					firstLetter = splitC[i].substring(0,1).toUpperCase();
					restWord = splitC[i].substring(1).toLowerCase();
					finalWord = finalWord + firstLetter+restWord + " ";
				}
				return finalWord;
			}
		}
		
		function showRecentSearchr() {
			if(document.getElementById('suggestionContainer').style.display == 'none') {
				document.getElementById('whatwherezip').style.display = 'block';
			}else {
				//document.getElementById('whatwherezip').style.display = 'none';
			}
		}

	

	     /* searchType variable is used to control the output of the search recent container.  This variable can be removed once integrated with server-side jsp file results.  This was just used for display purposes during HTML testing.  I added a conditions to the siteSearch() function that can be removed and replaced with the jsp results when the page loads.
		*/
		var searchType = 2;
		function siteSearch(){
        	//toolBarToggle(true);
			var suggestContainer = document.getElementById('suggestionContainer');
			suggestContainer.style.display = 'block';
			document.getElementById('RecentResult').style.display = 'block';
			document.getElementById('whatwhereForm').value = '';
        	var result = '<div class="rec-ac-content" style="display: block;">';
        	result += '<div class="yui-ac-ft" style="display: none;"/></div>';
     	  	document.getElementById('RecentResult').innerHTML = result;
        }