Commits

factormystic committed 9d0c303

Zip code now autodetected

Comments (0)

Files changed (1)

 
 var xmlHttp = null;
 
-var zip = 45219
-var url = "http://xoap.weather.com/weather/local/"+zip+"?cc=*&dayf=6&link=xoap&prod=xoap&par=1127063443&key=af5d7d563b56d53c";
+var zip_url = 'http://www.maxmind.com/app/mylocation';
+var last_zip = System.Gadget.Settings.readString("zip");
+alert("Last known zip value: "+last_zip);
+var zip = last_zip.length==5?last_zip:null;
+var weather_url = zip!=null?"http://xoap.weather.com/weather/local/"+zip+"?cc=*&dayf=6&link=xoap&prod=xoap&par=1127063443&key=af5d7d563b56d53c":null;
 
 System.Gadget.settingsUI = "settings.html";
 System.Gadget.onSettingsClosed = settingsClosed;
 		fonttype = System.Gadget.Settings.readString("fonttype");
 
 	temp.font = condition.font = fonttype;
+	
+	zip = System.Gadget.Settings.readString("zip");
+	weather_url = "http://xoap.weather.com/weather/local/"+zip+"?cc=*&dayf=6&link=xoap&prod=xoap&par=1127063443&key=af5d7d563b56d53c";
 }
 
 function ProcessRequest() 
         {
 			var data = xmlHttp.responseText;
 			
-			var start = data.indexOf('<tmp>')+5;
-			var end = data.indexOf('</tmp>',end);
-			temp.value = data.substr(start, end-start) + "°";
-			System.Gadget.Settings.writeString("temp", temp.value);
+			if(zip==null)
+			{
+				var start = data.indexOf('Zipcode')+'Zipcode*</td> <td class=output align="center"> '.length;
+				zip = data.substr(start, 5);
+				
+				alert('Got zip code: '+zip);
+				System.Gadget.Settings.writeString("zip", zip);
 
-			var start = data.indexOf('<t>',start)+3;
-			var end = data.indexOf('</t>',start);
-            condition.value = data.substr(start, end-start).toUpperCase();
-			System.Gadget.Settings.writeString("condition", condition.value);
-			
-			temp.width = temp.offsetWidth;
-			temp.left = 675-temp.width-10;
-			condition.width = condition.offsetWidth;
-			condition.left = 675-condition.width-10;
+				weather_url = "http://xoap.weather.com/weather/local/"+zip+"?cc=*&dayf=6&link=xoap&prod=xoap&par=1127063443&key=af5d7d563b56d53c";
+				update();
+			}
+			else
+			{
+				var start = data.indexOf('<tmp>')+5;
+				var end = data.indexOf('</tmp>',end);
+				temp.value = data.substr(start, end-start) + "°";
+				System.Gadget.Settings.writeString("temp", temp.value);
+
+				var start = data.indexOf('<t>',start)+3;
+				var end = data.indexOf('</t>',start);
+				condition.value = data.substr(start, end-start).toUpperCase();
+				System.Gadget.Settings.writeString("condition", condition.value);
+				
+				temp.width = temp.offsetWidth;
+				temp.left = 675-temp.width-10;
+				condition.width = condition.offsetWidth;
+				condition.left = 675-condition.width-10;
+			}
         }                    
     }
 	else
 	}
 
 	xmlHttp = new XMLHttpRequest(); 
-    xmlHttp.onreadystatechange = ProcessRequest;
-    xmlHttp.open("GET", url, true);
-    xmlHttp.send(null);
+	xmlHttp.onreadystatechange = ProcessRequest;
+	xmlHttp.open("GET", zip==null?zip_url:weather_url, true);
+	xmlHttp.send(null);
     
 	var bg = document.getElementById("bg");