$(document).ready(function()
	{
		OnModifyUserInfo();
		//ShowProgress(LogingLabel);
		
	});
function OnMouseHover(elem)
{
	if ( $(elem).hasClass("disabled") )
	{
		return;
	}
	$(elem).addClass("hover");
}
function OnMouseOut(elem)
{
	if ( $(elem).hasClass("disabled") )
	{
		return;
	}
	$(elem).removeClass("hover");
}
	
function OnModifyUserInfo()
{


	$('.ModifyPassword').removeClass('selected');
	$('.ModifyUserInfo').addClass('selected');
	
	title = "<div class='title'>"+Cloud_ModifyUserInfo+"</div>"
	phone_div = "<div class='item'><div class='label'>"+Cloud_Phone+":</div><input class='input_normal' id='phone' tabindex='1' type='text' value='' onblur='checkPhoneValid()'/></div>"
	email_div = "<div class='item'><div class='label'>"+Cloud_Email+":</div><input class='input_normal' id='email' tabindex='1' type='text' value=''checkEmailValid/></div>"
	nick_div = "<div class='item'><div class='label'>"+Cloud_NickName+":</div><input class='input_normal' id='nick' tabindex='1' type='text' value=''/></div>"
	errorInfo = "<div class='errorInfo' id='errorInfo'></div>"
	save = "<div class='save'><input type='button' id='save' value="+Cloud_Save+" class='login_btn_normal' onclick='DoSaveUserInfo()' /></div>"
	text = title+phone_div+email_div+nick_div+errorInfo+save;	
	$('#right').html(text);
}
function OnModifyPassword()
{

	$('.ModifyPassword').addClass('selected');
	$('.ModifyUserInfo').removeClass('selected');
	
	title = "<div class='title'>"+Cloud_ModifyPassword+"</div>"
	currentPwd_div = "<div class='item'><div class='label'>"+Cloud_CurrentPwd+":</div><input class='input_normal' id='currentPwd' tabindex='1' type='password' value='' onblur='checkCurrentPasswordValid()'/></div>"
	newPwd_div = "<div class='item'><div class='label'>"+Cloud_NewPwd+":</div><input class='input_normal' id='newPwd' tabindex='1' type='password' value='' onblur='checkNewPasswordValid()'/></div>"
	newPwd2_div = "<div class='item'><div class='label'>"+Cloud_NewPwd2+":</div><input class='input_normal' id='newPwd2' tabindex='1' type='password' value='' onblur='checkConfirmPasswordValid()'/></div>"
	errorInfo = "<div class='errorInfo' id='errorInfo'></div>"
	save = "<div class='save'><input type='button' id='savepwd' value="+Cloud_Save+" class='login_btn_normal' onclick='DoSavePassword()' /></div>"
	text = title+currentPwd_div+newPwd_div+newPwd2_div+errorInfo+save;	
	$('#right').html(text);
}


function checkPhoneValid()
{
	phone = document.getElementById("phone").value;
	checkUsernameValid(phone);
}
function checkEmailValid()
{
	email = document.getElementById("email").value;
	checkUsernameValid(email);
}
function checknickValid()
{
	var newNickname = document.getElementById('nick').value;
	if(isnickValid(newNickname))
	{
		return true;
	} 
	else
	{
		showUsernameErrorInfo(NickNameFormText);
		return false;
	}
}
function isnickValid(nickname){
	
	return (/^[a-zA-Z0-9-_.\u4e00-\u9fa5]{1,20}$/).test(nickname);
}
function checkUsernameValid(user_info)
{

	var reg = /@/;
	if(reg.test(user_info))
	{
		if(isEmailValid(user_info)){
			checkEmailPhoneAvailability(user_info,1);
			return getUsernameValid();
		} else{
			showUsernameErrorInfo(2);
			return false;
		}		
	}
	else
	{
		if(isPhoneValid(user_info))
		{
			checkEmailPhoneAvailability(user_info,2);
			return getUsernameValid();
		} 
		else
		{
			showUsernameErrorInfo(2);
			return false;
		}
	}
}	
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 callbackEmail(data)
{
	if (data.code=='200')
	{
	canSave = true;
	//showResponseForEmail('valid');
	}
	else
	{
	canSave = false;
	showUsernameErrorInfo(3);
	}
};
function callbackPhone(data)
{
	if (data.code=='200')
	{
	canSave = true;
	//showResponseForPhone('valid');
	}
	else
	{
	canSave = false;
	showUsernameErrorInfo(4);
	}
};

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

function DoSaveUserInfo()
{
	phone = document.getElementById("phone").value;
	email = document.getElementById("email").value;
	nick = document.getElementById("nick").value;
	if (isEmptyString(phone) && isEmptyString(email))
	{
		var info = '电话号码和邮箱不能同时为空！'
		showUsernameErrorInfo(info);
		return;
	}
	if (!canSave)
	{
		return;
	}
	text = '';
	if (phone=='')
	{}
	else
	{
		text = text + 'phone='+phone;
	}
	if (email=='')
	{}
	else
	{
		if (text=='')
		{
			text = text + 'email='+email;
		}
		else
		{
			text = text + '&email='+email;
		}
	}
	if (nick=='')
	{}
	else
	{
		text = text + '&nick='+nick;
	}
	ModifyUserInfo(text);

}
function DoSavePassword()
{

	currentPwd = document.getElementById("currentPwd").value;
	newPwd = document.getElementById("newPwd").value;
	newPwd2 = document.getElementById("newPwd2").value;
	if (!canSave)
	{
		return;
	}

	
	text = 'curpwd='+currentPwd + '&pwd='+newPwd;
	ModifyPassword(text);
}	
function ModifyUserInfo(info)
{
	var url = "https://cloud.ztedevices.com:8445/authorise/userinfomodify?";

	url = url + 'uid=' + user_data.uid +'&'+info+ "&callback=ModifyCallback"


	var script = document.createElement('script');
    script.setAttribute('src', url);
    document.getElementsByTagName('head')[0].appendChild(script); 
}
function ModifyPassword(info)
{
	var url = "https://cloud.ztedevices.com:8445/authorise/userpwdmodify?";

	url = url + 'uid=' + user_data.uid +'&'+info+ "&callback=ModifyCallback"

	var script = document.createElement('script');
    script.setAttribute('src', url);
    document.getElementsByTagName('head')[0].appendChild(script); 
}	
function ModifyCallback(data)
{
	console.log('ModifyCallback',data);
	if (data.code==200)
	{
		var data = {'res':'succ'};
		$.post("/ZTECloud/ModifyUserInfoResp",data,function(){});
	}
	else
	{
		var data = {'res':'fail'};
		$.post("/ZTECloud/ModifyUserInfoResp",data,function(){});
	}
}
function checkCurrentPasswordValid()
{
	var password = document.getElementById('currentPwd').value;
	checkPasswordValid(password);
}
function checkNewPasswordValid()
{
	var password = document.getElementById('newPwd').value;
	checkPasswordValid(password);
}
function checkPasswordValid(password)
{
	
	if(isEmptyString(password))
	{
		canSave = false;
		showUsernameErrorInfo(PasswordErorNullText);
	}
	if(isPasswordValid(password)){
		
	} 
	else
	{
		canSave = false;
		showUsernameErrorInfo(PasswordErorFormText);
	}
}
	
function checkConfirmPasswordValid()
{

	var password = document.getElementById("newPwd").value;
	var confirmPassword = document.getElementById("newPwd2").value;
	
	if(isEmptyString(confirmPassword))
	{
		canSave = false;
		showUsernameErrorInfo(ConfirmPasswordNoteText);
	}
	if(isPasswordValid(confirmPassword)){
		if(password != confirmPassword){
			showUsernameErrorInfo(ConfirmPasswordDiffText);
			canSave = false;
		}
		else
		{
			canSave = true;
		}

	} else{
		showUsernameErrorInfo(ConfirmPasswordText);
		canSave = false;
	}
}