//var monthtext=['January','February','March','April','May','June','July','August','September','October','November','December'];

var june = [14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
var july = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
var aug = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21];

function populatedayfield(dayfield, month)
{
	var thisDay = dayfield.value;
	
	for(var i=0; i<31; i++)
		dayfield.remove(0)
          
          var j = 0;                                     
	for (var i in month)      
    {              
                               
		dayfield.options[j]=new Option(month[i], month[i])  
        j++;
    }
	
	// If last month is selected
	if(document.getElementById("monthdropdown").selectedIndex == 2)
	{
		dayfield.remove(30)
	}                 
}      

function populatedropdown(dayfield, monthfield, yearfield)
{
	var today=new Date()
	var dayfield=document.getElementById(dayfield)
	var monthfield=document.getElementById(monthfield)
	var yearfield=document.getElementById(yearfield)
	var hourfield=document.getElementById("hourdropdown")
	var minutefield=document.getElementById("minutedropdown")
	
	populatedayfield(document.getElementById("daydropdown"), june);
	populatedayfield(document.getElementById("daydropdown2"), june);
	
	for (var m=0; m<3; m++)
		monthfield.options[m]=new Option(monthtext[m], m+1)

//	monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], today.getMonth()+1, true, true) //select today's month

	monthfield.options[0].selected = true;
	
	var thisyear=today.getFullYear()
	var baseyear = thisyear
	for (var y=0; y<2; y++)
	{
		yearfield.options[y]=new Option(thisyear, thisyear)
		thisyear+=1
	}
	

	var corr_month = month-6;
}

function updateday1()
{
	var elem = document.getElementById("monthdropdown");
	
	if(elem.selectedIndex > document.getElementById("monthdropdown2").selectedIndex)
	{
		document.getElementById("monthdropdown2").options[elem.selectedIndex].selected = true;
	}
	updatedaycount(elem);
    updatedaycount(document.getElementById("monthdropdow2"));
}

function updateday2()
{	
    updatedaycount(document.getElementById("monthdropdown")); 
	updatedaycount(document.getElementById("monthdropdown2"));
	
	check_dep_day();    
}


function updatedaycount(monthfield)
{		
    check_dep_day();
    
	var value1 = document.getElementById("monthdropdown").selectedIndex;
    var value2 = document.getElementById("monthdropdown2").selectedIndex;     
    
    if(value1 <= 0) month1 = june;
    else if(value1 == 1) month1 = july;
    else if(value1 == 2) month1 = aug;      
    
    if(value2 <= 0) month2 = june;
    else if(value2 == 1) month2 = july;
    else if(value2 == 2) month2 = aug;                             
	
	populatedayfield(document.getElementById("daydropdown"), month1);
    populatedayfield(document.getElementById("daydropdown2"), month2);
}

function check_dep_day()
{
	var month1 = document.getElementById("monthdropdown").selectedIndex;
	var month2 = document.getElementById("monthdropdown2").selectedIndex;
	
	var day1 = document.getElementById("daydropdown").selectedIndex;
	var day2 = document.getElementById("daydropdown2").selectedIndex;
	
	if(month1 >= month2 && day2 <= day1)
	{	
		if(day1 >= document.getElementById("daydropdown2").options.length-1)
		{		
			document.getElementById("monthdropdown2").options[month2+1].selected = true;
			document.getElementById("daydropdown2").options[0].selected = true;
		}
		else
		{
			document.getElementById("daydropdown2").options[day1+1].selected = true;
		}
	}
	
	if(month1 > month2)
	{
		document.getElementById("monthdropdown").options[month2].selected = true;
	}
}

function check_dep_year()
{
	document.getElementById("yeardropdown2").options[document.getElementById("yeardropdown").selectedIndex].selected = true;
}

function check_dep_year2()
{
	document.getElementById("yeardropdown").options[document.getElementById("yeardropdown2").selectedIndex].selected = true;
}

//populatedropdown(id_of_day_select, id_of_month_select, id_of_year_select)
window.onload=function()
{                        
	populatedropdown("daydropdown2", "monthdropdown2", "yeardropdown2");
	populatedropdown("daydropdown", "monthdropdown", "yeardropdown");     
	check_dep_day()
	
}
