var signin_temp = "" ;

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

function resetPassword(){	
	if($('#txtCode').blank()){
		$('#txtCode').focus() ;
		alert("Username can not be blank.") ;
		return false ;
	}
	
	if (confirm("Confirm you want to reset the password?")){
		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) {
	if ($('#txtCode').val() == '')
	{
		$('#txtCode').focus() ;
		alert("Username can not be blank.") ;
		return false ;
	}
	var data = { "type": "q","id": $('#txtCode').val()} ;
	var url = "request.pzx?c=" + signin_url + "&d=" + new Date().getTime() ;	
	callServer(url,"json",data,onSigninResponse,obj) ;
}

function onResetResponse(obj, resp){
	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=" + signin_url + "&d=" + new Date().getTime() ;	
				data.data = "" ;
				signin_temp = "" ;
				callServer(url,"json",data,onSigninResponse,obj) ;
			}
		}
		else {
			alert(data.mesg) ;
		}
	}
}
function createPwd() {
	$( "#sbg-pwd" ).dialog( "open" );
}
function checkPwd() {
	if ($('#txtPwd').val() == '') {
		$('#txtPwd').focus() ;
		alert("password can not be blank.") ;
		return false ;
	}
	return true ;
}
function encryptPwd(pwd) {
	return strToHex(xxtea_encrypt(pwd,signin_temp)) ;
}
function changePwd() {
	if($('#txtPwd1').blank()){
		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=" + 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();
	}
}