﻿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]);
				//}
				user_cache();
			}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');}
				user_cache();
			}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" style="position:absolute;z-index:10000;display:none;">';
	htm    +=   '<div class="xxwstop wjmm" style=" margin-top:20px;"><ul><li class="top_left"></li><li class="top_center"><span>忘记密码</span><a href="#" class="close" onclick="$(\'#getpassword_box\').slideToggle(\'fast\',function(){$(\'#mask_div\').slideToggle(\'fast\');});return false;">关闭</a></li><li class="top_right"></li></ul></div>';
	htm    +=   '<div class="xxwscon wjmm_con"><form id="form_getpassword" name="form_getpassword" method="get" action="#GetPassword" onsubmit="getPassword(\''+root_path+'\');return false;"><ul><li class="bot_line tit">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果忘记了帐号或密码，请提交您注册时填写的邮箱，系统会自动将您的帐号和密码发送到您的邮箱：</li><li class="bot_line a1">邮箱：<input name="gp_email"  class="text_01" id="gp_email" type="text" maxlength="50" />&nbsp;&nbsp;<input name="button" type="submit" class="xxwsbut01" onmouseover="this.className=\'xxwsbut02\'" onmouseout="this.className=\'xxwsbut01\'"   value="提 交"   onclick="this.form.submit();" /></li><li class="a2">·如果您还不是会员，请点击：<a href="/register/?model=company" target="_blank">注册单位会员</a>&nbsp;&nbsp;<a href="/register/?model=person" target="_blank">注册个人会员</a><br />·如需任何帮助，欢迎致电“3JOB网招集群”<br />&nbsp;&nbsp;&nbsp;&nbsp;全国客服：400－8119358</li></ul></form></div>'
	htm    +=   '<div class="xxwsbot wjmm"><ul><li class="bot_left"></li><li class="bot_center"></li><li class="bot_right"></li></ul></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-110;
	    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;"><form id="form_login" name="form_login" method="get" action="#" target="_blank" onsubmit="login(\''+root_path+'\',0);return false;">'
	htm    +=   '<div class="xxwstop dlts"><ul><li class="top_left"></li><li class="top_center"><span>服务提示</span><a href="#" class="close " onclick="$(\'#login_box\').slideToggle(\'fast\',function(){$(\'#mask_div\').slideToggle(\'fast\')});return false;">关闭</a></li><li class="top_right"></li></ul></div>';
	htm    +=   '<div  class="xxwscon dlts_con"><ul><li><span>  您刚才的操作需要登录后才能继续 ！</span></li><li>用户名：<input type="text" name="login_username" id="login_username"  class="text_01"/></li><li>密　码：<input type="password" name="login_password" id="login_password"  class="text_01"/></li><li class="but bot_line"><input name="button" type="submit" class="xxwsbut01" onmouseover="this.className=\'xxwsbut02\'" onmouseout="this.className=\'xxwsbut01\'"   value="立即登录" /></li><li>如果您还不是会员，请先注册!</li><li><img src="/files/images2010/icon_hand2.gif" width="19" height="12" /><a href="'+from_domain+'register/?model=company" target="_blank">注册单位会员</a>  <a href="'+from_domain+'register/?model=person" target="_blank">注册个人会员</a></li></ul></div>';
	htm    +=   '<div class="xxwsbot dlts"><ul><li class="bot_left"></li><li class="bot_center"></li><li class="bot_right"></li></ul></div>'
	htm    += '</form></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 =  document.documentElement.scrollTop - 280;
	if( t <= 0 ) t = 120; box.style.top  = t+150 + 'px';
	$(box).slideDown("fast",function(){$I('login_username').focus();});
}

function userbox(){
	var userBoxA = $I('index-user-boxA');
	var userBoxB = $I('index-user-boxB');
	if( !userBoxA || !userBoxB ) return;
	$.ajax({
		type : 'get',
		url : 'login/control?do=userbox',
		cache : false,
		dataType : 'text',
		timeout : 20000,
		success: function(text){
			if (text == 'N') {
				userBoxB.style.display = 'none';
				$(userBoxA).slideDown(300);
			} else {
				userBoxA.style.display = 'none';
				userBoxB.innerHTML = text;
				$(userBoxB).slideDown(300);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			userBoxA.style.display = 'none';
			userBoxB.innerHTML = 'Error : request timeout.';
			$(userBoxB).slideDown(300);
		}
	});
}

//动态返回用户信息
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');
		}
	});
}
