// Title: Timestamp picker
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_calendar/
// Version: 1.0.a (Date selector only)
// Date: 12-12-2001 (mm-dd-yyyy)
// Author: Denis Gritcyuk <denis@softcomplex.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
//
// Jonathan Markevich - Sept 11, 2003
//   Changed to modify combo boxes instead of a text box
//   Changed the input date format to yyyy/mm/dd
//   Added datepicker_init() function to insert different language weekday abbreviations
//
//   - Dec 30, 2003
//  Removed hyperlinks to months other than the current.
//   - Mar 12, 2004
//   Added styles block

var arr_months;
var week_days;
var n_weekstart; // day week starts from (normally 0 or 1)

var ctrl_day;
var ctrl_month;
var str_styles ="";
var str_dflt_style = ".wke{background:#DBEAF5;text-align:right;font-size:9pt;}"+
".wkd{background:#fff;text-align:right;font-size:9pt;}";

datepicker_init('en');
function show_calendar(str_target,str_datetime) {
	
	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt4(str_datetime));
	var dt_prev_month = new Date(dt_datetime);

	// Control names
	ctrl_day = new String("window.opener.document.getElementById('" + str_target + "_Day')");
	ctrl_month = new String("window.opener.document.getElementById('" + str_target + "_Month')");
	
        dt_prev_month.setMonth(dt_datetime.getMonth()-1);

	if (dt_datetime.getMonth()%12 != (dt_prev_month.getMonth()+1)%12) {
		dt_prev_month.setMonth(dt_datetime.getMonth());
		dt_prev_month.setDate(0);
	}
	
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	if ((dt_datetime.getMonth() + 1)%12 != dt_next_month.getMonth()%12)
		dt_next_month.setDate(0);
	
	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 (feel free to tune it for your particular application)
	// print calendar header
	var str_buffer = new String (
		"<html><head><title>Calendar</title>\n"+
		"<script>\n"+
		"  var dt_new = new Date('"+dt_datetime+"');\n"+
		"  var dt_diff=new Date();\n"+
		"  var i_months = ((12-dt_diff.getMonth()+dt_new.getMonth())%12);\n"+	
		"</script>\n"+
        "<style>body{background:white;font-family:Tahoma,Helvetica,sans-serif}\n"+
        "table{padding:0;border-width:1px;width:100%;}\ntd{padding:3px;}"+
        ".wkd, .wke {color:#ccc;font-size:9pt}\n"+
        ".wkd a,.wke a{color:#000;}\n"+
        str_styles+"</style>\n"+
		"</head>\n"+
		"<body>\n"+
		"<table class=\"clsOTable\">\n"+
		"<tr><td bgcolor=\"#4682B4\">\n"+
		"<table cellspacing=\"1\" >\n"+
		"<tr>\n	<td bgcolor=\"#4682B4\"><a href=\"javascript:window.opener.show_calendar('"+
		str_target+"', '"+ dt2dtstr4(dt_prev_month)+"');\">"+
		"<img src=\"images/prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"previous month\"></a></td>\n"+
		"	<td bgcolor=\"#4682B4\" colspan=\"5\">"+
		"<font color=\"white\">"
		+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+"', '"+dt2dtstr4(dt_next_month)+"');\">"+
		"<img src=\"images/next.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"next month\"></a></td>\n</tr>\n"
	);
		
    var dt_current_day = new Date(dt_firstday);
	// print weekdays titles
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td bgcolor=\"#87CEFA\">"+
		"<font color=\"white\" 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 header
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				//if (dt_current_day.getDate() == dt_datetime.getDate() &&
				//	dt_current_day.getMonth() == dt_datetime.getMonth())
					// print current date
				//	str_buffer += "	<td bgcolor=\"#FFB6C1\" align=\"right\">";
				//else 
                if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// weekend days
					str_buffer += "	<td class=\"wke\">";
				else
					// print working days of current month
					str_buffer += "	<td class=\"wkd\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// print days of current month
					str_buffer += "<a href=\"javascript:"+setcombos(str_target,dt_current_day)+"i_months; window.close();\">"+
                    dt_current_day.getDate()+"</a></td>\n";
				else 
					// print days of other months
					/* Change to adjust the combo boxes instead of changing the textboxes */
					str_buffer += ""+
                    dt_current_day.getDate()+"</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" +
		"</body>\n" +
		"</html>\n";

	var vWinCal = window.open("", "Calendar", 
		"width=200,height=250,status=no,resizable=yes,top=200,left=200");
	vWinCal.opener = self;
	vWinCal.focus();
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
}

function dt2dtstr4 (dt_datetime) {
	return (new String (
			(dt_datetime.getFullYear()+"/"+(dt_datetime.getMonth()+1)+"/"+dt_datetime.getDate())));
}

function datepicker_init(langcode) {
	switch (langcode) {
		case 'en':
			arr_months = "January,February,March,April,May,June,July,August,September,October,November,December".split(",");
			week_days = "Su,Mo,Tu,We,Th,Fr,Sa".split(",");
			n_weekstart = 6;
            str_styles = str_dflt_style;
		break;

		case 'ar':
			arr_months = "&#1610;&#1606;&#1575;&#1610;&#1585;,&#1601;&#1576;&#1585;&#1575;&#1610;&#1585;,&#1605;&#1575;&#1585;&#1587;,&#1575;&#1576;&#1585;&#1610;&#1604;,&#1605;&#1575;&#1610;&#1608;,&#1610;&#1608;&#1606;&#1610;&#1608;,&#1610;&#1608;&#1604;&#1610;&#1608;,&#1571;&#1594;&#1587;&#1591;&#1587;,&#1587;&#1576;&#1578;&#1605;&#1576;&#1585;,&#1606;&#1608;&#1601;&#1605;&#1576;&#1585;,&#1571;&#1603;&#1578;&#1608;&#1576;&#1585;,&#1583;&#1610;&#1587;&#1605;&#1576;&#1585;".split(",");
			week_days = "Su,Mo,Tu,We,Th,Fr,Sa".split(",");
			n_weekstart = 6; 
            str_styles="\n.clsOTable{direction:rtl;}"+
            "\n.wke,.wkd{background:white;text-align:right;font-size:9pt;}";
            
		break;

		default:
		break;
	}
}

// Set the combo boxes to the picked values
function setcombos(str_control,dt_datetime) {
	
	return ctrl_day + ".selectedIndex="+ (dt_datetime.getDate()-1) +";" + ctrl_month + ".selectedIndex=";
}

function changeDates(dp, rt) {
    var dm=gE(dp+"_Month"); var rm=gE(rt+"_Month");
    var dln = dm.value.length;
    var mth = dm.value.substring(dln-2,dln);  
    
    rm.selectedIndex = dm.selectedIndex;
    
    var day = parseInt(gE(dp+"_Day").value,10) + 3;
    var d = new Date(dm.value + "/" + day);
    
    gE(rt+"_Day").selectedIndex=d.getDate()-1;
    
    if (d.getMonth()+1 > parseInt(mth,10)) {rm.selectedIndex += 1;}
}

