// *-------------------------------------------------------------------
// * 프로젝트       : www.i-mom.co.kr
// * 프로그램명  : /static_root/js/validators.js
// * 설명      	: 입력된 폼 값의 유효성을 검사하기 위한 자바스크립트 함수 모음
// * Version  : 1.0
// * 작성일자       : 2009/07/23    작성자 : 조성익
// * 수정일자       :
// * 수정내용       :
//--------------------------------------------------------------------*/
// 사용 함수 내역
// 01. 공백입력 체크(전부다) 		: check_space(오브젝트)
// 02. 문자 길이, 허용문자 체크 	: check_str(허용문자, 길이, 오브젝트)
// 03. 주민번호 체크 함수			: check_jumin(주민번호앞자리 오브젝트,주민번호뒷자리 오브젝트)
// 04. 날짜 유효성 체크 			: check_date(스트링-YYYYMMDD)
// 05. RIGHT				: right(스트링,숫자)
// 06. 이메일 체크 				: check_email(스트링)
// 07. 다음으로 자동이동			: move_next(오브젝트, 포커스이동 오브젝트, 입력길이)
// 08. 댓글 글자수 200자 제한             : textarea_length_chk200 (오브젝트)
////////////////////////
// 01.공백입력 체크(전부다)//
////////////////////////
var url_join 	= "https://nid.naver.com/ajax/ajax_passwd_join.nhn?pw=";
var url_login 	= "https://nid.naver.com/ajax/ajax_passwd_login.nhn?pw=";
var url_nologin	= "https://nid.naver.com/ajax/ajax_passwd_nologin.nhn?pw=";

function check_space(str) {

	if (str.value.length=="0") {
	return true;
	}
	for (var i=0; i<str.value.length; i++) {
	var ch=str.value.charAt(i);
		if (ch==" " || ch=="\t") {
		return true;
		}
	}
	return false;
}


//////////////////////////////////////////
//02.문자 체크 (okstr이 허용문자, str이 비교문자)//
//////////////////////////////////////////
function check_str(okstr,oklen,str) {
	if (str.value.length=="0") {
	return true;
	}

	if (str.value.length < oklen) {
	return true;
	}

	if (okstr!="") {
		for (i=0;i<str.value.length;i++) {
			if(str.value == " " || okstr.indexOf(str.value.charAt(i))< 0) {
			return true;
			}
		}
	}
	return false;
}


/////////////////////
//03. 주민번호 체크 함수//
////////////////////
function check_jumin(Res1,Res2) {
	var jumin;
	var check=0;
	jumin = Res1.value + Res2.value;

	// 생년월일로 검색

	check = Res1.value.substr(2,2);
	check2 = Res1.value.substr(4,2);


	if(check<0 || check>12 || check2<0 || check2>31) {
	return true;
	}
	// 생년월일 검색 끝

	// 주민번호 검색식
	var IDtot = 0;
	var IDAdd = "234567892345";
	for(i=0;i<12;i++) {
	IDtot=IDtot+(jumin.substr(i,1) * IDAdd.substr(i,1));
	}

	IDtot = 11 - (IDtot%11);

	if(IDtot == 10) {
	IDtot=0;
	} else if(IDtot == 11) {
	IDtot=1;
	}


	if(Res2.value.substr(6,1) != IDtot) {
	return true
	}

return false;
}

// 내국인 주민번호 인증 수행
function check_jumin_str(Res1,Res2) {
	var jumin;
	var check=0;
	jumin = Res1 + Res2;
	
	if(!Res1) return false;
	if(!Res2) return false;

	// 생년월일로 검색

	check = Res1.substr(2,2);
	check2 = Res1.substr(4,2);


	if(check<0 || check>12 || check2<0 || check2>31) {
	return true;
	}
	// 생년월일 검색 끝

	// 주민번호 검색식
	var IDtot = 0;
	var IDAdd = "234567892345";
	for(i=0;i<12;i++) {
	IDtot=IDtot+(jumin.substr(i,1) * IDAdd.substr(i,1));
	}

	IDtot = 11 - (IDtot%11);

	if(IDtot == 10) {
	IDtot=0;
	} else if(IDtot == 11) {
	IDtot=1;
	}


	if(Res2.substr(6,1) != IDtot) {
	return fgn_no_chksum(Res1 + Res2);    // 내국인 인증 실패 시 외국인 인증 수행
	}

return false;
}

/* 외국인 주민 번호 체크 */
function fgn_no_chksum(reg_no) {
    var sum = 0;
    var odd = 0;
    
    buf = new Array(13);
    for (i = 0; i < 13; i++) buf[i] = parseInt(reg_no.charAt(i));

    odd = buf[7]*10 + buf[8];
    
    if (odd%2 != 0) {
      return true;
    }

    if ((buf[11] != 6)&&(buf[11] != 7)&&(buf[11] != 8)&&(buf[11] != 9)) {
      return true;
    }
    	
    multipliers = [2,3,4,5,6,7,8,9,2,3,4,5];
    for (i = 0, sum = 0; i < 12; i++) sum += (buf[i] *= multipliers[i]);


    sum=11-(sum%11);
    
    if (sum>=10) sum-=10;

    sum += 2;

    if (sum>=10) sum-=10;

    if ( sum != buf[12]) {
        return true;
    }
    else {
        return false;
    }
}

/////////////////////
//04. 날짜 유효성 체크 //
////////////////////
function check_date(iDate) {
	return isValidDay(iDate.substring(0, 4),iDate.substring(4, 6) ,iDate.substring(6, 8) );	
}

function isValidMonth(mm) {
    var m = parseInt(mm,10);
    return (m >= 1 && m <= 12);
}


function isValidDay(yyyy, mm, dd) {
    var m = parseInt(mm,10) - 1;
    var d = parseInt(dd,10);

    var end = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    if ((yyyy % 4 == 0 && yyyy % 100 != 0) || yyyy % 400 == 0) {
        end[1] = 29;
    }

    return (d >= 1 && d <= end[m]);
}

function isValidTime(time) {
    var year  = time.substring(0,4);
    var month = time.substring(4,6);
    var day   = time.substring(6,8);

    if (parseInt(year,10) >= 1900  && isValidMonth(month) &&
        isValidDay(year,month,day) ) {
        return true;
    }
    return false;
}

function checkpwd_join(pwd_id,id_id){
	focuswhere=0;
	if( document.getElementById(pwd_id).value.length <= 0 ) {
		//showhelpmsg(-1);
	} else if (document.getElementById(pwd_id).value.length > 16) {
		//sendLevel(10004);
	} else if (document.getElementById(pwd_id).value.length < 6) {
		//sendLevel(10002);
	} else {
		getAjaxResult(url_join + encodeURIComponent(document.getElementById(pwd_id).value)+"&id="+document.getElementById(id_id).value);
	}
}

function getAjaxResult(urls){
	try {
		var xmlhttp = getXmlHttp();
		xmlhttp.open("GET", urls);
		xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			pwdstr = xmlhttp.responseText
      if(pwdstr < 20000)
        pwdstat = false;
      else
        pwdstat = true;
			//sendLevel(pwdstr);
		}}
		xmlhttp.send(null);
	} catch(e) {
		//sendLevel(20000);
		if (window.bridgeGotTime) {
			throw e;
		} else {
			//page reload?
		}
	}
}

//////////////////
//05. right 함수 //
//////////////////
function right (a,b) {
	return a.substring(a.length-b,a.length);
}


//////////////////
//06. 이메일 체크 //
//////////////////
function check_email(str) {
	if (str.length=="0") {
		return true;
	}
	for (var i=0; i<str.length; i++) {
		var ch=str.charAt(i	)
		if (ch==" " || ch=="\t") {
		return true;
		}
		if(str.indexOf("@") != 1 && str.indexOf(".") < 1) {
		return true;
		}
	}
return false;
}

function check_email2(str){
	var arrMatch = str.match(/^(\".*\"|[A-Za-z0-9_-]([A-Za-z0-9_-]|[\+\.])*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z0-9][A-Za-z0-9_-]*(\.[A-Za-z0-9][A-Za-z0-9_-]*)+)$/); 
	if (arrMatch == null) {
        return true; 
	} 
	return false; 
}

//////////////////////////
//07. 다음으로 넘어가게 //
/////////////////////////
function move_next(arg,nextname,len) {
	var flag="true";
		if (arg.value.length==len) {
			flag="false";


		if (flag=="false") {
			nextname.focus() ;
			flag=="true"
			return;
		}
	}
}

/////////////////////
//08. 글자수 200자 제한 //
/////////////////////
function textarea_length_chk200(textAreaObj){ 
	length_chk(textAreaObj, 200);
}

function textarea_length_chk100(textAreaObj){  
	length_chk(textAreaObj, 100);
}

function textarea_length_chk50(textAreaObj){ 
	length_chk(textAreaObj, 50);
}

function length_chk(textAreaObj, cnt){ 
	if( textAreaObj == undefined){
		return;
	}
	if(textAreaObj.value.length > cnt ){
		alert(cnt + "자 까지 입력 가능합니다.");
		textAreaObj.value = textAreaObj.value.substring(0,(cnt - 1));
		textAreaObj.focus();
		return false;
	}
	return true;
}


// 숫자, 엔터, 탭 키만 입력가능
function numOnly(obj){
	var val = obj.value;

	re = /[^0-9]/gi;
	obj.value = "";
	obj.value = val.replace(re, "");
	
}

function onlyNum2() {
	alert(event.returnVar);
	if ( (event.keyCode<48) || (event.KeyCode>57) )
	{
		event.returnValue = "";
	}
}

// 날짜 수치 제한 처리 
function dayNumChk(dayNum, num)
{
	if(dayNum && num){
		var dayChkNum = parseInt(dayNum.value);
		num = parseInt(num);
		if(dayChkNum > num || dayNum.value == "00")
		{
			alert("날짜 형식이 올바르지 않습니다. ")
			dayNum.value = "";
			dayNum.focus();
		}
	}
}

//blur 시  마지막 날짜 체크
function chkDate(obj, num)
{
	if( obj.value == "0" ){
		alert("날짜 형식이 올바르지 않습니다.");
		obj.value = "";
		obj.focus();
	}
}

function yearMin(obj){
	if( obj.value != "" ){
		if(Number(obj.value) < 1980 ){
			alert("출생년도가 너무 이릅니다.");
			obj.value = "";
			obj.focus();
			return;
		}
	}
}

