// Javascript Date Selector
// by Warren Brown , warren@scully.xfiles.za.org (03/01/2004 Radiokop South Africa)
// Script to place Month/day/year onto a web page, leap year enabled

var date_arr = new Array;
var days_arr = new Array;

date_arr[0]=new Option("Enero",31);
date_arr[1]=new Option("Febrero",28);
date_arr[2]=new Option("Marzo",31);
date_arr[3]=new Option("Abril",30);
date_arr[4]=new Option("Mayo",31);
date_arr[5]=new Option("Junio",30);
date_arr[6]=new Option("Julio",31);
date_arr[7]=new Option("Agosto",30);
date_arr[8]=new Option("Setiembre",30);
date_arr[9]=new Option("Octubre",31);
date_arr[10]=new Option("Noviembre",31);
date_arr[11]=new Option("Diciembre",30);

function fill_select(f, dia, mes){
	var op_mes;
	document.writeln("<select name=\"days\"></select>");
	document.writeln("<select name=\"months\" onchange=\"update_days('"+f+"','"+dia+"');\">");
	for (x = 0; x < 12; x++) {
		if (mes == (x + 1))
			op_mes = "<option value=\"" + (x + 1) + "\" selected=\"selected\">";
		else
			op_mes = "<option value=\"" + (x + 1) + "\">";
		
		document.writeln(op_mes + date_arr[x].text + "</option>");
	}
	document.writeln("</select>");
	selection=document.getElementById(f).months[document.getElementById(f).months.selectedIndex].value;
}

function update_days(f, dia){
	var temp = document.getElementById(f).days.selectedIndex; 
	for(x=days_arr.length;x>0;x--){
		days_arr[x]=null;
		document.getElementById(f).days.options[x]=null;
	}
	selection=parseInt(date_arr[document.getElementById(f).months.selectedIndex].value);
	ret_val = 0;
	if(selection == 28){
		year=parseInt(document.getElementById(f).years.options[document.getElementById(f).years.selectedIndex].value);
		if (year % 4 == 0 && year % 100 != 0 ) ret_val=1;
		else
			if (year % 400 == 0)  ret_val=1;
			else
				ret_val=0;
	}
	selection = selection + ret_val        
	for(x=1;x < selection+1;x++){
		days_arr[x-1]=new Option(x);            
		document.getElementById(f).days.options[x-1]=days_arr[x-1];
	}
 	if(selection <= temp){
		temp=-1;
	}
	if (dia == -1) {
		if (temp == -1) {
			document.getElementById(f).days.options[0].selected = true;
		}else {
			document.getElementById(f).days.options[temp].selected = true;
		}
	}else {
		document.getElementById(f).days.options[dia - 1].selected = true;
	}
}       
function year_install(f,inicio,fin, dia, anho){
	var op_anho;
	document.writeln("<select name=\"years\" onchange=\"update_days('"+f+"','"+dia+"');\">")
	for(x=inicio;x>fin;x--){
		if(anho == x)
			op_anho = "<option value=\""+x+"\" selected=\"selected\">";
		else
			op_anho = "<option value=\""+x+"\">";
		document.writeln(op_anho+x+"</option>");
	}
	document.writeln("</select>");
	update_days(f, dia);
}
