// JavaScript Document

var day;
var month;
var year;
var cal_box;
var nummonths;
var monthfrom;
var monthto;


<!--by adding a listener we don't need to add the onclick event to the individual selects-->
if (document.getElementById && document.getElementsByTagName) {
	if (window.addEventListener)
	{
		window.addEventListener('load', selects, false);
	}
	else if (window.attachEvent)
	{
		window.attachEvent('onload', selects);
	}
}
function selects()
{
	//document.getElementById("arrivalDate").onchange = dow;
	//document.getElementById("arrivalMonth").onchange = dow;
	//document.getElementById("arrivalYear").onchange = dow;
	document.onclick = check;
}

function check(e){ 
	if(e)
	{
		var target = e.target; 
	}
	else
	{
		var target = event.srcElement; 
	}
	var obj = document.getElementById('calimage');
	var left = document.getElementById('left');
	var right = document.getElementById('right');
	var lefty = document.getElementById('lefty');
	var righty = document.getElementById('righty');

	if(target==obj || target == left || target == right|| target == lefty || target == righty)
	{
		document.getElementById('cal').style.display='block';
		norm();	
	} 
	else
	{
		document.getElementById('cal').style.display='none';
	}
} 

function dow()
{
	get_values('eql');
	var d=new Date(year,month,day);
	//document.getElementById("dow").innerHTML = weekday[d.getDay()];
}





//number of days in month
function days_in_month(year, month)
{
     return 32 - new Date(year, month, 32).getDate();
}

//first day of month
function day_of_month(year, month, layer)
{
     return new Date(year, month, 1).getDay();
}

//populate boxes on calendar
function populate_boxes(year, month, day, layer)
{
	//N.B. month, 0=January ... 11=December
	var monthtxt;
	switch (month)
	{
		case 0: monthtxt = arrmonthtxt[0]; break;
		case 1: monthtxt = arrmonthtxt[1]; break;
		case 2: monthtxt = arrmonthtxt[2]; break;
		case 3: monthtxt = arrmonthtxt[3]; break;
		case 4: monthtxt = arrmonthtxt[4]; break;
		case 5: monthtxt = arrmonthtxt[5]; break;
		case 6: monthtxt = arrmonthtxt[6]; break;
		case 7: monthtxt = arrmonthtxt[7]; break;
		case 8: monthtxt = arrmonthtxt[8]; break;
		case 9: monthtxt = arrmonthtxt[9]; break;
		case 10: monthtxt = arrmonthtxt[10]; break;
		case 11: monthtxt = arrmonthtxt[11]; break;
		default : "Jul";
	}
	
	//get number of days in month, so we know when to start end blanks
	var num_days_in_month = days_in_month(year, month);
	
	//get first day of month 0=Sunday, 6=Saturday
	var first_day_of_month = day_of_month(year, month);
	
	layer[2].innerHTML = monthtxt+" "+year;
	
	var cnt = 0;
	for(var i=0;i<42;i++)//10 is number of li to create and fill
	{
		var li = document.createElement("li");
		var newli = document.getElementById('ulcal').appendChild(li);
		
		if(i<first_day_of_month || cnt==num_days_in_month)//show blanks
		{
			newli.className="blank";
			newli.innerHTML="-";
		}
		else //show numbers
		{
			cnt=cnt+1;
			if(cnt==day)
			{
				newli.className = "select";
			}
			newli.innerHTML="<a href='javascript:selectday("+cnt+", this)'>"+cnt+"</a>";
		}
	}

}

function selectday(selday, t)
{
	for(var i=0;i<cal_box.length;i++)
	{
		if(cal_box[i].className == "select")
		{
			cal_box[i].className = "";
		}	
	}
	//document.getElementById('arrivalDate').selectedIndex = selday-1;
	document.getElementById('arrivalDate').selectedIndex = selday;
	//dow();
	hide('cal');
}

function hide(theelement) {
	document.getElementById(theelement).style.display = "none";
}

function unpopulate_boxes(layer)
{
	var cnt = 0;
	for(var i=layer.length-1;i>=12;i--)
	{
		document.getElementById('ulcal').removeChild(layer[i]);
	}
}

function get_values(action, amt)
{
	day = document.getElementById('arrivalDate').options[document.getElementById('arrivalDate').selectedIndex].text;
	//alert(document.getElementById('arrivalMonth').options[document.getElementById('arrivalMonth').selectedIndex].value);
	month = parseInt(Number(document.getElementById('arrivalMonth').options[document.getElementById('arrivalMonth').selectedIndex].value))-1;
	if(typeof(document.getElementById('arrivalYear').selectedIndex) != 'undefined' && typeof(document.getElementById('arrivalYear').options) != 'undefined') {
		year = document.getElementById('arrivalYear').options[document.getElementById('arrivalYear').selectedIndex].text;
	} else {
		year = document.getElementById('selyear').options[document.getElementById('selyear').selectedIndex].text;
	}
	cal_box = document.getElementById('cal').getElementsByTagName('li');
	nummonths = parseInt(document.getElementById('arrivalMonth').options.length);
	monthfrom = parseInt(document.getElementById('arrivalMonth').options[0].value)-1;
	monthto = parseInt(document.getElementById('arrivalMonth').options[nummonths-1].value)-1;
	if(typeof(document.getElementById('arrivalYear').options) != 'undefined') {
		numyears = parseInt(document.getElementById('arrivalYear').options.length);
		yearfrom = document.getElementById('arrivalYear').options[0].text;
		yearto = document.getElementById('arrivalYear').options[numyears-1].text;
	} else {
		numyears = parseInt(document.getElementById('selyear').options.length);
		yearfrom = document.getElementById('selyear').options[0].text;
		yearto = document.getElementById('selyear').options[numyears-1].text;
	}
	
	//now unpopulate the calendar lists in preparartion for a refresh
	unpopulate_boxes(cal_box);
		if(action=="add"&&month!=-1)
		{
			if(month==monthto&&year<yearto)
			{
				document.getElementById('arrivalMonth').selectedIndex = 0;
				document.getElementById('arrivalYear').selectedIndex = document.getElementById('arrivalYear').selectedIndex+1;
				populate_boxes(parseInt(year)+1, monthfrom, day, cal_box);
			}
			else if(month<monthto)
			{
				document.getElementById('arrivalMonth').selectedIndex = (parseInt(month)+1)-parseInt(monthfrom);
				populate_boxes(year, month+1, day, cal_box);
			}
			else {populate_boxes(year, month, day, cal_box);}
		}
		else if (action=="sub"&&month>monthfrom&&month != -1)
		{
			
			if(month==monthfrom&&year>yearfrom)
			{
				document.getElementById('arrivalMonth').selectedIndex = nummonths-1;
				document.getElementById('arrivalYear').selectedIndex = document.getElementById('arrivalYear').selectedIndex-1;
				populate_boxes(parseInt(year)-1, monthto, day, cal_box);
			}
			else if(month>monthfrom)
			{
				document.getElementById('arrivalMonth').selectedIndex = (parseInt(month)-1)-parseInt(monthfrom);
				populate_boxes(year, month-1, day, cal_box);
			}
			else {populate_boxes(year, month, day, cal_box);}
		}
		else if (action=="sub"&&month==monthfrom&&month != -1) {
			if(year==yearto) {
				document.getElementById('arrivalMonth').selectedIndex = nummonths-1;
				document.getElementById('arrivalYear').selectedIndex = document.getElementById('arrivalYear').selectedIndex-1;
				populate_boxes(year-1, month, day, cal_box);
			}
		}
		else if(action=="addyear")
		{
			if(year != yearto) {
				document.getElementById('arrivalYear').selectedIndex = document.getElementById('arrivalYear').selectedIndex+1;
				populate_boxes(year+1, month, day, cal_box);
			} else {
				document.getElementById('arrivalMonth').selectedIndex = nummonths-1;
				populate_boxes(year, monthto, day, cal_box);
			}
		}
		else if(action=="subyear")
		{
			if(year != yearfrom) {
				if(typeof(document.getElementById('arrivalYear').options) != 'undefined') {
					document.getElementById('arrivalYear').selectedIndex = document.getElementById('arrivalYear').selectedIndex-1;
					populate_boxes(year-1, month, day, cal_box);
				} else {
					document.getElementById('arrivalMonth').selectedIndex = 0;
					populate_boxes(year, monthfrom, day, cal_box);
				}
			} 
		}
		else
		{
			document.getElementById('arrivalMonth').selectedIndex = document.getElementById('arrivalMonth').selectedIndex = parseInt(month)-parseInt(monthfrom);
			//alert(month + " & " + monthfrom);
			populate_boxes(year, month, day, cal_box);
		}
}
function inc()
{
	get_values('add');
}
function dec()
{
	get_values('sub');
}
function incyear()
{
	get_values('addyear');
}
function decyear()
{
	get_values('subyear');
}
function norm()
{
	get_values('eql');
}

