//Event.observe(window, 'load', intiEventTracking);

var _timezone_id;
var _state_id;

function intiEventTracking()
{
	Event.observe($("zip"), "keyup", zipToTimeZone, false);
	Event.observe($("country"), "change", reloadTimezoneList, false);
	Event.observe($("country"), "change", reloadStateList, false);

	reloadTimezoneList();
	reloadStateList();
}

function zipToTimeZone(e)
{
	if( e.charCode == 32 )
	{
		Event.stop(e);
	}

	if( $("zip").value.length >= 5 )
	{
		var oForm = $("login_form");
		new Ajax.Request(remote_path + "ziptotimezone/ajaxToTimeZone/zip/"+$("zip").value, { method: 'get', onComplete: showResponse});
	}
}

function showResponse(responseHttpObj) 
{
		$("timezone").value=parseInt(responseHttpObj.responseText);
}

function reloadTimezoneList()
{
	// keep current timezone ID
	_timezone_id = $("timezone").value;

	new Ajax.Request(
		"ziptotimezone/ajaxTimezone/country_id/" + $("country").value,
		{ method: 'get', onComplete: responseReloadTimezoneList }
	);
}

function responseReloadTimezoneList(response)
{
	$("panel_timezone").innerHTML = response.responseText;
	$("timezone").style.width = "280px";

	// set previous timezone ID
	$("timezone").value = parseInt(_timezone_id);
}

function reloadStateList()
{
	// keep current state ID
	_state_id = $("state").value;

	new Ajax.Request(
		"ziptotimezone/ajaxState/country_id/" + $("country").value,
		{ method: 'get', onComplete: responseReloadStateList }
	);
}

function responseReloadStateList(response)
{
	$("panel_state").innerHTML = response.responseText;

	// set previous state ID
	$("state").value = parseInt(_state_id);
}
