// JavaScript Document
/* Javascript Date Selector
   by Warren Brown (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("January",31);
date_arr[1]=new Option("February",28);
date_arr[2]=new Option("March",31);
date_arr[3]=new Option("April",30);
date_arr[4]=new Option("May",31);
date_arr[5]=new Option("June",30);
date_arr[6]=new Option("July",31);
date_arr[7]=new Option("August",30);
date_arr[8]=new Option("September",30);
date_arr[9]=new Option("October",31);
date_arr[10]=new Option("November",31);
date_arr[11]=new Option("December",30);

function fill_select(id)
{
		txt = "<SELECT id=\"months"+id+"\" onchange=\"update_days("+id+")\">";
        for(x=0;x<12;x++)
                txt += "<OPTION value=\""+date_arr[x].value+"\">"+date_arr[x].text;
        txt += "</SELECT><SELECT id=\"days"+id+"\"></SELECT>";
		return txt;
//        selection=f.months[f.months.selectedIndex].value;
}

function update_days(id)
{
        temp=document.getElementById("days"+id).selectedIndex;
        for(x=days_arr.length;x>0;x--)
        {
                days_arr[x]=null;
                document.getElementById("days"+id).options[x]=null;
         }
        selection=parseInt(document.getElementById("months"+id)[document.getElementById("months"+id).selectedIndex].value);
        ret_val = 0;
        //if(f.months[f.months.selectedIndex].value == 28)
		if(selection == 28)
        {
                year=parseInt(document.getElementById("years"+id).options[document.getElementById("years"+id).selectedIndex].value); //find year selected
                if (year % 4 != 0 || year % 100 == 0 )   //check if its a leap year
						ret_val=0;
                else
                        if (year % 400 == 0)  
								ret_val=1;
                        else
                                ret_val=1;
        }
        selection = selection + ret_val;
        for(x=1;x < selection+1;x++)

        {
                days_arr[x-1]=new Option(x);
                document.getElementById("days"+id).options[x-1]=days_arr[x-1];
        }
        if (temp == -1) document.getElementById("days"+id).options[0].selected=true;
        else
             document.getElementById("days"+id).options[temp].selected=true;
}
function year_install(id)
{
        txt = "<SELECT id=\"years"+id+"\" onchange=\"update_days("+id+")\">"
        for(x=2006;x<2012;x++) 
			txt += "<OPTION value=\""+x+"\">"+x;
        txt += "</SELECT>";
        
		return txt;
}

function select_date(id){
	oldvalue = document.getElementById(fields[0]+id).firstChild.getAttribute("oldvalue")
	if(oldvalue == "--Date--"){
		today = new Date();

		document.getElementById("months"+id).options[today.getMonth()].selected=true;
		document.getElementById("years"+id).options[today.getFullYear()-2006].selected=true;
		document.getElementById("days"+id).options[today.getDate()-1].selected=true;
	} else {
		tag = document.getElementById(fields[0]+id);
		longdate = tag.getAttribute('date');
		parsedDate = parseDate(longdate)
		date = parsedDate[0]
		month = parsedDate[1]
		year = parsedDate[2]		
		/*longdate = tag.getAttribute('date');
		date = longdate.substr(0,longdate.indexOf('/'))
		theRest  = longdate.substr(longdate.indexOf('/')+1, longdate.length)
		month = theRest.substr(0, theRest.indexOf('/'))
		year = theRest.substr(theRest.indexOf('/')+1, theRest.length)*/
		document.getElementById("months"+id).options[month-1].selected=true;
		document.getElementById("years"+id).options[year-2006].selected=true;
		document.getElementById("days"+id).options[date-1].selected=true;
		
	}

}

function parseDate (longdate){
		date = longdate.substr(0,longdate.indexOf('/'))
		theRest  = longdate.substr(longdate.indexOf('/')+1, longdate.length)
		month = theRest.substr(0, theRest.indexOf('/'))
		year = theRest.substr(theRest.indexOf('/')+1, theRest.length)
		return (new Array (date, month, year))
}