'<script language="vbscript">
OPTION EXPLICIT
on error resume next

CONST i_DAYS_OUT_TO_SHOW = 3
CONST b_SHOW_TEMP_SCALE = false





'get current zip from external file (can switch when mobile)
DIM fs, strCurrentZip
strCurrentZip = 45231
SET fs = CreateObject("Scripting.FileSystemObject")
IF fs.FileExists("currentZip.txt") THEN strCurrentZip = fs.OpenTextFile("currentZip.txt").ReadAll()	
SET fs = nothing


'CONST str_WEATHERSERVICE_URL = "http://www.ejse.com/WeatherService/Service.asmx/GetExtendedWeatherInfo?zipCode="
CONST str_WEATHERSERVICE_URL = "http://www.ejse.com/WeatherService/Service.asmx/GetExtendedWeatherInfo2?userName=ejse.com@throbs.net&password=ZD5rMF9ixEPo&zipCode="


DIM oXMLHTTP
SET oXMLHTTP = CreateObject("MSXML2.XMLHTTP")

'get through cache service
call oXMLHTTP.open("GET", "http://throbs.net/web/services/dataCacher/?url=" & str_WEATHERSERVICE_URL & strCurrentZip, false)
oXMLHTTP.send()

'if failure from cache service, try directly
IF oXMLHTTP.status <> 200 THEN 
	call oXMLHTTP.open("GET", str_WEATHERSERVICE_URL & strCurrentZip, false)
	oXMLHTTP.send()
END IF

DIM xmlWeatherSource 
xmlWeatherSource = oXMLHTTP.ResponseText
SET oXMLHTTP = nothing



xmlWeatherSource = replace(xmlWeatherSource, "Temprature", "Temperature")	'fix misspelling
xmlWeatherSource = replace(xmlWeatherSource, "°", "&#176;")	' fix degrees char


DIM xmlDoc
SET xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.loadXML(xmlWeatherSource)

DIM strReturn
IF xmlDoc.hasChildNodes() THEN
	DIM cs_dtLastUpdated, cs_Location, strUpdated
	cs_dtLastUpdated = fnGetLastUpdatedFromEJSE(xmlDoc)
	IF cs_dtLastUpdated<>"" THEN 
		cs_dtLastUpdated = cdate(cs_dtLastUpdated)
		strUpdated = fnFormatShortFriendlyDate(cs_dtLastUpdated)
	ELSE
		cs_dtLastUpdated = Now()
		strUpdated = "(" & fnFormatShortFriendlyDate(cs_dtLastUpdated) & "?)"
	END IF

	cs_Location = xmlDoc.selectSingleNode("/ExtendedWeatherInfo/Info/Location").text
	strReturn = strCurrentZip & " weather @ " & strUpdated & " :" & vbCRLF
	strReturn = strReturn & _
		"Now:  " & fnGetCurWeatherFromEJSE(xmlDoc) '& vbCRLF

'	DIM i	' loop through forecast days
'	FOR i=1 TO i_DAYS_OUT_TO_SHOW
'	strReturn = strReturn & _
'		xmlDoc.selectSingleNode("/ExtendedWeatherInfo/Day" & i & "/Day").text & ":  " & _
'		fnGetDayForecastFromEJSE(xmlDoc, i) & IIF(i<>i_DAYS_OUT_TO_SHOW, vbCRLF, "")
'	NEXT
	
	
	SET xmlDoc = nothing
ELSE
	strReturn = "  Weather currently unavailable"
END IF



On Error Resume Next
wscript.Echo strReturn	'for cmd line
Echo strReturn		'for BGInfo
on error goto 0



'===================================================




FUNCTION fnFormatShortFriendlyDate(byval p_date)
	DIM strHour, strMinute, strMeridian, strWeekday, strMonth, strDay
	strHour = Hour(p_date) mod 12
	IF strHour = 0 THEN strHour = 12
	strMinute = right("00" & Minute(p_date), 2)
	strMeridian = IIF(instr(UCASE(p_date), "AM"), "am", "pm")
	strUpdated = strHour & ":" & strMinute & strMeridian
	strWeekday = left(WeekDayName(weekDay(p_date)), 3)
	strMonth = left(MonthName(Month(p_date)), 3)
	strDay = Day(p_date)
	fnFormatShortFriendlyDate = strUpdated & ", " & strWeekday & " " & strMonth & " " & strDay
END FUNCTION



FUNCTION fnGetLastUpdatedFromEJSE(xmlDocTemp)
	DIM strTemp, strTime, strDate
	strTemp = xmlDocTemp.selectSingleNode("/ExtendedWeatherInfo/Info/LastUpdated").text
	IF strTemp <> "" THEN 
		strTemp = mid(strTemp, instr(strTemp, ", ")+2)	'remove weekday name
		strTemp = replace(strTemp, " Eastern Standard Time.", "")
		strTemp = replace(strTemp, " Eastern Daylight Time.", "")
		strTemp = replace(strTemp, " PM", " pm")
		strTemp = replace(strTemp, " AM", " am")
		DIM iPos
		iPos = instr(strTemp, ", at ")
		strTime = mid(strTemp, iPos+5)
		strDate = left(strTemp, iPos-1)
		fnGetLastUpdatedFromEJSE = cdate(strTime & " " & strDate)
	END IF
END FUNCTION

FUNCTION fnGetCurWeatherFromEJSE(xmlDocTemp)
	on error resume next
	DIM cs_HTMLicon, cs_LastUpdated, cs_Forecast, cs_Temperature, cs_FeelsLike
	CONST strBranch = "/ExtendedWeatherInfo/Info/"
	WITH xmlDocTemp
		cs_Forecast = .selectSingleNode(strBranch & "Forecast").text
		cs_Temperature = .selectSingleNode(strBranch & "Temperature").text
		cs_FeelsLike = .selectSingleNode(strBranch & "FeelsLike").text
	END WITH
	IF err.number = 0 THEN 
		fnGetCurWeatherFromEJSE = _
			cs_Forecast &  ", " & _
			IIF(cs_Temperature<>"", cs_Temperature, "?°") & _
			IIF(cs_FeelsLike<>"", " (feels " & cs_FeelsLike & ")", "")
	END IF
	IF NOT b_SHOW_TEMP_SCALE THEN fnGetCurWeatherFromEJSE = replace(fnGetCurWeatherFromEJSE, "°F", "°")
	on error goto 0
END FUNCTION



FUNCTION fnGetDayForecastFromEJSE(xmlDocTemp, p_iDaysOut)
	on error resume next
	DIM cs_HTMLicon, cs_LastUpdated, cs_Forecast, cs_Low, cs_High, cs_PrecipChance
	DIM strBranch
	strBranch = "/ExtendedWeatherInfo/Day" & p_iDaysOut & "/"
	
	WITH xmlDocTemp
		cs_Forecast = .selectSingleNode(strBranch & "Forecast").text
		cs_Low = .selectSingleNode(strBranch & "Low").text & IIF(b_SHOW_TEMP_SCALE, "F", "")
		cs_High = .selectSingleNode(strBranch & "High").text & IIF(b_SHOW_TEMP_SCALE, "F", "")
		cs_PrecipChance = replace(.selectSingleNode(strBranch & "PrecipChance").text, " %", "%")
	END WITH
	IF err.number = 0 THEN 
		fnGetDayForecastFromEJSE = _
			cs_Forecast & ", " & cs_Low & " - " & cs_High & " (" & cs_PrecipChance & ")"
	END IF
	on error goto 0
END FUNCTION




' IIF recreated for vbscript
FUNCTION IIF(expr, truepart, falsepart)
	IF expr THEN 
		IIF = truepart
	ELSE
		IIF = falsepart
	END IF
END FUNCTION


