var signin_temp = "" ;

$(document).ready(function()
{
	$('#btnLogin').click(function () {
		onSignin($(this));
	});

	$('#btnPwdOk').click(function () {
		changePwd();
	});
	$('#txtCode').focus() ;
}) ;

function resetPassword(){

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

	if (confirm("Confirm you want to reset the password?")){
		var data = { "type": "RESET_PWD","id": $('#txtCode').val()} ;
		var url = "request.pzx?c=" + mobile_signin_url + "&d=" + new Date().getTime() ;
		callServer(url,"json",data,onResetResponse,null) ;
		$("#spanResetPwd").html("Processing..");
	}
}

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

	var data = { "type": "q","id": $('#txtCode').val(), "pwd1": $('#txtPwd').val()} ;
	var url = "request.pzx?c=" + mobile_signin_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onSigninResponse,obj) ;
}

function onResetResponse(obj, resp){
	$("#spanResetPwd").html("Reset Password");
	if (resp.status == "9") {
		alert(resp.mesg);
		return;
	}

	alert(resp.mesg);
}

function onSigninResponse(obj,data) {
	if (data == null)
	{
		alert('invalid response : ' + JSON.stringify(data)) ;
	}
	else
	{
		if (data.status == '0') {
			openSite(data.data + "&d=" + new Date().getTime());
		} else if (data.status == '6') {
			signin_temp = data.data ;
			data.data = "" ;
			$('#sbg-pwd').modal('show') ;
		} else if (data.status == '8') {
			if (checkPwd()) {
				signin_temp = data.data ;
				var data = { "type": "i","id": $('#txtCode').val(), "pwd": encryptPwd($('#txtPwd').val()) } ;
				var url = "request.pzx?c=" + mobile_signin_url + "&d=" + new Date().getTime() ;
				data.data = "" ;
				signin_temp = "" ;
				callServer(url,"json",data,onSigninResponse,obj) ;
			}
		}
		else {
			$('#err_mesg').text(data.mesg) ;
		}
	}
}
function createPwd() {
	$( "#sbg-pwd" ).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() == ''){
		alert("Please enter password.");
		$('#txtPwd1').focus();
	}
	else if ($('#txtPwd1').val() == $('#txtPwd2').val()) {
		$('#sbg-pwd').modal('hide');
		var data = { "type": "n","id": $('#txtCode').val(), "pwd": encryptPwd($('#txtPwd1').val()) } ;
		var url = "request.pzx?c=" + mobile_signin_url + "&d=" + new Date().getTime() ;
		signin_temp = "" ;
		callServer(url,"json",data,onSigninResponse,null) ;
	} else {
		alert("Password does not match. Please try again.");
		$('#txtPwd2').focus();
	}
}

function onLogout() {
	var data = { "type": "o"} ;
	var url = "request.pzx?c=" + mobile_signin_url  +"&d=" + new Date().getTime() ;
	callServer(url,"json",data,onLogoutResponse, null) ;
}
function onLogoutResponse(data) {
	openSite("index.pzx");
}