/**
 * @file_name position.js
 * @file_to index.jsp
 * @see  人才招聘
 * @author zhaohui
 * @version 1.0 2011-03-11
 */

//所选行业数组
var industrys_select;
//所选职能数组
var functionDetail_select;
// 选择的行业【结果】
var industryselectedTxt = "";
// 选择的职能【结果】
var functionselectedTxt = "";
// 登录后用户ID
var loginedUserId = "";

jQuery(document).ready(function(){
	var modify = getValue_util("modify_hidden");
	if(modify){
		getRegisteredDetail();
	}
});

// 显示招聘信息窗口
function toPositionView(id,type){
	var url;
	try{
	membersRegService.isRegister(function(data){
		if(data){
			if(type == 0){
				url = "/jsp/position/positionView.jsp?id=" + id;
			}else{
				url = "positionView.jsp?id=" + id;
			}
			window.showModalDialog(url,"newwin",
					"dialogHeight: 600px; dialogWidth: 700px;" +
					" dialogTop: 458px; dialogLeft: 166px; " +
					"edge: Raised; center: Yes; help: Yes; " +
					"resizable: Yes; status: Yes;"
					);
	  }else{
		    alert('对不起，您还未登录或者注册，请先登录或者注册然后再查看招聘信息！');
			//window.location.href = "/jsp/position/register.jsp";
			window.open("/jsp/position/register.jsp");
		}
	});
	}catch(ex){
		//alert(ex.message);
	}
}
function setUserObject(){
	var regUser;
	regUser = {
			username:getValue_util("username"),
			password:getValue_util("password"),
			type:"register"
	};
	return regUser;
}

function register(){
	if(validateReg()){
		var user = setUserObject();
		try{
			membersRegService.saveRegisterUser(user,saveRegCallback);
		}catch(ex){
			//alert('-->' + ex.message);
		}
	}	
}
function saveRegCallback(data){
	if(data == 0){
		alert("注册成功!");
		window.location.href = "/jsp/position/register_step1.jsp";
	}else if(data == 1){
		alert("注册失败！请尝试重新注册！");
	}else if(data == 2){
		alert("此用户名已注册，请重新注册！");
	}
	initUserReg();
}
function initUserReg(){
	setValue_util("username","");
	setValue_util("password","");
	setValue_util("password2","");
}


// 注册及登录用户名及密码验证
function validateReg(){
	var name = getValue_util("username");
	var password1 = getValue_util("password");
	var password2 = getValue_util("password2");
	var filter=/^S*[a-zA-z]{1}[a-z0-9A-Z_]{3,14}S*$/;
	if (!filter.test(name))
	{
		alert("您的用户名填写不正确，不能为空，会员名必须以字母开头，至少4位不能大于15位(可以字母、数字、下划线)");
		setFocus_util("username");
		return false
	}
	if (password1 =="" || password1.length<4)
	{
	alert("您的密码填写不正确，不能为空，且不能小于4位");
	setFocus_util("password");
	return false
	}
	if (password1 != password2)
	{
	alert("您两次输入的密码不一致，请重新输入");
	setFocus_util("password2");
	return false;
	}
	return true;
}
// =========注册详细信息============//
function setRegDetailObject(){
	var reg;
	try{
	reg = {
			userid:loginedUserId,
			realname:getValue_util("regName"),
			sex:jQuery('input[@name=radioSex][@checked]').val(),
			byear:getValue_util("byear"),
			bmonth:getValue_util("bmonth"),
			bday:getValue_util("bday"),
			height:getValue_util("height"),
			weight:getValue_util("weight"),
			zhuanye:getValue_util("zhuanye"),
			xueli:getValue_util("xueli"),
			registered:getValue_util("registered"),
			certnum:getValue_util("certnum"),//身份证
			nowmoney:getValue_util("nowmoney"),
			hunyin:getValue_util("hunyin"),
			sgyear:"",//上岗年
			sgmonth:"",//上岗月
			nianxian:getValue_util("nianxian"),//工作年限 
			nowadd:getValue_util("nowadd"),//现住址
			hometel:getValue_util("hometel"),
			mobile:getValue_util("mobile"),
			address:getValue_util("address"),//联系地址
			qq:getValue_util("qq"),//QQ		
			email:getValue_util("email"),//个人主页
			jyjl:getValue_util("jyjl"),//教育经历
			gzjl:getValue_util("gzjl"),//工作经历
			zwpj:getValue_util("zwpj"),//自我评价
			gzlx:getValue_util("gzlx"),//工作类型
			industry:industryselectedTxt,//行业
			functions:functionselectedTxt,//职能
			area:getValue_util("area"),//地点
			comtype:getValue_util("comtype"),//公司性质
			money:getValue_util("money") //工资要求
	};
	}catch(ex){
		//alert(ex.message);
	}
	return reg;
}
function chkRegDetail(){
	if(chkReg1()){
		disabledElement_util("regStep1_div");
		abledElement_util("regStep2_div");
		getIndustry();
		getFunction();
	}
}
function chkReg1()
{
if (getValue_util("regName")=="")
	{
	alert("请填写您的真实姓名");
	setFocus_util("regName");
	return false;
	}
if(getValue_util("byear")==""||getValue_util("bmonth")==""||getValue_util("bday")=="")
	{
	alert("出生日期填写不全,请重新选择");
	setFocus_util("byear");
	return false;
	}
var filter=/^\d*\.?\d{0,2}$/;
if(!filter.test(getValue_util("height"))&&getValue_util("height")!="")
	{
	alert("请正确的填入您的身高");
	setFocus_util("height");
	return false;
	}
var filter=/^\d*\.?\d{0,2}$/;
if(!filter.test(getValue_util("weight"))&& getValue_util("weight")!="")
	{
	alert("请正确的填入您的体重");
	setFocus_util("weight");
	return false
	}	
if (getValue_util("zhuanye")=="")
	{
	alert("请选择您的所学专业")
	setFocus_util("zhuanye");
	return false;
	}
if (getValue_util("xueli") == "")
	{
	alert("请选择您的学历");
	setFocus_util("xueli");
	return false;
	}
if (getValue_util("registered")=="")
	{
	alert("请选择您的户口所在地");
	setFocus_util("registered");
	return false;
	}
if (getValue_util("certnum")=="")
	{
	alert("请填入您的身份证号");
	setFocus_util("certnum");
	return false;
	}
if (getValue_util("nianxian")=="")
	{
	alert("请选择您的工作年限");
	setFocus_util("nianxian");
	return false;
	}
if (getValue_util("nowadd")=="")
	{
	alert("请填入您的现住址");
    setFocus_util("nowadd");
	return false;
	}
if (getValue_util("mobile")=="")
{
	alert("请填入您的手机号");
	setFocus_util("mobile");
	return false;
}
if (getValue_util("qq")=="")
{
	alert("请填入您的QQ号");
	setFocus_util("qq");
	return false;
}
if (getValue_util("email")=="")
{
	alert("请填入您的电子邮箱");
	setFocus_util("email");
	return false;
}
return true
}
function chkReg2(){
	if(getValue_util("gzlx") == ""){
		alert("请选择工作类型");
		setFocus_util("gzlx");
		return false;
	}
	if(jQuery("select[@name=industrySel_select] option").length == 0){
		setFocus_util("industrySel_select");
		alert("请选择行业");
		return false;
	}
	if(jQuery("select[@name=detailSelected_select] option").length == 0){
		alert("请选择职能");
		setFocus_util("detailSelected_select");
		return false;
	}
	if(getValue_util("area") == ""){
		alert("请选择工作地点");
		setFocus_util("area");
		return false;
	}
	if(getValue_util("comtype") == ""){
		alert("请选择公司性质");
		setFocus_util("comtype");
		return false;
	}
	if(getValue_util("money") == ""){
		alert("请输入工资");
		setFocus_util("money");
		return false;
	}
    if((getValue_util("jyjl") == "")|| (getValue_util("jyjl").length < 50)){
    	alert("您的教育培训经历未填写或内容过于简单，请认真填写此项以便于我们为您找到更加适合的岗位!");
    	setFocus_util("jyjl");
    	return false;
    }
    if((getValue_util("gzjl") == "")|| (getValue_util("gzjl").length < 50)){
    	alert("您的工作经历未填写或内容过于简单，请认真填写此项以便于我们为您找到更加适合的岗位!");
    	setFocus_util("jyjl");
    	return false;
    }
    if((getValue_util("zwpj") == "")|| (getValue_util("zwpj").length < 50)){
    	alert("您的自我评价未填写或内容过于简单，请认真填写此项以便于我们为您找到更加适合的岗位!");
    	setFocus_util("jyjl");
    	return false;
    }
	return true;
}
function doyear(this0)
{
	year0=this0.value
	this1=getElementById_util("bmonth");
	this2=getElementById_util("bday");
	if (year0%4==0)
	daylen=29
	else
	daylen=28
	if (this1.value==2)
	{
	this2.length=daylen+1;
	for (k=1;k<=daylen;k++)
	{
	this2.options[k].text=k
	this2.options[k].value=k
	}
	}
}
function domonth(this1){
	this0=getElementById_util("byear");
	this2=getElementById_util("bday");
	month0=this1.options.selectedIndex;
	year0=this0.value;
	if(month0>0 ){
		switch (month0){	
			case 1: daylen=31;  break;
			case 2: if  (year0%4 ==0)
							daylen=29;
						else
							daylen=28 ;
						 break;
	        case 3: daylen=31;  break;
			case 4: daylen=30;  break;
			case 5: daylen=31;  break;
			case 6: daylen=30;  break;
			case 7: daylen=31;  break;
			case 8: daylen=31;  break;
			case 9: daylen=30;  break;
			case 10: daylen=31;  break;
			case 11: daylen=30;  break;
			case 12: daylen=31;  break;		
		}
		this2.length=daylen+1;
		 for(var k =1;k<=daylen;k++){
		this2.options[k].text  =k;
	    this2.options[k].value =k;
	   }
	 }
}
//========多选列表操作============//
//获取所有行业
function getIndustry(){
	membersRegService.getIndustrys(getIndustryCallback);
}
function getIndustryCallback(data){
	try{
		fillSelect_util("industry_select",data,true);
	}catch(ex){
		//alert(ex.message);
	}
}
// 添加所选择行业
function addIndustry(){
	addSelected("industry_select","industrySel_select",5,industrys_select);
}
// 删除所选行业
function delIndustry(){
	delSelected("industrySel_select");
}
// 获取所选择的行业
function getSelectedIndustry(){
	var count = 0;
	var len = jQuery("select[@name=industrySel_select] option").length;
	jQuery("select[@name=industrySel_select] option").each(function(){
		if(count == (len-1)){
			industryselectedTxt += jQuery(this).val();	
		}else{
			industryselectedTxt += jQuery(this).val() + ',';
		}
		count++;
	});	
}

// 获取所有职能
function getFunction(){
	membersRegService.getFunctions(getFunctionCallback);
}
function getFunctionCallback(data){
	try{
		fillSelect_util("function_select",data,true);
	}catch(ex){
		//alert(ex.message);
	}
}
// 获取某职能一下的明细
function getFunctionDetail(){
	try{
		var funName = jQuery("#function_select option[@selected]").text();
		membersRegService.getFunctionDetail(funName,getFunDetailCallback);
	}catch(ex){
		//alert(ex.message);
	}
}
function getFunDetailCallback(data){
	fillSelect_util("functionDetail_select",data,true);
}
// 添加所选职能
function addFunction(){
	addSelected("functionDetail_select","detailSelected_select",5,functionDetail_select);
}
// 删除所选职能
function delFunction(){
	delSelected("detailSelected_select");	
}
//获取所选择的职能
function getSelectedFunction(){
	var count = 0;
	var len = jQuery("select[@name=detailSelected_select] option").length;
	jQuery("select[@name=detailSelected_select] option").each(function(){
		if(count == (len-1)){
			functionselectedTxt += jQuery(this).val();	
		}else{
			functionselectedTxt += jQuery(this).val() + ',';
		}
		count++;
	});	
}

/**
 * 添加选项
 * @param sel_1 源
 * @param sel_2 目标
 * @param size 列表大小
 */
function addSelected(sel_1,sel_2,size,array){
	var isSelect = true;
	var txt = jQuery("#"+sel_1+" [@selected]").text();
	array = new Array();
	array.push(txt);
	var len = jQuery("select[@name="+sel_2+"] option").length;
	if(len >= size){
		alert('对不起，您只能选择'+size+'项!');
		return;
	}else{
		jQuery("select[@name="+sel_2+"] option").each(function(){
			if(jQuery(this).val() == txt){
				alert('您已经选择了：' + txt+',请重新选择！');
				isSelect = false;
				return;
			}
		});
		if(isSelect){
			fillSelect_util(sel_2,array,false);
		}
   }
}
/**
 * 删除选项
 * @param sel
 * @return
 */
function delSelected(sel){
	var txt = jQuery("#"+sel+" option[@selected]").text();
	jQuery("select[@name="+sel+"] option").each(function(){
		if(jQuery(this).val() == txt){
			jQuery(this).remove();
		}
	});
}
// 注册信息包括新增及更新
function confirmReg(){
	if(chkReg2()){
		getSelectedFunction();
		getSelectedIndustry();
		membersRegService.saveRegisterDetail(setRegDetailObject(),saveRegDetailCallback);
	}
}
function saveRegDetailCallback(data){
	if(data){
	window.location.href = "/jsp/position/regsuccess.jsp";
	}else{
		alert('很抱歉，您注册失败了!!!');
	}
}
function regLogin(){
	if(validateReg()){
		membersRegService.login(getValue_util("username"),
			getValue_util("password"),loginCallback);
	}
}
function loginCallback(data){
	if(data){
		alert("登录成功！您可以点击‘个人信息修改’对求职信息进行更新.");
		abledElement_util("modify_img");
		initUserReg();
		/*
		if(confirm("登录成功！您是否要更新注册信息？")){
			window.location.href = "/jsp/position/register_step1.jsp?modify=true";
		}else{
			window.close();
		}*/			
	}else{
		alert("登录失败！");
    }
}
function modifyMessage(){
	window.location.href = "/jsp/position/register_step1.jsp?modify=true";
}

function getRegisteredDetail(){
	membersRegService.getRegisterDetail(getRegDetailCallback);
}
function getRegDetailCallback(data){
	try{
		if(equals_util(data)){
			setRegInfo(data);
		}
	}catch(ex){
		//alert(ex.message);
	}
}
function setRegInfo(data){
	var txt;
	var array;
	try{
		// 记录下当前登录用户ID
		loginedUserId = data.userid;
		setValue_util("regName",data.realname);
		if(data.sex == "男"){
			setChecked_util("manRadio");
			setUnchecked_util("womenRadio");
		}else{
			setChecked_util("womenRadio");
			setUnchecked_util("manRadio");
		}
		setValue_util("byear",data.byear);
		setValue_util("bmonth",data.bmonth);
		setValue_util("bday",data.bday);
		setValue_util("height",data.height);
		setValue_util("weight",data.weight);
		setValue_util("zhuanye",data.zhuanye);
		setValue_util("xueli",data.xueli);
		setValue_util("registered",data.registered);
		setValue_util("certnum",data.certnum);
		setValue_util("nowMoney",data.nowmoney);
		setValue_util("hunyin",data.hunyin);
		//setValue_util("",data.sgyear);
		//setValue_util("",data.sgmonth);
		setValue_util("nianxian",data.nianxian);
		setValue_util("nowadd",data.nowadd);
		setValue_util("hometel",data.hometel);
		setValue_util("mobile",data.mobile);
		setValue_util("qq",data.qq);
		setValue_util("address",data.address);
		setValue_util("email",data.email);
		
		setValue_util("jyjl",data.jyjl);
		setValue_util("gzjl",data.gzjl);
		setValue_util("zwpj",data.zwpj);
		setValue_util("gzlx",data.gzlx);
		txt = data.industry;
		array = txt.split(',');
		fillSelect_util("industrySel_select",array,true);
		txt = data.functions;
		array = txt.split(',');
		fillSelect_util("detailSelected_select",array,true);
		setValue_util("area",data.area);
		setValue_util("comtype",data.comtype);
		setValue_util("money",data.money);
		//setValue_util("",data.regDate);
	    
	}catch(ex){
		//alert(ex.message);
	}	
}
