
var hdl_clockTimeout = null;
var locationOffset = null;
var locationTimezone = null;
var month_names = new Array("January","February","March","April","May","June",
		"July","August","September", "October","November","December");
var daysOfTheWeek = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
		"Thursday", "Friday", "Saturday");

/**
	@brief Used to reset the time displayed on the weather module
	every minute.

	@return String containing the current time displayed in
	HH:MM pm timzone format. (i.e: 12:30 pm EST)
 */
function setClockTime() {
	var localTime = new Date();
	if(locationOffset) {
		var ms = localTime.getTime() + (localTime.getTimezoneOffset() *
			60000) + locationOffset * 3600000;
		var time =  new Date(ms);
	} else{
		var time = localTime;
	}
	setClockDate(time);
	var hour = time.getHours();
	var minute = time.getMinutes();

	/*
	   	The following conditions format the time so that it is displayed
		in standard time and based on the current time for the location
		the sun/moon image is updated accordingly.
	*/
	var curTime = "" + ((hour > 12) ? hour - 12 : hour);
	if(hour==0){
		curTime = "12";
	}

	curTime += ((minute < 10) ? ":0" : ":") + minute;
	curTime += (hour >= 12) ? " pm " : " am ";
	curTime += locationTimezone;
	return curTime;
}

/**
	@brief setClockDate is used to set the date for display on the clock 

	@param Date used for formatting and displaying the date in DayOfWeek,
	Month Day format. (i.e: Thursday, April 23)
 */
function setClockDate(time) {
	/* Verify the html element for the date exists. */
	if(document.getElementById('module-weather-date')) {
		var date = "";
		date += daysOfTheWeek[time.getDay()] + ", ";
		date += month_names[time.getMonth()] + " ";
		date += time.getDate();
		document.getElementById('module-weather-date').innerHTML = date;
	}
}

/**
	@brief Used to set the time and date for the display on the weather
	module.	This method is set to run once a minute.

	@param String containing the offset for the desired weather location.
	@param String containing the timezone for the desired weather location.
 */
function updateClock(offset, timezone) {
	var x = null;

	if(locationOffset == null) {
		locationOffset = formatOffset(offset);
	}
	if(locationTimezone == null) {
		locationTimezone = formatTimezone(timezone);
	}

	/* Verify that the hdl for the timeout isn't currently being used. */
	if(hdl_clockTimeout != null) {
		clearTimeout(hdl_clockTimeout);
	}
	if(document.getElementById('module-weather-time')) {
		var locTime = setClockTime();
		document.getElementById('module-weather-time').innerHTML = locTime;
	}
	hdl_clockTimeout = setTimeout("updateClock(locationOffset,\"locationTimezone\")",60001);
}

/**
	@brief Used to format the date data from the XML in order to provide
	the correctly formatted offset for the weather location.

	@param String that is to be formatted to provide the correct offset.

	@return String containing the properly formatted offset.
 */
function formatOffset(offset) {
	var tempOffset = offset.substring(offset.length - 5);
	tempOffset = tempOffset.replace(/0/g, "");
	return tempOffset.replace(/ /g, "");
}

/**
	@brief Used to format the date data from the XML in order to provide
	the correctly formatted timezone for the weather location.

	@param String that is to be formatted to provide the correct timezone.

	@return String containing the properly formatted timezone.
 */
function formatTimezone(timezone) {
	var tempTimezone = timezone.substring(timezone.length - 4);
	return tempTimezone.replace(/ /g, "");
}

/**
	@brief Used to validate the admin form.

	@note There is no need to validate the unit of temperature value since
	it is a prefilled drop down with the default selection being a valid
	option, which in this case is Fahrenheit (F).
 */
function validateAdminForm() {
	var cityTag = null; //!> The html element for the city.
	var stateTag = null; //!> The html element for the state.
	var stationTag = null; //!> The html element for the station id.
	var valid = true; //!> Boolean used to track the validation.

	document.getElementById('weather-admin-form-error').innerHTML = "";

	cityTag = document.getElementById('weather-admin-city-text');
	if(cityTag && cityTag.value.length <= 0) {
		addError('city');
		valid = false;
	}

	stateTag = document.getElementById('weather-admin-state-text');
	if(stateTag && stateTag.value.length <= 0) {
		addError('state');
		valid = false;
	}
	
	stationTag = document.getElementById('weather-admin-station-id-text');
	if(stationTag && stationTag.value.length <= 0) {
		addError('station ID');
		valid = false;
	}

	if(valid == false) {
		/* Display the div tag containing the error messages. */
		document.getElementById('weather-admin-form-error').style.display = "block";
		return valid;
	}
	
	/* Form has been validated so make sure the error div is cleared and not displayed. */
	document.getElementById('weather-admin-form-error').style.display = "none";
	return true;
}

/**
	@brief Used to add an error message resulting from the form validation check.

	@param String that is the name of the invalid field on the form.
 */
function addError(field) {
	document.getElementById('weather-admin-form-error').innerHTML += "Missing or invalid " + field + ".";
}

