var StaffManager = Class.create();

StaffManager.prototype = {
	initialize: function(){
	},
	
	checkRRNCert: function(){
		var rrnCertForm = $('rrnCertForm');
		var validator = doit.util.Validation;
		
		if(validator.formNecessaryValidate(rrnCertForm)){
			rrnCertForm.action = "edusupp/letr/reg/incCheckPsnInfoRRNUser.aff";
			rrnCertForm.submit();
		}
	},
	
	callRRNCertNext: function(){
		if(opener != null && opener.bbsMgr != null && opener.bbsMgr.goRRNCertNext){
			opener.bbsMgr.goRRNCertNext(jsonStr);
			self.close();
		}
	},
	
	// 팝업에서 다음 화면 으로 이동 등록 혹은 수정
	goRRNCertNext: function(jsonStr){
		var checkResult = jsonStr.evalJSON();		
		if(checkResult == undefined || checkResult == null){
			alert('오류가 발생했습니다');
			return;
		}
		
		
		if(checkResult.staff_idx == undefined || checkResult.staff_idx == null){
			this.goCreateStaff(checkResult);
			
		} else {
			if(checkResult.update_syn == undefined || checkResult.update_syn == null){
				alert('오류가 발생했습니다');
				return;
			} else if(checkResult.update_syn == 'Y'){
				this.goModifyStaffWithRRN(checkResult);
			} else if(checkResult.update_syn == 'N'){
				this.goCreateStaffWithRRN(checkResult);
			} else {
				alert('오류가 발생했습니다');
			}
		}
	},
	
	goCreateStaff: function(checkResult){
		var staffForm = $('staffForm');
		
		$('psninfo_kr_nm').value = checkResult.psninfo_kr_nm;
		$('psninfo_rrn').value = checkResult.psninfo_rrn;
		$('psninfo_psn_idnt_key').value = checkResult.psninfo_psn_idnt_key;
		$('stafftyp_cd_list').value = checkResult.stafftyp_cd_list;
		$('stafftyp_cd').value = checkResult.stafftyp_cd;
		
		staffForm.action="/user/bottom/agristaff/createStaffForm.aff";
		staffForm.submit();
	},
	
	goCreateStaffWithRRN: function(checkResult){
		var staffForm = $('staffForm');
		
		$('psninfo_kr_nm').value = checkResult.psninfo_kr_nm;
		$('psninfo_rrn').value = checkResult.psninfo_rrn;
		$('psninfo_psn_idnt_key').value = checkResult.psninfo_psn_idnt_key;
		$('stafftyp_cd_list').value = checkResult.stafftyp_cd_list;
		$('stafftyp_cd').value = checkResult.stafftyp_cd;
		
		$('staff_idx').value = checkResult.staff_idx;
		$('rrn_create_flag').value = "Y"; //Y:기존 정보를 불러와서 새로 등록 하기, N 또는 "" 또는 null - 일반 새로 등록 
		
		staffForm.action="/user/bottom/agristaff/createStaffForm.aff";
		staffForm.submit();
	},
	
	goModifyStaffWithRRN: function(checkResult){
		
		var staffForm = $('staffForm');
		
		$('stafftyp_cd_list').value = checkResult.stafftyp_cd_list;
		$('stafftyp_cd').value = checkResult.stafftyp_cd;
		
		$('staff_idx').value = checkResult.staff_idx;
		
		staffForm.action="/user/bottom/agristaff/modifyStaffForm.aff";
		staffForm.submit();	
	},
	
	goModifyStaff: function(){
		var staffForm = $('staffForm');
		staffForm.action="/user/bottom/agristaff/modifyStaffForm.aff";
		staffForm.submit();	
	},	
	
	toggleDetailForm: function(){
		$('detailForm').toggle();
	},
	
	addSchshp: function(){
		var schshp_body = $('schshp-body');
		var id = this.createUUID();
		
	
		var markup = ''+
		'<tr id="schshp-row_'+id+'">'+
			'<td class="tdcnt01">'+
			'<input type="text" name="staffObj.schshp_st_tm" id="schshp_st_tm_'+id+'" value="" class="input_box14"  readonly="readonly" /> <a href="javascript:calCall(\'schshp_st_tm_'+id+'\', document.staffForm );" ><img src="/images/admin/common/calendar/calendar_icon2.gif" width="17" height="17" border="0" ></a> ~ '+ 
			'<input type="text" name="staffObj.schshp_end_tm" id="schshp_end_tm_'+id+'" value="" class="input_box14"  readonly="readonly" /> <a href="javascript:calCall(\'schshp_end_tm_'+id+'\', document.staffForm );" ><img src="/images/admin/common/calendar/calendar_icon2.gif" width="17" height="17" border="0" ></a></td>'+
			'<td class="tdcnt01"><input type="text" name="staffObj.schshp_sch" value=""  class="input_box17" maxlength="30" /></td>'+
			'<td class="tdcnt01"><input type="text" name="staffObj.schshp_lesson" value="" class="input_box18" maxlength="30" /></td>'+
			'<td class="tdcnt01"><input type="text" name="staffObj.schshp_dgr" value="" class="input_box19" maxlength="30" /></td>'+
			'<td class="tdcnt02"><a href="#" onclick="bbsMgr.deleteSchshp(\''+id+'\');return false;"><img src="/images/user/common/btn/btn_delete.gif" alt="삭제" /></a></td>'+
		'</tr>';
			
		schshp_body.insert({bottom: markup});
	},
	
	deleteSchshp: function(id){
		$('schshp-row_'+id).remove();
	},
	
	addAct: function(){
		var act_body = $('act-body');
		var id = this.createUUID();
		var markup = ''+ 
		'<tr id="act-row_'+id+'">'+
			'<td class="tdcnt01">'+
			'<input type="text" name="staffObj.act_st_tm" id="act_st_tm_'+id+'" value="" class="input_box14"  readonly="readonly" /> <a href="javascript:calCall(\'act_st_tm_'+id+'\', document.staffForm );" ><img src="/images/admin/common/calendar/calendar_icon2.gif" width="17" height="17" border="0" ></a> ~ '+ 
			'<input type="text" name="staffObj.act_end_tm" id="act_end_tm_'+id+'" value="" class="input_box14"  readonly="readonly" /> <a href="javascript:calCall(\'act_end_tm_'+id+'\', document.staffForm );" ><img src="/images/admin/common/calendar/calendar_icon2.gif" width="17" height="17" border="0" ></a></td>'+
			'<td class="tdcnt01"><input type="text" name="staffObj.act_cnts" value="" class="input_box07" maxlength="30" /></td>'+
			'<td class="tdcnt01"><input type="text" name="staffObj.act_note" value="" class="input_box20" maxlength="30" /></td>'+
			'<td class="tdcnt02"><a href="#" onclick="bbsMgr.deleteAct(\''+id+'\'); return false;"><img src="/images/user/common/btn/btn_delete.gif" alt="삭제" /></a></td>'+
		'</tr>';
			
		act_body.insert({bottom: markup});
	},
	
	deleteAct: function(id){
		$('act-row_'+id).remove();
	},
	
	addAwd: function(){
		var awd_body = $('awd-body');
		var id = this.createUUID();
		var markup = ''+
		'<tr id="awd-row_'+id+'">'+
			'<td class="tdcnt01">'+
				'<input type="text" name="staffObj.awd_tm" id="awd_tm_'+id+'" value="" class="input_box14"  readonly="readonly" /> <a href="javascript:calCall(\'awd_tm_'+id+'\', document.staffForm );" ><img src="/images/admin/common/calendar/calendar_icon2.gif" width="17" height="17" border="0" ></a>'+
			'</td>'+
			'<td class="tdcnt01"><input type="text" name="staffObj.awd_cnts" value="" class="input_box07" maxlength="30" /></td>'+
			'<td class="tdcnt01"><input type="text" name="staffObj.awd_manager" value="" class="input_box20" maxlength="30" /></td>'+
			'<td class="tdcnt02"><a href="#" onclick="bbsMgr.deleteAwd(\''+id+'\');return false;"><img src="/images/user/common/btn/btn_delete.gif" alt="삭제" /></a></td>'+
		'</tr>';
			
		awd_body.insert({bottom: markup});
	},
	
	deleteAwd: function(id){
		$('awd-row_'+id).remove();
	},
	
	
	validateForm:function(){
		var validator = doit.util.Validation;
		var staffForm = $('staffForm');
		if(!validator.formNecessaryValidate(staffForm)){
			return false;
		}
		
		$('staffObj.fld_cd_list').value = this.getCheckValues('fld');
		$('staffObj.major_cd_list').value = this.getCheckValues('major');
		$('staffObj.itm_cd_list').value = this.getCheckValues('itm');
		
		if($('staffObj.psnObj.zipno11').value == "" || $('staffObj.psnObj.zipno12').value == ""){
			alert("우편번호를 입력해 주세요");
			return false;
		}
		if($('staffObj.psnObj.psninfo_addr1').value == "" || $('staffObj.psnObj.psninfo_dtldaddr1').value == ""){
			alert("주소를 입력해 주세요");
			return false;
		}
		
		if($('staffObj.psnObj.psninfo_tel1').value == "" || $('staffObj.psnObj.psninfo_tel2').value == "" || $('staffObj.psnObj.psninfo_tel3').value == ""){
			alert("전화번호를 입력해 주세요");
			return false;
		}
		if($('staffObj.psnObj.psninfo_mbp1').value == "" || $('staffObj.psnObj.psninfo_mbp2').value == "" || $('staffObj.psnObj.psninfo_mbp3').value == ""){
			alert("휴대폰 번호를 입력해 주세요");
			return false;
		}
		
		if($('staffObj.psnObj.psninfo_email1').value == ""){
			alert("이메일 주소를 입력해 주세요");
			return false;
		}
		if($('staffObj.staff_majorfld').value == ""){
			alert("전공분야를 입력해 주세요");
			return false;
		}
		if($('staffObj.staff_lctrsbjt').value == ""){
			alert("강의과목을 입력해 주세요");
			return false;
		}
		if($('staffObj.fld_cd_list').value == ""){
			alert("소속분야를 선택해 주세요");
			return false;
		}
		
		if($('staffObj.major_cd_list').value == ""){
			alert("전공을 선택해 주세요");
			return false;
		}
		
		if($('staffObj.itm_cd_list').value == ""){
			alert("품목을 선택해 주세요");
			return false;
		}
		
		return true;
	},
	
	
	createStaff: function(){
		if(!this.validateForm()){
			return;
		}		
		
		var staffForm = $('staffForm');
		$('staffObj.fld_cd_list').value = this.getCheckValues('fld');
		$('staffObj.major_cd_list').value = this.getCheckValues('major');
		$('staffObj.itm_cd_list').value = this.getCheckValues('itm');
		
		staffForm.action= "/user/bottom/agristaff/createStaff.aff";
		staffForm.submit();	
	},
	
	modifyStaff: function(){
		if(!this.validateForm()){
			return;
		}
		
		var staffForm = $('staffForm');
		$('staffObj.fld_cd_list').value = this.getCheckValues('fld');
		$('staffObj.major_cd_list').value = this.getCheckValues('major');
		$('staffObj.itm_cd_list').value = this.getCheckValues('itm');
		
		staffForm.action= "/user/bottom/agristaff/modifyStaff.aff";
		staffForm.submit();	
	},
	
	findZipcode: function(position){
		var url = "/user/common/zipno/showPopZipno.aff?zip_position=" + position;
		doit.ui.Popup.popWindow('findZipcode', url, 380, 420, 'no', 'no', 'no');
		
		return false;
	},
	
	setZipno: function(zipno,zip_sd,zip_gg,zip_dm,zip_bg, zip_position){
		if(zipno != null && zipno != ''){
			var zipArr = zipno.split('-');
			if(zip_position == 1) {
				$('staffObj.psnObj.zipno11').value = zipArr[0];
				$('staffObj.psnObj.zipno12').value = zipArr[1];
				
				$('staffObj.psnObj.psninfo_addr1').value = zip_sd+" "+zip_gg+" "+zip_dm+" "+zip_bg;
				$('staffObj.psnObj.psninfo_dtldaddr1').focus();
			} else if(zip_position == 2) {
				$('staffObj.psnObj.zipno21').value = zipArr[0];
				$('staffObj.psnObj.zipno22').value = zipArr[1];
				
				$('staffObj.psnObj.psninfo_addr2').value = zip_sd+" "+zip_gg+" "+zip_dm+" "+zip_bg;
				$('staffObj.psnObj.psninfo_dtldaddr2').focus();
			}
			
			
		}
	},
	
	getCheckValues: function(elnm){
		var staffForm = $('staffForm');
		var elList = Form.getInputs('staffForm', 'checkbox', elnm);
		var cd_list = new Array();
		
		if(elList != null && elList instanceof Array){
			elList.each(function(el){
				if(el.checked == true){
					cd_list.push(el.value);
				}
			});
		}
		
		return cd_list.join(',');
	},
	
	createUUID: function(){
		return [4, 2, 2, 2, 6].map(function(length) {
			var uuidpart = "";
			for (var i=0; i<length; i++) {
				var uuidchar = parseInt((Math.random() * 256)).toString(16);
				if (uuidchar.length == 1)
					uuidchar = "0" + uuidchar;
					uuidpart += uuidchar;
				}
				return uuidpart;
		 	}).join('-');
	},
	
	selectAll: function(){
		var all_check = $('all_check');
		var itemList = $('staffForm').getInputs("checkbox", "item_idx");
		
		itemList.each(function(item){
			item.checked = all_check.checked;
		});
	},
	
	goList: function() {
		var staffForm = $('staffForm');
		staffForm.action= "/user/bottom/agristaff/viewAgristaffInfo.aff";
		staffForm.submit();	
	},
	
	cancelAction: function() {
		if(opener != null && opener.bbsMgr != null && opener.bbsMgr.goViewagri){
			opener.bbsMgr.goViewagri();
			self.close();
		}
	},
	
	goViewagri: function() {
		window.location.href= "/user/bottom/agristaff/viewAgristaffInfo.aff";
	}
};