var setCurrentBase='/location/setCurrent?';
var setFavoriteBase='/location/setFavorite?';
var getFavoriteBase='/location/getFavorite';
var resolveBase='/location/resolve?';
var favMapBase='http://cdn.digitalcity.com/location/favorite.html?';
var buddyMapBase='http://cdn.digitalcity.com/location/mapbuddies.html?';
var noLocationsPage='/location/nolocation.html';
var currentLocation,favNames,favorites,buddyNames,buddies,backgroundResolve;
var useAX=typeof window.ActiveXObject!='undefined';
var req=null;
var usIndex=0;
var countries=new Array();
var NO_HIT_TEXT='- None -';
var MAX_FAV=7;
var MAX_TEXT='Max. favorites reached.';
var MAX_TITLE_LEN=64;
var replacement=null;

countries.push(new KV('AL','Albania'));
countries.push(new KV('DZ','Algeria'));
countries.push(new KV('AS','American Samoa'));
countries.push(new KV('AD','Andorra'));
countries.push(new KV('AO','Angola'));
countries.push(new KV('AI','Anguilla'));
countries.push(new KV('AG','Antigua and Barbuda'));
countries.push(new KV('AR','Argentina'));
countries.push(new KV('AM','Armenia'));
countries.push(new KV('AW','Aruba'));
countries.push(new KV('AU','Australia'));
countries.push(new KV('AT','Austria'));
countries.push(new KV('AZ','Azerbaijan'));
countries.push(new KV('BS','Bahamas'));
countries.push(new KV('BH','Bahrain'));
countries.push(new KV('BD','Bangladesh'));
countries.push(new KV('BB','Barbados'));
countries.push(new KV('BY','Belarus'));
countries.push(new KV('BE','Belgium'));
countries.push(new KV('BZ','Belize'));
countries.push(new KV('BJ','Benin'));
countries.push(new KV('BM','Bermuda'));
countries.push(new KV('BT','Bhutan'));
countries.push(new KV('BO','Bolivia'));
countries.push(new KV('BA','Bosnia and Herzegovina'));
countries.push(new KV('BW','Botswana'));
countries.push(new KV('BV','Bouvet Island'));
countries.push(new KV('BR','Brazil'));
countries.push(new KV('IO','British Indian Ocean Territory'));
countries.push(new KV('VG','British Virgin Islands'));
countries.push(new KV('BN','Brunei'));
countries.push(new KV('BG','Bulgaria'));
countries.push(new KV('BF','Burkina Faso'));
countries.push(new KV('BI','Burundi'));
countries.push(new KV('KH','Cambodia'));
countries.push(new KV('CM','Cameroon'));
countries.push(new KV('CA','Canada'));
countries.push(new KV('CV','Cape Verde'));
countries.push(new KV('KY','Cayman Islands'));
countries.push(new KV('CF','Central African Republic'));
countries.push(new KV('TD','Chad'));
countries.push(new KV('CL','Chile'));
countries.push(new KV('CN','China'));
countries.push(new KV('CX','Christmas Island'));
countries.push(new KV('CC','Cocos (Keeling) Islands'));
countries.push(new KV('CO','Colombia'));
countries.push(new KV('KM','Comoros'));
countries.push(new KV('CG','Congo'));
countries.push(new KV('CD','Congo - Democratic Republic of'));
countries.push(new KV('CK','Cook Islands'));
countries.push(new KV('CR','Costa Rica'));
countries.push(new KV('CI',"Cote d'Ivoire"));
countries.push(new KV('HR','Croatia'));
countries.push(new KV('CU','Cuba'));
countries.push(new KV('CY','Cyprus'));
countries.push(new KV('CZ','Czech Republic'));
countries.push(new KV('DK','Denmark'));
countries.push(new KV('DJ','Djibouti'));
countries.push(new KV('DM','Dominica'));
countries.push(new KV('DO','Dominican Republic'));
countries.push(new KV('TP','East Timor'));
countries.push(new KV('EC','Ecuador'));
countries.push(new KV('EG','Egypt'));
countries.push(new KV('SV','El Salvador'));
countries.push(new KV('GQ','Equitorial Guinea'));
countries.push(new KV('ER','Eritrea'));
countries.push(new KV('EE','Estonia'));
countries.push(new KV('ET','Ethiopia'));
countries.push(new KV('FK','Falkland Islands (Islas Malvinas)'));
countries.push(new KV('FO','Faroe Islands'));
countries.push(new KV('FJ','Fiji'));
countries.push(new KV('FI','Finland'));
countries.push(new KV('FR','France'));
countries.push(new KV('GF','French Guyana'));
countries.push(new KV('PF','French Polynesia'));
countries.push(new KV('TF','French Southern and Antarctic Lands'));
countries.push(new KV('GA','Gabon'));
countries.push(new KV('GM','Gambia'));
countries.push(new KV('GZ','Gaza Strip'));
countries.push(new KV('GE','Georgia'));
countries.push(new KV('DE','Germany'));
countries.push(new KV('GH','Ghana'));
countries.push(new KV('GI','Gibraltar'));
countries.push(new KV('GR','Greece'));
countries.push(new KV('GL','Greenland'));
countries.push(new KV('GD','Grenada'));
countries.push(new KV('GP','Guadeloupe'));
countries.push(new KV('GU','Guam'));
countries.push(new KV('GT','Guatemala'));
countries.push(new KV('GN','Guinea'));
countries.push(new KV('GW','Guinea-Bissau'));
countries.push(new KV('GY','Guyana'));
countries.push(new KV('HT','Haiti'));
countries.push(new KV('HM','Heard Island and McDonald Islands'));
countries.push(new KV('VA','Holy See (Vatican City)'));
countries.push(new KV('HN','Honduras'));
countries.push(new KV('HK','Hong Kong'));
countries.push(new KV('HU','Hungary'));
countries.push(new KV('IS','Iceland'));
countries.push(new KV('IN','India'));
countries.push(new KV('ID','Indonesia'));
countries.push(new KV('IR','Iran'));
countries.push(new KV('IQ','Iraq'));
countries.push(new KV('IE','Ireland'));
countries.push(new KV('IL','Israel'));
countries.push(new KV('IT','Italy'));
countries.push(new KV('JM','Jamaica'));
countries.push(new KV('JP','Japan'));
countries.push(new KV('JO','Jordan'));
countries.push(new KV('KZ','Kazakhstan'));
countries.push(new KV('KE','Kenya'));
countries.push(new KV('KI','Kiribati'));
countries.push(new KV('KW','Kuwait'));
countries.push(new KV('KG','Kyrgyzstan'));
countries.push(new KV('LA','Laos'));
countries.push(new KV('LV','Latvia'));
countries.push(new KV('LB','Lebanon'));
countries.push(new KV('LS','Lesotho'));
countries.push(new KV('LR','Liberia'));
countries.push(new KV('LY','Libya'));
countries.push(new KV('LI','Liechtenstein'));
countries.push(new KV('LT','Lithuania'));
countries.push(new KV('LU','Luxembourg'));
countries.push(new KV('MO','Macau'));
countries.push(new KV('MK','Macedonia - The Former Yugoslav Republic of'));
countries.push(new KV('MG','Madagascar'));
countries.push(new KV('MW','Malawi'));
countries.push(new KV('MY','Malaysia'));
countries.push(new KV('MV','Maldives'));
countries.push(new KV('ML','Mali'));
countries.push(new KV('MT','Malta'));
countries.push(new KV('MH','Marshall Islands'));
countries.push(new KV('MQ','Martinique'));
countries.push(new KV('MR','Mauritania'));
countries.push(new KV('MU','Mauritius'));
countries.push(new KV('YT','Mayotte'));
countries.push(new KV('MX','Mexico'));
countries.push(new KV('FM','Micronesia - Federated States of'));
countries.push(new KV('MD','Moldova'));
countries.push(new KV('MC','Monaco'));
countries.push(new KV('MN','Mongolia'));
countries.push(new KV('MS','Montserrat'));
countries.push(new KV('MA','Morocco'));
countries.push(new KV('MZ','Mozambique'));
countries.push(new KV('MM','Myanmar'));
countries.push(new KV('NA','Namibia'));
countries.push(new KV('NR','Naura'));
countries.push(new KV('NP','Nepal'));
countries.push(new KV('NL','Netherlands'));
countries.push(new KV('AN','Netherlands Antilles'));
countries.push(new KV('NC','New Caledonia'));
countries.push(new KV('NZ','New Zealand'));
countries.push(new KV('NI','Nicaragua'));
countries.push(new KV('NE','Niger'));
countries.push(new KV('NG','Nigeria'));
countries.push(new KV('NU','Niue'));
countries.push(new KV('NF','Norfolk Island'));
countries.push(new KV('KP','North Korea'));
countries.push(new KV('MP','Northern Mariana Islands'));
countries.push(new KV('NO','Norway'));
countries.push(new KV('OM','Oman'));
countries.push(new KV('PK','Pakistan'));
countries.push(new KV('PW','Palau'));
countries.push(new KV('PA','Panama'));
countries.push(new KV('PG','Papua New Guinea'));
countries.push(new KV('PY','Paraguay'));
countries.push(new KV('PE','Peru'));
countries.push(new KV('PH','Philippines'));
countries.push(new KV('PN','Pitcairn Islands'));
countries.push(new KV('PL','Poland'));
countries.push(new KV('PT','Portugal'));
countries.push(new KV('PR','Puerto Rico'));
countries.push(new KV('QA','Qatar'));
countries.push(new KV('RE','Reunion'));
countries.push(new KV('RO','Romania'));
countries.push(new KV('RU','Russia'));
countries.push(new KV('RW','Rwanda'));
countries.push(new KV('KN','Saint Kitts and Nevis'));
countries.push(new KV('LC','Saint Lucia'));
countries.push(new KV('VC','Saint Vincent and the Grenadines'));
countries.push(new KV('WS','Samoa'));
countries.push(new KV('SM','San Marino'));
countries.push(new KV('ST','Sao Tome and Principe'));
countries.push(new KV('SA','Saudi Arabia'));
countries.push(new KV('SN','Senegal'));
countries.push(new KV('CS','Serbia and Montenegro'));
countries.push(new KV('SC','Seychelles'));
countries.push(new KV('SL','Sierra Leone'));
countries.push(new KV('SG','Singapore'));
countries.push(new KV('SK','Slovakia'));
countries.push(new KV('SI','Slovenia'));
countries.push(new KV('SB','Solomon Islands'));
countries.push(new KV('SO','Somalia'));
countries.push(new KV('ZA','South Africa'));
countries.push(new KV('GS','South Georgia and the South Sandwich Islands'));
countries.push(new KV('KR','South Korea'));
countries.push(new KV('ES','Spain'));
countries.push(new KV('LK','Sri Lanka'));
countries.push(new KV('SH','St. Helena'));
countries.push(new KV('PM','St. Pierre and Miquelon'));
countries.push(new KV('SD','Sudan'));
countries.push(new KV('SR','Suriname'));
countries.push(new KV('SJ','Svalbard'));
countries.push(new KV('SZ','Swaziland'));
countries.push(new KV('SE','Sweden'));
countries.push(new KV('CH','Switzerland'));
countries.push(new KV('SY','Syria'));
countries.push(new KV('TW','Taiwan'));
countries.push(new KV('TJ','Tajikistan'));
countries.push(new KV('TZ','Tanzania'));
countries.push(new KV('TH','Thailand'));
countries.push(new KV('TG','Togo'));
countries.push(new KV('TK','Tokelau'));
countries.push(new KV('TO','Tonga'));
countries.push(new KV('TT','Trinidad and Tobago'));
countries.push(new KV('TN','Tunisia'));
countries.push(new KV('TR','Turkey'));
countries.push(new KV('TM','Turkmenistan'));
countries.push(new KV('TC','Turks and Caicos Islands'));
countries.push(new KV('TV','Tuvalu'));
countries.push(new KV('UG','Uganda'));
countries.push(new KV('UA','Ukraine'));
countries.push(new KV('AE','United Arab Emirates'));
countries.push(new KV('GB','United Kingdom'));
countries.push(new KV('US','United States of America'));
countries.push(new KV('VI','United States Virgin Islands'));
countries.push(new KV('UY','Uruguay'));
countries.push(new KV('UZ','Uzbekistan'));
countries.push(new KV('VU','Vanuatu'));
countries.push(new KV('VE','Venezuela'));
countries.push(new KV('VN','Vietnam'));
countries.push(new KV('WF','Wallis and Futuna'));
countries.push(new KV('PS','West Bank'));
countries.push(new KV('EH','Western Sahara'));
countries.push(new KV('YE','Yemen'));
countries.push(new KV('ZM','Zambia'));
countries.push(new KV('ZW','Zimbabwe'));

function KV(k,v){this.k=k;this.v=v;return this;}

function stripW(s){return s.replace(/^\s+/g,'').replace(/\s+$/g,'')}

function LocationReq(u,l){
	if(!(u&&l)) return null;
	this.url=u;
	this.label=l;
	this.doc=null;
	if(useAX) this.doc=new ActiveXObject("Microsoft.XMLHTTP");
	else this.doc=new XMLHttpRequest();
}

LocationReq.prototype.invoke=function(p){
	if(useAX) this.doc.onreadystatechange=p;
	else this.doc.onload=p;
	this.doc.open("GET", this.url, true);
	if(useAX) this.doc.send();
	else this.doc.send(null);
	req=this;
}

LocationReq.prototype.processCurrent=function(){
	if(!req||!req.doc||req.doc.readyState!=4) return;
	if(req.doc.status==200){
		var j=JSON.parse(req.doc.responseText);
		writeCurrent(j);
		var f=document.getElementById('editForm');
		doc=null;
        	if(f&&f.favAlso&&f.favAlso.checked==true) saveFavoriteJson(j);
        	else{hideEdit();markCurrentFavorite();loadFavoriteMap();}
	}
	else{
		alert('Error processing request.');
	}
}

LocationReq.prototype.processGetFavorite=function(){
	if(!req||!req.doc||req.doc.readyState!=4) return;
	if(req.doc.status==200) {
		writeFavorite(JSON.parse(req.doc.responseText));
		loadFavoriteMap();
	}
	else alert('Error processing request.');
	hideEdit();
}

LocationReq.prototype.processSetFavorite=function(){
	if(!req||!req.doc||req.doc.readyState!=4) return;
	if(req.doc.status==200) getFavorite('get favorite');
	else alert('Error processing request.');
}

LocationReq.prototype.processSetFavoriteNoRefresh=function(){
	if(!req||!req.doc||req.doc.readyState!=4) return;
	if(req.doc.status==200){
		var k=backgroundResolve.pop();
                latLonFill(k);
	}
}

LocationReq.prototype.processPreResolve=function(){
	if(!req||!req.doc||req.doc.readyState!=4) return;
	if(req.doc.status==200) saveResolved(JSON.parse(req.doc.responseText));
	else alert('Error processing request.');
}

LocationReq.prototype.processBackfillLatLon=function(){
	if(!req||!req.doc||req.doc.readyState!=4) return;
	if(req.doc.status==200) replaceWithResolved(JSON.parse(req.doc.responseText),req.label);
	else alert('Error processing request.');
}

LocationReq.prototype.processReplaceFavorite=function(){
	if(!req||!req.doc||req.doc.readyState!=4) return;
	if(req.doc.status==200){
		if(replacement){
			var l=replacement.k;var u=replacement.v;
			replacement=null;
			saveFavorite(null,l,u);
		}
	}
	else alert('Error processing request.');
}



function saveResolved(j){
	var l=firstResolved(j);
	if(!l){hideEdit(); return;}
	var u=null;
	u=setFavoriteBase;
	u+='label='+escape(document.getElementById('editForm').desc.value);
	if(l.street) u+='&street='+l.street;
	if(l.city) u+='&city='+l.city;
	if(l.state) u+='&state='+l.state;
	if(l.postcode) u+='&postcode='+l.postcode;
	if(l.country) u+='&country='+l.country;
	if(l.lat) u+='&lat='+l.lat;
	if(l.lon) u+='&lon='+l.lon;
	hideEdit();
	req=null;
	var lReq=new LocationReq(u,l);
	lReq.invoke(lReq.processSetFavorite);
}

function firstResolved(j){
	return (j&&j.locs&&j.locs.length>0)?j.locs[0]:null;
}

function replaceWithResolved(j,l){
	if(!j) return;
	var f=firstResolved(j);
	if(!f) return;
	favorites[l]=f;
	saveFavoriteJson(f,l);
}

function writeCurrent(j) {
	var h='';
	if(j.users&&j.users[0].loc_exception){
		h+='Problems retreiving current location.';
	}else if(j.users){
		var l=j.users[0].loc;
		currentLocation=l;
		h+=l.desc?unescape(l.desc)+'<br />':'';
		h+=l.street?unescape(l.street)+'<br />':'';
		h+=l.city?unescape(l.city)+' ':'';
		h+=l.state?l.state+' ':'';
		h+=l.postcode?l.postcode+'<br />':'';
		h+=l.country?l.country:'';
	}else h+='Current Location Not Set';
	document.getElementById('currentLocation').innerHTML=h;
}

function writeFavorite(j){
	favorites=new Array();
	favNames=new Array();
	var h='<ul>';
	if(!(j&&j.users&&j.users[0].loc_favorites&&
		j.users[0].loc_favorites.length>0))
		h+='<li><span class="nohit">'+NO_HIT_TEXT+'</span></li>';
	else{
		var f=j.users[0].loc_favorites;
		for(var i=0;i<f.length;i++){
			var l=f[i].label;
			favNames.push(l);
			favorites[l]=f[i].location;
			var el=escape(escape(l));
			h+='<li id="f'+i+'"><a href="javascript:deleteFavorite(\''+el+'\');" title="delete"><img src="http://cdn.digitalcity.com/aim_developer/manage_trash.gif" /></a> <a href="javascript:editFavorite(\''+el+'\');"><img src="http://cdn.digitalcity.com/aim_developer/manage_edit.gif" title="edit"/></a><a href="javascript:setCurrent(\''+el+'\');"><img src="http://cdn.digitalcity.com/location/star.gif" title="set as current" class="setCurrent"/></a>'+unescape(l)+'</li>';
		}
	}
	if(j.users[0].loc_favorites.length<MAX_FAV) h+='</ul><a href="javascript:editFavorite(\'\');">add favorite location</a>';
	else h+='</ul><div class="max">'+MAX_TEXT+'</div>';
	document.getElementById('favoriteList').innerHTML=h;
	markCurrentFavorite();
}

function setCurrent(l){
	var j=favorites[unescape(l)];
	if(!j) return;
	var u=setCurrentBase;
	u+='desc='+l;
	if(j.street) u+='&street='+j.street;
	if(j.city) u+='&city='+j.city;
	if(j.state) u+='&state='+j.state;
	if(j.postcode) u+='&postcode='+j.postcode;
	if(j.country) u+='&country='+j.country;
	req=null;
	var lReq=new LocationReq(u,l);
	lReq.invoke(lReq.processCurrent);
}

function clearCurrent(){
	var lReq=new LocationReq(setCurrentBase,'clear');
        lReq.invoke(lReq.processCurrent);
}

function markCurrentFavorite(){
	for(var i=0;i<favNames.length;i++){
		var f=document.getElementById('f'+i);
		if(!f) continue;
		if(currentLocation&&favNames[i]==currentLocation.desc) f.className='current';
		else f.className='';
	}
}

function setCurrentForm(f){
	if(!f) return;
	var u=setCurrentBase;
	var l=stripW(f.desc.value);
	if(l.length<1){alert('Location name required.');f.desc.focus();return false;}
	if(l.length>MAX_TITLE_LEN){alert('Location name over max ('+MAX_TITLE_LEN+').');f.desc.focus();return false;}
	u+='desc='+escape(l);
	if(f.street.value.length>0) u+='&street='+escape(f.street.value);
	if(f.city.value.length>0) u+='&city='+escape(f.city.value);
	if(f.state.value.length>0) u+='&state='+escape(f.state.value);
	if(f.postcode.value.length>0) u+='&postcode='+f.postcode.value;
	if(f.country) u+='&country='+f.country[f.country.selectedIndex].value;
	req=null;
	var lReq=new LocationReq(u,l);
	lReq.invoke(lReq.processCurrent);
}

function getFavorite(l){
	req=null;
	var lReq=new LocationReq(getFavoriteBase,l);
	lReq.invoke(lReq.processGetFavorite);
}

function saveFavorite(f,label,url){
	var u;
	var l;
	if(label&&url){
		l=label;
		u=url;
	}
	else if(f){
		l=f.desc.value;
		u=resolveBase;
		u+='label='+escape(l);
		if(f.street.value.length>0) u+='&street='+escape(f.street.value);
		if(f.city.value.length>0) u+='&city='+escape(f.city.value);
		if(f.state.value.length>0) u+='&state='+f.state.value;
		if(f.postcode.value.length>0) u+='&postcode='+f.postcode.value;
		if(f.country) u+='&country='+f.country[f.country.selectedIndex].value;
		if(f.lat.value.length>0) u+='&lat='+f.lat.value;
		if(f.lon.value.length>0) u+='&lon='+f.lon.value;
		var o=f.org_desc;
		if(o&&o.value.length>0&&o!=l) replacement=new KV(l,u);
	}
	else return;
	if(l.length<1){alert('Location name required.');f.desc.focus();return false;}
	if(l.length>MAX_TITLE_LEN){alert('Location name over max ('+MAX_TITLE_LEN+').');f.desc.focus();return false;}
	if(replacement) replaceFavorite(f.org_desc.value);
	else{
		req=null;
		var lReq=new LocationReq(u,l);
		lReq.invoke(lReq.processPreResolve);
	}
}

function saveFavoriteJson(j,b){
	if(!j) return;
	var l=(j.users&&j.users[0].loc)?j.users[0].loc:j;
	var f=document.getElementById('editForm');
	var label=b?b:(f&&f.desc&&f.desc.value)?f.desc.value:'';
	var u=setFavoriteBase+'label='+escape(label);
	if(l.street) u+='&street='+escape(l.street);
	if(l.city) u+='&city='+escape(l.city);
	if(l.state) u+='&state='+l.state;
	if(l.postcode) u+='&postcode='+l.postcode;
	if(l.country) u+='&country='+l.country;
	if(l.lat) u+='&lat='+l.lat;
	if(l.lon) u+='&lon='+l.lon;
	hideEdit();
	req=null;
	var lReq=new LocationReq(u,label);
	if(b)
		lReq.invoke(lReq.processSetFavoriteNoRefresh);
	else
		lReq.invoke(lReq.processSetFavorite);
}

function deleteFavorite(l){
	if(!confirm('Sure you want to delete saved favorite location\n"'+unescape(l)+'"?')) return;
	var u=setFavoriteBase+'label='+l;
	req=null;
	var lReq=new LocationReq(u,l);
	lReq.invoke(lReq.processSetFavorite);
}

function replaceFavorite(l){
	var u=setFavoriteBase+'label='+l;
	req=null;
	var lReq=new LocationReq(u,l);
	lReq.invoke(lReq.processReplaceFavorite);
}


function showEdit(h){
	var p=document.getElementById('editPop');
	p.innerHTML=h;
	p.style.display='block';
	var f=document.getElementById('editForm');
	if(f&&f.desc)f.desc.focus();
}

function hideEdit(){
	var f=document.getElementById('editPop');
	for(var i=0;i<f.childNodes.length;i++) f.removeChild(f.childNodes[i]);
	f.style.display='none';
}

function clearForm(f){
	f.desc.value='';
	f.street.value='';
	f.city.value='';
	f.state.value='';
	f.postcode.value='';
	f.country.selectedIndex=usIndex;
	f.desc.focus();
}

function countrySelect(selected){
	if(!selected) selected='US';
	var s='<select name="country" id="country" tabindex="10">'
	for(var i=0;i<countries.length;i++){
		var c=countries[i];
		s+='<option value="'+c.k+'"';
		if(c.k==selected) s+=' selected="selected"';
		if(c.k=='US') usIndex=i;
		s+='>'+c.v+'</option>';
	}
	s+='</select>';
	return s;
}

function editCurrent(){
	var j=currentLocation;
	var h='<form id="editForm"><h4>Change Current Location</h4><input type="text" name="desc" value="';
	h+=(j&&j.desc)?unescape(j.desc):'';
	h+='" class="rt" size="50" tabindex="1"/><div class="title">Name:</div><input name="street" type="text" value="';
	h+=(j&&j.street)?j.street:'';
	h+='" class="rt" size="50" tabindex="2"/><div class="title">Street:</div><input name="city" type="text" value="';
	h+=(j&&j.city)?j.city:'';
	h+='" class="rt" size="50" tabindex="3"/><div class="title">City:</div><input name="postcode" type="text" value="';
	h+=(j&&j.postcode)?j.postcode:'';
	h+='" size="10" tabindex="5" class="rt"/><div class="title state">State: &nbsp;&nbsp;&nbsp;<input name="state" type="text" value="';
	h+=(j&&j.state)?j.state:'';
	h+='" size="25" tabindex="4"/> &nbsp;&nbsp;Zip:</div>';
	h+=(j&&j.country)?countrySelect(j.country):countrySelect();
	h+='<p><input type="button" value="Save" class="btn" onclick="setCurrentForm(this.form);" tabindex="20"/><input type="button" value="Clear" class="btn" onclick="clearForm(this.form)" tabindex="21"><input type="button" value="Cancel" class="btn" onclick="hideEdit();" tabindex="22"/>';
	if(favNames.length<MAX_FAV) h+='<input type="checkbox" name="favAlso" class="btn" tabindex="23"> Save as Favorite too.</p></form>';
	showEdit(h);
}

function editFavorite(l){
	var j=favorites[l];
	var h='<form id="editForm">';
	h+=l?'<input type="hidden" name="org_desc" value="'+l+'"/>':'';
	h+=l?'<h4>Change':'<h4>Add';
	h+=' Favorite Location</h4><input type="text" name="desc" value="';
	h+=unescape(l);
	h+='" class="rt" size="50" tabindex="1"/><div class="title">Name:</div><input name="street" type="text" value="';
	h+=(j&&j.street)?j.street:'';
	h+='" class="rt" size="50" tabindex="2"/><div class="title">Street:</div><input name="city" type="text" value="';
	h+=(j&&j.city)?j.city:'';
	h+='" class="rt" size="50" tabindex="3"/><div class="title">City:</div><input name="postcode" type="text" value="';
	h+=(j&&j.postcode)?j.postcode:'';
	h+='" size="10" class="rt" tabindex="5"/><div class="title state">State: &nbsp;&nbsp;&nbsp;<input name="state" type="text" value="';
	h+=(j&&j.state)?j.state:'';
	h+='" size="25" tabindex="4"/> &nbsp;&nbsp;Zip:</div>';
	h+=(j&&j.country)?countrySelect(j.country):countrySelect();
	h+='<p><input type="button" value="Save" class="btn" onclick="saveFavorite(this.form);" tabindex="20"/><input type="button" value="Clear" class="btn" onclick="clearForm(this.form)" tabindex="21"><input type="button" value="Cancel" class="btn" onclick="hideEdit();" tabindex="22"/>';
	h+='<input type="hidden" name="lat" value=""/><input type="hidden" name="lon" value=""/></form>';
	showEdit(h);
}

function loadFavoriteMap(){
	var u='';
	backgroundResolve=new Array();
	if(currentLocation) u+='c=Current%20Location,'+currentLocation.lat+','+currentLocation.lon;
	for(var i=0;i<favNames.length;i++){
		var l=favNames[i];
		var f=favorites[l];
		if(!(f&&f.lat&&f.lon)) backgroundResolve.push(new KV(l,f));
		else{u+='&i'+i+'='+unescape(l)+','+f.lat+','+f.lon;}
	}
	if(u.length==0) u=noLocationsPage;
	else u=favMapBase+u;
	document.getElementById('map').src=u;
}

function latLonFill(kv){
	if(!kv) return;
	var l=kv.k;
	var j=kv.v;
	var u=resolveBase;
	u+='label='+l;
	if(j.street) u+='&street='+j.street;
	if(j.city) u+='&city='+j.city;
	if(j.state) u+='&state='+j.state;
	if(j.postcode) u+='&postcode='+j.postcode;
	if(j.country) u+='&country='+j.country;
	req=null;
	var lReq=new LocationReq(u,l);
	lReq.invoke(lReq.processBackfillLatLon);
}

function loadBuddies(j){
	var h='<ul>';
	buddyNames=new Array();
	buddies=new Array();
	if(!(j&&j.buddy_lists&&j.buddy_lists[0].groups&&j.buddy_lists[0].groups.length>0))
		h+='<li><span class="nohit">'+NO_HIT_TEXT+'</span></li>';
	else {
		var g=j.buddy_lists[0].groups;
		for(var i=0;i<g.length;i++){
			var b=g[i].buddies;
			for(var j=0;j<b.length;j++){
				var s=b[j].sn;
				if(!buddies[s] && b[j].loc && b[j].loc.location){
					buddies[s]=b[j].loc.location;
					buddyNames.push(s);
				}
			}
		}
		for(var i=0;i<buddyNames.length;i++){
			try{
				var s=buddyNames[i];
				var l=buddies[s];
				var t='';
				var d=(l.desc)?' @ '+unescape(l.desc):'';
				t+=l.street?unescape(l.street):'';
				t+=l.city?' '+unescape(l.city):'';
				t+=l.state?' '+l.state:'';
				t+=l.postcode?' '+l.postcode:'';
				t+=l.country?' '+l.country:'';
				h+='<li><input type="checkbox" id="b'+i+'" checked="checked" title="'+
					t+'"/>'+'<a href="javascript:toggle('+i+');" title="'+t+'">'+
					s+d+'</a></li>';
			} catch(x) {}
		}
	}
	h+='</ul>';
	document.getElementById('blList').innerHTML=h;
}

function toggle(i){
	var c=document.getElementById('b'+i);
	if(c)c.checked=!c.checked;
}

function setAll(){
	for(var i=0;i<buddyNames.length;i++)
		document.getElementById('b'+i).checked=true;
}

function clearAll(){
	for(var i=0;i<buddyNames.length;i++)
		document.getElementById('b'+i).checked=false;
}

function loadBuddyMap(){
	if(!buddyNames) return;
	var u='';
	for(var i=0;i<buddyNames.length;i++){
		var c=document.getElementById('b'+i);
		if(c&&c.checked){
			var l=buddies[buddyNames[i]];
			u+='&i'+i+'='+escape(buddyNames[i])+','+l.lat+','+l.lon;
		}
	}
	if(currentLocation&&currentLocation.desc&&currentLocation.lat&&currentLocation.lon)
		u+='&c='+currentLocation.desc+','+currentLocation.lat+','+currentLocation.lon;
	if(u.length==0) u=noLocationsPage;
	else u=buddyMapBase+u;
	document.getElementById('map').src=u;
}

