var zipsearch = false;//this is for checking if local rearch was already clicked 
function MyLocation(){
 document.getElementById("whatwherezip").className = 'whatwhereZipClass';
	if(!zipsearch){
	 document.whatwhere.where.value = '';
	 zipsearch = true;
  	} 
}

function MyLocationClose(dn){
	document.getElementById(dn+"zip").className = 'whatwhereZipClassHidden';
}

function determineDrilldownKey(presentationName) {
	var key = presentationName.split(",")[0].toLowerCase();
	key = key.replace(/ /g, "_");
	return key;
}

function determineLocationUrl(locid, loctype, presentationName) {
	var url;
	 switch(loctype) {
  	case "1":
  		url = "/weather/today/" + locid;
  		break;
  	case "9":
  		url = "/outlook/travel/businesstraveler/airportprofile/" + locid + ":" + loctype;
  		break;
  	case "5":
  		url = "/outlook/recreation/golf/weather/weekend/?clubId=" + locid;
  		break;
  	case "1003":
  	  url = "/outlook/weatherbyregion/" + locid;
  		break;
  	case "1001":
  		var countryKey = determineDrilldownKey(presentationName);
  		if (presentationName == "US") {
  			url = "/outlook/weatherbystate/";
  		} else {
  			url = "/outlook/weatherbycountry/" + countryKey;
  		}
  		break;
  	case "1000":
  		var stateKey = determineDrilldownKey(presentationName);
  		url = "/outlook/weatherbystate/" + stateKey;
  		break;
  	default:
  	 	url = "/weather/today/" + locid + ":" + loctype;
  	 	break;
    } 
	return url;
} //end determineUrl

function shortPN(pn) {
	// limit character count
	try {
	if(pn.length > 35) {
		pn = pn.substring(0,35);
		pn += "...";
	}
	} catch(Exception) { pn=null;}
	return pn;
}

//Recnt Search Example
//USGA0028:1:Atlanta, GA^12345:4:Schenectady,NY(12345)

//MyPrefs Example
//12345^4^Schenectady,+NY+(12345)^|*|*|*
var getMyPrefs = GetCookie("MyPrefs");
var getRecentLocations = getUserPreferences("27");

//Remove invalid elements, if you have an invalid zip, you will have element like 12000:*:*
var splitUpSearches = getRecentLocations.split("^");
for (var i=0;i<splitUpSearches.length;i++) {
	var search = splitUpSearches[i].split(":");
	if (search[1]=="*" && search[2]=="*") {
		//remove that element so we can update the userpreferences cookie finally
		splitUpSearches.splice(i,1);			
	}
}

if (splitUpSearches.length>0) {
	splitUpSearches=splitUpSearches.join("^");
	setUserPreferences('27',splitUpSearches);
}
else{
	setUserPreferences('27','');
}

getRecentLocations = getUserPreferences("27");

var getMyCityFromPrefs = "";
var getMyWatchListFromPrefs = "";
var watchListName = "";
var myWatchlist ="";
var myCity="";
var hasMyCity = false;
var hasMyWatchlist = false;
var hasRecentSearches = false;
var splitUpRecent = "";

if(getRecentLocations.length > 1){
	hasRecentSearches = true;
	splitUpRecent = getRecentLocations.split("^");
}

if(getMyPrefs.length > 1) {
	hasMyCity =  true;
	getMyCityFromPrefs = getMyPrefs.split("|").splice(0,1);
	myCity = unEscJava(getMyCityFromPrefs[0]);
	if(myCity.indexOf("\"") == 0) {
		myCity = myCity.substr(1);
	}
	getMyWatchListFromPrefs = getMyPrefs.split("|").splice(3,4);
	myWatchlist = unEscJava(getMyWatchListFromPrefs[0]);
}



//Watchlist Example
//12345:USFL0481:*:*:*:testing+this+thing:busch+gardens:*:*:*:Schenectady,+NY(12345):Tampa,+FL:*:*:*
if(myWatchlist != '' && myWatchlist != '*'){
	watchListCk = myWatchlist.split(":");
	hasMyWatchlist = true;		
}


function remove_recent_items(rsv,mcv,mwv){
	//rsv = Recent Search Value; mcv = My City Value; mwv = My Watchlist Value
	counter = 6;
	if(hasMyCity){
		//Begin mycity information	
		var myCityItem = mcv.split("^");
		var myCityId = myCityItem[0];	
		var myCityPresName = myCityItem[2];	
		if (myCityPresName.indexOf(":")!=-1){
			hasMyCity=false;
			hasMyWatchlist=false;
		}
	} 
	
	if(hasRecentSearches && hasMyCity && !hasMyWatchlist){ 
		//This is the recent search check
		for (var i=0;i<rsv.length;i++){
			var rsItem = rsv[i].split(":");
			var rsId = rsItem[0];
			if(rsId == myCityId){
				rsv.splice(i,1);
			}
		}
		if(rsv.length >= 5){
			//remove the last one
			rsv.splice(5,1);
		}
	}
	
	if(hasRecentSearches && hasMyWatchlist){
		var mwItem = mwv.split(":");
		wListCounter = 0;
		tempValue = "";
		//This is the watchlist check
		for (var m=0;m<5;m++){
			if(mwItem[m] != '*'){
				watchListPresName = mwItem[m+counter];
				if(typeof(watchListPresName)!='undefined' && watchListPresName != '*') { wListCounter++; }
			}
		}//end watchlist loop
		//This is the recent search check
		for (var j=0;j<rsv.length;j++){
			var rsItem = rsv[j].split(":");
			//compare rsv with mycity
			var rsId = rsItem[0];
			if(rsId == myCityId) { rsv.splice(j,1); }
		}
	
		//compare watchlist  with rsv
		for (var j=0;j<rsv.length;j++){
			var rsItem = rsv[j].split(":");	
			var rsPresName = rsItem[2];
			for (var k=0;k<5;k++){
				if(mwItem[k] != '*'){
					watchListPresName = mwItem[k+counter];
					if((typeof(watchListPresName)!='undefined' && watchListPresName.replace("--",",") == rsPresName) || 
					(typeof(mwItem[k])!='undefined' && mwItem[k] == rsItem[0])){
						//city check is for international
						rsv.splice(j,1); 
					}				
				}
				tempValue = watchListPresName;
			}//end watchlist loop
		}//end recent search loop
	}//ending the if
}//ends remove_items function

remove_recent_items(splitUpRecent,myCity,myWatchlist);










if(hasRecentSearches && !hasMyCity && !hasMyWatchlist) { //This user only has Recent Searches....
//alert("only recent searches");
	document.write('<DIV class="yui-ac-hd" id="recentSearches" style="display:block;">Recent Searches<!--<SPAN style="padding: 0px 0px 0px 50px;"><IMG src="http://i.imwx.com/web/common/icons/sm_close_box.gif" border="0" onMouseDown="MyLocationClose(\'whatwhere\')"></SPAN>--></DIV>');
	document.write('<div class="yui-ac-bd"><ul class="link" style="padding:0px;margin-top:0px;margin-bottom:0px;margin-left:4px;*margin-left:4px;">');
	try {
	for (var i=0;i<6;i++){
		var splitEachSearch = splitUpRecent[i].split(":");
		var presentationName = splitEachSearch[2];
		if(presentationName != "undefined" || presentationName != "Undefined" || presentationName != null) {
		
			var locationUrl = determineLocationUrl(splitEachSearch[0], splitEachSearch[1],presentationName);
			document.write('<li style="list-style:none;"><a href ="' + locationUrl + '" onmousedown="setClick();" onMouseUp="this.href=intelliTrak({\'href\':this.href,\'from\':\'recentsearch\'});" class="linkVerdanaText10">');
			presentationName = shortPN(presentationName);
			if(presentationName != null) {
						document.write(presentationName);
			}
			document.write('</a></li>');
		}
	}
}catch(Exception){}
	document.write('</ul></div>');
}
else if (hasRecentSearches && hasMyCity && !hasMyWatchlist) { //This user has Recent Searches and My Favorites....
//alert("has recent searches and my favorites");
	if (splitUpRecent.length > 0){
		
		document.write('<DIV class="yui-ac-hd" id="recentSearches" style="display:block;">Recent Searches<!--<SPAN style="padding: 0px 0px 0px 50px;"><IMG src="http://i.imwx.com/web/common/icons/sm_close_box.gif" border="0" onMouseDown="MyLocationClose(\'whatwhere\')"></SPAN>--></DIV>');
		document.write('<div class="yui-ac-bd"><ul class="link" style="padding:0px;list-style:none;margin-top:0px;margin-bottom:0px;margin-left:4px;*margin-left:4px;">');
		for (var i=0;i<splitUpRecent.length && i<5;i++){
				var splitEachSearch = splitUpRecent[i].split(":");
				var presentationName = splitEachSearch[2];
				var locationUrl = determineLocationUrl(splitEachSearch[0], splitEachSearch[1],presentationName);
				if(presentationName != "undefined" || presentationName != "Undefined" || presentationName != null) {
					document.write('<li style="list-style:none;"><a href ="' + locationUrl+'" onmousedown="setClick();" onMouseUp="this.href=intelliTrak({\'href\':this.href,\'from\':\'recentsearch\'});" class="linkVerdanaText10">');
					presentationName = shortPN(presentationName);
					if(presentationName != null) {
						document.write(presentationName);
					}
					document.write('</a></li>');
				}
		}
		document.write('</ul></div>');
	}
	//Begin mycity information
	var splitCityInfo = myCity.split("^");
	document.write('<DIV class="myFavorites">My Favorites</DIV>');
	document.write('<div class="yui-ac-bd"><ul class="link">');
	var locationUrl = determineLocationUrl(splitCityInfo[0], splitCityInfo[1],splitCityInfo[2]);
	document.write('<li><a href ="' + locationUrl+'" onmousedown="setClick();" onMouseUp="this.href=intelliTrak({\'href\':this.href,\'from\':\'recentsearch\'});" class="linkVerdanaText10">');
	var presentationName = splitCityInfo[2].replace("--",",");
	presentationName = shortPN(presentationName);
	if(presentationName != null) {
		document.write(presentationName);
	}
	document.write('</a></li>')
	document.write('</ul></div>');
}
else if (!hasRecentSearches && hasMyCity && hasMyWatchlist) { //This user has My Favorites and My Watchlist....
//alert("has my favorites and my watchlist");
	/*var splitCityInfo = myCity.split("^");
	document.write('<DIV class="myFavorites">My Favorites</DIV>');
	document.write('<div class="yui-ac-bd"><ul class="link">');
	var presentationName = splitCityInfo[2].replace("--",",");
	var locationUrl = determineLocationUrl(splitEachSearch[0], splitEachSearch[1],presentationName);
	document.write('<li><a href ="' + locationUrl+'" onmousedown="setClick();" onMouseUp="this.href=intelliTrak({\'href\':this.href,\'from\':\'recentsearch\'});" class="linkVerdanaText10">');
	presentationName = shortPN(presentationName);
	document.write(presentationName);
	document.write('</a></li>')
	
	//Begin the watchlist information
	var splitWatchlistInfo = myWatchlist.split(":");
	counter = 10;
	document.write('<li><a href ="/weather/today/'+splitCityInfo[0]+'" onmousedown="setClick();" onMouseUp="this.href=intelliTrak({\'href\':this.href,\'from\':\'recentsearch\'});" class="linkVerdanaText10">');
	var presentationName = splitCityInfo[2].replace("--",",");
	presentationName = shortPN(presentationName);
	document.write(presentationName);
	
	document.write('</a></li>')
	document.write('</ul></div>');
*/
}
else if(hasRecentSearches && hasMyCity && hasMyWatchlist) { //This user has Recent Searches, My City and My Watchlist...
//alert("has recent searches, my city, and watchlist");
	try {
		var splitCityInfo = myCity.split("^");
		var watchList=myWatchlist.split(":");
		var myCounter=0;
		for (var i=0;i<5;i++){
			if(watchList[i] != '*' && watchList[i]!='' && typeof(watchList[i])!='undefined' && watchList[i]!=splitCityInfo[0]) { myCounter ++; }
		}
		//add 1 to myCounter for mycity
		myCounter ++;
		if(splitUpRecent.length > 0 && myCounter < 6){
			document.write('<DIV class="yui-ac-hd" id="recentSearches" style="display:block;">Recent Searches<!--<SPAN style="padding: 0px 0px 0px 50px;"><IMG src="http://i.imwx.com/web/common/icons/sm_close_box.gif" border="0" onMouseDown="MyLocationClose(\'whatwhere\')"></SPAN>--></DIV>');
			document.write('<div class="yui-ac-bd"><ul class="link" style="padding:0px;list-style:none;margin-top:0px;margin-bottom:0px;margin-left:4px;">');
			var cntr = 6 - myCounter;
			for (var i=0;i< cntr && typeof(splitUpRecent[i])!='undefined';i++){
				var splitEachSearch = splitUpRecent[i].split(":");
				var presentationName = splitEachSearch[2].replace("--",",");
				document.write('<li style="list-style:none;"><a href ="/weather/today/'+splitEachSearch[0]+'" onmousedown="setClick();" onMouseUp="this.href=intelliTrak({\'href\':this.href,\'from\':\'recentsearch\'});" class="linkVerdanaText10">');
				presentationName = shortPN(presentationName);
				document.write(presentationName);
				document.write('</a></li>');
			}
			document.write('</ul></div>');
		}
		//Begin mycity information
		if(splitUpRecent.length == 0) { document.write('<DIV class="myFavorites">My Favorites</DIV>'); }
		else { document.write('<DIV class="myFavorites">My Favorites</DIV>'); }
		document.write('<div class="yui-ac-bd"><ul class="link">');
		document.write('<li><a href ="/weather/today/'+splitCityInfo[0]+'" onmousedown="setClick();" onMouseUp="this.href=intelliTrak({\'href\':this.href,\'from\':\'recentsearch\'});" class="linkVerdanaText10">');
		var presentationName = splitCityInfo[2].replace("--",",");
		presentationName = shortPN(presentationName);
		document.write(presentationName);
		document.write('</a></li>')
		//Begin the watchlist information CDG
		var splitWatchlistInfo = myWatchlist.split(":");
		counter = 10;
		for (var i=0;i<5;i++){
			if(splitWatchlistInfo[i] != splitCityInfo[0] && splitWatchlistInfo[i] != '*' && splitWatchlistInfo[i] !='' && typeof(splitWatchlistInfo[i])!='undefined' && typeof(splitWatchlistInfo[i+counter])!='undefined'){
				document.write('<li><a href ="/weather/today/'+splitWatchlistInfo[i]+'" onmousedown="setClick();" onMouseUp="this.href=intelliTrak({\'href\':this.href,\'from\':\'recentsearch\'});" class="linkVerdanaText10">');
				watchListName = splitWatchlistInfo[i+counter].replace("--",",");
				if(typeof(watchListName)!='undefined' && watchListName != '' && watchListName.length > 40) {
					watchListName = watchListName.substring(0,40);
					watchListName += "...";
				}
				document.write(watchListName);
				document.write('</a></li>');
			}
		}
		document.write('</ul></div>');
	} catch(Exception) {}
	
}
else if(!hasRecentSearches && hasMyCity && !hasMyWatchlist) { //This user has only has My Favorites.....
//alert("only my favorites");
	try {
		var splitCityInfo = myCity.split("^");
		document.write('<DIV class="myFavorites">My Favorites</DIV>');
		document.write('<div class="yui-ac-bd"><ul class="link">');
		document.write('<li><a href ="/weather/today/'+splitCityInfo[0]+'" onmousedown="setClick();" onMouseUp="this.href=intelliTrak({\'href\':this.href,\'from\':\'recentsearch\'});" class="linkVerdanaText10">');
		var presentationName = splitCityInfo[2].replace("--",",");
		presentationName = shortPN(presentationName);
		document.write(presentationName);
		document.write('</a></li>')
		document.write('</ul></div>');
	} catch(Exception) {}
}




function setRecentSearch(locId,locType,presentationName,from){
			if(from=='search_travel'){
				setUserPreferences(16,'Travel');
			}else if(from=='search_weddings'){
				setUserPreferences(16,'Weddings');
			}
			getRecentSearch=getUserPreferences('27');
			if(getRecentSearch==''){
				if(countryName.toLowerCase() == "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("^");
				alert(splitUpSearches);
			for(var i=0;i<splitUpSearches.length;i++){
				var splitEachSearch=splitUpSearches[i].split(":");
				if(splitEachSearch[0]==locId  && i<=6){
					alreadyExist=true;
				} else if(splitEachSearch[0]==locId && i>=7) {
					
				}
			}
			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.toLowerCase() == "united states") {
					setUserPreferences('27',locId+':'+locType+':'+presentationName+'^'+buff);
				} else {
					var temp = locName.split(",");
					setUserPreferences('27',locId+':'+locType+':'+temp[0]+", "+countryName+'^'+buff);
				}
			}else{
				if(countryName.toLowerCase() == "united states") {
					setUserPreferences('27',locId+':'+locType+':'+presentationName+'^'+getRecentSearch);
				} else {
					var temp = locName.split(",");
					setUserPreferences('27',locId+':'+locType+':'+temp[0]+", "+countryName+'^'+getRecentSearch);
				}
			}
		}
	}
}







function setRecentSearchNew(locId,locType,presentationName,countryName, stateCode, from){
var luceneLocId=locId;
var luceneLocType=locType;
var locName=presentationName;
var countryName = countryName;
			if(from=='search_travel'){
				setUserPreferences(16,'Travel');
			}else if(from=='search_weddings'){
				setUserPreferences(16,'Weddings');
			}
			getRecentSearch=getUserPreferences('27');
			if(getRecentSearch==''){
				if(countryName.toLowerCase() == "united states") {
					setUserPreferences('27',locId+':'+locType+':'+presentationName);
				} else {
					var temp = locName.split(",");
					setUserPreferences('27',locId+':'+locType+':'+presentationName);
				}
			}else{
				alreadyExist=false;
				var splitUpSearches=getRecentSearch.split("^");
			for(var i=0;i<splitUpSearches.length;i++){
				var splitEachSearch=splitUpSearches[i].split(":");
				if(splitEachSearch[0]==locId  && i<6){
					alreadyExist=true;
				} else if(splitEachSearch[0]==locId && i>=6) {
					
				}
			}
			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 != "undefined" && stateCode != "undefined" && countryName.toLowerCase() == "united states" ) {
					setUserPreferences('27',locId+':'+locType+':'+presentationName+'^'+buff);
				} else {
					var temp = locName.split(",");
					setUserPreferences('27',locId+':'+locType+':'+presentationName+'^'+buff);
				}
			}else{
				if(countryName != "undefined" && stateCode != "undefined" && countryName.toLowerCase() == "united states") {
					setUserPreferences('27',locId+':'+locType+':'+presentationName+'^'+getRecentSearch);
				} else {
					var temp = locName.split(",");
					setUserPreferences('27',locId+':'+locType+':'+presentationName+'^'+getRecentSearch);
				}
			}
		}
	}
}