
/*
<script type="text/javascript">
	//INIT_DATE YYYY-MM-DD WITHOUT LEADING 0
	create_date_html('day','month','year', 'yyyy-mm-dd');
</script>
*/
var default_day_text = '-day-';
var default_month_text = '-month-';
var default_year_text = '-year-';
var style_text = ''
var no_years= 100;
var today= new Date();
var currentYear = today.getFullYear();
var year_last = today.getFullYear()-8;

function create_date_html(day_id, month_id, year_id, init_date) {
/*INIT_DATE = YYYY-MM-DD WITHOUT LEADING 0*/

	var out = return_date_html(day_id, month_id, year_id, init_date);

	document.write(out);
	document.close;

	populate(day_id, month_id, year_id, init_date);
}

function create_monthDayDate_html(day_id, month_id, init_date) {
/*INIT_DATE = YYYY-MM-DD WITHOUT LEADING 0*/

	var out = return_dayMonth_html(day_id, month_id);

	document.write(out);
	document.close;

//	populateMonthDay(day_id, month_id, init_date);
}

function return_date_html(day_id, month_id, year_id, init_date) {
/*INIT_DATE = YYYY-MM-DD WITHOUT LEADING 0*/
	var out_day = '<select style="width:60px;" onMouseWheel="javascript:if(!MoveFocus(this)) event.returnValue=false;" '+style_text+'  name="'+day_id+'" id="'+day_id+'"><option value=0>'+default_day_text+'<\/option><\/select>';
	var out_month =  '<select style="width:90px;" onMouseWheel="javascript:if(!MoveFocus(this)) event.returnValue=false;" '+style_text+' name="'+month_id+'" id="'+month_id+'" onChange = "populate2(\''+day_id+'\',\''+month_id+'\',\''+year_id+'\')">' +
		  	'<option value="0">'+default_month_text+'<\/option>' +
		  	'<option value="1">January<\/option>' +
		  	'<option value="2">February<\/option>' +
		  	'<option value="3">March<\/option>' +
		  	'<option value="4">April<\/option>' +
		  	'<option value="5">May<\/option>' +
		  	'<option value="6">June<\/option>' +
		  	'<option value="7">July<\/option>' +
		  	'<option value="8">August<\/option>' +
		  	'<option value="9">September<\/option>' +
		  	'<option value="10">October<\/option>' +
		  	'<option value="11">November<\/option>' +
			'<option value="12">December<\/option>' +
		'<\/select>';
	out_year = '<select style="width:63px;" onMouseWheel="javascript:if(!MoveFocus(this)) event.returnValue=false;" '+style_text+' name="'+year_id+'" id="'+year_id+'" onChange = "populate2(\''+day_id+'\',\''+month_id+'\',\''+year_id+'\')"><option value=0>'+default_year_text+'<\/option><\/select>';

	var out = out_year + ' ' + out_month + ' ' + out_day;

	return out;
}

function return_dayMonth_html(day_id, month_id) {
	var out_day   = '<select onMouseWheel="javascript:if(!MoveFocus(this)) event.returnValue=false;" '+style_text+'  name="'+day_id+'" id="'+day_id+'"><option value=0>'+default_day_text+'<\/option><\/select>';
	var out_month = '<select onMouseWheel="javascript:if(!MoveFocus(this)) event.returnValue=false;" '+style_text+' name="'+month_id+'" id="'+month_id+'" onChange = "populateDayMonth(\''+day_id+'\',\''+month_id+'\')">' +
		  	'<option value="0">'+default_month_text+'<\/option>' +
		  	'<option value="1">January<\/option>' +
		  	'<option value="2">February<\/option>' +
		  	'<option value="3">March<\/option>' +
		  	'<option value="4">April<\/option>' +
		  	'<option value="5">May<\/option>' +
		  	'<option value="6">June<\/option>' +
		  	'<option value="7">July<\/option>' +
		  	'<option value="8">August<\/option>' +
		  	'<option value="9">September<\/option>' +
		  	'<option value="10">October<\/option>' +
		  	'<option value="11">November<\/option>' +
			'<option value="12">December<\/option>' +
		'<\/select>';

	var out = out_month + '/' + out_day;

	return out;
}


function populate(day_id, month_id, year_id, init_date) {
/*INIT_DATE = YYYY-MM-DD WITHOUT LEADING 0*/

	var day   = document.getElementById(day_id);
	var month = document.getElementById(month_id);
	var year  = document.getElementById(year_id);

	var date_i = init_date.split("-");

	if (init_date != '' && init_date != 0) {
		var year_sel = parseInt(date_i[0]);
		var month_sel = parseInt(date_i[1]);
		var day_sel = parseInt(date_i[2]);
	}

/*populate years*/
	for (i=1,j=year_last; i <=no_years ; i++, j--) {
		var y = String(j);
		year.options[i] = new Option(y,y);
	}

/*init date*/
	if (init_date != '' && init_date != 0) {
		for (i=0;i<=no_years;i++) {
			var y=0;
			y = year.options[i].value;
			if(y == year_sel) {
				year.options[i].selected=true;
				break;
			}
		}

		for (i=0;i<=12;i++) {
			if(i == month_sel) {
				month.options[i].selected=true;
				break;
			}
		}

		populate2(day_id, month_id, year_id);

		if (day.length > 1) {
			for (i=0; i <=31 ; i++) {
				var d = 0;
				d = day.options[i].value;
				if(d == day_sel){
					day.options[i].selected=true;
					break;
				}
			}
		}
	}
}


function populateMonthDay(day_id, month_id, init_date) {
/*INIT_DATE = MM-DD WITHOUT LEADING 0*/

	var day   = document.getElementById(day_id);
	var month = document.getElementById(month_id);
	var year  = currentYear;

	var date_i = init_date.split("-");

	if (init_date != '' && init_date != 0) {
		var month_sel = parseInt(date_i[0]);
		var day_sel = parseInt(date_i[1]);
	}

/*init date*/
	if (init_date != '' && init_date != 0) {

		for (i=0;i<=12;i++) {
			if(i == month_sel) {
				month.options[i].selected=true;
				break;
			}
		}

		populate2yearValue(day_id, month_id, year);

		if (day.length > 1) {
			for (i=0; i <=31 ; i++) {
				var d = 0;
				d = day.options[i].value;
				if (d == day_sel) {
					day.options[i].selected=true;
					break;
				}
			}
		}
	}
}

function populate2(day_id, month_id, year_id) {
	var number_of_days=0;
	var day=document.getElementById(day_id);
	var month=document.getElementById(month_id);
	var year=document.getElementById(year_id);

	if (month.options[0].selected || year.value == 0) {
		number_of_days = 0;
	} else if (month.options[2].selected) {
		if (year.value % 4 == 0)  {
			number_of_days = 29
		} else {
			number_of_days=28;
		}
	} else if (month.options[4].selected||month.options[6].selected||month.options[9].selected||month.options[11].selected) {
		number_of_days=30;
	} else {
		number_of_days=31;
	}

	while(day.childNodes.length > 0) {
		day.removeChild(day.childNodes[0]);
	}

	day.options[0] = new Option(default_day_text,0);

	if (number_of_days != 0) {
		for (i=1; i <=number_of_days ; i++) {
			var x= String(i);
			day.options[i] = new Option(x,x);
		}
	} else {
		for(var i=1;i<=31;i++) {
			day.options[i] = null;
		}
	}
}

function populate2yearValue(day_id, month_id, year_value) {
	var number_of_days=0;
	var day=document.getElementById(day_id);
	var month=document.getElementById(month_id);
	var year = year_value;

	if (month.options[0].selected || year == 0) {
		number_of_days = 0;
	} else if (month.options[2].selected) {
		if (year % 4 == 0)  {
			number_of_days = 29
		} else {
			number_of_days=28;
		}
	} else if (month.options[4].selected||month.options[6].selected||month.options[9].selected||month.options[11].selected) {
		number_of_days=30;
	} else {
		number_of_days=31;
	}

	while(day.childNodes.length > 0) {
		day.removeChild(day.childNodes[0]);
	}

	day.options[0] = new Option(default_day_text,0);

	if (number_of_days != 0) {
		for (i=1; i <=number_of_days ; i++) {
			var x= String(i);
			day.options[i] = new Option(x,x);
		}
	} else {
		for(var i=1;i<=31;i++) {
			day.options[i] = null;
		}
	}
}

function populateDayMonth(day_id, month_id) {
	var number_of_days=0;
	var day=document.getElementById(day_id);
	var month=document.getElementById(month_id);


	if (month.options[0].selected) {
		number_of_days = 0;
	} else if (month.options[2].selected) {
			number_of_days=28;
	} else if (month.options[4].selected||month.options[6].selected||month.options[9].selected||month.options[11].selected) {
		number_of_days=30;
	} else {
		number_of_days=31;
	}

	while(day.childNodes.length > 0) {
		day.removeChild(day.childNodes[0]);
	}

	day.options[0] = new Option(default_day_text,0);

	if (number_of_days != 0) {
		for (i=1; i <=number_of_days ; i++) {
			var x= String(i);
			day.options[i] = new Option(x,x);
		}
	} else {
		for(var i=1;i<=31;i++) {
			day.options[i] = null;
		}
	}
}

function MoveFocus(myItem)
{
	var found = false;
	var field = document.forms[0];
	for( i = 0; i < field.length; i++ ){
		if( field.elements[i].id == myItem.id ){
			for( j=i+1; j < field.length; j++ ){
				if(
					document.forms[0].elements[j].type == "text" ){
					document.forms[0].elements[j].focus();
					found = true;
					break;
				}
			}
		break;}}

	if(!found)self.focus();return false;
}
