function getDaysInMonth(month, year){
	var ar = new Array(13)
	ar[1] = 31;
	ar[2] = !(year%400) || !(year%4) && year%25 ? 29 : 28;
	ar[3] = 31;
	ar[4] = 30;
	ar[5] = 31;
	ar[6] = 30;
	ar[7] = 31;
	ar[8] = 31;
	ar[9] = 30;
	ar[10] = 31;
	ar[11] = 30;
	ar[12] = 31;
	return ar[month];
}


$(function(){

	$("input[restrictinput]")
		.bind("keyup keydown", function(e){
			regexp = "";
			if($(this).attr("restrictinput" == "digits"))
				regexp = /\D/;
			this.value = this.value.replace(regexp, "");
		})
		.each(function(){
			txt = ""
			if($(this).attr("restrictinput" == "digits"))
				txt = "только цифры";
			$(this).after(" <span class=inputinfo>" + txt + "</span>")
		})
		;
		
	$("input[readonly]")
		.bind("focus", function(e){
			this.blur();
		});

	$("input[required=required], select[required=required], textarea[required=required]")
		.after(' <b style="color: red; vertical-align: top;">*</b>')
		;

	$("form").bind("submit", function(e){
		cansubmit = true;

		p = $("input[name=password][required=required]", this).removeClass("invalid");
		p_a = $("input[name=password_a][required=required]", this).removeClass("invalid");
		if(p.length && p_a.length){
			if(p.val().length == 0 || p.val() != p_a.val()){
				if(cansubmit) $(this).focus();
				cansubmit = false;
				p.addClass("invalid");
				p_a.addClass("invalid");
			}
		}

		$("input[required=required][name!=password][name!=password_a], select[required=required], textarea[required=required]", this)
			.removeClass("invalid")
			.each(function(){
				if(
						$(this).parents(":hidden").length == 0 &&
						(
							( $(this).is(":checkbox") && !$(this).is(":checked")) ||
							( !this.value || String(this.value).replace(/\s+/g, "") == "" || $(this).is(":disabled") )
						)
					){
					if(cansubmit) $(this).focus();
					cansubmit = false;
					$(this).addClass("invalid").bind("change keyup", function(){ $(this).removeClass("invalid") });
				}
			})
			;

		$("input[invalid=invalid], select[invalid=invalid], textarea[invalid=invalid]").addClass("invalid").each(function(){ if(cansubmit) $(this).focus(); cansubmit = false; });


		if(cansubmit){
			$("input, select, textarea").each(function(){
				if($(this).parents(":hidden").length > 0)
					$(this).remove();
			});
		}

		return cansubmit;
	});


	

	$("select[rel^=year]")
		.each(function(){
			years_start = new Date().getFullYear() - 90;
			years_till = new Date().getFullYear() - 18;
			years_selected = $(this).attr("selected") ? $(this).attr("selected") : 1970;

			if($(this).attr("start")){
				years_start = $(this).attr("start");
				if(years_start == "now")
					years_start = years_till = new Date().getFullYear();
			}
			if($(this).attr("delta")){
				years_till = years_start + parseInt($(this).attr("delta"));
			}
			
			years_html = "";
			for(i = years_start; i <= years_till; i++)
				years_html += '<option value="' + i + '" ' + (years_selected == i ? "selected" : "") + '>' + i + '</option>';

			$(this).html(years_html);
			
		})
		.change(function(){
			$("select[rel^=month]").change();
		})
		;

	$("select[rel^=month]")
		.each(function(){
			month_selected = $(this).attr("selected") ? $(this).attr("selected") : 1;
			months_html = "";
			for(i = 1; i <= 12; i++)
				months_html += '<option value="' + i + '" ' + (month_selected == i ? "selected" : "") + '>' + i + '</option>';
			$(this).html(months_html);
		})
		.change(function(){
			days_html = "";
			$days = $("select[rel=day" + $(this).attr("rel").replace(/^month/i, "") + "]");
			$year = $("select[rel=year" + $(this).attr("rel").replace(/^month/i, "") + "]");
			days_in_month = getDaysInMonth($(this).val(), $year.val());

			for(i = 1; i <= days_in_month; i++)
				days_html += '<option value="' + i + '">' + i + '</option>';

			$days.html(days_html);
		})
		.change()
		;

	$("select[rel^=day]")
		.each(function(){
			$year = $("select[rel=year" + $(this).attr("rel").replace(/^day/i, "") + "]");
			$month = $("select[rel=month" + $(this).attr("rel").replace(/^day/i, "") + "]");
			days_in_month = getDaysInMonth($month.val(), $year.val());
			
			day_selected = $(this).attr("selected") ? $(this).attr("selected") : 1;
			days_html = "";
			for(i = 1; i <= days_in_month; i++)
				days_html += '<option value="' + i + '" ' + (day_selected == i ? "selected" : "") + '>' + i + '</option>';
			$(this).html(days_html);
		})
});
