﻿// JScript 파일


//****************공백이나 빈문자열인지 검사***************
function IsEmpty(obj)
{
	var str = "";
	if(typeof(obj) == "object") str = obj.value;
	else str = obj;
	str = str.replace(/^\s*/g,"").replace(/\s*$/g,"");
	
	if(str == '' || str == null)
	{
		return true;
	}
	else
	{
		return false;
	}
}


//영문과 숫자를 동시에 사용하는지 체크 합니다.
function fn_UseNumericAndEnglish(str)
{
	var sText = str.toUpperCase();
	var ValidCharsEng = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var ValidCharsNum = "0123456789";
	var IsEnglish = false;
	var IsNumeric = false;
	var IsHangul = false;
	var Char;
	var strLen = 0;
	strLen = sText.length;
	//한글체크
	for (var i = 0; i < parseInt(strLen); i++) 
	{ 
      Char = sText.charAt(i); 
      if (CheckHangul(Char) == true)
      {
		IsHangul = true;
		break;
      }
    }
 
	if (IsHangul == false)
	{
		for (i = 0; i < sText.length; i++) 
		{ 
		  Char = sText.charAt(i); 
	      
		  if (ValidCharsEng.indexOf(Char) > -1) 
		  {
			 IsEnglish = true;
			 break;
		  }
		}
			
		if (IsEnglish == true)
		{
			//숫자가 존재하는지 여부 체크
			for (i = 0; i < sText.length; i++) 
			{ 
			  Char = sText.charAt(i); 
			  if (ValidCharsNum.indexOf(Char) > -1) 
			  {
				 IsNumeric = true;
				 break;
			  }
			}
		}
	}
	
	if (IsHangul == true || IsEnglish == false || IsNumeric == false)
	{
		return false;
	}
	
	return true;
}

function CheckHangul(str)
{ 
    strarr = new Array(str.length); 
    schar = new Array('/','.','>','<',',','?','}','{',' ','\\','|','(',')','+','='); 
    flag = true; 
    for (i=0; i<str.length; i++)
    { 
        for (j=0; j<schar.length; j++)
        { 
            if (schar[j] ==str.charAt(i))
            { 
                flag = false; 
                break;
            } 
        } 
        strarr[i] = str.charAt(i);
        if ((strarr[i] >=0) && (strarr[i] <=9))
        { 
            flag = false; 
            break;
        }
        else if ((strarr[i] >='a') && (strarr[i] <='z'))
        { 
            flag = false; 
            break;
        }
        else if ((strarr[i] >='A') && (strarr[i] <='Z'))
        { 
            flag = false; 
            break;
        }
        else if ((escape(strarr[i]) > '%60') && (escape(strarr[i]) <'%80') )
        { 
            flag = false; 
            break;
        } 
    } 
    return flag;
}


/**************유효한 날짜인지****************/
function IsValidDate(strDate)
{
	return IsValidDateTime(strDate,'.');	
}

//sepChar는 날짜 구분자.
function IsValidDateTime(strDate,sepChar)
{
	//내용이 없을때는 참, 필수입력사항이면 Required사용.
	if(IsEmpty(strDate))
		return true;
	
	if(!(strDate.length == 10 || strDate.length == 8))
		return false;
	
	var _yyyy;
	var _mm;
	var _dd;
	sepChar = '.';
	
	if(strDate.length == 10)
	{
		_yyyy = strDate.split(sepChar)[0];
		_mm = strDate.split(sepChar)[1];
		_dd = strDate.split(sepChar)[2];
	}
	else
	{
		_yyyy = strDate.substr(0,4);
		_mm = strDate.substr(4,2);
		_dd = strDate.substr(6,2);				
	}
	
	//유효한(존재하는) 년도(年)인지 체크
	//1753년 1월 1일부터 9999년 12월 31일의 범위에 있는 날짜를 저장하려면 datetime을 사용하십시오.
	if(isNaN(_yyyy))
		return false;

	var y = parseInt(_yyyy,10);
	if (y < 1753 || y > 9999)
		return false;

	//유효한 월(月)인지 체크
	if(isNaN(_mm))
		return false;

	var m = parseInt(_mm,10);

	if(! (m >= 1 && m <= 12))
		return false;

	//유효한(존재하는) 일(日)인지 체크
	var d = parseInt(_dd,10);
	var end = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
		end[1] = 29;	// 윤년 체크
	if(! (d >= 1 && d <= end[m-1]))
		return false;
	return true;
}

function GetDate(sDT)
{
    var rtnDate;
    var year;
    var month;
    var day;
	
    //여러가지 format에 대한것이 필요하면 else if추가.
    if(typeof(sDT) == "string" && sDT.length == 10 && sDT.charAt(4) == "." && sDT.charAt(7) == ".")
    {
	    var arrDT = sDT.split(".");    		
	    if(arrDT.length != 3 || isNaN(arrDT[0]) == true || isNaN(arrDT[1]) == true || isNaN(arrDT[2]) == true)
		    return null;    			
	    year = arrDT[0];
	    month = arrDT[1];
	    day = arrDT[2];
    }	
    else if(typeof(sDT) == "string" && sDT.length == 10 && sDT.charAt(4) == "-" && sDT.charAt(7) == "-")
    {
	    var arrDT = sDT.split("-");    		
	    if(arrDT.length != 3 || isNaN(arrDT[0]) == true || isNaN(arrDT[1]) == true || isNaN(arrDT[2]) == true)
		    return null;    			
	    year = arrDT[0];
	    month = arrDT[1];
	    day = arrDT[2];
    }
    else if(typeof(sDT) == "string" && sDT.length == 10 && sDT.charAt(4) == "/" && sDT.charAt(7) == "/")
    {
	    var arrDT = sDT.split("/");    		
	    if(arrDT.length != 3 || isNaN(arrDT[0]) == true || isNaN(arrDT[1]) == true || isNaN(arrDT[2]) == true)
		    return null;    			
	    year = arrDT[0];
	    month = arrDT[1];
	    day = arrDT[2];
    }
    else if(typeof(sDT) == "string" && sDT.length == 8)
    {		
	    year = sDT.substring(0,4);
	    month = sDT.substring(4,6);
	    day = sDT.substring(6,8);
    }	
    else
    {
	    return null;
    }
	
    if(CheckDate(year, month, day) > 0)
	    return null;
		
    rtnDate = new Date(year, month-1, day);
	
    return rtnDate;
}

function GetDateText(sDT) {
	var rtn;
    var year;
    var month;
    var day;
    //여러가지 format에 대한것이 필요하면 else if추가.
    if(typeof(sDT) == "string" && sDT.length == 10 && sDT.charAt(4) == "." && sDT.charAt(7) == ".")
    {
	    var arrDT = sDT.split(".");    		
	    if(arrDT.length != 3 || isNaN(arrDT[0]) == true || isNaN(arrDT[1]) == true || isNaN(arrDT[2]) == true)
		    return null;    			
	    year = arrDT[0];
	    month = arrDT[1];
	    day = arrDT[2];
    }	
    else if(typeof(sDT) == "string" && sDT.length == 10 && sDT.charAt(4) == "-" && sDT.charAt(7) == "-")
    {
	    var arrDT = sDT.split("-");    		
	    if(arrDT.length != 3 || isNaN(arrDT[0]) == true || isNaN(arrDT[1]) == true || isNaN(arrDT[2]) == true)
		    return null;    			
	    year = arrDT[0];
	    month = arrDT[1];
	    day = arrDT[2];
    }
    else if(typeof(sDT) == "string" && sDT.length == 10 && sDT.charAt(4) == "/" && sDT.charAt(7) == "/")
    {
	    var arrDT = sDT.split("/");    		
	    if(arrDT.length != 3 || isNaN(arrDT[0]) == true || isNaN(arrDT[1]) == true || isNaN(arrDT[2]) == true)
		    return null;    			
	    year = arrDT[0];
	    month = arrDT[1];
	    day = arrDT[2];
    }
    else if(typeof(sDT) == "string" && sDT.length == 8)
    {		
	    year = sDT.substring(0,4);
	    month = sDT.substring(4,6);
	    day = sDT.substring(6,8);
    }	
    else
    {
	    return null;
    }
    return year + "년" + month + "월" + day + "일";
}

function CheckDate(year, month, day) 
{
	var dat_option = year % 4;
	year = parseInt(year, 10);
	month = parseInt(month, 10);
	day = parseInt(day, 10);
	
	if (isNaN(year) == true) { return 1; }
	if (year < 1970) { return 1; }
	
	if (isNaN(month) == true) { return 2; }
	if (isNaN(day) == true) { return 3; }
	if (day < 1 || day > 31) { return 3; }
	
	if (month == 2) {
		if (((dat_option == 0) && (day > 29)) || ((dat_option != 0) && (day > 28)) ) { return 3; }
	} else if ((month == 4) ||
				(month == 6) ||
				(month == 9) ||
				(month == 11)) {
		if (day > 30) { return 3; }
	} else if ((month == 1) ||
				(month == 3) ||
				(month == 5) ||
				(month == 7) ||
				(month == 8) ||
				(month == 10) ||
				(month == 12)) {
		if (day > 31) { return 3; }			
	} else {
		return 2;
	}
	
	return 0;
}

//	기준일(basic_day) 시점의 만 나이를 확인하여 미만이면 true입니다.
function isValidMaxAge( birth_day, max_age, basic_day) {
	if(birth_day == null || birth_day == "") return true;
    var sdt = GetDate(birth_day);
    var edt = GetDate(basic_day);
    if( sdt == null || edt == null ) return true;
    var by  = edt.getFullYear() - sdt.getFullYear();
    if( by < max_age ) return true;
    else if( by == max_age ) {
		if( edt.getMonth() < sdt.getMonth() ) return true;
		else if( edt.getMonth() == sdt.getMonth() ) {
			return edt.getDate() < sdt.getDate();
		}
	}
	return false;
}

// 소아의 경우 기준일(basic_day) 시점의 만 나이를 확인(12세 미만)이면 true입니다.
function isValidChildMaxAge(birth_day, max_age, min_age, basic_day) {
	if(birth_day == null || birth_day == "") return true;
    
    year1 = parseInt(birth_day.substring(0,4), 10);
    year2 = parseInt(basic_day.substring(0,4), 10);
    month1 = parseInt(birth_day.substring(4,6), 10);
    month2 = parseInt(basic_day.substring(4,6), 10);
    day1 = parseInt(birth_day.substring(6,8), 10);
    day2 = parseInt(basic_day.substring(6,8), 10);
    
    AfterDate = jsAddYear(birth_day, 12);
    
    date1 = new Date(year1, month1-1, day1);
    date2 = new Date(year2, month2-1, day2);
    
    if ((year2 - year1) * 12 + month2 - month1 == 0) { // 년도 월이 같을때
        if ((day2 - day1) >= min_age)
            return true;    
        else
            return false;
    }
    else { // 년도, 월 다를때
         if (basic_day <= AfterDate && (date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24 >= min_age) 
            return true; 
         else
            return false;
    }
       
	return false;
}

// 유아의 경우 기준일(basic_day) 시점의 만 나이를 확인(14일이상, 2세 미만)하여 미만이면 true입니다.
function isValidInfantMaxAge(birth_day, max_age, min_age, basic_day) {
	if(birth_day == null || birth_day == "") return true;
    
    year1 = parseInt(birth_day.substring(0,4), 10);
    year2 = parseInt(basic_day.substring(0,4), 10);
    month1 = parseInt(birth_day.substring(4,6), 10);
    month2 = parseInt(basic_day.substring(4,6), 10);
    day1 = parseInt(birth_day.substring(6,8), 10);
    day2 = parseInt(basic_day.substring(6,8), 10);

    AfterDate = jsAddYear(birth_day, 2);
    
    date1 = new Date(year1, month1-1, day1);
    date2 = new Date(year2, month2-1, day2);
    
    if ((year2 - year1) * 12 + month2 - month1 == 0) {  // 년도, 월 같을때
        if (day2 - day1 >= min_age)
            return true;
        else
            return false;
    }        
    else { // 년도, 월 다를때
        if (basic_day <= AfterDate && (date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24 >= min_age) 
            return true; 
        else
            return false;
    }    
	return false;
}

/*--------------------------------------------------
  기능   : 날짜에 년수를 더한다.
  INPUT  : startDt(YYYYMMDD), year
  RETURN : rtnValue : 날짜에 년수를 더한 날짜
           -1       : ERROR..!
           예) 20000110 + 1년 = 20010110
           예) 20000229 + 1년 = 20010228
     MSG :
----------------------------------------------------*/
function jsAddYear( startDt, plusYear ) {
 var rtnValue = -1 ;

 // input date의 날짜 체크
 if ( !IsValidDate(startDt)) {
  rtnValue = -1 ;
  return rtnValue ;
 }

 var yyyy = startDt.substring(0,4) +"" ;
 var mm   = startDt.substring(4,6) +"" ;
 var dd   = startDt.substring(6,8) +"" ;

 var newYyyy = (eval(yyyy) + eval(plusYear)) ;

 // 윤달(29일) 인 경우 28일로 고침
 // 예) 20000229 에 1년을 더하면 20000228
 var isYoonYear = false ;
 // 4 로 나누어 떨어지면 윤년
 // 100 으로 나누어 떨어지면 윤년 아님
 // 400 으로 나누어 떨어지면 윤년
 if ( (eval(newYyyy)%4) == 0 ) isYoonYear = true ;
 if ( (eval(newYyyy)%100) == 0 ) isYoonYear = false ;
 if ( (eval(newYyyy)%400) == 0 ) isYoonYear = true ;

 if ( (mm == '02') && (dd == '29') && !isYoonYear ) dd = '28' ;

 rtnValue = newYyyy + mm + dd ;

 return rtnValue ;
}

// 왕복의 경우 (귀국편 출발 YYYYMMDDHHMM) - (출국편 도착 YYYYMMDDHHMM) > 3) 3시간 이상 이어야 true입니다.  
function isValidFlight(dep_yyyymmddhhmm, arr_yyyymmddhhmm) {
    dep_year = parseInt(dep_yyyymmddhhmm.substring(0,4), 10);
    dep_month = parseInt(dep_yyyymmddhhmm.substring(4,6), 10);    
    dep_day = parseInt(dep_yyyymmddhhmm.substring(6,8), 10);
    dep_hh = parseInt(dep_yyyymmddhhmm.substring(8,10), 10);    
    dep_mm = parseInt(dep_yyyymmddhhmm.substring(10,12), 10); 
        
    arr_year = parseInt(arr_yyyymmddhhmm.substring(0,4), 10);
    arr_month = parseInt(arr_yyyymmddhhmm.substring(4,6), 10);
    arr_day = parseInt(arr_yyyymmddhhmm.substring(6,8), 10);
    arr_hh = parseInt(arr_yyyymmddhhmm.substring(8,10), 10);    
    arr_mm = parseInt(arr_yyyymmddhhmm.substring(10,12), 10); 

    dep_date = new Date(dep_year, dep_month-1, dep_day, dep_hh, dep_mm);
    arr_date = new Date(arr_year, arr_month-1, arr_day, arr_hh, arr_mm);
  
    var diff = Math.floor((arr_date-dep_date)/1000);
    
    var ss = padZero(diff%60);
    var mi = padZero(Math.floor(diff/60)%60);
    var hh = padZero(Math.floor(Math.floor(diff/60)/60));

    // 3시간 이상
    if (hh >= 3)
      return true;
    else
      return false;
}

function padZero(s) {
  return (""+s).length<2 ? "0"+s:s;
}

//	과거날짜면 true, 미래날짜면 fasle. 오늘 날짜도 과거임.
function IsValidPast( compare_date, today ) {
	var sdate = GetDate( compare_date );
	if( today == null )
		today = new Date();		

	if (sdate < today)
	  return true;
	else
	  return false;	
}

