﻿var target; 
var target_no;
var temp; 
var BY; 
var BM; 
var BD; 
var today_year; 
var today_month; 
var today_day; 
var callfunction;

function showCalendar(ymd, ifrm) 
{     
    if (ifrm == null)
        var ifrm = "";
    
    var tmp = ymd.split("-"); 
    var y = parseInt(tmp[0],10); 
    var m = parseInt(tmp[1],10); 
    var d = parseInt(tmp[2],10); 
    
    var _strPre = "<p class='arrow'><a href='javascript:showCalendar(\""+(m==1?(y-1)+"-"+12:y+"-"+(m-1))+"-"+d+"\",\"" + ifrm + "\")'><img src='http://image.bico.jp/site/100011/v2/img/common/btnL.gif' alt='이전 보기' /></a></p>";
    var _strNext = "<p class='arrow2'><a href='javascript:showCalendar(\""+(m==12?(y+1)+"-"+1:y+"-"+(m+1))+"-"+d+"\",\"" + ifrm + "\")'><img src='http://image.bico.jp/site/100011/v2/img/common/btnR.gif' alt='다음 보기' /></a></p>";
    
    // S : 공통으로 쓰는 부분
	var text = "<colgroup>";
	
	for (i=0; i < 7; i++)
	{
	    text += "<col width='*'/>";
	}
	
	text += "</colgroup>"; 
	text += "<thead>"; 
	text += "<tr>"; 
	text += "<th><img src='http://image.bico.jp/site/100011/v2/img/common/sun.gif' alt='sun' /></th>"; 
	text += "<th><img src='http://image.bico.jp/site/100011/v2/img/common/mon.gif' alt='mon' /></th>"; 
	text += "<th><img src='http://image.bico.jp/site/100011/v2/img/common/tue.gif' alt='tue' /></th>"; 
	text += "<th><img src='http://image.bico.jp/site/100011/v2/img/common/wed.gif' alt='wed' /></th>"; 
	text += "<th><img src='http://image.bico.jp/site/100011/v2/img/common/thu.gif' alt='thu' /></th>"; 
	text += "<th><img src='http://image.bico.jp/site/100011/v2/img/common/fri.gif' alt='fri' /></th>"; 
	text += "<th><img src='http://image.bico.jp/site/100011/v2/img/common/sat.gif' alt='sat' /></th>"; 
	text += "</tr>"; 
	text += "</thead>"; 
	// E : 공통으로 쓰는 부분
	
    
    var text1 = "<p class='year_month'>" + y + "년 <span>" + ((m < 10) ? ("0" + m) : m) + "월</span></p>";
		text1 += "<table>";
		
        text1 += text;
		
		text1 += "<tbody>"; 

    var d1 = (y+(y-y%4)/4-(y-y%100)/100+(y-y%400)/400+m*2+(m*5-m*5%9)/9-(m<3?y%4||y%100==0&&y%400?2:3:4))%7; 
  
    for (i = 0; i < 42; i++) 
    { 
        var _strDay = "";
        
//        if (i==0) 
//            text1 += "<tr>"; 
            
        if (i%7==0)
        { 
            text1 += "<tr>"; 
            _strDay = "<span class='sun'>" + (i+1-d1) + "</span>";
        }
        
        else if (i%7==6)
            _strDay = "<span class='sat'>" + (i+1-d1) + "</span>";
        
        else
            _strDay = i + 1 - d1;
        
        if((i+1-d1) == today_day && m==today_month && y==today_year) 
            text1 += "<td><a href='javascript:setDate(\""+y+"-"+ (m<10?"0"+m:m) +"-"+ ((i+1-d1)<10?("0"+(i+1-d1)):(i+1-d1)) +"\",\"" + ifrm + "\");nightchk(\"" + ifrm + "\",\"" + target_no + "\");'><span class='today'>" + _strDay + "</span></a></td>"; 
            
        else
        {
            if (i < d1 || i >= d1+(m*9-m*9%8)/8%2+(m==2?y%4||y%100==0&&y%400?28:29:30)) 
                text1 += "<td></td>";
                
            else 
                text1 += "<td><a href='javascript:setDate(\""+y+"-"+ (m<10?"0"+m:m) +"-"+ ((i+1-d1)<10?("0"+(i+1-d1)):(i+1-d1)) +"\",\"" + ifrm + "\");nightchk(\"" + ifrm + "\",\"" + target_no + "\");'>" + _strDay + "</a></td>"; 
        } 
    } 
    text1 += "</tr></tbody></table>"; 
    
        var m1 = m + 1;
        
    var text2 = "<p class='year_month'>" + (m==12?(y+1) + "년 <span>" + "0" + 1 : y + "년 <span>"+ ((m1<10) ? ("0" + m1) : m1)) + "월</span></p>";
	text2 += "<table>";
	
    text2 += text;
	
	text2 += "<tbody>"; 
	
    if(m1==13)
    {
        m1=1; 
        y=y+1;
    }
        
    var d2 = (y+(y-y%4)/4-(y-y%100)/100+(y-y%400)/400+m1*2+(m1*5-m1*5%9)/9-(m1<3?y%4||y%100==0&&y%400?2:3:4))%7; 
    
    for (i = 0; i < 42; i++) 
    {
        var _strDay = "";
       
//        if (i==0) 
//            text2 += "<tr>"; 
            
        if (i%7==0)
        { 
            text2 += "<tr>"; 
            _strDay = "<span class='sun'>" + (i+1-d2) + "</span>";
        }
        
        else if (i%7==6)
            _strDay = "<span class='sat'>" + (i+1-d2) + "</span>";
        
        else
            _strDay = i + 1 - d2;
            
        if((i+1-d2) == today_day && m1==today_month && y==today_year) 
            text2 += "<td><a href='javascript:setDate(\""+(y)+"-"+((m==12)?"0"+1:m1<10?"0"+m1:m1) +"-" +((i+1-d2)<10?("0"+(i+1-d2)):(i+1-d2))+ "\",\"" + ifrm + "\");nightchk(\"" + ifrm + "\",\"" + target_no + "\");'><span class='today'>" + _strDay + "</span></a></td>"; 
            
        else
        {
            if (i < d2 || i >= d2+(m1*9-m1*9%8)/8%2+(m1==2?y%4||y%100==0&&y%400?28:29:30)) 
                text2 += "<td></td>";
                
            else 
                text2 += "<td><a href='javascript:setDate(\""+(y)+"-"+((m==12)?"0"+1:m1<10?"0"+m1:m1) +"-" +((i+1-d2)<10?("0"+(i+1-d2)):(i+1-d2))+ "\",\"" + ifrm + "\");nightchk(\"" + ifrm + "\",\"" + target_no + "\");'>" + _strDay + "</a></td>"; 
        }
    } 
    text2 += "</tr></tbody></table>"; 

    var m2 = m + 2; 
    
    var text3 = "<p class='year_month'>" + (m==12?(y)+"년 <span>" +'0'+2: m==11?(y+1)+"년 <span>" +'0'+1:y +"년 <span>" +((m2 < 10) ? ('0' + m2) : m2)) + "월</span></p>";
	text3 += "<table>";
	
    text3 += text;
	
	text3 += "<tbody>";
	
	if(m2==14)
	    m2=2;
        
    else if(m2==13)
    {
        m2=1;
        y=y+1;
    } 
    
    var d3 = (y+(y-y%4)/4-(y-y%100)/100+(y-y%400)/400+m2*2+(m2*5-m2*5%9)/9-(m2<3?y%4||y%100==0&&y%400?2:3:4))%7; 
	
	for (i = 0; i < 42; i++) 
    {
        var _strDay = "";
        
//        if (i==0) 
//            text3 += "<tr>"; 
            
        if (i%7==0)
        { 
            text3 += "<tr>"; 
            _strDay = "<span class='sun'>" + (i+1-d3) + "</span>";
        }
        
        else if (i%7==6)
            _strDay = "<span class='sat'>" + (i+1-d3) + "</span>";
        
        else
            _strDay = i + 1 - d3;
            
        if((i+1-d3) == today_day && m2==today_month && y==today_year) 
            text3 += "<td><a href='javascript:setDate(\""+ y +"-"+ ((m==12)?"0"+2:(m==11)?"0"+1:m2<10?"0"+m2:m2)+"-"+((i+1-d3)<10?("0"+(i+1-d3)):(i+1-d3))+"\",\"" + ifrm + "\");nightchk(\"" + ifrm + "\",\"" + target_no + "\");'><span class='today'>" + _strDay + "</span></a></td>"; 
            
        else
        {
            if (i < d3 || i >= d3+(m2*9-m2*9%8)/8%2+(m2==2?y%4||y%100==0&&y%400?28:29:30)) 
                text3 += "<td></td>";
                
            else 
                text3 += "<td><a href='javascript:setDate(\""+ y +"-"+ ((m==12)?"0"+2:(m==11)?"0"+1:m2<10?"0"+m2:m2)+"-"+((i+1-d3)<10?("0"+(i+1-d3)):(i+1-d3))+"\",\"" + ifrm + "\");nightchk(\"" + ifrm + "\",\"" + target_no + "\");'>" + _strDay + "</a></td>"; 
        }
    }
    
    text3 += "</tr></tbody></table>"; 
    
    //달력조합
    textTotal = "<div class='calender_wrap'>";
	textTotal += "<div class='calender'>";
	textTotal += _strPre
	textTotal += "<div class='wrap_calender'>";
    textTotal += text1;	
    textTotal += "</div>";
    textTotal += "<div class='wrap_calender'>";
    textTotal += text2;
    textTotal += "</div>";	
    textTotal += "<div class='wrap_calender'>";
    textTotal += text3;
    textTotal += "</div>";	
    textTotal += _strNext
	textTotal += "</div>";
	textTotal += "<p class='close'><a href='javascript:closeCal(\"" + ifrm + "\");'><img src='http://image.bico.jp/site/100011/v2/img/common/close.gif' alt='닫기' /></a></p>";
	textTotal += "</div>";
	
    document.getElementById("calendar").innerHTML = textTotal;
} 


// by225 2009.11.19
var txtCheckIn_091119 = null;
var txtCheckOut_091119 = null;
var ddlDuration_091119 = null;
var strFunctionName = null;
function fixedshowCal(_strCheckIn,_strCheckOut,_strDuration,_strFunction,type)
{ 
    txtCheckIn_091119 = document.getElementById(_strCheckIn);
    txtCheckOut_091119 = document.getElementById(_strCheckOut);
    ddlDuration_091119 = document.getElementById(_strDuration);
    
    if(type == "In")
        showCal(_strCheckIn);
    
    else
        showCal(_strCheckOut);
        
    callfunction = _strFunction;
}
//

function showCal(textboxId, flag, ifrm)
{ 
    if(ifrm == "1")
        target = parent.document.getElementById(textboxId);
        
    else
        target = document.getElementById(textboxId);
        
    if(target.id.substring(0,12) == "checkOutDate")
        target_no = target.id.substring(12,13);
    
    else
        target_no = target.id.substring(11,12);
    
    var now_date   = new Date();
    today_year = now_date.getYear();
    
    if( today_year<1900) 
        today_year = today_year + 1900;
        
    today_month = return0(now_date.getMonth()+1);
    today_day = return0(now_date.getDate());  
    
    temp = target.value.split("-"); 
    
    if(target.value == "")
    {
        temp[0] = today_year;
        temp[1] = today_month;
        temp[2] = today_day; 
    }

    BY = temp[0]; 
    BM = temp[1]; 
    BD = temp[2]; 
        
    if(BM > 12)
        BM = 12;
        
    if(BD > 31)
        BD = 31;
        
    target.value = BY + "-" + BM + "-" + BD;
    
    //달력 생성 호출
    showCalendar(target.value, ifrm); 
    
    //달력 위치 셋팅
    var st = getRealOffsetTop(target)+20;
    var sl = getRealOffsetLeft(target); 
    //alert(sl); 
    //document.getElementById("calendar").style.top = st + "px"; 
    //if(flag) 
	//	document.getElementById("calendar").style.left = sl + "px";
	
	if(ifrm == "1")
        parent.document.getElementById("calendar").style.display=""; 
        
    else
        document.getElementById("calendar").style.display=""; 
} 

function setDate(d, ifrm){ 
    target.value = d; 

    if(target.id.substring(0,12) == "checkOutDate" && ifrm == "1")
        setDays(parent.document.getElementById("checkInDate" + target_no), target, ifrm);
        
    else if(target.id.substring(0,12) == "checkOutDate")
        setDays(document.getElementById("checkInDate" + target_no), target, ifrm);
        
    else if(target.id.substring(0,12) == "txtCheckOut")
        setDays(document.getElementById("txtCheckIn"), target, ifrm);
    
    closeCal(ifrm); 
    
    if(callfunction != "" && callfunction != undefined)
    {
        fixednightchk(txtCheckIn_091119.getAttribute('id'),txtCheckOut_091119.getAttribute('id'),ddlDuration_091119.getAttribute('id'))
        eval(callfunction);
    }
} 
function closeCal(ifrm){ 
    if(ifrm == "1")
        parent.document.getElementById("calendar").style.display="none"; 
    else
        document.getElementById("calendar").style.display="none"; 
} 

function return0(str)
{
	str=""+str;
	if (str.length==1) str="0"+str;
	return str;
}

function getRealOffsetTop(o) { return o ? o.offsetTop + getRealOffsetTop(o.offsetParent) : 0; } 
function getRealOffsetLeft(o) { return o ? o.offsetLeft + getRealOffsetLeft(o.offsetParent) : 0; } 
//달력만들기 끝



//그외 함수들
function nightchk(ifrm, no)
{
	if (ifrm == undefined && document.getElementsByName("checkInDate").value != "")
		ValofcheckOutDate("", no);
	else {
		if(parent.document.getElementsByName("checkInDate") != null && parent.document.getElementsByName("checkInDate").value != "" && ifrm == "1")
			ValofcheckOutDate(ifrm, no);	    
		else if(document.getElementsByName("checkInDate").value != "")
			ValofcheckOutDate("", no);
		else
			alert("Select CheckIn Date.");
	}
}

function fixednightchk(_strCheckIn,_strCheckOut,_strDuration)
{
    txtCheckIn_091119 = document.getElementById(_strCheckIn);
    txtCheckOut_091119 = document.getElementById(_strCheckOut);
    ddlDuration_091119 = document.getElementById(_strDuration);

    if(txtCheckIn_091119.value != "")
	    ValofcheckOutDate();
    else
	    alert("Select CheckIn Date.");
}


function ValofcheckOutDate(ifrm, no)
{
    if(no == null)
        var no = "";

    if(txtCheckIn_091119 != null)
    {
	    txtCheckOut_091119.value=addDays(txtCheckIn_091119,ddlDuration_091119.value);	
    }
    else
    {
        var days = 1 ;
        if(ifrm == "1")
        {
	        var chkInDateVal= parent.document.getElementsByName("checkInDate" + no)[0];
	        var checkOutDate= parent.document.getElementsByName("checkOutDate" + no)[0];	
	        var night= parent.document.getElementsByName("night" + no)[0];	
	        var daysobj= parent.document.getElementsByName("days" + no)[0];	
	        var spnCheckinDate= parent.document.getElementById("spnCheckinDate");
	        var spnDay= parent.document.getElementById("spnDay");						
	    }
	    else
	    {
	        var chkInDateVal= document.getElementsByName("checkInDate" + no)[0];
	        var checkOutDate= document.getElementsByName("checkOutDate" + no)[0];	
	        var night= document.getElementsByName("night" + no)[0];	
	        var daysobj= document.getElementsByName("days" + no)[0];	
	        var spnCheckinDate= document.getElementById("spnCheckinDate");
	        var spnDay= document.getElementById("spnDay");							
	    }

        if(night != null)
        {
            checkOutDate.value=addDays(chkInDateVal,night.value);	            
            days += (night.value) - 0;
            daysobj.value = days - 0;
        	
            if (spnCheckinDate != null)
                spnCheckinDate.innerHTML = chkInDateVal.value;
        	    
            if (spnDay != null)
                spnDay.innerHTML = night.value;
        }
	}
}

function addDays(isoDate, noOfDays) 
{
	var aDate=cvtISOToDate(isoDate);
	if (!aDate) return "";
	var millis=86400000 * noOfDays;
	aDate=new Date(aDate.getTime()+millis);
	with (aDate) {
		var mm=getMonth()+1;
		if (mm<10) {mm='0'+mm;}
		var dd=getDate();
		if (dd<10) {dd='0'+dd;}
		return (getFullYear() + '-' + mm + '-' + dd);
	}
}

function cvtISOToDate(isoDate) 
{
	var atomDate= isoDate.value.split('-');
	var aDate=new Date(parseInt(atomDate[0],10),parseInt(atomDate[1],10)-1,parseInt(atomDate[2],10),6,0,0);
	return aDate;
}

function setDays(chkInDate, chkOutDate, ifrm) 
{
	var InDate=cvtISOToDate(chkInDate);
	var OutDate=cvtISOToDate(chkOutDate);
	var NightNum = "";

	if(chkOutDate.id.substring(12,13) != null)
	    NightNum = chkOutDate.id.substring(12,13);

    if(chkInDate.id == "txtCheckIn")
        var Night = document.getElementById("ddlDuration");
        
	else if(ifrm == "1")
	    var Night = parent.document.getElementById("night" + NightNum);
	    
	else
	    var Night = document.getElementById("night" + NightNum);

	if (!InDate)
	    return "";
	    
	//var millis=86400000 * noOfDays;

	var Days = (OutDate - InDate) / 86400000;

	if(Days < 1)
	{
	    alert("체크아웃 날짜는 체크인 날짜보다 같거나 빠를 수 없습니다.");
	    
	    with (InDate) 
	    {
		    var mm=getMonth()+1;
		    if (mm<10) {mm='0'+mm;}
		    var dd=getDate();
		    if (dd<10) {dd='0'+dd;}
	        chkInDate.value = getFullYear() + '-' + mm + '-' + dd;
	        
	        Night.value = "1";
	        Night.selectedIndex = 0;
	    } 
	}
	
	else if(Days > 31)
	{
	    alert("체크아웃 날짜는 체크인 이후 한달까지 가능합니다.");
	    
	    with (InDate) 
	    {
		    var mm=getMonth()+1;
		    if (mm<10) {mm='0'+mm;}
		    var dd=getDate();
		    if (dd<10) {dd='0'+dd;}
	        chkInDate.value = getFullYear() + '-' + mm + '-' + dd;
	        
	        Night.value = "1";
	        Night.selectedIndex = 0;
	    } 
	}
	
	else
	{
	    for (i = 0; i < Night.length; i++) 
	    {
			if (Night[i].value == Days) 
			{
			    Night.value == Days;
				Night.selectedIndex = i;
			}			
	   }
	}
	
//	if(ifrm != "1")
//	    replaceSelects();	
}

function showIfrm(Id)
{
    if(location.href.indexOf("hotel_main.aspx?type=jeju") > 0)
        document.getElementById("ifrmcal").src = "../popup/calendar.aspx?type=" + Id + "&refurl=" + location.href;
    else
        document.getElementById("ifrmcal").src = "../popup/calendar.aspx?type=" + Id;
    document.getElementById("calendar").style.display = "";
}

function SetNum()
{
    document.getElementById("txtCheckIn").value = document.getElementById("txtCheckIn1").value;
    document.getElementById("txtCheckOut").value = document.getElementById("txtCheckOut1").value;
    document.getElementById("ddlDuration").value = document.getElementById("ddlDuration1").value;
}
