	$(document).ready(function(){
		// Prepare inputs
		$("#email1").attr("errorMsg", "Email không hợp lệ");
		$("#email2").attr("errorMsg", "Email không hợp lệ");
		$("#hoten").attr("errorMsg", "Nhập đầy đủ họ tên tiếng Việt");
		$("#dienthoai1").attr("errorMsg", "Số điện thoại không hợp lệ<BR>Chỉ sử dụng con số, không sử dụng chữ");
		$("#dienthoai2").attr("errorMsg", "Số điện thoại không hợp lệ<BR>Chỉ sử dụng con số, không sử dụng chữ");
		$("#diachi").attr("errorMsg", "Nhập địa chỉ liên hệ chi tiết");
		$("#ngaysinh").attr("errorMsg", "Nhập ngày sinh của bạn theo dạng ngày/tháng/năm");
		$("#cmnd").attr("errorMsg", "Nhập CMND của bạn hoặc của cha/mẹ nếu bạn chưa có CMND");
		$("#phieugiamgia").attr("errorMsg", "Mã số không hợp lệ, chỉ sử dụng con số");
		$("#hotenphuhuynh").attr("errorMsg", "Nhập đầy đủ họ tên của phụ huynh");
		$("#emailphuhuynh").attr("errorMsg", "Email không hợp lệ");
		$("#dienthoaiphuhuynh").attr("errorMsg", "Số điện thoại không hợp lệ<BR>Chỉ sử dụng con số, không sử dụng chữ");
		fill_select("tinhthanh", 31); //HCM default
		
		// TTG NC
		$("#dangky_ttg_nc").change( function() {
			if ($(this).val() == "-1" || $(this).val() == "0" || $(this).val() == "-2") {
				$("#span_giam_gia").hide();
				$("#phieugiamgia").show();
			} else {
				$("#span_giam_gia").show();
				$("#phieugiamgia").hide();
				$("#phieugiamgia").val("");
			}
		});
		
		// Phu huynh, same info
		$("body").keydown(function(){
			if ($("#email_ph_nt").attr("checked")) $("#emailphuhuynh").val( $("#email1").val() );
			if ($("#dienthoai_ph_nt").attr("checked")) $("#dienthoaiphuhuynh").val( $("#dienthoai1").val() );
		});		
		$("#dienthoai_ph_nt").change(function(){
			$("#dienthoaiphuhuynh").val( $("#dienthoai1").val() );
		});		
		$("#email_ph_nt").change(function(){
			$("#emailphuhuynh").val( $("#email1").val() );
		});		
		
		
		// The most important button
		$("#dangky_button").click(function() {
			if (is_form_valid()) {
				$(this).fadeOut("slow");
				clear_clean_input();
				$.ajax({
					type:'POST', data: $("#kForm").serialize(), url: 'dangky_khoahoc_ttg.php?k=' + $('#khid').val(), cache:false, dataType:'text',
					success: function(data) {
						if (data.indexOf('true') != -1) {
							vars = data.split(' ');
							hv_id = (vars[1] != null ? vars[1] : '');
							location.href="dangky_khoahoc_ttg.php?d=1&k=" + $('#khid').val() + "&h=" + hv_id;
						} else {
							$.prompt('Có lỗi xảy ra trong quá trình đăng ký<BR>Vui lòng thử lại lần nữa');
							$("#dangky_button").fadeIn('slow');
						}
					},
					failure: function(data) {
						$.prompt('Có lỗi xảy ra trong quá trình đăng ký<BR>Vui lòng thử lại lần nữa');
						$("#dangky_button").fadeIn('slow');
					}
				});
			}
		});
		
		// Show gioithieu
		$("#link_gioithieu").click(function() {
			$(this).hide();
			$("#gioithieuchitiet").slideToggle("medium");
		});
		
		var email_duplicate = false;
		$("#email1").change(function() {
			check_email();
		});
	});
	
		
	function check_email() {
		$.ajax({
			type:'POST', data: { email1:$('#email1').val(), action:'email' }, url: 'ajax_validate_khoahoc_ttg.php', dataType: 'json',
			success: function(data) {
			if (data != 'false' && data != '') {
				$.prompt('Bạn đã sử dụng email này để đăng ký cho một học viên khác (' + data.hoten + '). Bạn có muốn sử dụng lại email?',{
							buttons: { Có: true, Không: false },
							focus: 1,
							prefix:'jqismooth',
							top: '30%',
							callback: function(v,m,f){
								if (!v) {
									$("#email1").val("").focus();
									email_duplicate = false;
								} else {
									fill_form(data); // load existing data
									$("#hoten").focus();
									email_duplicate = true;
								}
							}
						});
			}
		}});
	}
	
	function fill_form(data) {
		$("#email2").val(data.email2).blur();
		$("#dienthoai1").val(data.dienthoai1).blur();
		$("#dienthoai2").val(data.dienthoai2).blur();
		$("#diachi").val(data.diachi);
		$("#hotenphuhuynh").val(data.hotenphuhuynh);
		$("#emailphuhuynh").val(data.emailphuhuynh);
		$("#dienthoaiphuhuynh").val(data.dienthoaiphuhuynh);
		$("#nghenghiepphuhuynh").val(data.nghenghiepphuhuynh);
		$("#noicongtacphuhuynh").val(data.noicongtacphuhuynh);
		fill_select("tinhthanh", data.tinhthanh);
		fill_select("quanhephuhuynh", data.quanhephuhuynh);
		fill_select("dangky_ttg_nc", data.ttg_nc_id);
		remove_dirty_input();
	}
