addLoadEvent("loadDropdowns");
function loadDropdowns(){
	var el = new Array();
	
	var form = document.getElementById("__fbform");
	
	el = getControls(form);
	
	for(var i = 0; i < el.length; i++){
		
		var o = new DateDropDown(el[i]);
		_controls.add(o);
		
	}
	
}

function getControls(el){
	var arr = new Array();
	var tmpArr;
	var nodes = el.childNodes;
	for(var i = 0; i < nodes.length; i++){
		if(nodes[i].tagName && nodes[i].tagName.toLowerCase() == "datedropdown")
			arr[arr.length] = nodes[i];
		else{
			tmpArr = getControls(nodes[i]);
			if(tmpArr)
				for(var j = 0; j < tmpArr.length; j++)
					arr[arr.length] = tmpArr[j];
		}
	}
	
	return arr;
}

function DateDropDown(el){
	this.type = "datedropdown";
	this.id = "";
	this.el = null;
	this.name = "";
	this.cssclass = "";
	this.style = "";
	this.output = "";
	this.startyear = 0;
	this.endyear = 0;
	this.showtoday = "false";
	
	this.load(el);
	
	if(this.showtoday == "true"){
		var today = new Date();
		this.render(today.getDate(), today.getMonth()+1, today.getFullYear());
	}
	else
		this.render(1, 1, this.startyear);
}

DateDropDown.prototype.load = function(el){
	this.el = el;
	this.id = el.attributes["id"].value;
	this.name = el.attributes["name"].value;
	if (el.attributes["style"])
		this.style = el.attributes["style"].value;
	this.cssclass = el.attributes["cssclass"].value;
	this.output = el.attributes["output"].value;
	this.startyear = el.attributes["startyear"].value;
	this.endyear = el.attributes["endyear"].value;
	this.showtoday = el.attributes["showtoday"].value;
}

DateDropDown.prototype.render = function(curday, curmonth, curyear){
	var thisHTML = "";
	var rowCount = 0;
	var val = "";
	
	
	var container = document.createElement("span");
	this.el.parentNode.insertBefore(container, this.el);
	
	thisHTML += "<input type=\"hidden\" id=\"" + this.id + "\" name=\"" + this.name + "\" value=\"\" />";
	
	thisHTML += "<select class=\"" + this.cssclass + "\" id=\"" + this.id + "_day\" onchange=\"javascript: ddd_changevalue('" + this.id + "', '" + this.output + "');\">";
	
	
	thisHTML += "</select>";
	
	thisHTML += "<select class=\"" + this.cssclass + "\" id=\"" + this.id + "_month\" onchange=\"javascript: ddd_writeDays('" + this.id + "');ddd_changevalue('" + this.id + "', '" + this.output + "');\">";
	
	for(var i = 1; i <= 12; i++)
		thisHTML += "<option value=\"" + i + "\"" + (curmonth == i ? " selected=\"selected\"" : "") + ">" + this.getMonth(i) + "</option>";
	
	thisHTML += "</select>";
	
	thisHTML += "<select class=\"" + this.cssclass + "\" id=\"" + this.id + "_year\" onchange=\"javascript: ddd_writeDays('" + this.id + "');ddd_changevalue('" + this.id + "', '" + this.output + "');\">";
	
	for(var i = this.startyear; i <= this.endyear; i++)
		thisHTML += "<option value=\"" + i + "\"" + (curyear == i ? " selected=\"selected\"" : "") + ">" + i + "</option>";
	
	thisHTML += "</select>";
		
	container.innerHTML = thisHTML;
	this.el.parentNode.removeChild(this.el);
	this.writeDays(curday);
	
	ddd_changevalue(this.id, this.output);
}

function ddd_writeDays(id){
	_controls.getControlById(id).writeDays();
}

DateDropDown.prototype.writeDays = function(curday){

	var ddyear = document.getElementById(this.id + "_year");
	var curyear = ddyear.options[ddyear.selectedIndex].value;
	
	var ddmonth = document.getElementById(this.id + "_month");
	var curmonth = ddmonth.options[ddmonth.selectedIndex].value;
	
	var days = new Array(-1, 31, ((curyear % 4 == 0 && curyear % 100 != 0) || curyear % 400 == 0 ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	var numdays = days[curmonth];
	
	var ddday = document.getElementById(this.id + "_day");
	var selindex = curday ? curday-1 : ddday.selectedIndex;
	
	ddday.options.length = 0;
	
	for(var i = 0; i < numdays; i++){
		ddday.options.length = ddday.options.length + 1;
		ddday.options[i].text = i+1;
		ddday.options[i].value = i+1;
	}
	
	ddday.selectedIndex = (selindex < ddday.options.length) ? selindex : ddday.options.length - 1;
}

DateDropDown.prototype.getMonth = function(i){
	return ddd_getMonth(i);
}

function ddd_getMonth(i){
	switch(i.toString()){
		case "1": return "Jan";
		case "2": return "Feb";
		case "3": return "Mar";
		case "4": return "Apr";
		case "5": return "May";
		case "6": return "Jun";
		case "7": return "Jul";
		case "8": return "Aug";
		case "9": return "Sep";
		case "10": return "Oct";
		case "11": return "Nov";
		case "12": return "Dec";
	}
}

function ddd_changevalue(id, output){
	var hi = document.getElementById(id);
	var dd = document.getElementById(id + "_day");
	var mm = document.getElementById(id + "_month");
	var yy = document.getElementById(id + "_year");
	
	switch(output){
		case "Short Date":
			hi.value = dd.options[dd.selectedIndex].value + "/" + mm.options[mm.selectedIndex].value + "/" + yy.options[yy.selectedIndex].value;
			break;
		case "Long Date":
			hi.value = dd.options[dd.selectedIndex].value + " " + ddd_getMonth(mm.options[mm.selectedIndex].value) + " " + yy.options[yy.selectedIndex].value;
			break;
		default:
			hi.value = dd.options[dd.selectedIndex].value + "/" + mm.options[mm.selectedIndex].value + "/" + yy.options[yy.selectedIndex].value;
			break;
	}
}
