<!--
/*=========================================================================
 * Filename    : /central/javascript/calendar.js
 * Type	       : JavaScript
 * Description : functions for display Calendar for selecting dates
 * Functions   : - show_calendar(str_target, str_datetime, expiry, expiryvalue) 
 *               - str2dt (strDate) 
 *               - dt2dtstr (selectedDate) 
 *=========================================================================*/

	/*=====================================================================*
	 * Function    : calendar ?show_calendar
	 * Description : display calendar for selecting dates
	 *=====================================================================*/
	function show_calendar(str_target, str_datetime, expiry, expiryvalue, anydate,refreshDate)   
	{
		var linkall = (typeof anydate) == "number";
		var arr_months = ["January", "February", "March", "April", "May", "June",
						  "July", "August", "September", "October", "November", "December"];
	
		var week_days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
		var weekstart = 0; // day week starts from (normally 0 or 1)
	
		var today = new Date();
		
		if(str_datetime == "0000-00-00"){
			var current = new Date();
		}
		else
			var current = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));
		
		var dt_prev_month = new Date(current);
		dt_prev_month.setMonth(current.getMonth()-1);
	
		var dt_next_month = new Date(current);
		dt_next_month.setMonth(current.getMonth()+1);
		
		var dt_firstday = new Date(current);
		dt_firstday.setDate(1);
		dt_firstday.setDate(1-(7+dt_firstday.getDay()-weekstart)%7);
		
		var dt_lastday = new Date(dt_next_month);
		dt_lastday.setDate(0);
		
		// html generation (feel free to tune it for your particular application)
		// print calendar header
		var str_buffer = new String (
		"<!DOCTYPE html\n" +
		"     PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" +
		"     \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"\n" +
		">\n" +		
		"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" +
		"<head>\n" +
		"<script language=JavaScript>\n" +
		"	function add_twoMonth(){" + 
		"       if(window.opener.document.hotsheetsForm.deal_date){ " +
		"			var year  = window.opener.document.hotsheetsForm.deal_date.value.substring(0,4) - 0;" + 
		"			var month = window.opener.document.hotsheetsForm.deal_date.value.substring(5,7) - 0;" + 
		"			var day   = window.opener.document.hotsheetsForm.deal_date.value.substring(8,10) - 0;" + 		
		"			var test = new Date(year,month,day);" + 
		"			var test2 = new Date(test.getTime() + 60*24*60*60*1000);" + 
		"			var me = test2.getMonth() + '';" + 
		"			var me2 = test2.getDate() + '';" + 
		"			var me3 = test2.getYear() + '';" +
		"   	    if(me == 0){ " +
		"				me = 12; " +
		"				me3 = me3 -1 " + 
		"			} " +
		"			if(me.length == 1)" + 
		"				me = '0' + me;" + 
		"			if(me2.length == 1)" + 
		"				me2 = '0' + me2;" + 
		"			window.opener.document.hotsheetsForm.deal_date2.value = me3 + '-' + me +'-'+ me2;" + 
		" 		}" + 
		"	}" +
		" function changeDuration(value){   \n" +
		" 				if(value){			\n" +
		" 					var m_array=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');		\n" +
		" 					var id = 'durationDate';										\n" +
		" 					var from_date = window.opener.document.hotsheetsForm.date.value;\n" +
		" 					if(from_date != '' && value > 0){								\n" +
		" 						if(window.opener.document.all){ 							\n" +
		" 							if(eval('document.all.' + id))							\n" +
		" 								objs = eval('document.all.' + id);					\n" +
		" 						}															\n" +
		" 						if (window.opener.document.getElementById){					\n" +
		"  							if(window.opener.document.getElementById(id))			\n" +
		" 								objs = window.opener.document.getElementById(id);	\n" +
		" 						}															\n" +
		" 						var dateList=from_date.split('-'); 							\n" +
		" 						var myDate=new Date();										\n" +		
		" 						var y = parseInt(dateList[0],10); 							\n" +
		" 						var m = parseInt(dateList[1],10);" +
		" 						var d = parseInt(dateList[2],10);" +
		"	 																				\n" +
		"						myDate.setFullYear(y,m - 1,d);							" +					
		" 						myDate.setDate(myDate.getDate() + parseInt(value));" +
		" 						var from_month = myDate.getMonth() + 1;				" +		
		" 						var from_month_str = m_array[from_month - 1];  " +
		" 						" +
		" 						if(from_month_str.length == 1)" +
		" 							from_month = '0' + from_month;" +
		" 						var from_day_str = myDate.getDate() + '';   " +
		" 						if(from_day_str.length == 1)" +
		" 							from_day_str = '0' + from_day_str;" +
		" 						if (parseInt(myDate.getYear(),10) < 1000)" +
		" 							var year = parseInt(myDate.getYear(),10) + 1900;" +
		" 						else" +
		" 							var year = myDate.getYear() + '';" +
		" 						" +
		" 						objs.innerHTML = ', ending ' + from_month_str + ' ' + from_day_str + ', ' + year;" +
		" 					} " +
		" 				}" +
		" 			}" +
		"</script>" + 
		"<meta http-equiv=\"Content-Type\" content=\"text/html;\" />\n" +
		"<title>Calendar</title>\n" +
		"<style type=\"text/css\"><!--\n" + 
		"a { text-decoration: none; font-family:verdana; font-size:10pt; color: #0060C0}\n" + 
		"td { font-family:verdana; font-size:10pt; color:#808080; }\n" + 
		"--></style>\n"  + 
		"</head>\n\n" +
		
		"<body bgcolor=\"white\">\n" +
		"<table 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)+"'");
		
		if((typeof expiry) != "undefined") // && expiryvalue=='')
			str_buffer += ", '"+expiry+"', '"+expiryvalue+"'";
		else
			str_buffer += ", '', ''";

		if(linkall)			
			str_buffer += ", 1";
		
		str_buffer += ");\">"+

		"<font style=\"color:#FFFFFF\">&lt;&lt;</font></a></td>\n" +
		
		"	<td bgcolor=\"#4682B4\" align=\"center\" colspan=\"5\">" +
		"<font face=verdana size=2 color=#FFFFFF>"  
		+arr_months[current.getMonth()]+" "+current.getFullYear()+"</font></td>\n"+
		
		"	<td bgcolor=\"#4682B4\" align=\"right\"><a href=\"javascript:window.opener.show_calendar('"
		+str_target+"', '"+dt2dtstr(dt_next_month)+ "'";
		
		if((typeof expiry) != "undefined") // && expiryvalue=='')
			str_buffer += ", '"+expiry+"', '"+expiryvalue+"'";
		else
			str_buffer += ", '', ''";

		if(linkall)			
			str_buffer += ", 1";
		
		str_buffer += ");\">"+
		"<font style=\"color:#FFFFFF\">&gt;&gt;</font></a></td>\n</tr>\n";
	
		var d = new Date(dt_firstday);
		
		// print weekdays titles
		str_buffer += "<tr>\n";
		for (var n=0; n<7; n++)
			str_buffer += "	<td bgcolor=\"#87CEFA\" align=\"center\"><font color=#000000 face=verdana size=2>"+
			week_days[(weekstart+n)%7]+"</font></td>\n";
		str_buffer += "</tr>\n";
	
		// print calendar table
		while (d.getMonth() == current.getMonth() ||  d.getMonth() == dt_firstday.getMonth()) 
		{
			// print row header
			str_buffer += "<tr>\n";
		
			for (var n_current_wday=0; n_current_wday<7; n_current_wday++) 
			{
				var linkit = 1;
				var todayLink = false;

				// print current date
				if (d.getDate() == current.getDate() && d.getMonth() == current.getMonth() && d.getYear() == current.getYear())
				{					
					str_buffer += "\t\t<td bgcolor=\"#FF0085\" width=\"14%\" align=\"center\">";
					todayLink = true;
				}
				// print past date
				else if(current.getMonth() < d.getMonth() || current.getMonth() > d.getMonth()) {										
					str_buffer += "\t\t<td bgcolor=\"#CDCDCD\" width=\"14%\" align=\"center\">";
					//linkit = (linkall) ? 1 : 0;
					linkit = 1;
				}
				else if(current.getMonth() == d.getMonth()) {
					if(d.getTime() < today.getTime()) {
						str_buffer += "\t\t<td bgcolor=\"#DBEAF5\" width=\"14%\" align=\"center\">";
						linkit = (linkall) ? 1 : 0;
					}
					else if(d.getTime() > today.getTime()) {
						str_buffer += "	<td bgcolor=\"white\" width=\"14%\" align=\"center\">";
						linkit = 1;
					}
					else {
						str_buffer += "	<td bgcolor=\"white\" width=\"14%\" align=\"center\">";
						linkit = (linkall) ? 1 : 0;
					}
					
				}
				// available date
				if(linkit == 1) {
					
					if(true){
						if((typeof expiry) != "undefined" && expiry != "" && expiryvalue == "")
							str_buffer += "<a href=\"javascript:window.opener."+str_target+".value='"+dt2dtstr(d)+"';  add_twoMonth();window.opener." +expiry+".value='"+dt2dtstr(d)+"';window.close();\">";
						else
							str_buffer += "<a href=\"javascript:window.opener."+str_target+".value='"+dt2dtstr(d)+"'; if(window.opener.document.hotsheetsForm.duration){ changeDuration(window.opener.document.hotsheetsForm.duration.value) }; add_twoMonth();window.close();\">"; 
					}
					else{
						if((typeof expiry) != "undefined" && expiry != "" && expiryvalue == "")
							str_buffer += "<a href=\"javascript:window.opener."+str_target+".value='"+dt2dtstr(d)+"'; window.opener." +expiry+".value='"+dt2dtstr(d)+"';window.close();\">";
						else
							str_buffer += "<a href=\"javascript:window.opener."+str_target+".value='"+dt2dtstr(d)+"'; window.close();\">";
					}
		
					if(todayLink)
						str_buffer += "<font color=\"#FFFFFF\">" + d.getDate() + "</font></a>";
					else	
						str_buffer += d.getDate() + "</a>";
				}
						
				// past date
				else 
					str_buffer += d.getDate();
		
				str_buffer += "</td>\n";
		
				d.setDate(d.getDate()+1);
				linkit=1;
			}
	
		
			// print row footer
			str_buffer += "</tr>\n";
		}
		
		// print calendar footer
		str_buffer +=
		"</table>\n" +
		"</td>\n</tr>\n</table>\n" +
		"</body>\n" +
		"</html>\n";
		
		var vWinCal = window.open("", "Calendar", 
		"width=250,height=210,status=no,resizable=yes,top=200,left=200");
		vWinCal.opener = self;
		
		var calc_doc = vWinCal.document;
		calc_doc.write (str_buffer);
		calc_doc.close();

		vWinCal.focus();
	}

	/*=====================================================================*
	 * Function    : calendar ?str2dt
	 * Description : convert given string to a Date Object
	 *=====================================================================*/
	function str2dt (strDate) 
	{

		var re_date = /^(\d+)-(\d+)-(\d+)$/;
		
		if (!re_date.exec(strDate))
			return alert("Invalid Datetime format: "+ strDate);
		else{
			var dd = RegExp.$3;
			var mm = RegExp.$2-1
			var yy = RegExp.$1;
			return (new Date (yy, mm, dd));
		}
	}


	/*=====================================================================*
	 * Function    : calendar ?dt2dtstr
	 * Description : convert Date Object to formatted string
	 *=====================================================================*/
	function dt2dtstr (selectedDate) 
	{
		var selectedYear = selectedDate.getFullYear();
		var selectedMonth = selectedDate.getMonth() + 1;
		var selectedDay = selectedDate.getDate();
	
		if (selectedMonth < 10) 
			selectedMonth = "0" + selectedMonth;
	
		if (selectedDay < 10) 
			selectedDay = "0" + selectedDay;
	
		return selectedYear + "-" + selectedMonth + "-" + selectedDay;
	}
	
	

/*=========================================================================*/
/*=========================================================================*/
-->
