$(document).ready(function(){
	LoginReady();
	
});

function LoginReady(){
	$('#butLoginSubmit').click(LoginAuth);
	
	$("#txtPassword").keydown(function(event){
		if(event.keyCode==13){
			LoginAuth();
		}
	});
	
	$("#forgetPassword").click(function(){
		$.getJSON(hosturl+'/Bend/GetPasswordRecoveryView',{},function(data){
			if(data.status=="success"){
				$("#divLogin").html(data.data);
				PasswordRecoveryReady();
				
			}else{
				FmvAlert(data.data);
			}
		});
	});
}


function PasswordRecoveryReady(){
	$("#txtUserName").click(function(){
		$("#txtUserName").val("");
		$("#txtEmail").val("");
	});
	
	$("#txtEmail").click(function(){
		$("#txtUserName").val("");
		$("#txtEmail").val("");
	});
	
	$("#butPasswordRetrieve").click(function(){
		username = $("#txtUserName").val();
		email = $("#txtEmail").val();
		
		$.ajax({
			url:hosturl+'/Bend/RetrievePassword',
			data:{'userName':username,'email':email},
			type:'POST',
			dataType:'json',
			success:function(data){
						if(data.status=="success"){
							FmvAlert("Please check your email:[ "+ data.email+" ] and read the instruction how to retrieve password.")
						}else{
							FmvAlert(data.data);
						}
					}
		});
	});
}


function LoginAuth(){
	this.userName = $('#txtUserName').val();
	this.password = $('#txtPassword').val();
	this.rememberMe = $('#chkRememberMe:checked').val();
	
	
	
	$.ajax({
		url:'Bend/Auth',
		type :"POST",
		data: {userName:this.userName,password:this.password,remember:this.rememberMe},
		dataType:"json",
		success:function(data){
			
			if(data.status=="success"){
				window.location = location.href;
				
			}else{
				FmvAlert(data.data);
			}
		}
		
	});
}
