﻿addLoadEvent(userbox);

function login(root_path,from){
	var loginPwd = $I('login_password').value;
	if( loginPwd.trim().length!=0 && loginPwd.length!=32 )
		$I('login_password').value = hex_md5(hex_md5($I('login_password').value));
	$.ajax({
		type : 'get',
		url : (''==root_path?root_path:root_path+'login')+'/control?do=qlogin',
		data : $('#form_login').serialize(),
		cache : false,
		dataType : 'text',
		timeout : 10000,
		beforeSend : function(XMLHttpRequest){
			loading(1);
		},
		success: function(text){
			var arr = text.split('|');
			if( 'OK' == arr[0] ){
				if( from && from!=0 )
					location.href = from;
				else if( from == 0 ){
					location.reload();
				}else
					userbox();
				//导航发布按钮属性
				var lkA = $I('nav_position_add'); if( lkA ){$(lkA).attr('islogin','true');$(lkA).attr('iscompany',arr[1]);$(lkA).attr('isperson',arr[2]);}
				var lkB = $I('nav_resume_add'); if( lkB ){$(lkB).attr('islogin','true');$(lkB).attr('iscompany',arr[1]);$(lkB).attr('isperson',arr[2]);}
				//首页缓存用户信息
				var ucache = $I('user_cache');
				if( ucache ){
					$(ucache).attr('islogin','true'); $(ucache).attr('iscompany',arr[1]); $(ucache).attr('isperson',arr[2]);
					$(ucache).attr('username',arr[3]); $(ucache).attr('realname',arr[4]); $(ucache).attr('hidden',arr[5]);
				}
			}else{
				alert(text.split('|')[0]);
				try{$I(text.split('|')[1]).focus();}catch(e){};
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert('请求超时，请重试。');
		},
		complete: function(){
			loading(0);
		}
	});
}

function loginout(root_path,jump){
	if( !confirm('确定退出登录？') ) return;
	$.ajax({
		type : 'get',
		url : root_path+'login/control?do=loginout',
		cache : false,
		dataType : 'text',
		timeout : 20000,
		beforeSend : function(XMLHttpRequest){
			loading(1);
		},
		success: function(text){
			if( 'OK'==text ){
				if( jump )
					location.href = jump;
				else
					userbox();
				//导航发布按钮属性
				//var lkA = $I('nav_position_add');
				//if( lkA ){$(lkA).attr('islogin','false');$(lkA).attr('iscompany','false');$(lkA).attr('isperson','false');}
				//var lkB = $I('nav_resume_add');
				//if( lkB ){$(lkB).attr('islogin','false');$(lkB).attr('iscompany','false');$(lkB).attr('isperson','false');}
				//首页缓存用户信息
				var ucache = $I('user_cache');
				if( ucache ){$(ucache).attr('islogin','false');$(ucache).attr('iscompany','false');$(ucache).attr('isperson','false');$(ucache).attr('username','false');$(ucache).attr('realname','false');}
			}else{
				alert('请求出错，请重试！');
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert('请求出错，请重试！');
		},
		complete: function(){
			loading(0);
		}
	});
}

function getPassword(root_path){
	$.ajax({
		type : 'get',
		url : root_path+'login/control?do=getpassword&email='+$I('gp_email').value,
		cache : false,
		dataType : 'text',
		timeout : 20000,
		beforeSend : function(XMLHttpRequest){
			loading(1);
		},
		success: function(text){
			if( 'OK' == text ){
				alert('登录密码已发送至您的邮箱，请注册查收。');
				$('#getpassword_box').slideToggle("fast");
				$('#mask_div').slideToggle("fast");
			}else{
				alert(text.split('|')[0]);
				try{$I(text.split('|')[1]).focus();}catch(e){};
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert('请求出错，请重试！');
		},
		complete: function(){
			loading(0);
		}
	});
}

function getPasswordBox(event,root_path){
	event = event || window.event;
	if( $I('getpassword_box') ){$('#getpassword_box').slideToggle("fast",function(){$('#mask_div').slideToggle("fast");}); return;}
	var htm = '<div id="mask_div" style="position:absolute;left:0px;top:0px;width:'+$(document).width()+'px;height:'+$(document).height()+'px;z-index:999;background:#000;filter:alpha(opacity=30);opacity:0.3;"></div>';
	htm    += '<div id="getpassword_box" class="getpassword" style="position:absolute;z-index:10000;display:none;">';
	htm    +=   '<div class="TT"><a href="#" onclick="$(\'#getpassword_box\').slideToggle(\'fast\',function(){$(\'#mask_div\').slideToggle(\'fast\');});return false;">关闭</a></div>';
	htm    +=   '<div class="CC">';
	htm    +=     '<span class="tip">如果忘记了帐号或密码，请提交您注册时填写的邮箱，系统会自动将您的帐号和密码发送到您的邮箱：</span>';
	htm    +=     '<form id="form_getpassword" name="form_getpassword" method="get" action="#GetPassword" onsubmit="getPassword(\''+root_path+'\');return false;">';
	htm    +=       '邮&nbsp;&nbsp;&nbsp;&nbsp;箱：<input name="gp_email" class="em" id="gp_email" type="text" maxlength="50" /><input class="sm" type="submit" value="提交" />';
	htm    +=     '</form>';
	htm    +=   '</div>';
	htm    +=   '<div class="BB">';
	htm    +=     '·如果您还不是会员，请点击：<a href="'+root_path+'register/?model=company">注册单位会员</a><a href="'+root_path+'register/?model=person">注册个人会员</a><br />·如需任何帮助，欢迎致电“3JOB网招集群”全国客服：400－8119358';
	htm    +=   '</div>';
	htm    += '</div>';
	$(document.body).append(htm);
	var box = $I('getpassword_box');
	box.style.left = $(document).width()/2 - 200 + 'px';
	box.style.top  = event.clientY + document.documentElement.scrollTop -280 + 'px';
	$(box).slideDown("fast");
}

function loginBox(event,root_path,model,from_domain){
	event = event || window.event;
	model = model || 'person'; //注册模式
	from_domain = from_domain || '';
	if( $I('login_box') ){
		var box = $I('login_box');
		box.style.left = $(document).width()/2 - 180 + 'px';
		var t = event.clientY + document.documentElement.scrollTop - 280;
		if( t <= 0 ) t = 120; box.style.top  = t + 'px';
		$('#login_register').attr('href',from_domain+'register/?model='+model);
		$('#login_box').slideToggle("fast",function(){$('#mask_div').slideToggle("fast",function(){$I('login_username').focus();})});
		return;
	}
	var htm = '<div id="mask_div" style="position:absolute;left:0px;top:0px;width:'+$(document).width()+'px;height:'+$(document).height()+'px;z-index:999;background:#000;filter:alpha(opacity=10);opacity:0.1;"></div>';
	htm    += '<div id="login_box" style="position:absolute;z-index:10000;display:none;width:360px;background:#fff;border:1px solid #999;">';
	htm    +=   '<form id="form_login" name="form_login" method="get" action="#" target="_blank" onsubmit="login(\''+root_path+'\',0);return false;">';
	htm    +=   '<table style="border:8px solid #eee;" width="100%" border="0" cellspacing="0" cellpadding="6">';
	htm    +=     '<tr><td colspan="2" align="right"><a style="font-size:14px;font-weight:bold;color:#f00;" href="#" onclick="$(\'#login_box\').slideToggle(\'fast\',function(){$(\'#mask_div\').slideToggle(\'fast\')});return false;">×</a> </td></tr>';
	htm    +=     '<tr><td colspan="2" style="text-indent:5.6em;font-weight:bold;">您还没有登录，请登录后再进行操作：</td></tr>';
	htm    +=     '<tr><td width="62" align="right" style="padding-right:0;">用户名：</td><td><input name="login_username" type="text" id="login_username" size="30" maxlength="30" /></td></tr>';
	htm    +=     '<tr><td align="right" style="padding-right:0;">密&nbsp;&nbsp; 码：</td><td><input name="login_password" type="password" id="login_password" size="30" maxlength="30" onfocus="select();" /></td></tr>';
	htm    +=     '<tr><td>&nbsp;</td><td><input name="login_model" type="checkbox" class="checkbox" id="login_model" value="hidden" /> <label for="login_model" title="以隐身模式登录的用户将不会被显示在现场招聘大厅的在线列表中。">隐身模式登录</label></td></tr>';
	htm    +=     '<tr><td>&nbsp;</td><td><input type="submit" id="button" class="button" value="立即登录" />&nbsp; &nbsp; <a id="login_register" style="text-decoration:underline;color:#f00;" href="'+from_domain+'register/?model='+model+'" target="_blank">新用户请注册</a></td></tr>';
	htm    +=   '</table>';
	htm    +=   '</form>';
	htm    += '</div>';
	$(document.body).append(htm);
	var box = $I('login_box');
	box.style.left = $(document).width()/2 - 180 + 'px';
	var t = event.clientY + document.documentElement.scrollTop - 280;
	if( t <= 0 ) t = 120; box.style.top  = t + 'px';
	$(box).slideDown("fast",function(){$I('login_username').focus();});
}

function userbox(){
	var userBox = $I('form_login');
	if( !userBox ) return;
	if( !$I('login_box') ) return;
	userBox.innerHTML = '<p style="text-align:center;">loading...</p>';
	$.ajax({
		type : 'get',
		url : 'login/control?do=userbox',
		cache : false,
		dataType : 'text',
		timeout : 20000,
		success: function(text){
			userBox.innerHTML = text;
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			userBox.innerHTML = 'Error : request timeout.';
		}
	});
}

//动态返回用户信息
function user_cache(){
	var ucache = $I('user_cache');
	if( !ucache ) return;
	ucache = $(ucache);
	$.ajax({
		type : 'get',
		url : 'login/control?do=user_cache',
		cache : false,
		dataType : 'text',
		timeout : 20000,
		success: function(text){
			var arr = text.split('(,)');
			try{
				ucache.attr('islogin',arr[0]);
				ucache.attr('isperson',arr[1]);
				ucache.attr('iscompany',arr[2]);
				ucache.attr('isvip',arr[3]);
				ucache.attr('username',arr[4]);
				ucache.attr('com_name',arr[5]);
				ucache.attr('realname',arr[6]);
				ucache.attr('sex',arr[7]);
				ucache.attr('group',arr[8]);
				ucache.attr('jobs',arr[9]);
				ucache.attr('hidden',arr[10]);
			}catch(e){}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ucache.attr('islogin','false');
			ucache.attr('isperson','false');
			ucache.attr('iscompany','false');
			ucache.attr('isvip','false');
			ucache.attr('username','');
			ucache.attr('com_name','');
			ucache.attr('realname','');
			ucache.attr('sex','');
			ucache.attr('group','');
			ucache.attr('jobs','');
			ucache.attr('hidden',true);
		}
	});
}