function openFeedback(time_table_id, instructor, course) { 
	$("#instructor_name").html(instructor);
	$("#course_name").html(course);
	$("#time_table_id").val(time_table_id);
}
function closeFeedback() { 
	$(".nps_error").html("");
	$("#comment1").val("");
    $("#comment2").val("");
    $("#comment3").val("");
	$("#comment1").hide();
	$("#comment2").hide();
	$("#comment3").hide();
	$("#thankyou").hide();
	$("#thankyou2").hide();
	$("#groupx_nps").show();
	$(".rating1").attr('checked', false);
	$(".rating2").attr('checked', false);
	$(".rating3").attr('checked', false);
	$("#firstname").val("");
	$("#lastname").val("");
	$("#contact_number").val("");
	$("#email").val("");
	$("#accesscard").val("");
}

$(document).ready(function() {
	$(".rating1").click(function() { 
		if ($("#comment1").val() == "") {
			$("#comment1").css("color", "#999").val("Any particular reason for your rating?").show();
			$("#rating1_error").html("");
		} else { 
			$("#comment1").show();		
		}
	});
	$(".rating2").click(function() { 
		if ($("#comment2").val() == "") {
			$("#comment2").css("color", "#999").val("Any particular reason for your rating?").show();
			$("#rating2_error").html("");
		} else { 
			$("#comment2").show();		
		}
	});
	$(".rating3").click(function() { 
		if ($("#comment3").val() == "") {
			$("#comment3").css("color", "#999").val("Any particular reason for your rating?").show();
			$("#rating3_error").html("");
		} else { 
			$("#comment3").show();		
		}
	});
	$("#comment1").focus(function() { 
		if ($("#comment1").val() == "Any particular reason for your rating?") {
			$("#comment1").css("color", "#000").val("");			
		}
	});
	$("#comment1").blur(function() { 
		if ($("#comment1").val() == "") {
			$("#comment1").val("Any particular reason for your rating?");			
		}
	});
	$("#comment2").focus(function() { 
		if ($("#comment2").val() == "Any particular reason for your rating?") {
			$("#comment2").css("color", "#000").val("");
		}
	});
	$("#comment2").blur(function() { 
		if ($("#comment2").val() == "") {
			$("#comment2").val("Any particular reason for your rating?");
		}
	});
	$("#comment3").focus(function() { 
		if ($("#comment3").val() == "Any particular reason for your rating?") {
			$("#comment3").css("color", "#000").val("");						
		}
	});
	$("#comment3").blur(function() { 
		if ($("#comment3").val() == "") {
			$("#comment3").val("Any particular reason for your rating?");						
		}
	});
	$("#nps_submit_ratings").click(function() { 
		var err = 0;
		$(".nps_error").html("");
		if (!$("input[name=rating1]:checked").val()) { 
			$("#rating1_error").html("Please select a rating");
			err = 1;
		}
		if (!$("input[name=rating2]:checked").val()) { 
			$("#rating2_error").html("Please select a rating");
			err = 1;
		}
		if (!$("input[name=rating3]:checked").val()) { 
			$("#rating3_error").html("Please select a rating");
			err = 1;
		}

		if (err == 0) {
			$("#groupx_nps").hide();
			$("#thankyou").show();
			$.ajax({
			  type: 'POST',
			  url: '/time_table_ratings/ajax_save_ratings',
			  data: { rate_class: $('input[name=rating1]:checked').val(), comment_class: $("#comment1").val(), rate_instructor: $('input[name=rating2]:checked').val(), comment_instructor: $("#comment2").val(), rate_recommend: $('input[name=rating3]:checked').val(), comment_recommend: $("#comment3").val(), time_table_id: $("#time_table_id").val() },
			  success: function(html) { 
				$("#rating_id").val(html);
			  }
			});
		}
	});
	$("#nps_submit_details").click(function() { 
		var email = $("#email").val();
		var contact_number = $("#contact_number").val();

		var err = 0;
		$("#thankyou .nps_error").hide();
		if ($("#firstname").val() == "") {
			$("#nps_details_firstname").html("Name is a required field").show();
			err = 1;
			$("#firstname").focus();
		}
		if ($("#lastname").val() == "") {
			$("#nps_details_lastname").html("Surname is a required field").show();
			if (err == 0) {
				$("#lastname").focus();
			}
			err = 1;
		}
		if (contact_number == "") {
			$("#nps_details_contact_number").html("Contact number is a required field").show();
			if (err == 0) {
				$("#contact_number").focus();
			}
			err = 1;
		} else { 
			if (checkInternationalPhone(contact_number) == false) {
				$("#nps_details_contact_number").html("Please enter your phone number").show();
				err = 1;
			}
		}

		if (email == "") {
			$("#nps_details_email").html("Email is a required field").show();
			if (err == 0) {
				$("#nps_details_email").focus();
			}
			err = 1;			
		} else { 
			if (echeck(email) == false) {
				$("#nps_details_email").html("Please enter a valid email address").show();
				if (err == 0) {
					$("#nps_details_email").focus();
				}
				err = 1;			
			}
		}

		if ($("#accesscard").val() == "") {
			$("#nps_details_accesscard").html("Access Card is a required field").show();
			if (err == 0) {
				$("#accesscard").focus();
			}
			err = 1;
		}
		if (err == 0) {
			$("#thankyou").hide();
			$("#thankyou2").show();
			$.ajax({
			  type: 'POST',
			  url: '/time_table_ratings/ajax_save_personal_details',
			  data: { firstname: $("#firstname").val(), lastname: $("#lastname").val(),contact_number: $("#contact_number").val(), email: $("#email").val(), accesscard: $('#accesscard').val(), rating_id: $('#rating_id').val() },
			  success: function(html) { 
			  }
			});
			setTimeout('$(".close").click()',2000);
		}
	});
	$("#nps_cancel_rating").click(function() { 
		$('.close').click()
		$('#nps_feedback').dialog("close");		
	});
	$("#nps_cancel_details").click(function() {
		$('.close').click()
		// $('#nps_feedback').dialog("close");
	});
});
