Event.observe(window, 'load', init);

function init() {
	// Debugging turned on
	//$('debug').style.display = 'block';	
}

var global_sesskey = '49174c1096d21d13ea1dd86dfd2d8684';

function checkLogin(transport) {

	var notice = $('notice');
	if ((m = transport.responseText.match(/user\=(.*);sess_id\=(.*);/))) {
		var user = m[1];
		var sess_id = m[2]
		notice.update('<div id=\'notice_big\'>Logged in as ' + user + '. (<a href=\'https://www.sandiadistrict.org/index.php?page=account\'>My Account</a> | <a id=\'logout_href\' onclick=\'return false;\' href=\'/inc/ajax/login.php?logout=true\'>Logout</a>)</div>').show();
		Event.observe('logout_href', 'click', handleLogout);

		// Keep our global session key
		global_sesskey = sess_id;
						
	} else {

		// Place login form
		notice.update("<div id='notice_big'><a href='https://www.sandiadistrict.org/index.php?secure'><img border='0' src='images/secure_login.png' align='absmiddle' alt='Secure Login'></a></div><div id='notice_small'><a href='https://www.sandiadistrict.org/index.php?page=create_account'>Create a new account.<img border='0' src='images/user_icon_new.gif' align='absmiddle'></a></div>").show();

	}

	if(document.getElementById("mbframe")) {	
		location = "/index.php?tab=&page=mb";
	}
	
}

function handleLogin() {
	var notice = $('notice_big');
	var params = Form.serialize($("login_form"));
	params += "&login=true&sesskey="+global_sesskey;
	
	notice.update('Checking login . . .');
	
	new Ajax.Request( '/inc/ajax/login.php', {
		method: 'post',
		parameters: params,
		onSuccess: checkLogin
	})	
	return false;
}

function handleLogout() {
	var notice = $('notice_big');
	notice.update('Logging out . . .');

	new Ajax.Request( '/inc/ajax/login.php', {
		method: 'post',
		parameters: 'sesskey='+global_sesskey+"&logout=true",
		onSuccess: checkLogin
	})
}


