var test;
function showUsernameErrorInfo(errorInfo) {
	var usernameNote = document.getElementById("usernameNote");
	if (errorInfo == 1) {
		text = UserNameErorNullText;
	} else if (errorInfo == 2) {
		text = UserNameErorFormText;
	} else if (errorInfo == 3) {
		text = UserNameEmailUsedText;
	} else if (errorInfo == 4) {
		text = UserNamePhoneUsedText;
	}
	usernameNote.innerHTML = text;
	usernameNote.className = "note_error";
}

function showUsernameNote() {
	document.getElementById("username").className = 'input_normal';
	var usernameNote = document.getElementById("usernameNote");
	usernameNote.innerHTML = UserNameNoteText;
	usernameNote.className = 'note_text';
}

function showPasswordNote() {
	document.getElementById("password").className = 'input_normal';
	var passwordNote = document.getElementById("passwordNote");
	passwordNote.innerHTML = PasswordNoteText;
	passwordNote.className = 'note_text';
}

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

function showConfirmPasswordNote() {
	document.getElementById("confirmPassword").className = 'input_normal';
	var confirmPasswordNote = document.getElementById("confirmPasswordNote");
	confirmPasswordNote.innerHTML = ConfirmPasswordText;
	confirmPasswordNote.className = 'note_text';
}

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

function showNicknameNote() {
	document.getElementById("nickname").className = 'input_normal';
	var nicknameNote = document.getElementById("nicknameNote");
	nicknameNote.innerHTML = NickNameNoteText;
	nicknameNote.className = 'note_text';
}

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

function showCaptchaNote() {
	document.getElementById("register_captcha").className = 'input_captcha_normal';
	var registerCaptchaNote = document.getElementById("registerCaptchaNote");
	registerCaptchaNote.innerHTML = CaptChaNoteText;
	registerCaptchaNote.className = 'note_text';
}

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

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

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

function showResponseForEmail(text) {
	if (text == 'valid') {
		setUsernameValid(true);
		showOK("usernameNote");
	} else {
		setUsernameValid(false);
	}
}

function showResponseForPhone(text) {
	if (text == 'valid') {
		setUsernameValid(true);
		showOK("usernameNote");
	} else {
		setUsernameValid(false);
	}
}

function initCaptchaNote() {
	var registerCaptchaNote = document.getElementById("registerCaptchaNote");
	registerCaptchaNote.innerHTML = "&nbsp";
	registerCaptchaNote.className = "note_text";
}

function checkUsernameValid() {
	var username = document.getElementById('username').value;

	if (isEmptyString(username)) {
		showUsernameErrorInfo(1);
		return false;
	}

	var reg = /@/;
	if (reg.test(username)) {
		if (isEmailValid(username)) {
			checkEmailPhoneAvailability(username, 1);
			return getUsernameValid();
		} else {
			showUsernameErrorInfo(2);
			return false;
		}
	} else {
		if (isPhoneValid(username)) {
			checkEmailPhoneAvailability(username, 2);
			return getUsernameValid();
		} else {
			showUsernameErrorInfo(2);
			return false;
		}
	}
}

function callbackEmail(data) {
	if (data.code == '200') {
		showResponseForEmail('valid');
	} else {
		showResponseForEmail('invalid');
		showUsernameErrorInfo(3);
	}
};
function callbackPhone(data) {
	if (data.code == '200') {
		showResponseForPhone('valid');
	} else {
		showResponseForPhone('invalid');
		showUsernameErrorInfo(4)
	}
};
function ShowRegisterRespose(data) {
	if (data.code == '200') {
		console.log('register succ', data);
		test = data;
		uid = data.result.uid;
		token = data.result.token;
		nickname = data.result.nickname;
		username = '';
		if (data.result.hasOwnProperty('mobile')) {
			type = 'mobile';
			username = data.result.mobile;
		} else {
			type = 'email';
			username = data.result.email;
		}
		var data = {
			"result" : 'succ',
			"uid" : uid,
			'token' : token,
			'nickname' : nickname,
			'type' : type,
			'username' : username
		}
		$$.post("/ZTECloud/RegisterRes", data, function() {
		});
	} else {
		var data = {
			"result" : 'fail'
		}
		$$.post("/ZTECloud/RegisterRes", data, function() {
		});
	}
}

function checkEmailPhoneAvailability(userName, type) {

	var url = "https://cloud.ztedevices.com:8445/authorise/checkuserinfo?";
	if (type == 1) {
		url = url + 'email=' + userName + "&callback=callbackEmail"
	} else {
		url = url + 'phone=' + userName + "&callback=callbackPhone"
	}
	var script = document.createElement('script');
	script.setAttribute('src', url);
	document.getElementsByTagName('head')[0].appendChild(script);

}

function checkPasswordValid() {

	var password = document.getElementById('password').value;

	if (isEmptyString(password)) {
		showPasswordErrorInfo(PasswordErorNullText);
		return false;
	}
	if (isPasswordValid(password)) {
		showOK("passwordNote");
		return true;
	} else {
		showPasswordErrorInfo(PasswordErorFormText);
		return false;
	}
}

function checkConfirmPasswordValid() {

	var password = document.getElementById("password").value;
	var confirmPassword = document.getElementById("confirmPassword").value;

	if (isEmptyString(confirmPassword)) {
		showConfirmPasswordErrorInfo(ConfirmPasswordNoteText);
		return false;
	}
	if (isPasswordValid(confirmPassword)) {
		if (password != confirmPassword) {
			showConfirmPasswordErrorInfo(ConfirmPasswordDiffText);
			return false;
		}
		showOK("confirmPasswordNote");
		return true;
	} else {
		showConfirmPasswordErrorInfo(ConfirmPasswordText);
		return false;
	}
}

function checkNicknameValid() {

	var newNickname = document.getElementById('nickname').value;

	if (isEmptyString(newNickname)) {
		initNicknameNote();
		return true;
	}
	if (isNicknameValid(newNickname)) {
		showOK("nicknameNote");
		return true;
	} else {
		showNicknameErrorInfo(NickNameFormText);
		return false;
	}
}

function initNicknameNote() {
	var nicknameNote = document.getElementById("nicknameNote");
	nicknameNote.innerHTML = NickNameRule;
	nicknameNote.className = "note_text";
}

function reloadCapImg() {
	var captCha = document.getElementById("register_captcha_image");
	captCha.src = "http://cloud.ztedevices.com/zcloud/captcha.show"
}

function checkCaptchaValid() {
	var captcha = document.getElementById("register_captcha").value;
	if (isEmptyString(captcha)) {
		showRegisterCaptchaErrorInfo(CaptChaNoteText);
		return false;
	} else {
		initCaptchaNote();
		return true;
	}
}

function isAgreementCheckboxOn() {

	var agreementCheckbox = document.getElementById("agreementCheckbox");

	if (agreementCheckbox.checked) {
		initAgreementNote();
		return true;
	} else {
		showCheckboxErrorInfo(PlsAcceptAgreement);
		return false;
	}

}

function DoRegister(url) {

	url = url + "&callback=ShowRegisterRespose"

	var script = document.createElement('script');
	script.setAttribute('src', url);
	document.getElementsByTagName('head')[0].appendChild(script);
}