/*******************************************************************************/
/* 작성자 : 정현수
/* 작성일 : 2011-04-04
/* 작성목적 : 폼데이터 유효성 판단 자바스크립트
/* 사용법 : var checkObj = new HsDataValidator(); <-- 객체를 생성한후
/*           checkobj.checkData(obj, title, etcAttu); 폼 데이터 필수입력 체크
/*           checkobj.chkSSN(obj1, obj1); 주민번호 체크
/*           checkobj.chkLengthPlus(obj, title, limitCnt); 데이터 길이 체크 (이상)
/*           checkobj.chkLengthDis(obj, title, limitCnt); 데이터 길이 체크 (이하)
/*           checkobj.chkEmail(email); 이메일 체크
/*******************************************************************************/
		HsDataValidator = function(){
			var objAttu = '';
			$thisObj = null;
			
			//폼데이트 체크
			this.checkData = function(obj, title, etcAttu){
				$thisObj = $('#'+obj);

				if(etcAttu != ''){
					objAttu = etcAttu;
				}else{
					objAttu =  getObjAttu();
				}

				var rtn = true;
				rtn = checkFormData(objAttu, title, obj);
				return rtn;
			}
			
			//주민번호 체크
			this.chkSSN = function(obj1, obj2){
				var count =0;
				for (var i = 0; i <=5 ; i++) {
					count = count + ((i%8+2) * parseInt($("input[id*='" +obj1+ "']").val().substring(i,i+1)));
				}
				
				for (var i = 6; i <=11 ; i++) {
					count = count + ((i%8+2) * parseInt($("input[id*='" +obj2+ "']").val().substring(i-6,i-5)));
				}
				
				count = 11 - (count %11);
				count = count % 10;
				
				if (count != $("input[id*='" +obj2+ "']").val().substring(6,7)) {
					alert('잘못된 주민번호 입니다.');
					return false;
				}else{
					return true;
				}
			}

			//데이터 길이 체크 (이상)
			this.chkLengthPlus = function(obj, title, limitCnt){
				$thisObj = $('#'+obj);
				var objVal = $thisObj.val();

				if(objVal.length < limitCnt){
					alert(title + '를 '+limitCnt+' 이상으로 입력하여 주시기 바랍니다.');
					$thisObj.focus();
					return false;
				}else{
					return true;
				}
			}

			//데이터 길이 체크 (이하)
			this.chkLengthDis = function(obj, title, limitCnt){
				$thisObj = $('#'+obj);
				var objVal = $thisObj.val();

				if(objVal.length > limitCnt){
					alert(title + '를 '+limitCnt+' 이하로 입력하여 주시기 바랍니다.');
					$thisObj.focus();
					return false;
				}else{
					return true;
				}
			}
			
			//이메일 체크
			this.chkEmail = function(email){
				var regExp = /[a-z0-9]{2,}@[a-z0-9-]{2,}\.[a-z0-9]{2,}/i;
				
				if(!regExp.test(email)){
					alert('잘못된 e-mail 형식입니다.');
					return false;
				}else{
					return true;
				}
			}


			//데이터 값 비교
			this.chkDataCompare = function(obj, comData, message){
				$thisObj = $('#'+obj);
				var thisVal = $thisObj.val();

				if(thisVal != comData){
					alert(message);
					return false;
				}else{
					return true;
				}
			}


			//각 엘리멘트의 속성 알아내기
			function getObjAttu(){
				var rtn = '';

				if($thisObj.is(':text') == true || $thisObj.is(':password') == true || $thisObj.is(':file') == true){
					rtn = 'text';
				}else{
					if($thisObj.is(':radio') == true  || $thisObj.is(':checkbox') == true){
						rtn = 'checked';
					}
				}

				return rtn;
			}
			
			//폼데이터 유효성 체크
			function checkFormData(attu, title, obj){
				var rtn = true;

				switch(attu){

					case 'text' : 
						if($thisObj.val() == ''){
							alert(title + '(을)를 입력하여 주십시오.');
							$thisObj.focus();
							rtn = false;
						}
						break;

					case 'checked' :
						var cnt = 0;

						$("input[id*='" +obj+ "']").each(function(){
							if($(this).attr('checked') == true){
								cnt++;
							}
						});

						if(cnt == 0){
							alert(title + '(을)를 선택하여 주십시오.');
							rtn = false;
						}

						break;

					case 'select' : 
						if(!$('#'+obj+' > option:selected').val()) {
							alert(title + '(을)를 선택하여 주십시오.');
							rtn = false;
						}
						break;

					case 'textarea' : 
						if($thisObj.val() == ''){
							alert(title + '(을)를 입력하여 주십시오.');
							$thisObj.focus();
							rtn = false;
						}
						break;

					case 'hidden' : 
						if($thisObj.val() == ''){
							alert(title + '(을)를 입력하여 주십시오.');
							$thisObj.focus();
							rtn = false;
						}
						break;
				}

				return rtn;

			}

		}
