var reg_flag = 2;
serverDomain_host  = "http://i.114.com.cn/";
/**********************************
*弹出窗相关函数开始
**********************************/
//IE6 default returnValue is true so has BUG
function doane(event) 
{
	e = event ? event : window.event;
	if(isIe) {
		e.returnValue = false;
		e.cancelBubble = true;
	} else if(e) {
		e.stopPropagation();
		e.preventDefault();
	}
}
var isIe=(document.all)?true:false;
function setSelectState(state)
{
	var objl=document.getElementsByTagName('select');
	for(var i=0;i<objl.length;i++)
	{
		objl[i].style.visibility=state;
	}
}
function alertWin(title, msg, w, h)
{ 
	var titleheight = "22px"; // 提示窗口标题高度 
	var bordercolor = "#3b89cb"; // 提示窗口的边框颜色 
	var titlecolor = "#fff"; // 提示窗口的标题颜色 
	var titlebgcolor = "#3b89cb"; // 提示窗口的标题背景色
	var bgcolor = "#eef6ff"; // 提示内容的背景色
	
	var iWidth = document.body.clientWidth; 
	var iHeight = document.body.clientHeight; 
	var iTop = document.documentElement.scrollTop;
	var bgObj = document.createElement("div"); 
	bgObj.id="back";
	//bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;filter:Alpha(Opacity=40);opacity:0.4;background-color:#fff;z-index:101;";
	bgObj.style.cssText = "";
	document.body.appendChild(bgObj); 
	
	var msgObj=document.createElement("div");
	msgObj.id="mesWindow";
	msgObj.style.cssText = "position:absolute;top:"+(iTop+(window.screen.availHeight-h)/2)+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px;text-align:left;border:3px solid "+bordercolor+";background-color:"+bgcolor+";line-height:22px;z-index:102;";
	document.body.appendChild(msgObj);
	
	var table = document.createElement("table");
	msgObj.appendChild(table);
	table.style.cssText = "margin:0px;border:0px;padding:0px;";
	table.cellSpacing = 0;
	var tr = table.insertRow(-1);
	var titleBar = tr.insertCell(-1);
	titleBar.style.cssText = "width:99%;height:"+titleheight+"px;text-align:left;padding:3px;margin:0px;font:bold 14px '宋体';color:"+titlecolor+";border:1px solid " + bordercolor + ";cursor:move;background-color:" + titlebgcolor;
	titleBar.style.paddingLeft = "10px";
	titleBar.innerHTML = title;
	var moveX = 0;
	var moveY = 0;
	var moveTop = 0;
	var moveLeft = 0;
	var moveable = false;
	var docMouseMoveEvent = document.onmousemove;
	var docMouseUpEvent = document.onmouseup;
	titleBar.onmousedown = function() {
		var evt = getEvent();
		moveable = true; 
		moveX = evt.clientX;
		moveY = evt.clientY;
		moveTop = parseInt(msgObj.style.top);
		moveLeft = parseInt(msgObj.style.left);
		
		document.onmousemove = function() {
			if (moveable) {
				var evt = getEvent();
				var x = moveLeft + evt.clientX - moveX;
				var y = moveTop + evt.clientY - moveY;
				if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight) ) {
					msgObj.style.left = x + "px";
					msgObj.style.top = y + "px";
				}
			}	
		};
		document.onmouseup = function () { 
			if (moveable) { 
				document.onmousemove = docMouseMoveEvent;
				document.onmouseup = docMouseUpEvent;
				moveable = false; 
				moveX = 0;
				moveY = 0;
				moveTop = 0;
				moveLeft = 0;
			} 
		};
	}
	
	var closeBtn = tr.insertCell(-1);
	closeBtn.style.cssText = "cursor:pointer; padding:2px 5px 2px 2px;background-color:" + titlebgcolor;
	closeBtn.innerHTML = "<span style='font-size:16pt; color:"+titlecolor+";'>×</span>";
	closeBtn.onclick = function(){ 
		document.body.removeChild(bgObj); 
		document.body.removeChild(msgObj); 
		if(isIe)
		{
		setSelectState('');
		}
	} 
	var msgBox = table.insertRow(-1).insertCell(-1);
	//msgBox.style.cssText = "font:12px '宋体';";
	msgBox.colSpan  = 2;
	msgBox.innerHTML = msg;
	
    // 获得事件Event对象，用于兼容IE和FireFox
    function getEvent() {
	    return window.event || arguments.callee.caller.arguments[0];
    }
	
	if(isIe)
	{
		setSelectState('hidden');
	}	
} 

//关闭窗口
function close_window()
{
	if(document.getElementById('back')!=null)
	{
		document.getElementById('back').parentNode.removeChild(document.getElementById('back'));
	}
	if(document.getElementById('mesWindow')!=null)
	{
		document.getElementById('mesWindow').parentNode.removeChild(document.getElementById('mesWindow'));
	}
	if(isIe)
	{
	setSelectState('');
	}
}
/**********************************
*弹出窗相关函数结束
**********************************/
/**********************************
*登录注册-相关函数开始
**********************************/
//用户注册
function userReg()
{
	window.open(serverDomain_host+"?module=user&view=reg");
}
//提交登陆信息
function submit_form(pos)
{
	//var autologin = document.getElementById("autologin").checked;
	var input_autologin = true;
	var input_name = $("#input_name").val();
	var input_pwd = $("#input_pwd").val();
	var length = input_name.replace(/[^x00-xff]/g,"xx").length;
	//var type = {/$type/};
	var type = 0;
	if(length > 16 || length < 4)
	{
		alert("用户名最长为14个字符/最短为4个字符");
		$("#input_name").focus();
		return false;
	}
	if(input_name.match(/^([\u4e00-\u9fa5]|[0-9A-Z_a-z])*$/g) == null)
	{
		alert("用户名只能输入字母、数字或中文");
		$("#input_name").focus();
		return false;
	}
	var length2 = input_pwd.length;
	if(length2 > 14 || length2 < 4)
	{
		 alert("密码最长为14个字符/最短为4个字符");
		 $("#input_pwd").focus();
		 return false;
	}
	var date = new Date();
	$.getJSON(serverDomain_host+"?view=login&jsoncallback=?", {input_name: input_name, input_pwd: input_pwd,Op:"loging",time:date,input_type:type,input_autologin:input_autologin}, function(data){
				if (data.res ==1)
		  		{
		  			login_or_not(pos);
		  			syn(data.script, function(){return true;});
		  			close_window();//关闭窗口*/		
		  		}
		  		else if(data.res == 2)
		  		{
		  			alert("用户名或者密码错误，请重输！");
		  			$("#input_pwd").val("");
		  			$("#input_pwd").focus();
		  		}
		  		else if(data.res == 3)
		  		{
		  			alert("您非会员，请先注册新会员！");
		  		}
		  		else if(data.res == 4)
		  		{
		  			alert("该帐号已被冻结，如有疑问请联系114客服人员。");
		  		}
		  		else if(data.res == 5)
		  		{
		  			alert("该帐号已被删除，如有疑问请联系114客服人员。");
		  		}
		   	});
}
//p3p
function syn(urls, callback) {
    var url = urls[urls.length-1], urls2 = [];
    if( urls.length == 0 ) 
    {
    	return callback();
    }
    for(var i = 0; i < urls.length-1; i++) 
        urls2[urls2.length] = urls[i];
    $.getScript(url, function(){ syn(urls2, callback); });
}
function userLogin(position){
	close_window();
	msgInfo = "<ul class=\"theForm\">"
	msgInfo += "<li><label>用户名：</label><input type=\"text\" class=\"thisText\" id=\"input_name\" /></li>";
	msgInfo += "<li><label>密码：</label><input type=\"password\" class=\"thisText\" id=\"input_pwd\" /></li>";
	msgInfo += "<li><input type=\"checkbox\" checked=checked  id=\"autologin\" />下次自动登录</li>";
	msgInfo += "<li><button class='thisBtn' onclick='submit_form(\""+position+"\");'>立即登录</button>&nbsp;&nbsp;";
	msgInfo += "<a href='"+serverDomain_host+"/index.php?module=user&view=getpwd' target='_blank' style='color:#0000cc;'>找回密码</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:void();\" onclick='userReg();doane(event);'><font color='#cc0000'>注册新用户</font></a></li>";
	msgInfo += "</ul>"
	alertWin('用户登录',msgInfo,330,225);
	$('.theForm').bind('keyup',function(event) {
        if(event.keyCode == 13){
            submit_form(position);
        }
    });	
}
/**********************************
*登录注册-相关函数结束
**********************************/
//头部是否登录
function login_top_bar(_value)
{
	var html = _value+"，欢迎您！进入<a href='http://i.114.com.cn/?view=mangerUrl' target='_blank'><font color=\"#cc0000\">&nbsp;我的导航&nbsp;</font></a>或<a href=\"javascript:void(0);\" onclick=\"user_login_submit();doane(event);\"><font color=\"#cc0000\">&nbsp;安全退出&nbsp;</font></a>！"
	$("#login_or_not").html(html);
}
function login_or_not(pos)
{
	if($.cookie("iweb_user")=='')
    {
        return false;
    }
    else
    {
    	user_name();//得到用户名
    }
}
//退出
function user_login_submit()
{
    var date = new Date();
    $.ajax({
            type : "GET",
            url : serverDomain_host+"/index.php?module=user&view=signout",
            async:false,
           	dataType : "jsonp",
           	jsonp: 'jsoncallback',
           	data : "&Op=logout&time="+date,
		   	success: function(data){
	  					if (data.res ==1)
	  					{
	  						window.location.reload();
	  					}
	  					else if(data.res == 2)
	  					{
	  						alert("出错啦");
	  					}
	   }});		  				  		
}
//得到用户名
function user_name()
{
	$.ajax({
            type : "GET",
            url : serverDomain_host+"index.php?module=user&view=userinfoajax",
            async:false,
           	dataType : "jsonp",
           	jsonp: 'jsoncallback',
           	data : "&Op=getusername",
		   	success: function(data){
		   		login_top_bar(data);
				}});
}

