/**
 * @filename		global_login_frm.js
 * @description		
 * @copyright		Copyright 2008, xunlei.com.
 * @version		
 * @modifiedby		xiaocai (cyy0523xc@gmail.com) 
 * @lastmodified	2008-9-28
 */

var login_is_running = false;

String.prototype.trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function refresh_page(){
	location.href = location.href;
}

function cancel_div(div_id){
	var o = document.getElementById(div_id);
	o.parentNode.removeChild(o);
}

//获取元素的纵坐标
function getTop(e){
	var offset=e.offsetTop;
	if(e.offsetParent!=null){ offset+=getTop(e.offsetParent);}
	return offset;
}
//获取元素的横坐标
function getLeft(e){
	var offset=e.offsetLeft;
	if(e.offsetParent!=null){ offset+=getLeft(e.offsetParent);}
	return offset;
}

//获取滚动条的位置
function getScroll(){
	var t, l, w, h;
	if (document.documentElement && document.documentElement.scrollTop) {
		t = document.documentElement.scrollTop;
		l = document.documentElement.scrollLeft;
		w = document.documentElement.scrollWidth;
		h = document.documentElement.scrollHeight;
	} else if (document.body) {
		t = document.body.scrollTop;
		l = document.body.scrollLeft;
		w = document.body.scrollWidth;
		h = document.body.scrollHeight;
	}
	return { t: t, l: l, w: w, h: h };
} 


function get_login_frm_html(){
	var code = "<form id='login_frm' onsubmit='login_box_submit();return false;'>"
				+ "<div id='login_box' class='mountBox' style='left:35%;'>"
                + "	<div class='bg_r'></div>"
                + "    <div class='bg_b'></div>"
                + "    <div class='m login'>"
                + "        <a href='javascript:global_login_frm_unshow()' title='关闭' class='Aclose'>关闭</a>"
                + "        <h5>请登录</h5>"
                + "        <ul>"
                + "            <li>"
                + "                <p>用户名：</p>"
                + "                <p class='p2'><input id='login_username' name='username' type='text' class='textinput' onblur='login_check_username();' /></p>"
                + "            </li>"
				+ "			<li id='login_username_alert_li' style='display:none;'><p>&nbsp;</p><p class='p3' style='color:red;'><span id='login_username_alert'></span></p></li>"
                + "            <li>"
                + "                <p>密&nbsp;&nbsp;码：</p>"
                + "                <p class='p2'><input name='password' type='password' class='textinput' onblur='login_check_password();' /> <a href='http://i.xunlei.com/register/password_1.htm' class='A1' target='_blank'>忘记密码？</a></p>"
                + "            </li>"
				+ "			<li><p>&nbsp;</p><p class='p3' style='color:red;'><span id='login_password_alert'></span></p></li>"
                + "            <li id='login_success_alert' class='btn'><input name='' type='submit' id='login_submit_btn' value=' 登 录 ' /> "
				+ "			<a href='http://i.xunlei.com/register/register.htm' target='_blank'>免费注册</a></li>"
                + "        </ul>"
                + "    </div>"
                + "</div>"
				+ "</form>";
	return code;
}

function global_login_frm_show(click_obj, is_top_click){
	if(document.getElementById('global_login_frm') == null){
		create_login_opacity_div();

		var div = document.createElement("div");
		div.setAttribute('id', 'global_login_frm');
		div.innerHTML = get_login_frm_html();
		document.body.appendChild(div);
		document.getElementById('login_username').focus();

		//var b = document.getElementById('login_box');
		//b.style.posLeft =   document.body.scrollLeft + (document.body.clientWidth  - b.clientWidth)/2;  
		//b.style.posTop  =   document.body.scrollTop  + (document.body.clientHeight - b.clientHeight)/2;  
		var top = 0;
		if(typeof(is_top_click) == 'boolean'){
			if(is_top_click){
				top = 160;
			}
		}
		if(top==0){
			top = parseInt(getTop(click_obj));
		}
		//alert(top);
		document.getElementById('login_box').style.top = top + 'px';
	}
}

function global_login_frm_unshow(){
	cancel_div('login_half_opacity_div');
	cancel_div('global_login_frm');
}


function login_check_username(){
	var input = document.getElementById('login_frm').getElementsByTagName('input')[0];
	if(input.value.trim() == ''){
		document.getElementById('login_username_alert_li').style.display = 'block';
		document.getElementById('login_username_alert').innerHTML = '<font color=red>用户名不能为空</font>';
		//input.focus();
		return false;
	}
	document.getElementById('login_username_alert_li').style.display = 'none';
	document.getElementById('login_username_alert').innerHTML = '';

	if(login_fail_alert){
		document.getElementById('login_password_alert').innerHTML = '';
	}
	return true;
}

function login_check_password(){
	var input = document.getElementById('login_frm').getElementsByTagName('input')[1];
	if(input.value.trim() == ''){
		document.getElementById('login_password_alert').innerHTML = '密码不能为空';
		//input.focus();
		return false;
	}
	document.getElementById('login_password_alert').innerHTML = '';
	return true;
}

function login_box_submit(){
	if(!login_check_username()){
		return false;
	}
	if(!login_check_password()){
		return false;
	}
	
	document.getElementById('login_submit_btn').disabled = true;
	document.getElementById('login_submit_btn').value = '登录处理中......';

	var pars = getQueryString('login_frm');
	var url = 'http://user.movie.xunlei.com/login.php?action=form_login'; 

	var ajax = new xlAjax(url, pars, login_box_submit_success);
	ajax.post();
}

var login_fail_alert = false;
function login_box_submit_success(request){
	var json = eval('(' + request.responseText + ')');
	if(1==parseInt(json.tid)){
		document.getElementById('login_submit_btn').value = '登录';
		document.getElementById('login_success_alert').innerHTML = "<font color='red'><b>登录成功，3秒之后自动关闭......</b></font>";
		user_is_login = 1;
		setTimeout("global_login_frm_unshow();refresh_page();", 3000);
	}else{
		login_fail_alert = true;
		document.getElementById('login_password_alert').innerHTML = '<b>'+json.msg+'</b>';
		document.getElementById('login_submit_btn').value = '登录';
		document.getElementById('login_submit_btn').disabled = false;
	}
}

function create_login_opacity_div(){
	var div = document.createElement("div");
	div.setAttribute('id', 'login_half_opacity_div');
	div.style.height = document.body.clientHeight;
	document.body.appendChild(div);
}
