

(function($){
	
	$.fn.extend({
		
		renderDatebox : function(pairField) {
			
			var dateBox = $(this);
			var dateBoxId = dateBox.attr("id");
			
			var monthId = dateBoxId + "Month";
			var monthName = monthId;
			
			var dayId = dateBoxId + "Day";
			var dayName = dayId;
			
			var yearId = dateBoxId + "Year";
			var yearName = yearName;
			
			var month = "<select id=\"" + monthId + "\" name=\"" + monthName + "\" >" 
					  + "<option value=\"\"> </option>"
					  + "<option value=\"01\">January</option>"
					  + "<option value=\"02\">February</option>"
					  + "<option value=\"03\">March</option>"
					  + "<option value=\"04\">April</option>"
					  + "<option value=\"05\">May</option>"
					  + "<option value=\"06\">June</option>"
					  + "<option value=\"07\">July</option>"
					  + "<option value=\"08\">August</option>"
					  + "<option value=\"09\">September</option>"
					  + "<option value=\"10\">October</option>"
					  + "<option value=\"11\">November</option>"
					  + "<option value=\"12\">December</option>"
					  + "</select>";
			  
			var day = "<select id=\"" + dayId + "\" name=\"" + dayName + "\" >"
					  + "</select>";
					  
			var yearOptions = "";
			var d = new Date();
			var maxYear = d.getFullYear() - 15;
			var minYear = d.getFullYear() - 100;
			for(var x=maxYear; x >= minYear;x--) {
				yearOptions += "<option value=\"" + x + "\">" + x + "</option>";
			}
					  
			var year = "<select id=\"" + yearId + "\" name=\"" + yearName + "\" >"
					  + "<option value=\"\"> </option>"
					  + yearOptions
					  + "</select>";
			
			var field = month + " " + day + " " + year;
			
			dateBox.html(field);
			dateBox.calculateDayOptions();
			
			
			
			var setPair = function() {
				if(typeof pairField == "undefined")
					return false;
					
				var pair = $("#" + pairField);
				var dateValue = dateBox.getDateboxValue();
				//alert(dateValue);
				pair.val(dateValue);
			}
			
			var fixDaybox = function(){
				dateBox.calculateDayOptions();
				setPair();
			};
			
			
			$("#"+ monthId).change(fixDaybox);
			$("#"+ dayId).change(setPair);
			$("#"+ yearId).change(fixDaybox);
		},
		
		//EVENTS
		
		calculateDayOptions : function() {
			
			var month = $(this).getDateboxMonth();
			var day = $(this).getDateboxDay();
			var year = $(this).getDateboxYear();
			
			var monthInt = parseInt(month);
			var yearInt = parseInt(year);
			
			var maxDay = 31;
			var minDay = 1;
			
			switch(monthInt) {
				case 1:	 maxDay = 31; break;
				case 2:	 
						if(yearInt%4 == 0)
							maxDay = 29; 
						else maxDay = 28;
						break;
				case 3:	 maxDay = 31; break;
				case 4:	 maxDay = 30; break;
				case 5:	 maxDay = 31; break;
				case 6:	 maxDay = 30; break;
				case 7:	 maxDay = 31; break;
				case 8:	 maxDay = 31; break;
				case 9:	 maxDay = 30; break;
				case 10: maxDay = 31; break;
				case 11: maxDay = 30; break;
				case 12: maxDay = 31; break;
			}
			
			var dayInt = parseInt(day);
			
			if(dayInt > maxDay)
				dayInt = maxDay;
			
			var dayOptions = "";
			dayOptions += "<option value=\"\"> </option>";
			for(var x=minDay; x <= maxDay;x++) {
				var selected = (dayInt == x) ? " selected " : "";
				dayOptions += "<option value=\"" + x + "\" " + selected + " >" + x + "</option>";
			}
			
			var dateBox = $(this);
			var dateBoxId = dateBox.attr("id");
			
			var dayId = dateBoxId + "Day";
			var dayBox = $("#" + dayId);
			dayBox.html(dayOptions);
		},
		
		//GET FUNCTIONS
		getAge : function() {
			var M = parseInt($(this).getDateboxMonth());
			var D = parseInt($(this).getDateboxDay());
			var Y = parseInt($(this).getDateboxYear());
			var now=new Date(),m=now.getMonth()+1,d=now.getDate();
			return now.getFullYear()-Y+(M>m?-1:M==m&&D>d?-1:0);
		},
		
		getDateboxValue : function() {
						
			var month = $(this).getDateboxMonth();
			var day = $(this).getDateboxDay();
			var year = $(this).getDateboxYear();
			
			if(year.length > 0 &&
				day.length > 0 && 
				month.length > 0)			
				return month + "/" + day + "/" + year;
			else return false;
		},
		
		getDateboxMonth : function() {
			
			var dateBox = $(this);
			var dateBoxId = dateBox.attr("id");
			
			var monthId = dateBoxId + "Month";
			var monthName = monthId;
			
			var month = $("#" + monthId).val();
			
			if(month.length == 1)
				month = "0" + month;
			
			return month;
		},
		
		getDateboxDay : function() {
			var dateBox = $(this);
			var dateBoxId = dateBox.attr("id");
			
			var dayId = dateBoxId + "Day";
			var dayName = dayId;
			
			var day = $("#" + dayId).val();
			
			return day;
		},
		
		getDateboxYear : function() {
			var dateBox = $(this);
			var dateBoxId = dateBox.attr("id");
			
			var yearId = dateBoxId + "Year";
			var yearName = yearName;
			
			var year = $("#" + yearId).val();
			
			return year;
		},
		 
		setDatebox : function(dateGiven) {
			
			
			var d = Date.parseDate(dateGiven, "Y-m-d");
			
			if(d == null)
				return false;
			
			var dateBox = $(this);
			var dateBoxId = dateBox.attr("id");
			
			//alert(d.toString());
			 
			var year = d.getFullYear();
			var month = d.getMonth() + 1;
			if(month < 10)
				month = "0" + month;
			var day = d.getDate();
			if(day < 10)
				day = "0" + day;
			
			//alert(year + " " + month + " " + day);
			 
			var yearId = dateBoxId + "Year";
			var monthId = dateBoxId + "Month";
			var dayId = dateBoxId + "Day";
			
			if(year)
				$("#" + yearId).val(year);
			if(month)
				$("#" + monthId).val(month);
			if(day)
				$("#" + dayId).val(day);
			
			return $(this).getDateboxValue();
		}
		
	});
	
})(jQuery);