
function storeCookies() {
	var prefsForm = $("tpgNotifierPrefsPanel");
	var cookie = new TPG.Cookie("tpgNotifierPrefs");
	cookie.profileIndex = TPG.getCheckedValue(prefsForm.sndFile);
	cookie.schemeIndex = TPG.getCheckedValue(prefsForm.sndScheme);
	cookie.enableLogger = $('showLogMessages').checked;
	cookie.customSoundPath = $("customSoundPath").value;
	cookie.notificationMethod = TPG.getCheckedValue(prefsForm.notificationMethod);
	cookie.store(30);
	//return false;
}

function init() {
	var prefsForm = $("tpgNotifierPrefsPanel");
	var notificationMethod;
	if (prefsForm) {
		notificationMethod = TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'notificationMethod', "liveregion", ["flash", "alerts", "liveregion"]);
		prefsForm.onsubmit = storeCookies;
		TPG.setCheckedValue(prefsForm.notificationMethod, notificationMethod);
		if (notificationMethod != "flash") toggleSoundFormFields(true);
		TPG.setCheckedValue(prefsForm.sndFile, TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'profileIndex', 0));
		TPG.setCheckedValue(prefsForm.sndScheme, TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'schemeIndex', 0));
		
		//Safari doesn't support onfocus, opera doesn't fire onclick when change radio buttons with the keyboard. For now we'll just use both	
		$('showLogMessages').checked = TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'enableLogger', false);
		$("customSoundPath").value = TPG.Cookie.getCookieParam('tpgNotifierPrefs', 'customSoundPath', "");
		$('useFlash').onclick = function() {toggleSoundFormFields(false)};
		$('useAlerts').onclick = function() {toggleSoundFormFields(true)};
		$('useLiveRegion').onclick = function() {toggleSoundFormFields(true)};
		$('useFlash').onfocus = function() {toggleSoundFormFields(false)};
		$('useAlerts').onfocus = function() {toggleSoundFormFields(true)};
		$('useLiveRegion').onfocus = function() {toggleSoundFormFields(true)};
	}
}

function toggleSoundFormFields(disable) {
	TPG.setDisabledFormSegment('soundFileOptions', disable);
	TPG.setDisabledFormSegment('soundSchemeOptions', disable);	
}

window.onload = init;

