var login_temp = "" ;
//var L_VERSION ="669";
$(document).ready(function() 
{ 
	$('#btnChangePwd').button().bind('click',this,onChangePwd) ;
	$('#btnLogin').button().bind('click',this,onLogin) ;
	$('#txtName').focus() ;
}) ;

/*function version() {
	var data = { "type": L_VERSION} ;
	var url = "request.pzx?c=" + login_url + "&d=" + new Date().getTime() ;	
	callServer(url,"json",data,onLoginResponse,null) ;
	
	
	
}*/


function onChangePwd() {
if ($('#txtPwd1').val()=='') {
	swal.fire("Password can't be blank.");
	$('#txtPwd1').focus();
}else if ($('#txtPwd1').val() == $('#txtPwd2').val()) {
	$("#modalNewPassword").modal('hide');
	var data = { "type": "n","id": $('#txtName').val(), "pwd": encryptPwd($('#txtPwd1').val()) } ;
	var url = "request.pzx?c=" + login_url + "&d=" + new Date().getTime() ;	
	login_temp = "" ;
	callServer(url,"json",data,onLoginResponse,null) ;
	} else {
		swal.fire("Password does not match. Please try again.");
		$('#txtPwd2').focus();
	}
}

function onLogin(obj) {
	$('#err_mesg').text('') ;
	$('#err_summary').text('') ;
	if ($('#txtName').val()=='')
	{
		$('#err_name').show() ;
		$('#txtName').focus() ;
		$('#err_mesg').text("user name can not be blank.") ;
		return false ;
	}
	else {
		$('#err_name').hide() ;
	}
			
	
	var data = { "type": "q","id": $('#txtName').val()} ;
	var url = "request.pzx?c=" + login_url + "&d=" + new Date().getTime() ;	
	callServer(url,"json",data,onLoginResponse,obj) ;
}
	
function onLoginResponse(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') {
			login_temp = data.data ;
			data.data = "" ;
			createPwd() ;
		} else if (data.status == '8') {
			if (checkPwd()) {
				login_temp = data.data ;
				var data = { "type": "i","id": $('#txtName').val(), "pwd": encryptPwd($('#txtPwd').val()) } ;
				var url = "request.pzx?c=" + login_url + "&d=" + new Date().getTime() ;	
				data.data = "" ;
				login_temp = "" ;
				callServer(url,"json",data,onLoginResponse,obj) ;
			}
		} 
		else {
			$('#err_mesg').text(data.mesg) ;
		}
	}
}
function createPwd() {
	$("#modalNewPassword").modal('show');
}
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,login_temp)) ;
}