/**
 * This file contains the code necessary to handle generic CMS Admin functions
 * and is a modification of the Ext version
 */

/**
 * 
 */
function onPasswordReset(data, textStatus)
{
	if (data === undefined) {
		alert("Your password could not be reset.");
		return false;
	}
	
	var obj_response;
	try {
		obj_response = JSON.parse(data);
	} catch (e) {
		alert("Your password could not be reset.");
		return false;
	}
	
	if (!obj_response.success) {
		alert("Sorry, an error has occurred while your password was being reset. Please contact your webmaster for further assistance or contact us at support@radiuswebtools.com");
		return false;
	}
	
	alert("Your password has been reset.");
	return true;
}

/**
 * 
 */
function resetPassword()
{
	$('#itr_forgotpassform').ajaxSubmit({
		type: "POST",
		success: function (data, text) {
			if (onPasswordReset(data, text)) {
				$('#itr_forgot_password').dialog('close');
			}
		}
	});
}

/**
 * 
 * 
 * @author Saint Wesonga <swesonga@intheround.com>
 * 
 * @param string userName Optional. The name of the user.
 * @return jQuery object
 */
function createForgotPasswordWindow(userName)
{
	//Handle the optional parameters
	if (userName === undefined) {
		userName = '';
	}
	
	$('#itr_forgotpass_username').val(userName);
	
	$('#itr_forgotpass_reset').click(function () {
		resetPassword();
		return false;
	});
	
	$('#itr_forgotpass_cancel').click(function () {
		$('#itr_forgot_password').dialog('close');
		return false;
	});
	
	$('#itr_forgot_password').dialog({
		title: 'Forgot my password',
		modal: true,
		autoOpen: false,
		width: 375,
		top: 70,
		dialogClass: 'itr_cms',
		resizeble: true
	});
	
	return $('#itr_forgot_password');
}

