//// calendar.js
// v.3 DST fix
// images fixed
// v21l2 floating Arrive/Departure area

var arrMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
var arrDaysFull = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

document.write('<div id="caldiv" class="caldiv" style="visibility:hidden;"></div>');

var calendPlace = null;
// calendPlace - the place where the calendar currently opened:
// 0 - in Calculate Rates area (above the map)
// 1 - C & S window
// 2 - Specials on the map popup (map_green_detail_label)
// 3 - Specials on the list popup  (list_detail_label)
// 12 - Coupons on the map popup 
// 13 - Coupons on the list popup

var strBackFunc = null; // keeps function that will be called on selecting date
var oCalInput = null;   // 
var calPadding = true;  // whether cells of dates beyond current month to be displayed as empty and disabled(true) or as regular days (false)
//var strCalendarDate = null;
var dateCalendarDate = null; // date obj, to keep date on calendar select, used in popups (calendPlace>0)

////////////////////////////////
//     DisplayCal
// positions and displays the calendar.

function DisplayCal(oInput, BackFunc)
{
// oInput contains Date to initialize  and also used for positioning the calendar

if(oCalInput==oInput){CloseCal(); return;};

oCalInput = oInput;
var dateInput = oInput.value;

strBackFunc = BackFunc;

var addLeft = 0;
var addTop = 0;

if(calendPlace==0)
{

if(oInput.name =='arrDateTemp')
var oTmpInput = parkingCaseForm.arrHours;
else
var oTmpInput = parkingCaseForm.depHours;

var pos = getAbsolutePositionOf(oTmpInput);
addTop = oTmpInput.offsetHeight;


if(!CUSTOM){
if(globJSBrowser == "Firefox")addLeft = 2;
else if(globJSBrowser == "IE")addLeft = 0;
else if(globJSBrowser == "Chrome")addLeft = 3;
else if(globJSBrowser == "Safari")addLeft = 3;
}


}
else
{
var pos = getAbsolutePositionOf(oInput);
}




// DIV containing the whole calendar
var oCal = document.getElementById("caldiv"); 

oCal.style.left = parseInt(pos[0]) + addLeft;
oCal.style.top = parseInt(pos[1]) + parseInt(addTop) + 3;

PopulateCal(dateInput);

if(calendPlace==0)oCal.style.zIndex = 35001;
else  oCal.style.zIndex = 3010;

oCal.style.visibility = "visible";

}

////////////////////////////////
//     PopulateCal
//  parse and output the calendar HTML code

function PopulateCal(Month)
{

if(!Month)
{ // no day will be selected on the calendar
  // called when year, month input changed
var displayYear = document.getElementById("calYear").value;
var displayMonth = document.getElementById("calMonth").value;
}
else
{
arrMonth =  Month.split("-");

var displayYear = arrMonth[0]; 
var displayMonth = arrMonth[1];

var displayDate = 0;
if(arrMonth[2])
displayDate = arrMonth[2];
}

var strSpecCoupMonth = null;

var indx = displayYear + displayMonth;



if(calendPlace > 1)
{

// checkShowMonth [r_rg_functions.js]
if(checkShowMonth(new Date(displayYear, displayMonth, 1))) return;

// get string for month days to be enabled / disabled
if(calendPlace/10 < 1) 
strSpecCoupMonth  =  lastSelectedGarage.arrSpecialsMonth[indx][0];    
else
strSpecCoupMonth  =  lastSelectedGarage.arrCouponsMonth[indx][0];    

}
// DS window
if(calendPlace == 1)
{
var arrDisabledWeekDays = CheckDSCalendar();
//alert(arrDisabledWeekDays.toString());
}


var dateNow = new Date();

                
// Month dropdown field
var selMonths = '<select id="calMonth" class="cal" onChange="PopulateCal()">';

for(var m = 0; m <12; m++)
{
 
if(dateNow.getFullYear() == displayYear)
if(m < dateNow.getMonth())continue;

var sel = ""; if(m == displayMonth) sel = " selected";
selMonths += '<option' + sel + ' value="' + m + '">' + arrMonths[m].substr(0,3) + '</option>';
}

selMonths += '</select>';  // Month  dropdown field

// Year  dropdown field
var selYears = '<select id="calYear" class="cal" onChange="PopulateCal()">';

for(var y = 0; y < 3; y++)
{
var year = parseInt(dateNow.getFullYear()) + y;
var sel = ""; if(year == displayYear) sel = " selected";
selYears += '<option' + sel + ' value="' + year + '">' + year + '</option>';
}

selYears += '</select>';  // Year  dropdown field

// today date used for disabling past dates, "<" button and months earlier than mow
var todayDate = new Date();

todayDate = new Date(todayDate.getFullYear(), todayDate.getMonth(), todayDate.getDate());

var todayText = "Today: " + arrMonths[todayDate.getMonth()].substr(0,3) +
                "-" +  todayDate.getDate() + "-" +  todayDate.getFullYear();

// parsing the start date of the calendar
var date1stMonth = new Date(displayYear, displayMonth , 1);
var weekMonthStart =  date1stMonth.getDay();
var dateCalStart = new Date(date1stMonth.valueOf() - weekMonthStart*(3600*1000*24) );

// prev & next months for arrow buttons
var nextMonth = new Date(displayYear, parseInt(displayMonth) + 1 , 1);
var previuosMonth = new Date(displayYear, displayMonth - 1 , 1);

var prevDisable = 0;
if(previuosMonth < new Date(todayDate.getFullYear(), todayDate.getMonth() , 1))var prevDisable = 1;


var ts_CurrDay = dateCalStart.valueOf();

var cells = '<tr>';


// the calendar cells (dates) combining

currDate = dateCalStart;

for(var d=0; d < 42; d++)
{
//currDate = new Date(ts_CurrDay);

currDay = currDate.getDate();
currMonth = currDate.getMonth();
currYear = currDate.getFullYear();
currWD = currDate.getDay(); 
loc_class = "";

if(calPadding && currDate.getMonth() != displayMonth)
{
inner = "&nbsp;";
loc_class = "pad"; // padding ( dates beyond of the current month range)
funcClick = "";
}  
else
{
inner = currDay;
loc_class = "plain";
funcClick = "onClick=\"SelectCal('" + currYear + "-" + currMonth + "-" + currDay + "');\"";

if(currDay == displayDate && currMonth == displayMonth)
{loc_class = "select";
funcClick = "onClick=\"CloseCal();\"";}
 
// disable some days
if(strSpecCoupMonth && currMonth == displayMonth)
if(strSpecCoupMonth.substr(currDay-1, 1)==0)
{loc_class = "disable";funcClick = "";}

// disable for DS
if(calendPlace == 1)
{
if(!arrDisabledWeekDays[currWD])
{loc_class = "disable";funcClick = "";}
}

if(currDate < todayDate){ loc_class = "past"; funcClick = "";}

}


cells += '<td class="' + loc_class + '" ';
if(loc_class == "plain")
cells += 'onmouseover="HighlightCal(this,1);" onmouseout="HighlightCal(this,0);"';

if(loc_class == "past")
cells += ' title="Past dates cannot be selected."';
else if(loc_class == "disable")
cells += ' title="There are no Specials for this date."';

cells += ' ' + funcClick + '>' + inner + '</td>';


if(d%7 == 6) cells +=  "</tr><tr>"

//ts_CurrDay +=  3600*1000*24;  // next day ( adding 24 hours)

currDate = new Date(currDate.getFullYear(), currDate.getMonth() , parseInt(currDate.getDate() +1));

}
cells += '</tr>';


// combining the whole string of the calendar HTML contents

var strOut = '';

strOut += '<table class="cal">';

/*strOut += '<tr><td style="padding:0">' + '<table border="0" cellspacing="0" cellpadding="0" style="margin-top:-3px; margin-right:-4px;"><tr><td width="100%"></td>' +
'<td title="Close" onClick="CloseCal();" style="font-size:8pt;background:#ffffcc;color:#ee0000;border:1px #ee0000 solid; padding:0 3px 0 3px;cursor:pointer;"><b>X</b></td></tr></table>';
 +  '</td></tr>'; */ 

strOut += '<tr><td>';

strOut += '<table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>';

strOut += '<td width="12px">&nbsp;</td><td style="padding-top:8px;">';



if(!prevDisable)
{
strOut += '<input type="image" src="' + PATH_IMG + 'images/calend_arrow_l.gif" class="arrbtn" alt="<" ';
strOut += ' onClick="PopulateCal(\''
 + previuosMonth.getFullYear() + "-" + previuosMonth.getMonth() + '\')" ';
 }
else
{
strOut += '<input type="image" src="' + PATH_IMG + 'images/calend_arrow_l_dis.gif" class="arrbtn" style="cursor:default;" ';
strOut += ' title="Past dates cannot be selected"';
} 
 
strOut += '>';

strOut += '</td><td style="padding-top:7px;">';

strOut += selMonths;

strOut += '</td><td style="padding-top:7px;">';

strOut += selYears;

strOut += '</td><td style="padding-top:8px;">';

strOut += '<input type="image" src="' + PATH_IMG + 'images/calend_arrow_r.gif" class="arrbtn" alt=">" onClick="PopulateCal(\''
    + nextMonth.getFullYear() + "-" + nextMonth.getMonth() +
'\')">';

strOut += '</td><td style="vertical-align:top;text-align:right;width:12px">';

strOut +=  '<div title="Close" onClick="CloseCal();" ' +
'style="font-size:8pt;background:#ffffcc;color:#ee0000;border:1px #ee0000 solid; padding:0 3px 0 3px;cursor:pointer;margin-top:0px; margin-right:0px;font-weight:bold;">X</div>';

strOut += '</td><tr></table>';

strOut += '</td></tr><tr><td><center>' +
'<table class="calend">' +
'<tr class="week"><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td> </tr>' +
cells +
'</table></center>' +
'</td></tr><tr><td class="bottom" onmouseover="this.style.color=\'#ffff55\';" onmouseout="this.style.color=\'#ffffff\';"' +
' onClick="SelectCal(\'' + todayDate.getFullYear() + "-" + todayDate.getMonth() + "-" +  todayDate.getDate()   + '\')">' +
todayText +
'</td></tr></table>';

document.getElementById("caldiv").innerHTML = strOut; // the calendar HTML contents

}

////////////////////////////////
//     SelectCal

function SelectCal(strDate)
{ // user clicks the calendar cell ( date)

document.getElementById("caldiv").style.visibility = "hidden";
// calendar return selectd date both in hidden input and dateCalendarDate
var arr_date = strDate.split("-");
dateCalendarDate = new Date(arr_date[0] , arr_date[1], arr_date[2]);

oCalInput.value = strDate;

if(typeof(strBackFunc!='undefined') && strBackFunc!=null)
strBackFunc(); // function to call after selecting date

oCalInput = null;
}

////////////////////////////////
//     CloseCal
function CloseCal()
{
document.getElementById("caldiv").style.visibility = "hidden";
oCalInput = null;
}

////////////////////////////////
//     HighlightCal
function HighlightCal(oElem, bOn)
{
// Highlight the calendar cell when mouse moves over the date cell
if(bOn)
oElem.className = oElem.className + " highlight";
else
oElem.className = oElem.className.replace(" highlight", "");
}

/* **** written by MIP <handyprog@mail.ru> for BestParking.com  **** */

