﻿var Name;
var Password;
var Error;
var FPwrong;
var pwdwrong;
var noMember;
var fpConnect;
var loginErr;

var usertype="C";

$(document).ready(function(){
	$(".trigger").tooltip({
						  position:"bottom center",
						  events:{def:"click,mouseout"}
						  });
	$(".log_cls").click(function (){
								  $("#log_cls2").text($(this).text());
								  $(".tooltip").hide();
								  });
	$("#userguidA").click(function (){
								   if($(this).val()=="点击箭头选择登录方式"){
									   $(this).val("").css({"color":"#333"});
									   }
								   });
	$("#userguidA").blur(function (){
								   if($(this).val()==""||$(this).val()==null){$(this).val("点击箭头选择登录方式").css({"color":"#c1c1c1"});};
								   });
});

function loginnav(){
	var userGuid = document.getElementById("userguidA").value.replace(/^\s*|\s*$/g,"");
	var passWord = document.getElementById("passwdA").value;
	
	var logtypetext = $("#log_cls2").text();	
	var logtype = "1";
	
	if(logtypetext == "用户名"){
		logtype = "3"
	}else if(logtypetext == "会员号"){
		logtype = "1"
	}else{
		logtype = "2"
	}
	
	setWrongInfo();	

	if(checkInputnav(userGuid,passWord)){
	
		if(FFPno(userGuid))
			usertype="M";
		
		checkUsernav(userGuid,passWord,logtype);
		 
		//window.location.href = "/B2C/modules/account/accountMain.jsp";		
	}	

}

function loginnavEn(){
	var userGuid = document.getElementById("userguidA").value.replace(/^\s*|\s*$/g,"");
	var passWord = document.getElementById("passwdA").value;

	setWrongInfo();	

	if(checkInputnav(userGuid,passWord)){
	
		if(FFPno(userGuid))
			usertype="M";
		
		checkUsernav(userGuid,passWord,"");
		 
		//window.location.href = "/B2C/modules/account/accountMain.jsp";		
	}	

}


function setWrongInfo(){
	if(language=="zh"){
		Name="用户名不能为空，请重新输入。";
		Password="密码不能为空,请重新输入。";
		pwdwrong="用户名或密码错误,请重新输入!";
		FPwrong="明珠卡号或密码错误,请重新登录";
		noMember="没有该会员或者密码错误，请重新登录。";
		fpConnect="明珠卡号登录连接错误！";
		loginErr="登录出错。";
		
	
	}else{
		Name="User ID can not be empty，please input again.";
		Password="Password can not be empty, please input again.";
		pwdwrong="Wrong user name or password ,enter again please.";
		FPwrong="Please confirm correctness of user ID and PPF card number";
		noMember="No such member or wrong password！";
		fpConnect="FP Connection refused: connect!";
		loginErr="login error.";
	
	}

}

function checkInputnav(userGuid,passWord){
	if(userGuid == "" || userGuid == "点击箭头选择登录方式")
	{
		alert(Name);
		return false;
	}
	/*if (escape(userGuid).indexOf('%u') != -1) {
		alert("请输入正确格式的用户名");
		return false;
	}*/
	if(/.*[\u4e00-\u9fa5]+.*$/.test(userGuid)) { 
		alert("请输入正确格式的用户名"); 
		return false; 
	} 
	if(passWord == "")
	{
		alert(Password);
		return false;
	}
	return true;
}

function FFPno(cardno){
	if(cardno.length!=0 && cardno.length!=8 && cardno.length!=12)			
		return false;
		
	if(isNaN(cardno)) 
		return false;
		
	if(cardno.length==12){
		var arr = cardno.split("");
		var n = 0;
		for(var j=1; j<arr.length; j++) n+= arr[j] * 1;
		if( n%7 != arr[0] * 1)   
			return false;
		else if(cardno.length > 12)
			return false;
	}
			
	return true;
}

function checkUsernav(userGuid, passWord,logtype){
			
	try{
		var urlXml = "/B2C/data/account/login.xsql?userguid=" + userGuid + "&passwd=" + passWord + "&usertypes=" + usertype + "&logType=" +logtype;
		
		$.ajax({
			type: "POST",
			url: urlXml,
			dataType: "xml",
			async: false,
			complete: function(msg){ 			
				var retDom=msg.responseXML;
				
				if(!retDom){
					alert("请确认您输入的用户名和密码无误");
					return false;
				}else if($(retDom).find("bound").length > 0){
					if($(retDom).find("bound:first").text() != "false" && $(retDom).find("bound:first").text() != "true"){

						if(language=="en"){
							if($(retDom).find("bound:first").text()=="很抱歉，密码不正确！如果您的会员信息与明珠俱乐部会员信息已经完成合并，请使用明珠俱乐部会员密码进行登录。"){
								alert("Incorrect password.  If your account has been merged with your Sky Pearl account, please login with your Sky Pearl account password.");
							}else if($(retDom).find("bound:first").text()=="该用户不存在。") {
								alert("Invalid Account.");
							}else{
								alert("sorry,the login failed ,please retry.");
							}
						}else{
							alert($(retDom).find("bound:first").text());
						}													
						return false;
					}else if($(retDom).find("bound:first").text() == "false"){
						
							if(language=="en"){
								window.parent.location.href = "../common/ffpRemind_en.jsp?prePage=mainpage";//window.location.href;
							}else{
								window.parent.location.href = "../common/ffpRemind.jsp?prePage=mainpage";//window.location.href;
							}
							
							return false;
						
					}else{  //登陆成功，刷新
						window.location.href = window.location.href;
					}
				}																	
			}  
		});
		
	}catch(e){
		alert(loginErr);
	}
}

function loginagain(userGuid, passWord){
	checkUsernav(userGuid, passWord);
}
