//********************************************************************************
// JAVASCRIPT FILE
//********************************************************************************
// Author: Mark Trappett.
// Date: 2/1/2007 - WO#48948.
// Purpose: Provides helper functions for GCAP Weather support.
//********************************************************************************

//--------------------------------------------------------------------------------
// Purpose: Sets the Form value and submits the form to get a new Region
//--------------------------------------------------------------------------------
function load_weather() {
  document.forms['weather_selector'].submit();
}

//--------------------------------------------------------------------------------
// Purpose: Writes out the City name and Condition Icon for Display over a map
//--------------------------------------------------------------------------------
function funWriteCityMapIcon(sCityName, sCityID, nMarginLeft, nMarginTop, sCityImage, nCityImageWidth, nConditionID, sRegionID, nIconMarginLeft, nIconMarginTop) {
	if (nIconMarginLeft == null) nIconMarginLeft = (nCityImageWidth == 85) ? nMarginLeft + 22 : nMarginLeft + 16;
	if (nIconMarginTop == null) nIconMarginTop = nMarginTop - 30;

	var sCityNameID = sCityName.replace(/\s/g,"");

	if (sCityImage != null && sCityImage != "") document.writeln("<div id=\"" + sCityNameID + "Label\" class=\"AlphaWeatherCity\" style=\"margin-left: " + nMarginLeft + "px; margin-top: " + nMarginTop + "px; width: " + nCityImageWidth + "px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/gbinc/gcapWeatherInclude/images/" + sCityImage + "', sizingMethod='scale')\" onclick=\"document.location.href='?wxRegionId=" + sRegionID + "&wxTownId=" + sCityID + "';\"><a href=\"?wxRegionId=" + sRegionID + "&wxTownID=" + sCityID + "\"><img src=\"/gbinc/gcapWeatherInclude/images/" + sCityImage + "\" height=\"20\" width=\"" + nCityImageWidth + "\" alt=\"" + sCityName + "\" /></a></div>");
	document.writeln("<div id=\"" + sCityNameID + "Condition\" class=\"AlphaWeatherIcon\" style=\"margin-left: " + nIconMarginLeft + "px; margin-top: " + nIconMarginTop + "px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + funGetWxIconByNum(nConditionID, "small") + "', sizingMethod='scale')\"><img id=\"wx" + sCityNameID + "Icon\" src=\"" + funGetWxIconByNum(nConditionID, "small") + "\" alt=\"" + sCityName + ": " + funGetWxCondition(nConditionID) + "\" height=\"40\" width=\"40\" /></div>");
}

//--------------------------------------------------------------------------------
// Purpose: Adds a postfix to a number like "st" or "nd" or "rd"
//--------------------------------------------------------------------------------
function funFormatDayNumber(nNumber) {
	var sTemp = nNumber.toString();
	if (sTemp == "11" || sTemp == "12" || sTemp == "13") {
		sTemp = sTemp + "th";
	}
	else {
		var sLastChar = sTemp.substring(sTemp.length-1, sTemp.length);
		if (sLastChar == "1") sTemp = sTemp + "st";
		else if (sLastChar == "2") sTemp = sTemp + "nd";
		else if (sLastChar == "3") sTemp = sTemp + "rd";
		else sTemp = sTemp + "th";
	}

	return sTemp;
}

//--------------------------------------------------------------------------------
// Purpose: Display the Conditions Text in the Right Hand Side
//--------------------------------------------------------------------------------
var gsWeatherDate, gsWeatherHeader1, gsWeatherSummary1, gsWeatherHeader2, gsWeatherSummary2, gsWeatherHeader3, gsWeatherSummary3
function funDisplayConditionDetails(sDate, sHeader1, sSummary1, sHeader2, sSummary2, sHeader3, sSummary3) {

	// Update the Text Dynamically using gfunDynamicTextUpdate from gbinc/jsfiles/incCommonAPI.js
	var objHeader = g_getRawObject("wxTextHeader1");
	if (objHeader == null) {
		// Objects not built so just store the Data
		gsWeatherDate = sDate
		gsWeatherHeader1 = sHeader1
		gsWeatherSummary1 = sSummary1
		gsWeatherHeader2 = sHeader2
		gsWeatherSummary2 = sSummary2
		gsWeatherHeader3 = sHeader3
		gsWeatherSummary3 = sSummary3
	}
	else {
		if (sDate == null && sHeader1 == null && sSummary1 == null) {
			// Use Stored Data
			sDate = gsWeatherDate
			sHeader1 = gsWeatherHeader1
			sSummary1 = gsWeatherSummary1
			sHeader2 = gsWeatherHeader2
			sSummary2 = gsWeatherSummary2
			sHeader3 = gsWeatherHeader3
			sSummary3 = gsWeatherSummary3
		}
		if (sDate != null && sDate != "") gfunDynamicTextUpdate("wxDateStamp", sDate);
		if (sHeader1 != null && sHeader1 != "") gfunDynamicTextUpdate("wxTextHeader1", sHeader1);
		if (sSummary1 != null && sSummary1 != "") gfunDynamicTextUpdate("wxTextSummary1", sSummary1);
		if (sHeader2 != null && sHeader2 != "") gfunDynamicTextUpdate("wxTextHeader2", sHeader2);
		if (sSummary2 != null && sSummary2 != "") gfunDynamicTextUpdate("wxTextSummary2", sSummary2);
		if (sHeader3 != null && sHeader3 != "") gfunDynamicTextUpdate("wxTextHeader3", sHeader3);
		if (sSummary3 != null && sSummary3 != "") gfunDynamicTextUpdate("wxTextSummary3", sSummary3);
	}
}

//--------------------------------------------------------------------------------
// Purpose: Returns the 2 digit integer value for the three character month passed
// to the function.
//--------------------------------------------------------------------------------
function funGetValForMonth(sMonth)
{
	switch (sMonth)
	{
		case "Jan":
			temp = 1;
			break;
		case "Feb": 
			temp = 2;
			break;
		case "Mar":  
			temp = 3;
			break;
		case "Apr":  
			temp = 4;
			break;
		case "May":
			temp = 5;
			break;
		case "Jun":
			temp = 6;
			break;
		case "Jul": 
			temp = 7;
			break;
		case "Aug": 
			temp = 8;
			break;
		case "Sep": 
			temp = 9;
			break;
		case "Oct": 
			temp = 10;
			break;
		case "Nov":
			temp = 11;
			break;
		case "Dec":
			temp = 12;
			break;
		default:
			temp = "Unknown Month";
	} // end switch
	
	return temp;
	
} // end function funGetValForMonth().


//--------------------------------------------------------------------------------
// Purpose: Returns the three character month for the 2 digit integer value passed
// to the function.
//--------------------------------------------------------------------------------
function funGetMonthForVal(nMonth)
{
	switch (nMonth+1)
	{
		case 1:
			temp = "Jan";
			break;
		case 2: 
			temp = "Feb";
			break;
		case 3:  
			temp = "Mar";
			break;
		case 4:  
			temp = "Apr";
			break;
		case 5:
			temp = "May";
			break;
		case 6:
			temp = "Jun";
			break;
		case 7: 
			temp = "Jul";
			break;
		case 8: 
			temp = "Aug";
			break;
		case 9: 
			temp = "Sep";
			break;
		case 10: 
			temp = "Oct";
			break;
		case 11:
			temp = "Nov";
			break;
		case 12:
			temp = "Dec";
			break;
		default:
			temp = "Unknown Month";
	} // end switch
	
	return (temp);
} // end function funGetMonthForVal().


//--------------------------------------------------------------------------------
// Purpose: Returns the complete filename and imagepath for the large weather 
// icons displayed on the 5-Day Forecast page based on the Weather ID number
// passed to the function in wxiconNum.
// Details: The mappings are based upon the WCFDAILY.XML document.
//--------------------------------------------------------------------------------
function funGetWxIconByNum(wxiconNum, sSize)
{
	var temp = "";
	var imagePath = "/gbinc/gcapWeatherInclude/images/"; 	// Path to weather icons. 
	if (sSize == null) sSize = "large"
	
	switch (wxiconNum)
	{
		case 0:
			temp = "Clearsky_night.jpg";
			break;
		case 1: 
			temp = "weathericon_sunshine.gif";
			break;
		case 2:  
			temp = "weathericon_cloud.gif";
			break;
		case 3:  
			temp = "weathericon_suncloud.gif";
			break;
		case 4:
			temp = "weathericon_cloud.gif";
			break;
		case 5:
			temp = "weathericon_fog.gif";
			break;
		case 6: 
			temp = "weathericon_fog.gif";
			break;
		case 7: 
			temp = "weathericon_cloud.gif";
			break;
		case 8: 
			temp = "weathericon_cloud.gif";
			break;
		case 9: 
			temp = "weathericon_lightrain.gif";
			break;
		case 10:
			temp = "weathericon_lightrain.gif";
			break;
		case 11:
			temp = "weathericon_lightrain.gif";
			break;
		case 12:
			temp = "weathericon_lightrain.gif";
			break;
		case 13:
			temp = "weathericon_heavyrain.gif";
			break;
		case 14:
			temp = "weathericon_heavyrain.gif";
			break;
		case 15:
			temp = "weathericon_heavyrain.gif";
			break;
		case 16:
			temp = "weathericon_snow.gif";
			break;
		case 17:
			temp = "weathericon_snow.gif";
			break;
		case 18:
			temp = "weathericon_snow.gif";
			break;
		case 19:
			temp = "weathericon_snow.gif";
			break;
		case 20:
			temp = "weathericon_snow.gif";
			break;
		case 21:
			temp = "weathericon_snow.gif";
			break;
		case 22:
			temp = "weathericon_snow.gif";
			break;
		case 23:
			temp = "weathericon_snow.gif";
			break;
		case 24:
			temp = "weathericon_snow.gif";
			break;
		case 25:
			temp = "weathericon_snow.gif";
			break;
		case 26:
			temp = "weathericon_snow.gif";
			break;
		case 27:
			temp = "weathericon_snow.gif";
			break;
		case 28:
			temp = "weathericon_lightning.gif";
			break;
		case 29:
			temp = "weathericon_lightning.gif";
			break;
		case 30:
			temp = "weathericon_lightning.gif";
			break;
		case 31:
			temp = "weathericon_lightning.gif";
			break;
		default:
			temp = "unknown_weather_icon.gif";
	} // end switch
	
	// Check for Size
	if (sSize == "small") {
		if (temp == "unknown_weather_icon.gif") {
			temp = "small_weather_icon_undefined.png";
		}
		else if (temp.indexOf("weathericon_") == 0) {
			temp = temp.replace(/weathericon_/i,"weathericon_small_");
			temp = temp.replace(/\.gif/i,".png");
		}
	}
	else if (sSize == "medium") {
		if (temp.indexOf("weathericon_") == 0) {
			temp = temp.replace(/weathericon_/i,"weathericonSM_");
		}
	}
	return (imagePath + temp);
	
} // end function funGetWxIconByNum().



// Takes the month name passed in nMonth and returns the name of the month.
function funGetMonthName(sMonth)
{
	var temp = "";
	
	switch (sMonth)
	{
		
		case "Jan": 
			temp = "January";
			break;
		case "Feb": 
			temp = "February";
			break;
		case "Mar":
			temp = "March";
			break;
		case "Apr": 
			temp = "April";
			break;
		case "May": 
			temp = "May";
			break;
		case "Jun": // Mist
			temp = "June";
			break;
		case "Jul": 
			temp = "July";
			break;
		case "Aug":
			temp = "August";
			break;
		case "Sep":
			temp = "September";
			break;
		case "Oct":
			temp = "October";
			break;
		case "Nov":
			temp = "November";
			break;
		case "Dec":
			temp = "December";
			break;
		default:
			temp = "Error-Month Undefined";
	} // end switch
	return (temp); // Pass the name of the month back to calling function.
} // funGetMonthName().


// Takes the condition passed in nCondition and returns the weather condition associated 
// with the number.
function funGetWxCondition(nCondition)
{
	var temp = "";
	
	switch (nCondition)
	{
		
		case 0:
			temp = "Clear sky (Night)";
			break;
		case 1: 
			temp = "Sunny (Day)";
			break;
		case 2:  
			temp = "Partly Cloudy (Night)";
			break;
		case 3:  
			temp = "Sunny Intervals";
			break;
		case 4:
			temp = "Dust Storm";
			break;
		case 5:
			temp = "Mist";
			break;
		case 6: 
			temp = "Fog";
			break;
		case 7: 
			temp = "(White) Medium-level cloud";
			break;
		case 8: 
			temp = "(Black) Low-level cloud";
			break;
		case 9: 
			temp = "Light rain shower (Night)";
			break;
		case 10:
			temp = "Light rain shower (Day)";
			break;
		case 11:
			temp = "Drizzle";
			break;
		case 12:
			temp = "Light rain";
			break;
		case 13:
			temp = "Heavy rain shower (Night)";
			break;
		case 14:
			temp = "Heavy rain shower (Day)";
			break;
		case 15:
			temp = "Heavy rain";
			break;
		case 16:
			temp = "Sleet shower (Night)";
			break;
		case 17:
			temp = "Sleet shower (Day)";
			break;
		case 18:
			temp = "Sleet";
			break;
		case 19:
			temp = "Hail shower (Night)";
			break;
		case 20:
			temp = "Hail shower (Day)";
			break;
		case 21:
			temp = "Hail";
			break;
		case 22:
			temp = "Light snow shower (Night)";
			break;
		case 23:
			temp = "Light snow shower (Day)";
			break;
		case 24:
			temp = "Light snow (Night)";
			break;
		case 25:
			temp = "Heavy snow shower (Night)";
			break;
		case 26:
			temp = "Heavy snow shower (Day)";
			break;
		case 27:
			temp = "Heavy snow";
			break;
		case 28:
			temp = "Thundery shower (Night)";
			break;
		case 29:
			temp = "Thundery shower (Day)";
			break;
		case 30:
			temp = "Thunder storm";
			break;
		case 31:
			temp = "Tropical Storm";
			break;
		default:
			temp = "Unknown";
	} // end switch
	return (temp); // Pass the weather condition back to calling function.
} // funGetWxCondition().