$(document).ready(function() {

	if ($(".date_control").length) {
		date = new Date();
		
		months = Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
		years = Array();
		
		for (i = parseInt(date.getFullYear()); i < (parseInt(date.getFullYear()) + 20); i++) {
			years.push(parseInt(i));
		}
		
		$(".date_control").each(function() {
			$(this).hide();
			values = $(this).val().split('/');
			
			day = (values[0]) ? values[0] : 'Day';
			
			html = "<input class='dc_inp' type='text' name='day[" + $(this).attr('name') + "]'"; 
			html += " style='width: 40px;' value='" + day + "' title='Day' onfocus='if(this.value == this.title) { this.value = \"\"; }' /> &nbsp;";
			
			html += "<select class='dc_inp' name='mon[" + $(this).attr('name') + "]' style='width: 100px;'>";
			html += "<option value=''>Month</option>";
			for (i = 0; i < months.length; i++) {
				html += "<option value='" + parseInt(i + 1) + "'";
				if (values[1] && values[1] == (i+1)) {
					html += " selected=\"selected\"";
				}
				html += ">" + months[i] + "</option>";
			}
			html += "</select>&nbsp;";
			
			html += "<select class='dc_inp' name='yea[" + $(this).attr('name') + "]' style='width: 100px;'>";
			html += "<option value=''>Year</option>";
			for (i = 0; i < years.length; i++) {
				html += "<option";
				if (values[2] && values[2] == years[i]) {
					html += " selected=\"selected\"";
				}
				html += ">" + years[i] + "</option>";
			}
			html += "</select>&nbsp;";
			
					
			$(".date_control").after(html);
		});
		
		
		$("input.dc_inp").keyup(function() {
			value = $(this).val();
			if (value.match(/\D/g)) {
				$(this).val(value.substring(0, value.length-1));
			}
		});
		
		$(".dc_inp").change(function() {
			field_name = $(this).attr('name').slice(4,-1);
			day = $("input[name='day[" + field_name + "]']").val();
			month = $("select[name='mon[" + field_name + "]']").val();
			year = $("select[name='yea[" + field_name + "]']").val();
			
			if (day == 'Day' || day == '') {
				day = 1;
			}
			
			if (month == '') {
				month = 1;
			}
			
			if (year == '') {
				year = years[0];
			}
			
			$(".date_control[name='" + field_name + "']").val(day + '/' +	month + '/' + year);
			
			
			if ($(this).attr('name') == 'day') {
				if ($(this).val() == "") {
					$(this).val($(this).attr("title"));
				}
			}
			
		});

	}

	
	if ($("textarea.wysiwyg".length)) {
		$("textarea.wysiwyg").each(function() {
			elem = $(this);
			
			$(this).parent().replaceWith(elem);
		});
		
		$('textarea.wysiwyg').wysiwyg();
		
	}
});