// calendar.js
// 2008/02/05  Rob Davis MSN BEET
// functions used by page calendar, drop downs etc.
// this file should stripped of comments and spaces automatically before deployment

// checks if selected date is in the future
// input obj: form object containing dropdowns
// output : boolean 
function futureDate(obj) {
	var selectObj = obj.getElementsByTagName("select");
	var sD = selectObj["day"].value;
	var sM = selectObj["month"].value;
	var sY = selectObj["year"].value;
	var dat = new Date();
	var cD = dat.getDate();
	var cM = dat.getMonth();
	var cY = dat.getYear();
	if (cY<1900) cY+=1900;
	if (sY>cY) return true ;
	if ((sY==cY) && (sM>cM)) return true ;
	if ((sY==cY) && (sM==cM) && (sD>cD)) return true ;
	return false ;
}

// setup calendars for today
// input: none, details are scraped from document.
// output: none
// Can be called multiple times
function setCalendar() {
	var today=new Date();
	var obj = document.getElementsByTagName("form");
	for (var i=0;i<obj.length;i++) {
		var selectObj = obj[i].getElementsByTagName("select");
		if (selectObj["month"]) {
			selectObj["month"].value=today.getMonth();
			removeAllList(selectObj["year"]);
			currentYear = today.getYear();
			if (currentYear<=1900) currentYear+=1900;
			for (var c=currentYear+1;c>=1900;c--) { addToList(selectObj["year"],c,c); }
			selectObj["year"].value=currentYear;
			updateDaysFrom(obj[i]);
			selectObj["day"].value=today.getDate();
			updateDaysFrom(obj[i]);
			// assign event handler
			if (selectObj["day"]) {
				selectObj["day"].onchange=buildCalURLFromDay;
			}
		}
	}
	oneShootZIndexFix();
}

// dynamic event handler for "day" changes
// input: none, event object supplied by browser is all that is needed
// output: none
function buildCalURLFromDay() {
	buildCalURL(event.srcElement.parentNode.parentNode);
}


// handler for submit link
// input: none, event object supplied by browser is all that is needed
// output: boolean
// will only continue if date is in the past that popup blocker is not interfering
function onclickCalHandler() {
	var obj=document.getElementById(event.srcElement.id);
	var objForm = document.getElementById(event.srcElement.id.replace("_submit",""));
	if (futureDate(objForm)) {
		alert(objForm['futureDateError'].value);
		return(false);
	}
	var wls=obj.href;
	var start=wls.indexOf("?d=")+3;
	d=wls.substring(start,wls.length);
	d=unescape(d);
	var whandle = window.open(d,null,'toolbar=0,location=no,directories=0,status=0,scrollbars=yes,resizable=1,width=1,height=1,top=0,left=0');
	if (!whandle) {
		alert(objForm['popupBlockerError'].value);
		return false;
	}
	return(false);
}


// builds an anchor object containing the form data
// input obj: form object containing dropdowns
// output : none
// is used to work around various browser restrictions for submitting forms that return an file.
function buildCalURL(obj) {
	var fieldsCon = new Array( 
						"day",
						"month",
						"year",
						"summary",
						"description",
						"language",
						"frequency",
						"alertdescription",
						"interval",
						"filename"
	)
	var calURL="";
	for (var i=0;i<fieldsCon.length-1;i++) {
		calURL+=fieldsCon[i]+"="+obj[fieldsCon[i]].value+"&";
	}
	calURL+=fieldsCon[i]+"="+obj[fieldsCon[fieldsCon.length-1]].value;
	calURL=obj.action+"?"+calURL;	//
	calURL=escape(calURL);
	calURL=calURL.replace(/\//g,'%2F');
	var aobj=document.getElementById(obj.id+"_submit");
	if (aobj) { 
		aobj.href=obj["downloadURL"].value+"?d="+calURL;
		aobj.onclick=onclickCalHandler;
	}
}

// update all calendars
// input obj: form object containing dropdowns
// output : none 
// if input is null then all forms are processed within the document.
function updateDays(obj) {
	if (obj) {
		updateDaysFrom(obj.parentNode);
	} else {
		obj = document.getElementsByTagName("form");
		for (var i=0;i<obj.length;i++) {
			if (obj.getElementsByTagName("select"))
				updateDaysFrom(obj[i]);
		}
	}
}

// update the number of month days for a given form object
// input obj: form object containing dropdowns
// output : none 
function updateDaysFrom(obj) {
	selectObj = obj.getElementsByTagName("select");
	currentDay = selectObj["day"].value;
	updateDaysSelect(selectObj["day"],selectObj["month"],selectObj["year"]);
}

// return the number of days for a given month and year
// input : month - integer representing Month value 0-11
// input : year - integer representing year
// output : negative number if month is not 0-11
 function getDays(month,year) {
	var dat = new Date();
	dat.setDate(1);
	dat.setMonth(month); // set after setDate(1) or very strange things start to happen
	dat.setYear(year); 
	var days=0;
	while (dat.getMonth()==month) {
		days++;
		dat = new Date(dat.setDate(days));
	}
	days--;
	return(days);
 }

// remove all items from list
// input : object supporting "remove" method
// output : none
function removeAllList(obj) {
	for(var i=obj.options.length-1;i>=0;i--)	{obj.remove(i);	}
}

// add single item to a list object
// input : select object
// output : none
function addToList(obj, value, text ) {
	var opt = document.createElement('option');
	opt.text = text;
	opt.value = value;
	obj.options.add(opt);
}
 
// update days based on month
// input : d - object for "day"
// input : m - object for "month"
// input : y - object for "year"
 // output : none
function updateDaysSelect(d,m,y) {
	var currentDay=d.value;
	removeAllList(d);
	if (y.value<1900) y.value+=1900;
	for (var i=0;i<getDays(m.value,y.value);i++) { addToList(d,i+1,i+1); }
	//preserve date where possible
	d.value=currentDay;
	if (d.value=="") {
		d.value=1;
	}
	buildCalURL(d.parentNode.parentNode);
}

// Fix browser bug, does not negatively affect other browsers
// input: none
// output: none
function oneShootZIndexFix() {
	var obj = document.getElementsByTagName("div");
	for (var i=0;i<obj.length;i++) {
		if(obj[i].className=="calendar_con_selection") {
			obj[i].style.backgroundcolor="#00f";
			obj[i].style.zIndex=9;
			i=obj.length;
		}
	}
	
}

// verifies file has loaded and been processed by JavaScript interpretor.
var calref1=0;