

var isLoginBtnValid = true;

$(document).ready(function(){
	//if(login_data.autologin == 1 && login_data.access_tocken != "")
	//{
	//	LoginUserBaiduToken(login_data.access_tocken);
	//	getUserInfo();
		/*var access_tocken = data.access_tocken;
		var url = "https://openapi.baidu.com/xcloud/1.0/authorize?"
		var userdata = "response_type=token&client_id=bAGi5ceahOVMkirhACu7QMIL&redirect_uri=oob" + "&third_access_token=" 
			+ encodeURIComponent(zte_token) + "&display=page";
			
		url = url + userdata + "&callback=showLoginBaiduRespose";
		
		setLoginBtnValid(false);

		doLogin(url);*/
	//}
	//else
	//{
	//	setLoginBtnValid(true);
	//}
	$('#username').focus();
})
function LoginUseBaiduToken(baidu_toke)
{
	var data = {};
	data.file= [];
	$.post("/ZTECloud/LoginUseBaiduToken",data,function(){});
}
function getUserInfo(access_tocken){
	/*url = 'https://pcs.baidu.com/rest/2.0/pcs/file',
	get_data = {"access_token":access_tocken,
	"path":path,
	"method":"list",
	}
	$.get(url, get_data,
	  function(data){
		GetsubDir(path, data, storage, id);
	  });*/

	window.location.href = 'ClondMainView.html';
}
function getLocalLoginInformation(){
	var data;
	$.post("/ZTECloud/getLocalLoginInformation", "", function(result){
		data = result;
		},"json");
	return data;
}

function saveLocalLoginInformation(data){
	$.post("/ZTECloud/saveLocalLoginInformation", data, function(){
		});
}
function setLoginBtnValid(flag){
	isLoginBtnValid = flag;
}

function getLoginBtnValid(){
	return isLoginBtnValid;
}

function entryRegister() {
	window.location.href = "register.html";
}

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

function login(){

	if(!getLoginBtnValid()){
		return;
	}
	
	setLoginBtnValid(false);
	
	//if(!(checkUsernameValid() && checkPasswordValid() && checkCaptchaValid()))
		if(!(checkUsernameValid() && checkPasswordValid() ))
		{
			setLoginBtnValid(true);
		return;
		}
		
	$('#progressLabel').html(LogingLabel);
	$('#progress').modal();
	
	var url = "https://cloud.ztedevices.com:8445/authorise/useraddaccount?";
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	var captcha = 'ssss'//document.getElementById("register_captcha").value;
	
	var reg = /@/;
	var user_type;
	if(reg.test(username)==true)
	{
		user_type = "email";	
	}
	else
	{
		user_type = "phone";
	}
	console.log('login',user_type,username,reg.test(username));
	UserName = username;
	var userdata = user_type + "=" + encodeURIComponent(username) + "&pwd="
			+ encodeURIComponent(password) + "&captcha=" + encodeURIComponent(captcha);
	url = url + userdata + "&callback=showLoginZTERespose";
	
	showLogingNote(true);
	doLogin(url);

	
}

function showLogingNote(type){
	return;
	var note = document.getElementById("Loging_Note");
	if(type)
	{
		note.innerHTML = Cloud_Loging_Warning;
	}
	else
	{
		note.innerHTML = "";
	}	
}

function showLoginBaiduRespose(response){
	
}

function toblank(){
	window.location.href = 'about:blank'
}

function showLoginZTERespose(response){
	console.log('showLoginZTERespose',response);
	//$('a.modalCloseImg').click();
	if(response.code == '200')
	{
		
		var data = {};
		data.uid = response.result.uid;;
		data.username = UserName;
		data.zte_token = response.result.token;
		if (response.result.hasOwnProperty('baidu_token'))
		{
			data.baidu_token = response.result.baidu_token;	
		}
		else
		{
			//var data = {};
			//data.file = '';
			//$.post("/ZTECloud/ShowActivateNotice", data);
		}
		$.post("/ZTECloud/SaveBaiduTokenAndLogin", data);
		//setTimeout('toblank()',4000)
		toblank()
		setLoginBtnValid(true);
	}
	else if(response.code == '900')
	{
		var data = {};
		data.file= Cloud_Username_Is_Not_Exist;
		
		$.post("/ZTECloud/ShowMessageBox",data,function(){});
		//showLoginErrorInfo(Cloud_Username_Is_Not_Exist,"username",0);
	}
	else if(response.code == '901')
	{
		var data = {};
		data.file= Cloud_Password_Is_Error;
		$.post("/ZTECloud/ShowMessageBox",data,function(){});
		//showLoginErrorInfo(Cloud_Password_Is_Error,"password",0);
	}
	else
	{
		var data = {};
		data.file= Cloud_Login_Failed;
		$.post("/ZTECloud/ShowMessageBox",data,function(){});
		//alert(Cloud_Login_Failed);
	}
	//reloadImg("login_captcha_image");
	showLogingNote(false);
	setLoginBtnValid(true);
	StopProgress()
}
function StopProgress()
	{
		$('a.modalCloseImg').click();
	}
	
function ShowProgress(label)
	{
		$('#progressLabel').html(label);
		$('#progress').modal();
	}
function showLoginErrorInfo(errorInfo,location,type){
	var show_location = "loginResult_" + location;
	var loginResult = document.getElementById(show_location);

	var loginResult_p = document.getElementById(show_location+"_p");
	
	if(type == 1)
	{
		loginResult.style.color = "bule";
		loginResult_p.style.visibility = "hidden";	
	}
	else
	{
		loginResult.style.color = "red";
		loginResult_p.style.visibility = "visible";		
	}
	
	loginResult.innerHTML = errorInfo;
}

function showRegisterPage() {
	window.location.href = "register.html";
}

function callbackEmail(data){
	
	if (data.code=='200')
	{	
		showLoginErrorInfo("","username",1);
		verify_success = true;
	}
	else
	{
		showLoginErrorInfo(Cloud_Email_Check_Failed,"username",0);
	}
}
function callbackPhone(data){
	
	if (data.code=='200')
	{
		showLoginErrorInfo("","username",1);
	}
	else
	{
		showLoginErrorInfo(Cloud_Phone_Check_Failed,"username",0);
	}
}
function checkEmailPhoneAvailability(userName,type){
	
    var url = "https://cloud.ztedevices.com:8445/authorise/checkuserinfo?";
	if (type == "email")
	{
		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 doLogin(url){
	var script = document.createElement('script');
    script.setAttribute('src', url);
    document.getElementsByTagName('head')[0].appendChild(script); 
}

function changeLocalAutoConfig(x){
	var y=document.getElementById(x).checked;
	var data = {};
	if(!y)
	{
		
		data.autologin = 0;
		
	}
	else
	{	
	data.autologin = 1;
	
	}
	saveLocalLoginInformation(data);
}

function LoginBaiduFromPython(third_tocken){
	var data = {};
	data.third_tocken = third_tocken;
	$.post("/ZTECloud/LoginToBaidu", data, function() {

				}, "json")
}








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

		if(isEmptyString(username)){
			showUsernameErrorInfo(1);
			console.log('aaa');
			return false;
		}
	
		var reg = /@/;
		if(reg.test(username))
		{
			if(isEmailValid(username)){
				$('#usernameNote').text('')
				return true;
			} else{
				showUsernameErrorInfo(2);
				console.log('ccc');
				return false;
			}		
		}
		else
		{
				if(isPhoneValid(username)){
					$('#usernameNote').text('')
					return true;
				} else{
					console.log('eee');
					showUsernameErrorInfo(2);
					return false;
				}
		}
	}
function showUsernameErrorInfo(errorInfo)
	{
		var usernameNote = document.getElementById("usernameNote");
		if (errorInfo == 1){
			text = UserNameErorNullText;
			}
		else if (errorInfo == 2){
			text = UserNameErorFormText;
			}

		usernameNote.innerHTML = text;
		usernameNote.className = "note_error";
	}
function showPasswordNote(){
	document.getElementById("password").className = 'input_normal';
	var passwordNote = document.getElementById("passwordNote");
	passwordNote.innerHTML = PasswordNoteText;
	passwordNote.className = 'note_text';
	}
function checkPasswordValid(){
	
	var password = document.getElementById('password').value;
	
	if(isEmptyString(password)){
		showPasswordErrorInfo(PasswordNoteText);
		return false;
	}
	if(isPasswordValid(password)){
		//showOK("passwordNote");
		$('#passwordNote').text('')
		return true;
	} else{
		showPasswordErrorInfo(PasswordErorFormText);
		return false;
	}
	}	
function showPasswordErrorInfo(errorInfo){
	var passwordNote = document.getElementById("passwordNote");
	passwordNote.innerHTML = errorInfo;
	passwordNote.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 checkCaptchaValid(){
	var captcha = document.getElementById("register_captcha").value;
	if(isEmptyString(captcha)){
		showRegisterCaptchaErrorInfo(CaptChaNoteText);
		return false;
	} else{
		initCaptchaNote();
		return true;
	}
	}
function showRegisterCaptchaErrorInfo(errorInfo){
	var registerCaptchaNote = document.getElementById("registerCaptchaNote");
	registerCaptchaNote.innerHTML = errorInfo;
	registerCaptchaNote.className = "note_error";
	}
function initCaptchaNote(){
	var registerCaptchaNote = document.getElementById("registerCaptchaNote");
	registerCaptchaNote.innerHTML = "&nbsp";
	registerCaptchaNote.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 showOK(id){
	var note = document.getElementById(id);
	note.innerHTML = "&nbsp;";
	note.className = "note_OK";
	}