/***
	Class "DateForm"
	
Set TabStop=3 to read this file the best way.

USAGE:

1. Constructor DateForm()
	var a = DateForm("a");					//	Current date is default
	var b = DateForm("b","01/01/1999");	//	Jan 1, 1999 is default
	var c = DateForm("c","Apr 1, 2000");	//	Apr 1, 2000 is default
	var d = DateForm("d", new Date(1999,0,1));	//	Jan 1, 1999 is default
	var e = DateForm("e", new Date(1999,0,1), "def");
	
	The first parameter must be presented - it is the name of the variable the new
	object is going to be assigned to.
	
	The second parameter is a default date. When ommited the current date is default.
	The date could be set in one of the form showed above.
	
	The third parameter is a default class for form HTML elements. 
	When ommited class is default for form elements.

2. Methods getMonthField() and getDateField()
	Both methods have the only parameter - name of the "SELECT" tag. They return
	the character string - complete select tag. You may then insert it into
	document with "document.write" method.
	
3. Method getYearField()
   This method is absolutely same as two previous ones but it has two aditional
	parameters - first and last years to be shown. By default it shows ten
	years before and ten years after the default date (see constructior description above).
	
All other methods are internal and should not be performed directly.	 
		
***/

function DateForm(selfName,initialDate, className) {
	if (!selfName) alert("First Parameter of DateForm must be set");
	this.name = selfName;
	if (!initialDate) initialDate = new Date();
	else if (typeof(initialDate)=="string") initialDate = new Date(initialDate);
	if (!className) className = "";
	else if (typeof(className) != "string") className = ""+className;
	this.curMonth = initialDate.getMonth();
	this.curDate = initialDate.getDate();
	this.curYear = initialDate.getFullYear();
	this.curHour = initialDate.getHours();
	this.curMinutes = initialDate.getMinutes();
	this.getMonthField = _dateFormGetMonthField;
	this.getDateField = _dateFormGetDateField;
	this.getYearField = _dateFormGetYearField;
	this.getHourField = _dateFormGetHourField;
	this.getMinutesField = _dateFormGetMinutesField;
	this.correctYear =  _dateFormCorrectYear;
	this.correctDays =  _dateFormCorrectDays;
	this.createCompleteForm = _df_createCompleteForm;
	this.className = className;
}

function getTwoDigits(x) {
	return (x>9) ? ""+x : "0"+x;
}

function _dateFormGetMonthField(selName,tb) {
	var ___internal___class___string___ = (this.className == "")?"":" class='"+this.className+"'";
	if (! selName) selName = "myMonth";
	this.monthSelectName = selName;
	var res = "<SELECT name='"+this.monthSelectName+"' onchange='"+this.name+".correctDays(form)'";
	res += ___internal___class___string___;
	if (tb) res += " TABINDEX="+tb;
	res += ">\n";
	for (var i=0; i<12; i++) {
		res += "<OPTION VALUE='"+(i+1)+"'";
		if (i==this.curMonth) res += " SELECTED";
		res += ">"+_internal_monthNames[i]+"\n";
	}
	return res+"</SELECT>";
}

function _dateFormGetDateField(selName,tb) {
	var ___internal___class___string___ = (this.className == "")?"":" class='"+this.className+"'";
	if (! selName) selName = "myDate";
	this.dateSelectName = selName;
	var limit = _internal_in_month(this.curYear,this.curMonth);
	var res = "<SELECT name='"+this.dateSelectName+"'";
	res += ___internal___class___string___;
	if (tb) res += " TABINDEX="+tb;
	res += ">\n";
//	alert("this.curDate="+this.curDate+"\nthis.curYear="+this.curYear+"\nthis.curMonth="+this.curMonth);
	for (var i=1; i<=limit; i++) {
		res += "<OPTION VALUE='"+i+"'";
		if (i==this.curDate) res += " SELECTED";
		res += ">"+i+"\n";
	}
	return res+"</SELECT>";
}

function _dateFormGetYearField(selName,yearFirst,yearLast,tb) {
	var ___internal___class___string___ = (this.className == "")?"":" class='"+this.className+"'";
	if (! selName) selName = "myYear";
	this.yearSelectName = selName;
	if (!yearFirst) yearFirst = this.curYear-10;
	var yf = parseInt(yearFirst);
	if (isNaN(yf)) this.curYear-10;
	if (!yearLast) yearhLast = this.curYear+10;
	var yl = parseInt(yearLast);
	if (isNaN(yl)) yl = this.curYear+10;
	var res = "<SELECT name='"+this.yearSelectName+"' onchange='"+this.name+".correctYear(form)'";
	if (tb) res += " TABINDEX="+tb;
	res += ___internal___class___string___;
	res += ">\n";
	for (var i=yf; i<=yl; i++) {
		res += "<OPTION VALUE="+i;
		if (i==this.curYear) res += " SELECTED";
		res += ">"+i+"\n";
	}
	return res+"</SELECT>";
}

function _dateFormCorrectDays(form) {
	var daySelect = form[this.dateSelectName];
	var yearSelect = form[this.yearSelectName];
	var curD = daySelect.selectedIndex+1;
	var curY = parseInt(yearSelect.options[yearSelect.selectedIndex].value);
	var daysInMonth = _internal_in_month(curY,form[this.monthSelectName].selectedIndex);
	if (curD > daysInMonth) curD = 1;
	for (var i=1; i<=daysInMonth; i++) 
		daySelect.options[i-1] = new Option(getTwoDigits(i),""+i,false,i==curD);		
	daySelect.options.length = daysInMonth;
	daySelect.selectedIndex = curD-1;
}

function _dateFormCorrectYear(form) {
	var yearSelect = form[this.yearSelectName];
	var newYear = parseInt(yearSelect.options[yearSelect.selectedIndex].value);
	if (
			(_internal_isLeap(newYear) && _internal_isLeap(this.curYear)) ||
			(!_internal_isLeap(newYear) && !_internal_isLeap(this.curYear))
		) return;
	this.curYear = newYear;		
	this.correctDays(form);
}

function _dateFormGetHourField(selName,tb) {
	var ___internal___class___string___ = (this.className == "")?"":" class='"+this.className+"' ";
	if (! selName) selName = "myHour";
	this.hourSelectName = selName;
	var res = "<SELECT name='"+this.hourSelectName+"'";
	res += ___internal___class___string___;
	if (tb) res += " TABINDEX="+tb;
	res += ">\n";
	for (var i=0; i<24; i++) {
		res += "<OPTION VALUE='"+i+"'";
		if (i == this.curHour) res += " SELECTED";
		res += ">"+getTwoDigits(i)+"\n";
	}
	return res+"</SELECT>";
}

function _dateFormGetMinutesField(selName,tb) {
	var ___internal___class___string___ = (this.className == "")?"":" class='"+this.className+"' ";
	if (! selName) selName = "myMinutes";
	this.minutesSelectName = selName;
	var res = "<SELECT name='"+this.minutesSelectName+"'";
	res += ___internal___class___string___;
	if (tb) res += " TABINDEX="+tb;
	res += ">\n";
	for (var i=0; i<60; i++) {
		res += "<OPTION VALUE='"+i+"'";
		if (i == this.curMinutes) res += " SELECTED";
		res += ">"+getTwoDigits(i)+"\n";
	}
	return res+"</SELECT>";
}

var _internal_monthNames = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var _internal_days = [31,28,31,30,31,30,31,31,30,31,30,31];

function _internal_isLeap(year) {
	return ((year%4 == 0) && (year%100 != 0)) || (year%400 == 0);
}

function _internal_in_month (year,month) {
	if (month==1) return (_internal_isLeap(year))?29:28;
	return _internal_days[month];
}

function _df_createCompleteForm(mName,dName,yName,first,last) {
	return this.getMonthField(mName)+this.getDateField(dName)+this.getYearField(yName,first,last);
}