var forgot_password_emailValidation = '';
$(function() {
	var width = 350;
	var height = 220;
	var confirm_width = 500;
	var confirm_height = 170;
	if ($.browser.msie){
		width = 350;
		height = 300;
		confirm_width = 500;
		confirm_height = 250;
	}
	$("#forgot_password_modal").dialog( {
		bgiframe : true,
		autoOpen : false,
		height : height,
		width : width,
		modal : true,
		buttons: {
		'Trimite': function() {
			send_forgot_password();
		},
		'Renunta': function() {
			$(this).dialog('close');
		}
	}
	});

	$("#forgot_password_modal_confirm").dialog( {
		bgiframe : true,
		autoOpen : false,
		height : confirm_height,
		width : confirm_width,
		modal : true,
		buttons: {
		OK: function() {
			$(this).dialog('close');
		}
	}
	});	
});

function open_forgot_password() {
	$.post(
		'forgot_password_ajax.php',
		{action:'forgot_password'},
		function(data) {
			$('#forgot_password_modal').html(data);
			$('#forgot_password_modal').dialog('option', 'title', 'Ai uitat parola?');
			$('#forgot_password_modal').dialog('open');
			
			forgot_password_emailValidation = new LiveValidation('forgott_email', {onlyOnBlur:true, onInvalid:function(){ this.insertMessage( this.createMessageSpan() ); }});
			forgot_password_emailValidation.add(Validate.Presence);
			forgot_password_emailValidation.add(Validate.Email);
		});
}

function send_forgot_password(){
	var areAllValid = LiveValidation.massValidate( [ forgot_password_emailValidation ] );
	if(areAllValid){
		$.post(
			'forgot_password_ajax.php',
			{action:'send_email',email:$('#forgott_email').val()},
			function(response){
				if(response.ok){
					$('#forgot_password_modal').dialog('close');
					$('#forgot_password_modal_confirm').html(response.message);
					$('#forgot_password_modal_confirm').dialog('option', 'title', 'Confirmare!');
					$('#forgot_password_modal_confirm').dialog('open');
				}
				else{
					$('#forgot_password_modal_confirm').html(response.message);
					$('#forgot_password_modal_confirm').dialog('option', 'title', 'Error!');
					$('#forgot_password_modal_confirm').dialog('open');
				}
			},
			'json'
		);
	}
}