
var isRegisterBtnValid = true;

function setRegisterBtnValid(flag){
	isRegisterBtnValid = flag;
}

function getRegisterBtnValid(){
	return isRegisterBtnValid;
}

/*function initNicknameNote(){
	var nicknameNote = document.getElementById("nicknameNote");
	nicknameNote.innerHTML = "æ˜µç§°ç”±1-20ä½ä¸­ã€è‹±æ–‡æˆ–æ•°å­—ç»„æˆ";
	nicknameNote.className = "note_text";
}*/

/*function initCaptchaNote(){
	var registerCaptchaNote = document.getElementById("registerCaptchaNote");
	registerCaptchaNote.innerHTML = "è¯·è¾“å…¥éªŒè¯ç ";
	registerCaptchaNote.className = "note_text";
}*/

function initAgreementNote(){
	var checkboxNote = document.getElementById("checkboxNote");
	checkboxNote.innerHTML = "";
	checkboxNote.className = "note_text";
}



/*function showNicknameNote(){
	document.getElementById("nickname").className = 'input_normal';
	var nicknameNote = document.getElementById("nicknameNote");
	nicknameNote.innerHTML='æ˜µç§°ç”±1-20ä½ä¸­ã€è‹±æ–‡æˆ–æ•°å­—ç»„æˆ';
	nicknameNote.className = 'note_text';
//	nicknameNote.style.color="black";
//	focusInput("nickname");
}*/

/*function showPasswordNote(){
	document.getElementById("password").className = 'input_normal';
	var passwordNote = document.getElementById("passwordNote");
	passwordNote.innerHTML='å¯†ç ç”±6-16ä½å­—æ¯ã€æ•°å­—æˆ–å­—ç¬¦ç»„æˆï¼ŒåŒºåˆ†å¤§å°å†™';
	passwordNote.className = 'note_text';
//	passwordNote.style.color="black";
//	focusInput("password");
}*/

/*function showConfirmPasswordNote(){
	document.getElementById("confirmPassword").className = 'input_normal';
	var confirmPasswordNote=document.getElementById("confirmPasswordNote");
	confirmPasswordNote.innerHTML="è¯·å†æ¬¡è¾“å…¥å¯†ç ";
	confirmPasswordNote.className = 'note_text';
//	confirmPasswordNote.style.color="black";
//	focusInput("confirmPassword");
}*/

/*function showCaptchaNote(){
	document.getElementById("register_captcha").className = 'input_captcha_normal';
	var registerCaptchaNote=document.getElementById("registerCaptchaNote");
	registerCaptchaNote.innerHTML="è¯·è¾“å…¥éªŒè¯ç ";
	registerCaptchaNote.className = 'note_text';
}*/

function checkUsernameFormat(){
	
	var username = document.getElementById('username').value;
	
	if(isEmptyString(username)){
		showUsernameErrorInfo(1);
		return false;
	}
	
	var reg = /@/;
	if(reg.test(username)){
		if(!isEmailValid(username)){
			showUsernameErrorInfo(2);
			return false;
		}		
	}else{
		if(!isPhoneValid(username)){
			showUsernameErrorInfo(2);
			return false;
		}
	}
	return true;
}

/*function checkNicknameValid(){
	
//	unFocusInput("nickname");
	
	var newNickname = document.getElementById('nickname').value;
	
	if(isEmptyString(newNickname)){
		initNicknameNote();
		return true;
	}
	if(isNicknameValid(newNickname)){
		showOK("nicknameNote");
		return true;
	} else{
		showNicknameErrorInfo("æ˜µç§°æ— æ•ˆï¼Œè¯·é‡æ–°è¾“å…¥");
		return false;
	}
}*/

/*function checkPasswordValid(){
	
//	unFocusInput("password");
	
	var password = document.getElementById('password').value;
	
	if(isEmptyString(password)){
		showPasswordErrorInfo("è¯·è¾“å…¥å¯†ç ");
		return false;
	}
	if(isPasswordValid(password)){
		showOK("passwordNote");
		return true;
	} else{
		showPasswordErrorInfo("å¯†ç æ— æ•ˆï¼Œè¯·é‡æ–°è¾“å…¥");
		return false;
	}
}*/

/*function checkConfirmPasswordValid(){
	
//	unFocusInput("confirmPassword");
	
	var password = document.getElementById("password").value;
	var confirmPassword = document.getElementById("confirmPassword").value;
	
	if(isEmptyString(confirmPassword)){
		showConfirmPasswordErrorInfo("è¯·è¾“å…¥ç¡®è®¤å¯†ç ");
		return false;
	}
	if(isPasswordValid(confirmPassword)){
		if(password != confirmPassword){
			showConfirmPasswordErrorInfo("ä¸¤æ¬¡å¯†ç ä¸ä¸€è‡´ï¼Œè¯·é‡æ–°è¾“å…¥");
			return false;
		}
		showOK("confirmPasswordNote");
		return true;
	} else{
		showConfirmPasswordErrorInfo("ç¡®è®¤å¯†ç æ— æ•ˆï¼Œè¯·é‡æ–°è¾“å…¥");
		return false;
	}
}*/

/*function checkCaptchaValid(){
	
//	unFocusInput("register_captcha");
	
	var captcha = document.getElementById("register_captcha").value;
	
	if(isEmptyString(captcha)){
		showRegisterCaptchaErrorInfo("è¯·è¾“å…¥éªŒè¯ç ");
		return false;
	} else{
		initCaptchaNote();
		return true;
	}
}*/

/*function isAgreementCheckboxOn(){
	
	var agreementCheckbox = document.getElementById("agreementCheckbox");
	
	if(agreementCheckbox.checked){
		initAgreementNote();
		return true;
	} else{
		showCheckboxErrorInfo("è¯·æŽ¥å—åè®®");
		return false;
	}

}*/

function isAllParameterValid(){
	var isUsenameValid = false;
	var isNicknameValid = false;
	var isPasswordValid = false;
	var isConfirmPwdValid = false;
	var isCaptchaValid = false;
	var isAgreementCheckboxValid = false;
	
	if(checkUsernameFormat()){
		isUsenameValid = true;
	}
	if(checkNicknameValid()){
		isNicknameValid = true;
	}
	if(checkPasswordValid()){
		isPasswordValid = true;
	}
	if(checkConfirmPasswordValid()){
		isConfirmPwdValid = true;
	}
	if(checkCaptchaValid()){
		isCaptchaValid = true;
	}
	if(isAgreementCheckboxOn()){
		isAgreementCheckboxValid = true;
	}
	
	return (isUsenameValid && isNicknameValid && isPasswordValid
			&& isConfirmPwdValid && isCaptchaValid && isAgreementCheckboxValid);
}

/*function checkboxStatu()) {
	
	var agreementCheckbox = document.getElementById("agreementCheckbox");
	if(agreementCheckbox.checked){
		document.getElementById("RegisterBtn").disabled = false;		
	} else{
		document.getElementById("RegisterBtn").disabled = true;
	}
}*/

/*function showOK(id){
	var note = document.getElementById(id);
	note.innerHTML = "&nbsp;";
	note.className = "note_OK";
//	note.style.color = "black";
}*/

/*function showNicknameErrorInfo(errorInfo){
//	document.getElementById("nickname").className = 'input_1_error';
	
	var nicknameNote = document.getElementById("nicknameNote");
	nicknameNote.innerHTML = errorInfo;
	nicknameNote.className = "note_error";
//	nicknameNote.style.color="red";
}*/

/*function showPasswordErrorInfo(errorInfo){
//	document.getElementById("password").className = 'input_1_error';
	
	var passwordNote = document.getElementById("passwordNote");
	passwordNote.innerHTML = errorInfo;
	passwordNote.className = "note_error";
//	passwordNote.style.color="red";
}*/

/*function showConfirmPasswordErrorInfo(errorInfo){
//	document.getElementById("confirmPassword").className = 'input_1_error';
	
	var confirmPasswordNote = document.getElementById("confirmPasswordNote");
	confirmPasswordNote.innerHTML = errorInfo;
	confirmPasswordNote.className = "note_error";
//	confirmPasswordNote.style.color="red";
}*/

/*function showRegisterCaptchaErrorInfo(errorInfo){
//	document.getElementById("register_captcha").className = 'input_3_error';
	
	var registerCaptchaNote = document.getElementById("registerCaptchaNote");
	registerCaptchaNote.innerHTML = errorInfo;
	registerCaptchaNote.className = "note_error";
//	registerCaptchaNote.style.color="red";
}*/

/*function showCheckboxErrorInfo(errorInfo){
	var checkboxNote = document.getElementById("checkboxNote");
	checkboxNote.innerHTML = errorInfo;
	checkboxNote.className = "note_error";
//	checkboxNote.style.color="red";
}*/

// function checkRegisterCaptcha(){
// var captcha = document.getElementById("register_captcha").value;
// $.ajax({ url: "checkCaptcha.ajax",
// dataType: "text",
// data: "captcha="+ captcha,
// success: function(res) {
// showResponseForCaptcha(res);
// },
// error: function(res) {
// showNoResponseForCaptcha();
// }
// });
// }


//
// function showResponseForCaptcha(text){
// var registerCaptchaNote = document.getElementById("registerCaptchaNote");
// if(text == "success"){
// registerCaptchaNote.innerHTML = "OK";
// registerCaptchaNote.style.color = "black";
// sendRegisterRequest();
// }else{
// registerCaptchaNote.innerHTML = "éªŒè¯ç é”™è¯¯";
// registerCaptchaNote.style.color = "red";
// reloadImg('register_captcha_image');
// }
// }



// function showNoResponseForCaptcha(){
// var registerCaptchaNote = document.getElementById("registerCaptchaNote");
// registerCaptchaNote.innerHTML = "éªŒè¯ç é”™è¯¯";
// registerCaptchaNote.style.color = "red";
// reloadImg('register_captcha_image');
// }

function changeCheckboxState(obj){
	var srcValue=obj.getAttribute("src");
	if(srcValue.indexOf("on")>0){
		obj.src = obj.src.replace("select_on.png","select_off.png");
	}else{
		obj.src = obj.src.replace("select_off.png","select_on.png");
		var checkboxNote=document.getElementById("checkboxNote");
		checkboxNote.innerHTML="";
		document.getElementById("OKBtn").focus();
	}
}

function loginCatchEnter(e) {
	var code;
	if (!e) {
		code = e.keyCode;
	} else {
		code = e.which;
		if (code == null) {
			code = e.keyCode;
		}
	}
	if (code == 13) { // ENTER
		backtoLogin();
		return false;
	}
	return true;
}

function backtoLogin(){
	window.location.href = "login.do";
}

function registerCatchEnter(e) {
	var code;
	if (!e) {
		code = e.keyCode;
	} else {
		code = e.which;
		if (code == null) {
			code = e.keyCode;
		}
	}
	if (code == 13) { // ENTER
		register();
		return false;
	}
	return true;
}

function register(){
	
	if(!getRegisterBtnValid()){
		return;
	}
	
	setRegisterBtnValid(false);
	
	if(!isAllParameterValid()){
		setRegisterBtnValid(true);
		return;
	}
	
	var username = document.getElementById("username").value;
	var nickname = document.getElementById("nickname").value;
	var password = document.getElementById("password").value;
	var captcha = document.getElementById("register_captcha").value;
	
	var basicUrl = "https://cloud.ztedevices.com:8445/authorise/userregister?";
	//var projectName =  document.getElementById("projectName").value;
	
	var reg = /@/;
	if(reg.test(username))
	{
		pre = 'email='
	}
	else
	{
		pre = 'phone='
	}

	userdata = pre + encodeURI(username) + "&pid=" + encodeURIComponent(nickname) + "&pwd=" + encodeURIComponent(password) +"&captcha=" + encodeURIComponent(captcha);
	url = basicUrl + userdata;
	
	DoRegister(url);
	return
    jQuery.getJSON(url + "&jsoncallback=?", function(data){
    	showRegisterResponse(data.authRes);
    	setRegisterBtnValid(true);
    }).error(function(){
    	showRegisterErrorInfo("å†…éƒ¨æœåŠ¡å™¨é”™è¯¯");
    	reloadImg("register_captcha_image");
	});
}

/*function showResponseForEmail(text){
	if(text=='valid'){
		setUsernameValid(true);
		showOK("usernameNote");
	} else{
		setUsernameValid(false);
		showUsernameErrorInfo("è¯¥é‚®ç®±å·²ä½¿ç”¨");
	}
}

function showResponseForPhone(text){
	if(text=='valid'){
		setUsernameValid(true);
		showOK("usernameNote");
	}else{
		setUsernameValid(false);
		showUsernameErrorInfo("è¯¥æ‰‹æœºå·å·²ä½¿ç”¨");
	}
}*/

function showRegisterResponse(responseText){
	var reloadImage = false;
	
	if (responseText == 'success') {
		var registerResult = document.getElementById("registerResult");
		registerResult.innerHTML = "";
		window.location.href = "mainframe.do";
	} else if(responseText == 'fail'){
		showRegisterErrorInfo("å†…éƒ¨æœåŠ¡å™¨é”™è¯¯");
		reloadImage = true;
	} else if(responseText == 'captcha_error'){
		showRegisterCaptchaErrorInfo("éªŒè¯ç é”™è¯¯ï¼Œè¯·é‡æ–°è¾“å…¥");
		reloadImage = true;
	} else if(responseText == 'user_used'){
		var username = document.getElementById('username').value;
		if(isEmailValid(username)){
			showUsernameErrorInfo("è¯¥é‚®ç®±å·²ä½¿ç”¨");
		} else if(isPhoneValid(username)){
			showUsernameErrorInfo("è¯¥æ‰‹æœºå·å·²ä½¿ç”¨");
		}
		reloadImage = true;
	} else if (responseText == "user_is_null") {
		showUsernameErrorInfo("é‚®ç®±/æ‰‹æœºå·ä¸èƒ½ä¸ºç©º");
	} else if (responseText == "password_is_null") {
		showPasswordErrorInfo("è¯·è¾“å…¥å¯†ç ");
	} else if (responseText == "captcha_is_null"){
		showRegisterCaptchaErrorInfo("è¯·è¾“å…¥éªŒè¯ç ");
	} else if(responseText == 'user_invalid'){
		showUsernameErrorInfo("é‚®ç®±/æ‰‹æœºå·æ— æ•ˆ");
	} else if(responseText == 'nickname_invalid'){
		showNicknameErrorInfo("æ˜µç§°æ— æ•ˆï¼Œè¯·é‡æ–°è¾“å…¥");
	} else if(responseText == 'password_invalid'){
		showPasswordErrorInfo("å¯†ç æ— æ•ˆï¼Œè¯·é‡æ–°è¾“å…¥");
	} else{
		showRegisterErrorInfo("é”™è¯¯ä¿¡æ¯:" + responseText);
		reloadImage = true;
	}
	if(reloadImage){
		reloadImg("register_captcha_image");
	}
}

function showRegisterErrorInfo(errorInfo){
	var registerResult = document.getElementById("registerResult");
	registerResult.innerHTML = errorInfo;
	registerResult.className = "note_error";
}

function showLoginPage(){
	window.location.href = "login.do";
}
function ReadAgreement(){
	// var data = {};
	// data.file= [];
	// $.post("/ZTECloud/ViewAgreement",data,function(){})
	window.location.href = "ZTEAgreement.html";
}
function LoadLoginPage(){
	//var data = {};
	//data.file= [];
	//$.post("/ZTECloud/LoadLoginPage",data,function(){})
	window.location.href = "login.html";
}
function BackToRegister(){
	history.go(-1);
	var data = {};
	data.file= [];
	$.post("/ZTECloud/BackToRegister",data,function(){})
}