/*extern KIC, Ajax, $ */

if(typeof Session == "undefined") { var Session = {}; }

Session.sendLoginRequest = function() {
	var username = $('iUsername').value;
	var password = $('iPassword').value;

	if ( username === '' || password === '' ) {
		UI.Session.displayErrorMessage( 'Please provide username and password.' );
		return;
	}

	UI.Session.displayErrorMessage( '' );
	UI.disableButton( 'bLogin', 'SENDING REQUEST...' );

	var nav = '';
	var loc = window.location.toString();
	var index = loc.indexOf("?nav=");
	if (index >= 0) {
		nav = loc.substring(index+5);
	}
	Services.Session.login( username, password, nav, Session.handleLoginRequest );

	return false;
};

Session.handleLoginRequest = function() {
	if ( KIC.Services.isReady() ) {
		var response = parseJSON( Ajax.getResponse() );		// not parseJSONWithLogin()
		if ( response.Success ) {
			window.location = response.Destination;
		}
		else {
			UI.Session.displayErrorMessage( response.Message );
			UI.enableButton( 'bLogin', 'LOGIN' );
		}
	}
};

Session.sendForgotPasswordRequest = function() {
	var username = $('iUsername').value;
	if ( username !== '' ) {
		$('sError').innerHTML = '';
		UI.disableButton( 'bSubmit', 'SENDING REQUEST...' );
		Services.Session.forgotPassword( username, Session.handleForgotPasswordRequest );
	}
	else {
		$('sError').innerHTML = 'Please provide a username or email address.<br><br>';
	}
};

Session.handleForgotPasswordRequest = function() {
	if ( KIC.Services.isReady() ) {
		var response = parseJSON( Ajax.getResponse() );		// not parseJSONWithLogin()
		if ( response.Success ) {
			$('dForm').style.display = 'none';
			$('dResponse').style.display = '';
		}
		else {
			$('sError').innerHTML = response.Message+'<br><br>';
			UI.enableButton( 'bSubmit', 'SUBMIT' );
		}
	}
};

Session.sendResetPasswordRequest = function() {
	var password = $('iPassword').value;
	var conf = $('iConfirm').value;

	if ( password === '' ) {
		$('sError').innerHTML = 'Please provide a new password.<br><br>';
	}
	else if ( password != conf ) {
		$('sError').innerHTML = 'New password and confirmation must match.<br><br>';
	}
	else {
		$('sError').innerHTML = '';
		UI.disableButton( 'bSubmit', 'Sending Request...' );

		//var nav = '';
		//var loc = window.location.toString();
		//var index = loc.indexOf("?nav=");
		//if (index >= 0) {
		//	nav = loc.substring(index+5);
		//}
		var nav = $('iNav').value

		Services.Session.resetPassword( password, nav, Session.handleResetPasswordRequest );
	}
};

Session.handleResetPasswordRequest = function() {
	if ( KIC.Services.isReady() ) {
		var response = parseJSON( Ajax.getResponse() );		// not parseJSONWithLogin()
		if ( response.Success ) {
			window.location = response.Destination;
		}
		else {
			// display error message, and re-enable submit button
			$('sError').innerHTML = response.Message+'<br><br>';
			UI.enableButton( 'bSubmit', 'CHANGE MY PASSWORD' );
		}
	}
};


