var signin_temp = "" ;
$(document).ready(function() 
{ 
	$('#btnLogin').click(function () {
		onSignin($(this));
	});	
	$('#btnChangePwd').click(function () {
		changePwd();
	});
	$('#txtCode').focus() ;
}) ;

function resetPassword(){
	
	if($('#txtCode').val()==""){
		$('#err_code').show() ;
		$('#txtCode').focus() ;
		$('#err_mesg').text("Username can not be blank.") ;
		return false ;
	} else {
		$('#err_code').hide() ;
	}

	Swal.fire({
        title: "Reset Password Confirmation",
        text: "Confirm you want to reset the password?",
        type: "warning",
        showCancelButton: true,
        confirmButtonText: "Yes",
        cancelButtonColor: '#d33',
    }).then(function(result) {
        if (result.value) {
            var data = { "type": "RESET_PWD","id": $('#txtCode').val()} ;
			var url = "request.pzx?c=" + signin_url + "&d=" + new Date().getTime() ;	
			callServer(url,"json",data,onResetResponse,null) ;
        }
    });
}

function onSignin(obj) {
	$('#err_mesg').text('') ;
	$('#err_summary').text('') ;
	if ($('#txtCode').val()=="")
	{
		$('#err_code').show() ;
		$('#txtCode').focus() ;
		$('#err_mesg').text("Username can not be blank.") ;
		return false ;
	}
	else 
		$('#err_code').hide() ;
			
	var data = { "type": "q","id": $('#txtCode').val(), "cmd": $('#txtCommand').val(), } ;
	var url = "request.pzx?c=" + signin_url + "&d=" + new Date().getTime() ;	
	callServer(url,"json",data,onSigninResponse,obj) ;
}

function onResetResponse(obj, resp){
	if(resp.status == "9") {
		Swal.fire("Error",resp.mesg,"error");
		return;
	}
	
	Swal.fire("Success",resp.mesg,"success");
}
	
function onSigninResponse(obj,data) {
	if (data == null) {
		Swal.fire("Error",'invalid response : ' + JSON.stringify(data),"error");
	}
	else 
	{
		if (data.status == '0') {
			openSite(data.data + "&d=" + new Date().getTime());
		} else if (data.status == '6') {
			signin_temp = data.data ;
			data.data = "" ;
			$('#modalNewPassword').modal('show') ;
		} else if (data.status == '8') {
			if (checkPwd()) {
				signin_temp = data.data ;
				var data = { "type": "i","id": $('#txtCode').val(), "pwd": encryptPwd($('#txtPwd').val()), "cmd": $('#txtCommand').val(), } ;
				var url = "request.pzx?c=" + signin_url + "&d=" + new Date().getTime() ;	
				data.data = "" ;
				signin_temp = "" ;
				callServer(url,"json",data,onSigninResponse,obj) ;
			}
		}
		else {
			$('#err_mesg').text(data.mesg) ;
		}
	}
}
function createPwd() {
	$( "#modalNewPassword" ).dialog( "open" );
}
function checkPwd() {
	if ($('#txtPwd').val()=="") {
		$('#err_pwd').show() ;
		$('#txtPwd').focus() ;
		$('#err_mesg').text("Password can not be blank.") ;
		return false ;
	} else {
		$('#err_pwd').hide() ;
	}
	return true ;
}
function encryptPwd(pwd) {
	return strToHex(xxtea_encrypt(pwd,signin_temp)) ;
}
function changePwd() {
	if($('#txtPwd1').val()==""){
		Swal.fire("Warning","Please enter password.","warning");
		$('#txtPwd1').focus();
	}
	else if ($('#txtPwd1').val() == $('#txtPwd2').val()) {
		$('#modalNewPassword').modal('hide');
		var data = { "type": "n","id": $('#txtCode').val(), "pwd": encryptPwd($('#txtPwd1').val()) } ;
		var url = "request.pzx?c=" + signin_url + "&d=" + new Date().getTime() ;	
		signin_temp = "" ;
		callServer(url,"json",data,onSigninResponse,null) ;
	} else {
		Swal.fire("Warning","Password does not match.","warning");
		$('#txtPwd2').focus();
	}
}