﻿function ej_refillDay(obj,yearMonth, idx)
{
    var year_month = yearMonth.split("-");
    var cur_date = new Date(Number(year_month[0]), Number(year_month[1]) -1, 1);
    var lastday = new Date(Number(year_month[0]), Number(year_month[1]), 0);
    
    for(var i =0;i<lastday.getDate();i++)
    {
        cur_date.setDate(i+1);
        if(obj.options.length <= i){
            obj.options[i] = new Option((i+1)+"일 ("+getDayName(cur_date.getDay())+")",(i+1));
        }else{
            if(i+1 < 10)
                obj.options[i].text = "0" + (i+1) + "일 (" + getDayName(cur_date.getDay()) + ")";
            else
                obj.options[i].text = (i+1) + "일 (" + getDayName(cur_date.getDay()) + ")";
        }
    }
    
    if(obj.options.length > lastday.getDate()){
        for(var i = obj.options.length - 1;i>= lastday.getDate();i--){
            obj.options.remove(i);
        }
    }
    
    obj.reInitializeSelectBox();
    
    if(idx == 1){
        document.getElementById('AvailabilitySearchInputSelectView_DropDownListMarketMonth2').value = yearMonth;
    }
}

function CheckDaysOfMonth(day, month, year)
{
	daysInMonth = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	daysInFeb	= (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );

	if ((month==2 && day>daysInFeb) || day > daysInMonth[month-1])
	{
		return false;
	}

	return true;
}

function ej_dateCheck()
{
    var dept_month_ctrl = document.getElementById('AvailabilitySearchInputSelectView_DropDownListMarketMonth1');
    var dept_day_ctrl = document.getElementById('AvailabilitySearchInputSelectView_DropDownListMarketDay1');
    var arrv_month_ctrl = document.getElementById('AvailabilitySearchInputSelectView_DropDownListMarketMonth2');
    var arrv_day_ctrl = document.getElementById('AvailabilitySearchInputSelectView_DropDownListMarketDay2');
    var roundTrip_ctrl = document.getElementById('AvailabilitySearchInputSelectView_RoundTrip');
    
    var dept_months = dept_month_ctrl.value.split("-");
    var dept_day = dept_day_ctrl.value;
    var arrv_months = arrv_month_ctrl.value.split("-");
    var arrv_day = arrv_day_ctrl.value;
    
    if(!CheckDaysOfMonth(parseInt(dept_day), parseInt(dept_months[1]), parseInt(dept_months[0])))
    {
        alert("유효한 날짜가 아닙니다.\n날짜를 다시 선택해 주세요.");
        dept_day_ctrl.focus();
        return false;
    }
    if(roundTrip_ctrl.checked  && !CheckDaysOfMonth(parseInt(arrv_day), parseInt(arrv_months[1]), parseInt(arrv_months[0])))
    {
        alert("유효한 날짜가 아닙니다.\n날짜를 다시 선택해 주세요.");
        arrv_day_ctrl.focus();
        return false;
    }    
    
    var today = new Date();
    var dept_date = new Date(Number(dept_months[0]), Number(dept_months[1])-1 ,Number(dept_day),23,59,59,999);
    var arrv_date = new Date(Number(arrv_months[0]), Number(arrv_months[1])-1 ,Number(arrv_day),23,59,59,999);
    
    if(today.getTime() > dept_date.getTime())
    {
        alert("이미 지나간 날짜 입니다.\n날짜를 다시 선택해 주세요.");
        dept_day_ctrl.focus();
        return false;
    }
    if(roundTrip_ctrl.checked  && today.getTime() > arrv_date.getTime())
    {
        alert("이미 지나간 날짜 입니다.\n날짜를 다시 선택해 주세요.");
        arrv_day_ctrl.focus();
        return false;
    }
    
    if(roundTrip_ctrl.checked  && dept_date.getTime() > arrv_date.getTime())
    {
        alert("오는날이 가는날보다 이전입니다.\n날짜를 다시 선택해 주세요.");
        arrv_day_ctrl.focus();
        return false;
    }
    
   return true;
}

function ej_setLowPriceSearch()
{
    var _objt = document.getElementById('AvailabilitySearchInputSelectView_DropDownListSearchBy');

    _objt.value = "calanderView";

    if(ej_validate()) __doPostBack('AvailabilitySearchInputSelectView$LinkButtonNewSearch','');
}

function ej_validate()
{
    var origin = document.getElementById('AvailabilitySearchInputSelectView_DropDownListMarketOrigin1');
    var dest = document.getElementById('AvailabilitySearchInputSelectView_DropDownListMarketDestination1');
    var adult = document.getElementById('AvailabilitySearchInputSelectView_DropDownListPassengerType_ADT');
    var child = document.getElementById('AvailabilitySearchInputSelectView_DropDownListPassengerType_CHD');
    var infant = document.getElementById('AvailabilitySearchInputSelectView_DropDownListPassengerType_INFANT');
    
    if(origin.selectedIndex == 0)
    {
        alert("출발지를 선택해 주세요.");
        origin.focus();
        return false;
    }
    if(dest.selectedIndex == 0)
    {
        alert("도착지를 선택해주세요.");
        dest.focus();
        return false;
    }
    
    if(adult.value == "0" && child.value == "0" && infant.value == "0")
    {
        alert("탑승인원을 선택하세요.");
        adult.focus();
        return false;
    }    
    else if(adult.value == "0" && (child.value != "0" || infant.value != "0"))
    {
        alert("성인동반 없이 유아,소아는 예매 할 수 없습니다.");
        adult.focus();
        return false;
    }
    else if(parseInt(adult.value) + parseInt(child.value) > 9)
    {
        alert("10석이상의 좌석예매는 콜센터로 문의 해주세요.");
        return false;
    }
    else if(parseInt(infant.value) > parseInt(adult.value))
    {
        alert("유아의 수가 동반 성인의 수보다 많을수 없습니다.");
        infant.focus();
        return false;
    }    
    
    if(!ej_dateCheck()) return false;
    
    alert("인터넷 항공권 구입시에는 예약과 동시에 구매를 하셔야합니다.\n구매하지 않으시면 예약은 자동으로 취소됩니다.");
    
    return true;
}

function getDayName(day_no)
{
    switch(day_no){
        case 0: return "일";
        case 1: return "월";
        case 2: return "화";
        case 3: return "수";
        case 4: return "목";
        case 5: return "금";
        case 6: return "토";
    }
}