
function signupWin() {
    $j('#signup').dialog('open');
}
function forgotWin() {
    $j('#forgot').dialog('open');
}

function login() {
    $j('#login_form').submit();
    return;
}

function j_alert(message) {
    $j("#alertmsg").text(message);
    $j("#alert").dialog('open');
}

function showForgotSuccess() {
    // $j("#em").text('');
}

jQuery(function(){
    $j('#alert').dialog(
    {
	autoOpen: false, 
	modal: true,
	buttons: {
		'OK':function(){
		    $j(this).dialog('close')
		}
	}
    });

    $j('#signup').dialog(
    {
	title: 'Register',
	autoOpen: false, 
	height: 365,
	width: 300,
	modal: true,
	buttons: {
	    'Register':function(){
	    	var success = true;
	    	if($j('#reg_pw').val() != $j('#reg_vf').val()) {
		    $j('#regmsg').text(messages.register.passwordsDontMatch);
		    success = false;
		}
		if($j('#reg_em').val().length == 0) {
		    $j('#regmsg').text(messages.register.emailRequired);
		    success = false;
		}
		if(success) {
		    $j.ajax({
			type: "post",
			url: '/account/register.php',
			data: ({
			    'first_name' : $j('#reg_fn').val(),
			    'last_name' : $j('#reg_ln').val(),
			    'email' : $j('#reg_em').val(),
			    'password' : $j('#reg_pw').val(),
			    'verify' : $j('#reg_vf').val(),
			    'telephone' : $j('#reg_tl').val(),
			    'admin_name' : $j('#admin_name').val()
			    }),
			dataType: 'json',
			success: function(data) {
			    if(data.success == true) {
				top.document.location.replace('/');
			    } else {
			        $j('#regmsg').text(eval(data.message));
			    }
			}
		    });
		    clearForm('#regform');
		    $j('#regmsg').text('');
		    $j(this).dialog('close')
		}
	    },
	    'Cancel': function() {
		clearForm('#regform');
		$j('#regmsg').text('');
		$j(this).dialog('close');
	    }
	},
	close: function() {
		$j('#regmsg').text('');
		clearForm('#regform');
	}
    });
    $j('#forgot').dialog({
	title: 'Forgot your Password?',
	autoOpen: false,
	height: 251,
	width: 300,
	modal: true,
	buttons: {
	    'Reset Password': function() {
	    	var e = $j("#forgot_email").val();
		if(e!= null && e.length > 0) {
		    $j.ajax({
			type: "post",
			url: '/account/retrieve.php',
			data: ({'email' : e}),
			dataType: 'json',
			success: function(data) {
			    if(data.success == true) {
				// $j("#em").text(p);
				j_alert(messages.login.successfulAccountRetrieval + e);
			    } else {
			        j_alert(eval(data.message));
			    }
			},
			failure: function(data) {
			    j_alert(data);
			}
		    });
		    clearForm('#forgotform');
		    $j(this).dialog('close');
		}
	    },
	    'Cancel': function() {
		clearForm('#forgotform');
		$j(this).dialog('close');
	    }
	},
	close: function() {
	    clearForm('#forgotform');
	}
    });
    



});

function clearForm(formId) {
$j(':input',formId)
 .not(':button, :submit, :reset, :hidden')
 .val('')
 .removeAttr('checked')
 .removeAttr('selected');
    
}
