﻿// Title: Timestamp picker
// Description: See the demo at url
// URL: http://us.geocities.com/tspicker/
// Script featured on: http://javascriptkit.com/script/script2/timestamp.shtml
// Version: 1.0
// Date: 12-05-2001 (mm-dd-yyyy)
// Author: Denis Gritcyuk <denis@softcomplex.com>; <tspicker@yahoo.com>
// Notes: Permission given to use this script in any kind of applications if
//    header lines are left unchanged. Feel free to contact the author
//    for feature requests and/or donations


/*
 * Date picker
 * PL
 */


function show_calendar(str_target, str_datetime, element) 
{

	/*
	 * Tab of months
	 */
var arr_months = [
		"Январь",
		"Февраль",
		"Март",
		"Апрель",
		"Май",
		"Июнь",
		"Июль",
		"Август",
		"Сентябрь",
		"Октябрь",
		"Ноябрь",
		"Декабрь"];


	/*
	 * Tab of days
	 */
	var week_days = ["вc", "пн", "вт", "ср", "чт", "пт", "сб"];
	
	
	/*
	 * Day week starts from (normally 0 or 1)
	 */
	var n_weekstart = 1;


	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));
	
	
	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth()-1);
	
	
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	
	
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	
	
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);
	
	
	
	
	/*
	 * Html generation
	 */ 
	var str_buffer = new String (
		"<html>\n"+
		"<head>\n"+
		"	<title>Kalendarz</title>\n"+
		"</head>\n"+
		"<body bgcolor=\"White\">\n"+
		"<table class=\"clsOTable\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
		"<tr><td bgcolor=\"#4682B4\">\n"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td bgcolor=\"#4682B4\"><a href=\"javascript:window.opener.show_calendar('"+
		str_target+"', '"+ dt2dtstr(dt_prev_month)+"', '" + element + "');\">"+
		"<img src=\"gfx/prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"previous month\"></a></td>\n"+
		"	<td bgcolor=\"#4682B4\" colspan=\"5\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
		+arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</font></td>\n"+
		"	<td bgcolor=\"#4682B4\" align=\"right\"><a href=\"javascript:window.opener.show_calendar('"
		+str_target+"', '"+dt2dtstr(dt_next_month)+"', '" + element + "');\">"+
		"<img src=\"gfx/next.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"next month\"></a></td>\n</tr>\n"
	);
	


	var dt_current_day = new Date(dt_firstday);


	/*
	 * dt_choosen_date - Date which is choosen
	 */
	var dt_choosen_date = new Date(getStartDates(element));
	dt_choosen_date.setHours(0);
	
	
	/*
	 * dt_today_date - Actual date
	 */
	var dt_today_date = new Date(now_date);	
	dt_today_date.setHours(0);
	
	
	/*
	 * checkIn and departure Date
	 */
	var dt_checkInDate = new Date(getStartDates("ns_checkInDate"));
	var dt_departureDate = new Date(getStartDates("ns_departureDate"));
	dt_checkInDate.setHours(0);
	dt_departureDate.setHours(0);
	
	
	/*
	 * Print information
	 */
	if (element=="ns_checkInDate")
	str_buffer +=		
		"<tr><td colspan=\"8\" style=\"font-family: Verdana; font-size: 9px; color: white;\">Дата приезда: " + dt_choosen_date.getDate() + "." + (dt_choosen_date.getMonth()+1) + "." +  dt_choosen_date.getFullYear() + "</td></tr>\n";
	else if (element=="ns_departureDate")
	str_buffer +=		
		"<tr><td colspan=\"8\" style=\"font-family: Verdana; font-size: 9px; color: white;\">Дата выезда: " + dt_choosen_date.getDate() + "." + (dt_choosen_date.getMonth()+1) + "." +  dt_choosen_date.getFullYear() + "</div></td></tr>\n";	
	
	
	/*
	 * print weekdays titles
	 */
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td bgcolor=\"#87CEFA\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"+
		week_days[(n_weekstart+n)%7]+"</font></td>\n";


		
		
	/*
	 * print calendar table
	 */
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() || dt_current_day.getMonth() == dt_firstday.getMonth()) 
	{

		/*
		 * print row heder
		 */
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) 
		{

			/*
			 * print choosen date		
			 */
			if (dt_current_day.getDate() == dt_choosen_date.getDate() &&
					dt_current_day.getMonth() == dt_choosen_date.getMonth() &&
					dt_current_day.getFullYear() == dt_choosen_date.getFullYear()
					)
					str_buffer += "	<td bgcolor=\"#FFB6C1\" style=\"border: 1px solid #d55757;\" align=\"right\">";



			/*
			 * print days after checkInDate and departureDate
			 */
			else if (dt_current_day >= dt_checkInDate && dt_current_day <= dt_departureDate)
			{
					str_buffer += "	<td bgcolor=\"#fff9e7\" style=\"border: 1px solid #d55757;\" align=\"right\">";
			}


			/*
			 * print today date		
			 */
//			else if (dt_current_day.getDate() == dt_today_date.getDate() &&
//					dt_current_day.getMonth() == dt_today_date.getMonth() &&
//					dt_current_day.getFullYear() == dt_choosen_date.getFullYear())
//					str_buffer += "	<td bgcolor=\"#a8e598\" align=\"right\">";


			/*
			 * weekend days	
			 */
			else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
				str_buffer += "	<td bgcolor=\"#DBEAF5\" align=\"right\">";
	
	
			/*
			 * print working days of current month
			 */
			else
			str_buffer += "	<td bgcolor=\"white\" align=\"right\">";




			/*
			 * print days of current month
			 */
			if (dt_current_day.getMonth() == dt_datetime.getMonth())
			str_buffer += "<a href=\"javascript:window.opener."+str_target+
			".value='"+dt2dtstr(dt_current_day)+"'; window.opener.checkDates(window.opener.document.getElementById('" + element + "')); window.close();\">"+
			"<font color=\"black\" face=\"tahoma, verdana\" size=\"2\">";


			/*
			 * print days of other months
			 */
			else 
			str_buffer += "<a href=\"javascript:window.opener."+str_target+
			".value='"+dt2dtstr(dt_current_day)+"'; window.opener.checkDates(window.opener.document.getElementById('" + element + "')); window.close();\">"+
			"<font color=\"gray\" face=\"tahoma, verdana\" size=\"2\">";
			str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
			dt_current_day.setDate(dt_current_day.getDate()+1);

		}
		
		/*
		 * print row footer
		 */
		str_buffer += "</tr>\n";

	}
	
	
	/*
	 * print calendar footer
	 */
	str_buffer +=
		"</table>\n" +
		"</tr>\n</td>\n</table>\n";
	str_buffer +=		
		"</body>\n" +
		"</html>\n";



		
	/*
	 * Create window
	 */
	var vWinCal = window.open("", "Calendar", "width=200,height=240,status=no,resizable=yes,top=200,left=200");
			
	vWinCal.opener = self;
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();




	/*
	 * Focus
	 */
	if (vWinCal.opener.focus) 
	{
		vWinCal.focus();
	}
}




/*
 * Conversion str to date type
 */ 
function str2dt(str_datetime) 
{
	tab_date = str_datetime.split(".");
	return (new Date (tab_date[2], tab_date[1]-1, tab_date[0]));
}



/*
 * Conversion date to str type
 */
function dt2dtstr(dt_datetime) 
{
	var d, m, Y;
	
	d = dt_datetime.getDate();
	d = d<10 ? '0'+d : d;
		
	m = (dt_datetime.getMonth() + 1)
	m = m<10 ? '0'+m : m;
		
	Y = dt_datetime.getFullYear();
		
	return (new String (d + "." + m + "." + Y));
}




/*
 * ----------------------
 * Validate date functions
 * ----------------------
 */




/*
 * changeNumOfNights
 */
function changeNumOfNights(element)
{

	var checkindate_temp = Date.UTC(old_checkindate.getFullYear(), old_checkindate.getMonth(), old_checkindate.getDate());

	old_departuredate = new Date((checkindate_temp + (element.value * 86400000)));
	count_days = element.value;
	
	d = old_departuredate.getDate();
	d = d<10 ? '0'+d : d;
		
	m = (old_departuredate.getMonth() + 1);
	m = m<10 ? '0'+m : m;
	
	Y = old_departuredate.getFullYear();		
		
	document.getElementById("ns_departureDate").value = d + '.' + m + '.' + Y;		
}



 
/*
 * checkDates
 */
function checkDates(T)
{
	var checkindate = getStartDates("ns_checkInDate");
	var departuredate;
	var d=0, m=0, Y=0;

	/*
	 * #1 checkInDate must be over nowDate
	 */
	if (checkindate < now_date)
	{
		alert('Изменить параметры выбора ночлегов. Дата приезда в отель выбрана неправильно.');
		
		d = old_checkindate.getDate();
		d = d<10 ? '0'+d : d;
		
		m = (old_checkindate.getMonth() + 1)
		m = m<10 ? '0'+m : m;
		
		Y = old_checkindate.getFullYear();
		
		document.getElementById("ns_checkInDate").value = d + '.' + m + '.' + Y;
	}
	
	
	/*
	 * #2 Make new departureDate = old_departuredate + count_days
	 */
	if (checkindate >= now_date && T.id == "ns_checkInDate")
	{
		departuredate = new Date(checkindate + (count_days * 86400000));
		
		d = departuredate.getDate();
		d = d<10 ? '0'+d : d;
		
		m = (departuredate.getMonth() + 1)
		m = m<10 ? '0'+m : m;
		
		Y = departuredate.getFullYear();		

		document.getElementById("ns_departureDate").value = d + '.' + m + '.' + Y;	
	}
	

	/*
	 * #3 departureDate must be over chceckInDate
	 */
	if (countDays()<1) 
	{
		alert('Изменить параметры выбора ночлегов. Просьба выбрать минимум 1 ночлег.');
		
		d = old_departuredate.getDate();
		d = d<10 ? '0'+d : d;
		
		m = (old_departuredate.getMonth() + 1)
		m = m<10 ? '0'+m : m;
		
		Y = old_departuredate.getFullYear();
		
		document.getElementById("ns_departureDate").value = d + '.' + m + '.' + Y;	
	}	
	
	 
	/*
	 * #4 departureDate must be over chceckInDate but no more than 60 days
	 */
	if (countDays()>60) 
	{
		alert('Изменить параметры выбора ночлегов. Максимальное количество ночлегов для выбора 60.');
		
		d = old_departuredate.getDate();
		d = d<10 ? '0'+d : d;
		
		m = (old_departuredate.getMonth() + 1)
		m = m<10 ? '0'+m : m;
		
		Y = old_departuredate.getFullYear();
		
		document.getElementById("ns_departureDate").value = d + '.' + m + '.' + Y;	
	}
	
	
	/*
	 * Change old_checkindate, old_departuredate and count_days
	 */
	old_checkindate = new Date(getStartDates("ns_checkInDate"));
	old_departuredate = new Date(getStartDates("ns_departureDate"));
	count_days = countDays();
	
	
	/*
	 * Show count_days
	 */
	document.getElementById("ns_numOfNightsSelect").value = count_days;
	
}




/*
 * countDays
 * return: number of days beetwen start and end dates
 */
function countDays()
{
	var my_checkindate=0, my_departuredate=0, my_checkindate_str, my_departuredate_str;
	var count_days=0;
	var d=0,m=0,R=0;
	
	
	my_checkindate_str = document.getElementById("ns_checkInDate").value;
	my_departuredate_str = document.getElementById("ns_departureDate").value;	
	
	
	R = my_checkindate_str.slice(6,10);
	m = my_checkindate_str.slice(3,5) - 1;
	d = my_checkindate_str.slice(0,2);
	my_checkindate = Date.UTC(R, m, d);
	
	R = my_departuredate_str.slice(6,10);
	m = my_departuredate_str.slice(3,5) - 1;
	d = my_departuredate_str.slice(0,2);
	my_departuredate = Date.UTC(R, m, d);


	count_days = my_departuredate - my_checkindate;
	if (count_days!=0)
	count_days = count_days / 86400000;
	
	return count_days;
}




/*
 * getStartDates
 * return: Time elapsed since 1 January 1970 00:00:00 in miliseconds 
 */
function getStartDates(element)
{
	var my_date=0, my_date_str;
	var d=0, m=0, R=0;
	
	my_date_str = document.getElementById(element).value;
	
	R = my_date_str.slice(6,10);
	m = my_date_str.slice(3,5) - 1;
	d = my_date_str.slice(0,2);
	my_date = Date.UTC(R, m, d);
	
	return my_date;
} 


