/**
 * 
 * 登陆js
 * @author fbj
 */
 
 if (!window.mobile) {
	window.mobile = {};
}
if (!window.mobile.logon) {
	window.mobile.logon = {};
}
(function(package){
	package.showPan = function(){
		if(document.getElementById("logon") != null){
		
			document.getElementById("logon").style.display = "block";
		}
		else{
			var u_r = location.href;
			var html = [];
			html.push('<div class="loginBox clearfix">');
			html.push('	<div class="loginContent">');
			html.push('    	<h2>登录</h2>');
			html.push('    	<form onsubmit="return false;">');
			html.push('        	<p><span>用户名：</span><input class="textInput" type="text" id="name"/></p>');
			html.push('        	<p><span>密码：</span><input class="textInput" type="password" id="pwd"/><a class="fg" href="http://passport.mapbar.com/accounts/forgotpasswd.jsp" target="_blank">忘记密码</a></p>');
			html.push('            <input class="but_submit" value="" type="submit" onclick="mobile.logon.logon()"/>');
			html.push('            <input class="but_cancle" value="" type="button" onclick="mobile.logon.cancel()"/>');
			html.push('        </form>');
			html.push('    </div>');
			html.push('	<div class="regContent">');
			html.push('    	<h2>注册</h2>');
			html.push('        <p>拥有图吧专属帐户，建立图吧互联网个人中心，享受图吧家园更多服务。</p>');
			html.push('        <input class="but_reg" value="" type="button" onclick="location.href=\'http://passport.mapbar.com/reg.jsp?setUrl='+u_r+'\'"/>');
			html.push('    </div>');
			html.push('</div>');
			
			var div = document.createElement('div');
			div.style.position = "absolute";
			div.style.left = (document.documentElement.clientWidth/2+document.documentElement.scrollLeft-250)+"px";
			div.style.top = (document.documentElement.clientHeight/2+document.documentElement.scrollTop-152)+"px";
			div.id = "logon";
			
			div.innerHTML = html.join("");
			document.body.appendChild(div);
			//document.getElementById("lDiv").className="loginBox";
		}
	}
	package.cancel = function(){
		if(document.getElementById("logon") != null){
			document.getElementById("logon").style.display = "none";
		}
	}
	package.setCookie = function(name, value, option) {
		//用于存储赋值给document.cookie的cookie格式字符串
		var str = name + "=" + encodeURIComponent(value);
		if (option) {
		//如果设置了过期时间
		if (option.expireDays) {
		var date = new Date();
		var ms = option.expireDays*24*3600*1000;
		date.setTime(date.getTime() + ms);
		str += "; expires=" + date.toGMTString();
		};
		if (option.path) {
		str += "; path=" + option.path;
		}
		if (option.domain) {
		str += "; domain=" + option.domain;
		}
		if (option.secure) {
		str += "; true";
		}
		}
		document.cookie = str;
	}
	package.setMCookie = function(name, value, option) {
		var domain = location.href.indexOf("mapbar.com") == -1 ? "" : "mapbar.com";
		if (!option) {
		 	option = {expireDays : '30', domain : domain, path : '/'};
		} else {
		 	option.domain = domain;
		}
		package.setCookie(name, value, option);
	}, 
	package.getCookie = function(name)//取cookies函数        
	{
	    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	     if(arr != null) return decodeURIComponent(arr[2]); return null;
	
	}
	package.delCookie = function(name)//删除cookie
	{
	    var exp = new Date();
	    exp.setTime(exp.getTime() - 1);
	    var cval="";//package.getCookie(name);
	    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
	}
	package.delAllCookie = function(){
//		package.setMCookie("userId","");
//		package.setMCookie("userIdc","");
//		package.setMCookie("userName","");
        package.delCookie("userId");
        package.delCookie("userIdc");
        package.delCookie("userName");
	}
	package.logonOut = function(){
		if(confirm("您确定要退出吗？")){
			package.delAllCookie();
			var u_l = "http://passport.mapbar.com/index.php?url="+location.href;
			document.getElementById("logonInfo").innerHTML='<li><a href='+u_l+' class="login_bar">登录</a>'+
		        '<li><a href="http://bbs.mapbar.com/" class="login_bar">论坛</a>'+
		        '<li><a href="http://www.mapbar.com/help/help.html" class="login_bar">帮助</a>'+
		        '<li class="navMenuBtn"><a class="hide" href="###">网站导航</a>'+ 
		          '<!--[if lte IE 6]><a href="###">网站导航<table><tr><td><![endif]-->'+
		          '<ul>'+
		            '<li> <strong>查询系统</strong> <a href="http://www.mapbar.com/search/" target="_blank" title="地图搜索" >地图搜索</a> <a href="http://bus.mapbar.com/" target="_blank" title="公交查询" >公交查询</a> <a href="http://nav.mapbar.com/" target="_blank" title="驾车导航" >驾车导航</a> <a href="http://www.mapbar.com/search/#c=北京市&itab=3" target="_blank" title="周边查询">周边查询</a>'+
		              '<div class="hackbox">&nbsp;</div>'+              
		              '<div class="navMenuLine">&nbsp;</div>'+
		              '<strong>客户端</strong> <a href="http://mobile.mapbar.com" target="_blank" title="无线首页" >无线首页</a> <a href="http://mobile.mapbar.com/ditu/iphone" target="_blank" title="手机地图" >手机地图</a> <a href="http://mobile.mapbar.com/daohang/iphone" target="_blank" title="手机导航" >手机导航</a>'+
		              '<div class="hackbox">&nbsp;</div>'+
		              '<div class="navMenuLine">&nbsp;</div>'+
		              '<strong>生活服务</strong> <a href="http://tuan.mapbar.com/" target="_blank" title="团购导航" >团购导航</a> <a href="http://hotel.mapbar.com/" target="_blank" title="酒店预订" >酒店预订</a> <a href="http://piao.mapbar.com/" target="_blank" title="票务预订" >票务预订</a> <a href="http://xianlu.mapbar.com/" target="_blank" title="旅游线路" >旅游线路</a> <a href="http://travel.mapbar.com/" target="_blank" title="旅游景点" >旅游景点</a> <a href="http://jipiao.mapbar.com/" target="_blank" title="机票预订" >机票预订</a> <a href="http://zuche.mapbar.com/" target="_blank" title="租车服务" >租车服务</a>'+
		              '<div class="hackbox">&nbsp;</div>'+
		              '<div class="navMenuLine">&nbsp;</div>'+
		              '<strong>技术服务</strong> <a href="http://open.mapbar.com/index.jsp" target="_blank" title="地图API" >地图API</a> <a href="http://hy.mapbar.com/" target="_blank" title="行业应用" >行业应用</a> </li>'+
		          '</ul>'+
		          '<!--[if lte IE 6]></td></tr></table></a><![endif]-->'+ 
		        '</li>';
			if(document.getElementById("usName")!=null){
				document.getElementById("usName").value="";
			}
			if(document.getElementById("imei")!=null){
				document.getElementById("imei").value="";
			}
			if(document.getElementById("rand")!=null){
				document.getElementById("rand").value="";
			}
			if(document.getElementById("result")!=null){
				document.getElementById("result").innerHTML="&nbsp;";
			}
		}
	}
	package.checkLogon = function(userName , pwd){
	
		 var head=document.getElementsByTagName("head")[0];
		 var s = document.createElement("script");
       	 head.appendChild(s);
         s.setAttribute("language", "javascript");
	     s.setAttribute("type", "text/javascript");
	    // s.setAttribute("id", this.id);
	     s.setAttribute("src", "http://hi.mapbar.com/mapjson/checkLogin.jsp?userName=" + encodeURI(userName) + "&password=" + encodeURI(pwd) + "&r=" + Math.random());
		 s.onload = s.onreadystatechange = function(){
		 	//package.logonChk();
		 	if (typeof ActiveXObject!="undefined") {
				if(s.readyState&&s.readyState=="loaded")package.logonChk();
				if(s.readyState&&s.readyState=="complete")return;
			}else{
				package.logonChk();
			} 
		 
		 }
	}
	package.logonChk = function(){
		if (userInfo.loginState.nameState == "1" && userInfo.loginState.passState == "1") {
			this.userId = userInfo.user.userid;
			this.userName = userInfo.user.userName;
			if (userInfo.user.useridc) {
				this.userIdc = userInfo.user.useridc;
			}
			
			package.setMCookie("userId", this.userId, {expireDays:30, path:"/"});
			package.setMCookie("userIdc", this.userIdc, {expireDays:30, path:"/"});
			package.setMCookie("userName", this.userName, {expireDays:30, path:"/"});
			//this.loginInfo();
			if(window.location.href.indexOf("licence") >=0)// == "http://mobile.mapbar.com/Downloads/licence.jsp")
			{
				
				if(window.location.href.indexOf("licenceAction") >=0)
					window.location.href = window.location.href.replace("licenceAction","licence");
				else
					window.location.reload();
			}	
			
				
			//显示登录信息
			package.showInfo(this.userName , this.userId);
			//关闭登录窗口
			package.cancel();
			if(document.getElementById("usName") != null){
				document.getElementById("usName").value = this.userName;
				
			}
			if(document.getElementById("name") != null){
				document.getElementById("name").value="";
			}
			if(document.getElementById("pwd") != null){
				document.getElementById("pwd").value="";
			}
			if(document.getElementById("result")!=null){
				document.getElementById("result").innerHTML="&nbsp;";
			}
			//if ($("loginPanel")) {
			//	$("loginPanel").parentNode.removeChild($("loginPanel"));
			//}
			//location.reload();  当用户未登陆时，点击创建，出现登录阴影层，登录以后转向创建页面
			
		} else {
			if (userInfo.loginState.nameState == "0") {
				alert("\u7528\u6237\u540d\u4e0d\u5b58\u5728");
			} else {
				if (userInfo.loginState.passState == "0") {
					alert("\u5bc6\u7801\u9519\u8bef");
				}
			}
		}
	}
	package.showInfo = function(userName , userId){
		if(typeof userName != "undefined" && userName != null && userName != "")
			document.getElementById("logonInfo").innerHTML='<li><a href="http://hi.mapbar.com/'+userId+'/" target="_blank">'+userName+'</a>，欢迎回来！'+
			    '<li><a href="javascript:;" onclick="mobile.logon.logonOut();return false;">注销</a>'+
		        '<li><a href="http://bbs.mapbar.com/" class="login_bar">论坛</a>'+
		        '<li><a href="http://www.mapbar.com/help/help.html" class="login_bar">帮助</a>'+
		        '<li class="navMenuBtn"><a class="hide" href="###">网站导航</a>'+ 
		          '<!--[if lte IE 6]><a href="###">网站导航<table><tr><td><![endif]-->'+
		          '<ul>'+
		            '<li> <strong>查询系统</strong> <a href="http://www.mapbar.com/search/" target="_blank" title="地图搜索" >地图搜索</a> <a href="http://bus.mapbar.com/" target="_blank" title="公交查询" >公交查询</a> <a href="http://nav.mapbar.com/" target="_blank" title="驾车导航" >驾车导航</a> <a href="http://www.mapbar.com/search/#c=北京市&itab=3" target="_blank" title="周边查询">周边查询</a>'+
		              '<div class="hackbox">&nbsp;</div>'+              
		              '<div class="navMenuLine">&nbsp;</div>'+
		              '<strong>客户端</strong> <a href="http://mobile.mapbar.com" target="_blank" title="无线首页" >无线首页</a> <a href="http://mobile.mapbar.com/ditu/iphone" target="_blank" title="手机地图" >手机地图</a> <a href="http://mobile.mapbar.com/daohang/iphone" target="_blank" title="手机导航" >手机导航</a>'+
		              '<div class="hackbox">&nbsp;</div>'+
		              '<div class="navMenuLine">&nbsp;</div>'+
		              '<strong>生活服务</strong> <a href="http://tuan.mapbar.com/" target="_blank" title="团购导航" >团购导航</a> <a href="http://hotel.mapbar.com/" target="_blank" title="酒店预订" >酒店预订</a> <a href="http://piao.mapbar.com/" target="_blank" title="票务预订" >票务预订</a> <a href="http://xianlu.mapbar.com/" target="_blank" title="旅游线路" >旅游线路</a> <a href="http://travel.mapbar.com/" target="_blank" title="旅游景点" >旅游景点</a> <a href="http://jipiao.mapbar.com/" target="_blank" title="机票预订" >机票预订</a> <a href="http://zuche.mapbar.com/" target="_blank" title="租车服务" >租车服务</a>'+
		              '<div class="hackbox">&nbsp;</div>'+
		              '<div class="navMenuLine">&nbsp;</div>'+
		              '<strong>技术服务</strong> <a href="http://open.mapbar.com/index.jsp" target="_blank" title="地图API" >地图API</a> <a href="http://hy.mapbar.com/" target="_blank" title="行业应用" >行业应用</a> </li>'+
		          '</ul>'+
		          '<!--[if lte IE 6]></td></tr></table></a><![endif]-->'+ 
		        '</li>';
	}
	package.logon = function(){
		var userName = document.getElementById("name").value;
		var pwd = document.getElementById("pwd").value;
		//alert(document.getElementById("name").value)
		if(userName == null || userName == ""){
			alert("用户名不能为空！");
		}
		else if(pwd == null || pwd == ""){
			alert("密码不能为空！");
		}
		else{
			package.checkLogon(userName , pwd);
		}
	}
}
)(window.mobile.logon)


/**
 * 
 * 下载激活文件js
 * @author fbj
 */
 
 if (!window.mobile) {
	window.mobile = {};
}
if (!window.mobile.download) {
	window.mobile.download = {};
}
	window.mobile.download = ({
	check : function(){
		
		var flag = true;
		
		var userName = document.getElementById("usName").value;
		if(userName == null || userName.replace(/ +/i , "") == ""){
			alert("用户名不能为空！")
			flag = false;
		}
		
		if(flag){
			var imei = document.getElementById("imei").value;
			if(imei != null && imei.replace(/ +/i , "") != ""){
				var val = imei;
				if(val.length == 8 || val.length == 14 || val.length == 15)
				{
					var re1 = /^[a-zA-Z]+$/;
					var re2 = /^[a-zA-Z0-9]+$/;
					if(re1.test(val))//都是字母
					{
						alert("识别码不能都是字母！")
						flag = false;
					}
					else if(re2.test(val))
					{
						flag = true;
					}
					else
					{
						alert("识别码必须是8,14,15位字母或数字组合!")
						flag = false;
					}	
				}
				else
				{
					alert("识别码必须是8,14,15位字母或数字组合!")
					flag = false;
				}
				
			}else{
				alert("识别码必不能为空!")
				flag = false;
			}
		
		}
		
		if(flag){	
			var rand = document.getElementById("rand").value;
			if(rand == null || rand.replace(/ +/i , "") == ""){
				alert("验证码不能为空！")
				flag = false;
			}else if(!(/^\d{4}$/i.test(rand))){
				alert("验证码格式不正确！")
				flag = false;
			}
			
		}
			
		return flag;
	
	},
	submit : function(id){
		if(document.getElementById("logonInfo") != null){
			if(document.getElementById("logonInfo").innerHTML.indexOf("欢迎回来")==-1){
				mobile.logon.showPan();
				return false;
			}
			else
				return this.check();
		}
		else
			return this.check();
	}

})
			
