// Global JS
// Created by DCW (7-31-08)

/* Primary/secondary Nav specific code */
var hdNav={myOffset:[],mySec:[],myPrimary:[],mySecOffset:[],myCount:function(){var a=document.getElementById("hdNavTable").getElementsByTagName("td").length-1;var b=0;var c=0;for(i=0;i<=a;i++){this.myOffset[i]=b;b=b+0+document.getElementById("hdNavTable").getElementsByTagName("td")[i].offsetWidth;this.mySec[i]=document.getElementById('hdMI'+i).offsetWidth;this.myPrimary[i]=document.getElementById("hdNavTable").getElementsByTagName("td")[i].offsetWidth;if(this.myPrimary[i]>=this.mySec[i]){d=this.myPrimary[i];document.getElementById('hdMI'+i).style.width=d+"px";c=(this.myOffset[i]+((this.myPrimary[i]-d)/2))-12;}else if(this.mySec[i]>this.myPrimary[i]){c=(this.myOffset[i]-((this.mySec[i]-this.myPrimary[i])/2));}
this.mySecOffset[i]=c;if(i==0){document.getElementById('hdMI'+i).style.left="0px";}else if(i==a){document.getElementById('hdMI'+i).style.left=(920-this.mySec[i])+"px";}else{document.getElementById('hdMI'+i).style.left=c+"px";}}},show:function(){obj=document.getElementById("hdHatSignIn");obj2=document.getElementById("hdProducts");obj3=document.getElementById("hdProducts2");if(obj.className=='displayBlock'||obj.className==''){obj.className='displayNone';obj2.className='displayNone';obj3.className='displayBlock';}else{obj.className='displayBlock';obj3.className='displayNone';obj2.className='displayBlock';}},up:function(myNumber){MyLocationClose('whatwhere');if(hdNav.myOffset==""){hdNav.myCount();hdNav.myCount();}
var a=document.getElementById("hdNavTable").getElementsByTagName("td").length-1;var b=0;var c=0;for(i=0;i<=a;i++){this.down(i);}
document.getElementById('hdMI'+myNumber).className='navSecond navSecondUp';if(hbBMDefault==('hdM'+myNumber)){document.getElementById('hdM'+myNumber).className='navSelected';}else{document.getElementById('hdM'+myNumber).className='navHovered';}},down:function(myNumber){if(hbBMDefault==('hdM'+myNumber)){document.getElementById('hdM'+myNumber).className='navSelected';}else{document.getElementById('hdM'+myNumber).className='navNotSelected';}
document.getElementById('hdMI'+myNumber).className='navSecond navSecondDown';}};
/* End Primary/secondary Nav specific code  */


/***** Begin TypeAhead Handling Functions Specific ****/
var luceneLocId="";var luceneLocType="";var locName="";var countryName = "";

function setRecentSearch(locId,locType,presentationName,from){
			//alert("Quick Debug For Daniel: "+locId+", "+locType+", "+presentationName+", "+ from+", "+ countryName);
			if(from=='search_travel'){
				setUserPreferences(16,'Travel');
			}else if(from=='search_weddings'){
				setUserPreferences(16,'Weddings');
			}
			getRecentSearch=getUserPreferences('27');
			if(getRecentSearch==''){
				if(countryName == "UNITED STATES") {
					setUserPreferences('27',locId+':'+locType+':'+presentationName);
				} else {
					var temp = locName.split(",");
					setUserPreferences('27',locId+':'+locType+':'+temp[0]+", "+countryName);
				}
			}else{
				alreadyExist=false;
				var splitUpSearches=getRecentSearch.split("^");
			for(var i=0;i<splitUpSearches.length;i++){
				var splitEachSearch=splitUpSearches[i].split(":");
				if(splitEachSearch[0]==locId){
					alreadyExist=true;
				}
			}
			if(!alreadyExist){
				if(splitUpSearches.length==6){
					tempRecentSearch=getRecentSearch.split("^").splice(0,5);
					var buff="";
					for(var i=0;i<tempRecentSearch.length;i++){
						if(i==0){
							buff+=tempRecentSearch[i];
						}else{
							buff+="^"+tempRecentSearch[i];
						}
					}
				if(countryName == "UNITED STATES") {
					setUserPreferences('27',locId+':'+locType+':'+presentationName+'^'+buff);
				} else {
					var temp = locName.split(",");
					setUserPreferences('27',locId+':'+locType+':'+temp[0]+", "+countryName+'^'+buff);
				}
			}else{
				if(countryName == "UNITED STATES") {
					setUserPreferences('27',locId+':'+locType+':'+presentationName+'^'+getRecentSearch);
				} else {
					var temp = locName.split(",");
					setUserPreferences('27',locId+':'+locType+':'+temp[0]+", "+countryName+'^'+getRecentSearch);
				}
			}
		}
	}
}
function addLocIDLocTypeToSearch(locID,locType,lName, countryN){
	luceneLocId=locID;
	luceneLocType=locType;
	locName=lName;
	countryName = countryN;
	checkFormForLucene();
}
//Added to override the sendQuery Function

function isZipGood(s) {
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

     if (!reZip.test(s)) {
          //alert("Zip Code Is Not Valid");
          return false;
     }

return true;
}


function checkFormForLucene() {
	document.location = "/search/enhancedlocalsearch?whatprefs=&what=Weather36HourBusinessTravelerCommand&lswe="+stringFilterChecker(document.whatwhere.where.value)+"&lswa=Weather36HourBusinessTravelerCommand&loctypes=1003%2C1001%2C1000%2C1%2C9%2C5%2C11%2C13%2C19%2C20&from=searchbox_localwx&googleTypeSearch=on&where="+stringFilterChecker(document.whatwhere.where.value)+"&Search.x=0&Search.y=0&Search=Search";
	return false;
}
/*
function checkFormForLuceneOld(){
	
	var WWW_host=window.location.hostname;
	if(WWW_host!='delta.weather.com'&&WWW_host!='w3.weather.com'&&WWW_host!='beta.weather.com') {
		WWW_host='www.weather.com';
		WWW_host='http://'+WWW_host;
		document.whatwhere.action=WWW_host+"/search/enhancedlocalsearch";
		if(document.whatwhere.where.value=="Enter city or US zip") {
			return false;
		} else if(document.whatwhere.whatprefs.selectedIndex>0) {
			var thisVal=document.whatwhere.whatprefs.options[document.whatwhere.whatprefs.selectedIndex].value;
			document.whatwhere.what.value=thisVal;
			var retVal=changeCommandToVal(thisVal);
			setUserPreferences('16',retVal);
			document.whatwhere.from.value="whatwhere_drop";
		} else if(getUserPreferences('16')&&getUserPreferences('16').length>1) {
			var thisVal=getUserPreferences('16');
			var retVal=changeValToCommand(thisVal);
			document.whatwhere.what.value=retVal;
		} else {
			document.whatwhere.what.value='WeatherLocalUndeclared';
		}
	}
	whatWhereValue=stringFilterChecker(document.whatwhere.where.value);
	whatWhereValue=recognizeZipCodeForLucene(whatWhereValue);
	document.whatwhere.where.value=whatWhereValue;
	document.whatwhere.lswa.value=document.whatwhere.what.value;
	document.whatwhere.lswe.value=document.whatwhere.where.value;
	if(isZipGood(whatWhereValue)) {
			setRecentSearchNew(whatWhereValue, 1, whatWhereValue, '','');
	}
	
	//setRecentSearchNew(whatWhereValue, 1, whatWhereValue, '','');
	if(luceneLocId!=""&&luceneLocType!="") {
		tempLoc=locName.split(', ');
		t=tempLoc[0].replace(" ","_").toLowerCase();
		
		if(luceneLocType==1) {
			
			
			//setRecentSearch(luceneLocId,luceneLocType,locName,'');
			var lp="/weather/local/";
			if(wh=='Weather36HourAchesAndPainsCommand') {
				lp="/outlook/health/achesandpains/local/";
			} else if(wh=='Weather36HourAllergiesCommand') {
				lp="/outlook/health/allergies/local/";
			} else if(wh=='Weather36HourAirQualityCommand') {
				lp="/outlook/health/airquality/local/";
			} else if(wh=='Weather36HourSunSafetyCommand') {
				lp="/outlook/health/skin/local/";
			} else if(wh=='Weather36HourColdAndFluCommand') {
				lp="/outlook/health/coldandflu/local/";
			} else if(wh=='Weather36HourHomeCommand') {
				lp="/outlook/homeandgarden/home/local/";
			} else if(wh=='Weather36HourGardenCommand') {
				lp="/outlook/homeandgarden/garden/local/";
			} else if(wh=='Weather36HourSchooldayCommand') {
				lp="/outlook/homeandgarden/schoolday/local/";
			} else if(wh=='Weather36HourSportsCommand') {
				lp="/outlook/events/sports/local/";
			} else if(wh=='Weather36HourWeddingCommand') {
				lp="/outlook/events/weddings/local/";
			} else if(wh=='Weather36HourInterstateCommand') {
				lp="/outlook/driving/interstate/local/";
			} else if(wh=='Weather36HourBoatAndBeachCommand') {
				lp="/outlook/recreation/boatandbeach/local/";
			} else if(wh=='Weather36HourGolfCommand') {
				lp="/outlook/recreation/golf/local/";
			} else if(wh=='Weather36HourSkiCommand') {
				lp="/outlook/recreation/ski/local/";
			} else if(wh=='Weather36HourBusinessTravelerCommand') {
				lp="/outlook/travel/businesstraveler/local/";
			} else if(wh=='Weather36HourVacationPlannerCommand') {
				lp="/outlook/travel/vacationplanner/local/";
			} else if(wh=='Weather36HourOutdoorsCommand') {
				lp="/outlook/recreation/outdoors/local/";
			} else if(wh=='Weather36HourFitnessCommand') {
				lp="/outlook/health/fitness/local/";
			} else if(wh=='Weather36HourHealthCommand') {
				lp="/outlook/health/general/local/";
			} else if(wh=='Weather36HourPetsCommand') {
				lp="/outlook/homeandgarden/pets/local/";
			}
		//alert("CheckForm" + lp);
		document.location=WWW_host+lp+luceneLocId+"?lswe="+document.whatwhere.lswe.value+"&lwsa="+document.whatwhere.lswa.value+"&from=searchbox_typeahead";
		return false;
	}

else if(luceneLocType==5){document.location="/outlook/recreation/golf/weather/weekend/?clubId="+luceneLocId+"&from=searchbox_typeahead&lswe="+document.whatwhere.lswe.value;
return false;}
else if(luceneLocType==9){document.location="/outlook/travel/businesstraveler/airportprofile/"+luceneLocId+"?from=searchbox_typeahead&lswe="+document.whatwhere.lswe.value;return false;}
else if(luceneLocType==11){document.location="/outlook/recreation/ski/weather/tenday/"+luceneLocId+"?from=searchbox_typeahead&lswe="+document.whatwhere.lswe.value;return false;}
else if(luceneLocType==17){return false;}else if(luceneLocType==1000){document.location="/outlook/weatherbystate/"+t+"?from=searchbox_typeahead&lswe="+document.whatwhere.lswe.value;return false;}
else if(luceneLocType==1001){document.location="/outlook/weatherbycountry/"+t+"?from=searchbox_typeahead&lswe="+document.whatwhere.lswe.value;return false;}
else if(luceneLocType==1002){document.location="/common/drilldown/"+luceneLocId+".html?from=searchbox_typeahead&lswe="+document.whatwhere.lswe.value;return false;}
else{return true;}
}
else{if(document.whatwhere.where.value.length==5&&document.whatwhere.where.value>0&&document.whatwhere.where.value<100000){var lp="/weather/local/";var wh=document.whatwhere.what.value;if(document.whatwhere.from.value=='whatwhere'||document.whatwhere.from.value=='whatwhere_drop'||document.whatwhere.from.value=='searchbox'){updateRecentSearch(document.whatwhere.where.value);}
if(wh=='Weather36HourAchesAndPainsCommand')lp="/outlook/health/achesandpains/local/";else if(wh=='Weather36HourAllergiesCommand')lp="/outlook/health/allergies/local/";else if(wh=='Weather36HourAirQualityCommand')lp="/outlook/health/airquality/local/";else if(wh=='Weather36HourSunSafetyCommand')lp="/outlook/health/skin/local/";else if(wh=='Weather36HourColdAndFluCommand')lp="/outlook/health/coldandflu/local/";else if(wh=='Weather36HourHomeCommand')lp="/outlook/homeandgarden/home/local/";else if(wh=='Weather36HourGardenCommand')lp="/outlook/homeandgarden/garden/local/";else if(wh=='Weather36HourSchooldayCommand')lp="/outlook/homeandgarden/schoolday/local/";else if(wh=='Weather36HourSportsCommand')lp="/outlook/events/sports/local/";else if(wh=='Weather36HourWeddingCommand')lp="/outlook/events/weddings/local/";else if(wh=='Weather36HourInterstateCommand')lp="/outlook/driving/interstate/local/";else if(wh=='Weather36HourBoatAndBeachCommand')lp="/outlook/recreation/boatandbeach/local/";else if(wh=='Weather36HourGolfCommand')lp="/outlook/recreation/golf/local/";else if(wh=='Weather36HourSkiCommand')lp="/outlook/recreation/ski/local/";else if(wh=='Weather36HourBusinessTravelerCommand')lp="/outlook/travel/businesstraveler/local/";else if(wh=='Weather36HourVacationPlannerCommand')lp="/outlook/travel/vacationplanner/local/";else if(wh=='Weather36HourOutdoorsCommand')lp="/outlook/recreation/outdoors/local/";else if(wh=='Weather36HourFitnessCommand')lp="/outlook/health/fitness/local/";else if(wh=='Weather36HourHealthCommand')lp="/outlook/health/general/local/";else if(wh=='Weather36HourPetsCommand')lp="/outlook/homeandgarden/pets/local/";document.location=WWW_host+lp+document.whatwhere.where.value+"?lswe="+document.whatwhere.lswe.value+"&lwsa="+document.whatwhere.lswa.value+"&from="+document.whatwhere.from.value;return false;}else{return true;}}}
*/

function recognizeZipCodeForLucene(search) {
	filteredNumbers="1234567890";
	var i;
	var returnString="";
	for(i=0;i<search.length;i++) {
		var c=search.charAt(i);
		for(j=0;j<filteredNumbers.length;j++) {
			if(c==filteredNumbers.charAt(j))returnString+=c;}
		}
	if(returnString.length!=5) {
		returnString=search;
	}
	return returnString;
}


/***** End TypeAhead Handling Specific Functions *****/
