
YAHOO.namespace("example.calendar"); 
YAHOO.util.Event.addListener(window, "load", init);

function updateShowDateList()
{
    if (document.getElementById("ddlShowDates") != null)
    {
//        if(document.getElementById("txtDate").value.length > 0)
        if(document.getElementById("hidTempCheckInDate").value.length > 0)
        {
//            changedatelist(document.getElementById("txtDate").value, 
            changedatelist(document.getElementById("hidTempCheckInDate").value, 
            document.getElementById(noOfNights).value,
            document.getElementById("ddlShowDates"));
        }
    }
}

function init() 
{ 	
    var showBookingFrom = "";
    var currentDate;
    var isBookingInFeature = false;
    var isMaxBookingDateNull = false;
    var maxBookingDate;
    
    currentDate = toDaysDate;
    
    if (shows == null || shows == "")
    {
        showBookingFrom = passedBookinfFrom;
        maxBookingDate = passedBookingUntil;
    }
    else
    {
        var show = document.getElementById(shows).value;
        
        if (show != null && show.split("~t")[0] != "0000")
        {
            showBookingFrom = show.split("~t")[1];
            maxBookingDate = show.split("~t")[2];
        }
    }
    
    if (showBookingFrom != null && showBookingFrom != "")
    {
        if (new Date(showBookingFrom) >= new Date(currentDate))
        {
            isBookingInFeature = true;
            currentDate = showBookingFrom;
        }
    }
    
    if (document.getElementById("txtDate") != null)
    {
        var selectedDate = document.getElementById("txtDate").value;
        
        if (new Date(selectedDate) < new Date(currentDate))
            document.getElementById("txtDate").value = "";

        if ((new Date(selectedDate) < new Date(currentDate)) || (new Date(selectedDate) > new Date(maxBookingDate)))
            document.getElementById("txtDate").value = "";
    }
               
    var splitedDate = currentDate.split('/')
    var month = splitedDate[0];
    var day = splitedDate[1];
    var splitedYear = splitedDate[2].split(' ');
    var year = splitedYear[0];
    
     if (maxBookingDate != null && maxBookingDate != "")
        splitedDate = maxBookingDate.split('/')
    else
    {
        isMaxBookingDateNull = true;
        splitedDate = currentDate.split('/');
    }
       
    var maxMonth = splitedDate[0];
    var maxDay = splitedDate[1];
    splitedYear = splitedDate[2].split(' ');
    var maxYear = splitedYear[0];
    
    var pageDate = month + "/" + year;
    
    var min;
    min = month + "/" + day + "/" + year;
    
    var max;
    if (isMaxBookingDateNull)
        max = maxMonth + "/" + maxDay + "/" + (parseInt(maxYear) + 1);
    else
        max = maxMonth + "/" + maxDay + "/" + maxYear;
              
    YAHOO.example.calendar.cal1 = new YAHOO.widget.CalendarGroup("cal1","cal1Container",  
			{ 
			    title: "Arrival Date:", 
			    close: true,
			    pagedate: pageDate, 
			    mindate: min, 
			    maxdate: max,
			    START_WEEKDAY: 1
			} 
		);
		                                                     
    YAHOO.example.calendar.cal1.selectEvent.subscribe(selectHandler, YAHOO.example.calendar.cal1, true); 
    var linkClose = YAHOO.util.Dom.getElementsByClassName("link-close", "a", this.oDomContainer)[0] || document.createElement("a");
    YAHOO.util.Event.addListener(linkClose, "click", closeHandler, YAHOO.example.calendar.cal1, true );
    YAHOO.example.calendar.cal1.render(); 
    
    var oElement = document.getElementById("calImage");
    YAHOO.util.Event.addListener(oElement, "click", showCalendar, YAHOO.example.calendar.cal1, true); 
    YAHOO.example.calendar.cal1.cfg.setProperty("MDY_DAY_POSITION", 1); 
    
    YAHOO.example.calendar.cal1.cfg.setProperty("MDY_MONTH_POSITION", 2); 
    YAHOO.example.calendar.cal1.cfg.setProperty("MDY_YEAR_POSITION", 3);
} 
   
function showCalendar(cal)
{
    var calImage =document.getElementById("calImage");
    var pos =  getPos(calImage);
    var left = (pos.x + calImage.width) - 280;
    var top = pos.y + calImage.height +5;
    //$("#cal1Container").css('width',275);
    toggleSearchElements("hidden");
    YAHOO.example.calendar.cal1.show();
    YAHOO.util.Dom.setXY('cal1Container' , [left , top]);
}

var closeHandler = function()
{
    toggleSearchElements("visible");
};
   
var selectHandler = function(type,args,obj)
{ 

    var oElement = document.getElementById("cal1Container");
    oElement.style.display="none"; 
    var selDate = YAHOO.example.calendar.cal1.getSelectedDates()[0];
    var mStr = YAHOO.example.calendar.cal1.cfg.getProperty("MONTHS_LONG")[selDate.getMonth()];
    var wStr = YAHOO.example.calendar.cal1.cfg.getProperty("WEEKDAYS_LONG")[selDate.getDay()];  
    var yStr = selDate.getFullYear();
    var dStr = selDate.getDate();
    
    if (document.getElementById("txtDate") != null)
    {
        document.getElementById("txtDate").value =wStr + ", " + dStr + ", " + mStr + " " + yStr;
        if(document.getElementById("hidTempCheckInDate") != null)
        {
            document.getElementById("hidTempCheckInDate").value = mStr + " " + dStr + ", " + yStr;
            updateShowDateList();
        }
    }
    toggleSearchElements("visible");
}; 

function getPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		do 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);

		return {x:curleft, y:curtop}
	}
}

function toggleSearchElements(directive)
{	
    if(navigator.appVersion.indexOf('MSIE 6') >0)
    {   
    }
}

//if(document.getElementById("txtDate") != null && document.getElementById("txtDate").value != "")
if(document.getElementById("hidTempCheckInDate") != null && document.getElementById("hidTempCheckInDate").value != "")
{
    updateShowDateList();
}