var WindowObjectReference = null;

function watchForm(form) {
	var serialized = $(form).serialize();
	
	window.onbeforeunload = function unloadBehavior(event) {
		if ($(form).serialize() != serialized) {
			var msg = "Your changes are not saved.";
			if (event) event.returnValue = msg;
			return msg;
		}
	};

	Event.observe(form, "submit", function(){
		window.onbeforeunload = null;});
}

function toggleState() {
	switch($('country').value) {
	case 'US':
		$('stateother').hide();
		var options = $('state').options;
		var matchStr = $F('stateother').toLowerCase();
		if (!$F('state')) {
			for(var i=0; i<options.length; i++) {
				if (options[i].value.toLowerCase() == matchStr ||
						options[i].innerHTML.toLowerCase() == matchStr) {
					$('state').selectedIndex = i;
					break;
				}
			}
		}
		$('state').show();
	    break;
	default:
		$('state').hide();
		$('stateother').show()
	}
}

function zeroFill(number, length) {
	number = number.toString();
	while(number.length < length) {
		number = "0" + number;
	}
	return number;
}

function updateCounter() {
	var ms = new Date().getTime() - startTime;
	var hours;
	var minutes;
	var seconds;

	hours = parseInt(ms / 3600000);
	ms -= hours * 3600000;
	minutes = parseInt(ms / 60000);
	ms -= minutes * 60000;
	seconds = parseInt(ms / 1000);
	ms -= seconds * 1000;
	ms = parseInt(ms / 100)

	$('counter').innerHTML = zeroFill(hours,2) + ':' + zeroFill(minutes,2) + ':' + zeroFill(seconds,2) + '.' + ms;

	window.setTimeout(updateCounter, 100)
}

function openRequestedPopup(strUrl, strWindowName) {
	if (WindowObjectReference == null || WindowObjectReference.closed) {
		WindowObjectReference = window.open(strUrl, strWindowName,
				"locationbar=no,resizable=yes,scrollbars=yes,status=yes,width=950,height=595");
	}
	WindowObjectReference.focus();
}
