function canned_response(){
	$("#canned_response").showModalBox();

	$("#canned_response #txt_id").val( "" );
	$("#canned_response #txt_question").val( "" );
	$("#canned_response #txt_answer").val( "" );
	$("#canned_response #txt_priority").val( "" );
	
	
	
	$("#question_list").change(function(){
		get_question_answer();
	});
	
	$("#question_list").keyup(function(){
		get_question_answer();
	});
	
	function get_question_answer(){
		$("#canned_response #txt_id").val( $("#question_list").val() );
		$("#canned_response #txt_question").val( $("tr#id_" + $("#question_list").val() + " td.question").text() );
		$("#canned_response #txt_answer").val( $("#id_" + $("#question_list").val()   + " td.answer").text() );
		$("#canned_response #txt_priority").val( $("#id_" + $("#question_list").val() + " td.priority").text() );
	}
				
	
	$("#btn_add_response").click(function(){
		
		if($("#canned_response #txt_question").val().length < 1 || $("#canned_response #txt_answer").val().length < 1){
			alert("Sorry the question and answer field must be filled.");
			return false;
		}
		
		$("#canned_response #txt_action").val("add");
		form_submit();	
		return false;
	});
	
	
	$("#btn_save_response").click(function(){
		
		if($("#canned_response #txt_question").val().length < 1 || $("#canned_response #txt_answer").val().length < 1){
			alert("Sorry the question and answer field must be filled.");
			return false;
		}
		
		
		$("#canned_response #txt_action").val("save");
		form_submit();	
		return false;
	});
	
	$("#btn_delete_response").click(function(){
		
		if($("#question_list").val() == '0'){
			alert("Sorry you can not delete blank field!");
			return false;
		}
		
		
		if( confirm("Are you sure, you want to delete this question/answer??") ){
			
			$("#canned_response #txt_action").val("delete");
			form_submit();	
			return false;
		}
	});
	
	
	function form_submit(){
		var text = $("#frm_canned_response").serializeArray();
		$("#canned_response button").attr("disabled", "true");
		
		$.ajax({type	: "POST",
		   	url 	: "./canned_response.php",
			data	: text,
			dataType: 'json',
			timeout : 59000,
			success	: callback_success,
			error	: err_error
		   });
		
		return false;
	}
	
	
	$("#btn_done_response").click(function(){
		$("#canned_response").removeModalBox();
	});
	
	
	
	
	function callback_success(data, status){
		//alert(data);
		
		if(data['errorcode'] == "success"){
			if($("#canned_response #txt_action").val() == "add"){
				tr = $("<tr></tr>"); 
				tr.attr("id", "id_" + data['last_id']);
				tr.append( $("<td class='question'></td>").text( $("#canned_response #txt_question").val() ) );
				tr.append( $("<td class='answer'></td>").text( $("#canned_response #txt_answer").val() ) );
				tr.append( $("<td class='priority'></td>").text( $("#canned_response #txt_priority").val() ) );
				$("#canned_response_table").append(tr);
				
				$("#question_list").append( $("<option value='" + data['last_id'] +  "'></option>").text( $("#canned_response #txt_question").val() ) );
				
				alert("Your question has been saved");
			}else if($("#canned_response #txt_action").val() == "save"){
				curr_id = $("#txt_id").val();
				$("#question_list option[value='" + curr_id + "']").text( $("#canned_response #txt_question").val() );
				
				$("tr#id_" + curr_id + " td.question").text( $("#canned_response #txt_question").val() );
				$("tr#id_" + curr_id + " td.answer").text( $("#canned_response #txt_answer").val() );
				$("tr#id_" + curr_id + " td.priority").text( $("#canned_response #txt_priority").val() );
				
				alert("Your question has been saved");
			}else if($("#canned_response #txt_action").val() == "delete"){
				curr_id = $("#txt_id").val();
				$("#question_list option[value='" + curr_id + "']").remove();
				
				$("tr#id_" + curr_id).remove();
				$("#canned_response #txt_id").val( "" );
				$("#canned_response #txt_question").val( "" );
				$("#canned_response #txt_answer").val( "" );
				$("#canned_response #txt_priority").val( "" );
				
				
				alert("Question has been deleted");
			}

			
		}else{
			var msg = "Sorry there was a problem. Please try again \nPossible Error:\n" + data['errormsg'];
			alert(msg);
		}
		
		$("#canned_response button").removeAttr("disabled");
	}
	
	function err_error(xhr, reason, ex){
		alert(reason);
		$("#canned_response button").removeAttr("disabled");
	}
	
	
	
	e.preventDefault();
	return false;	
}



function add_cc(){
	txt = $("#cc_coachid").val();
	emailaddress = txt.substring( txt.indexOf( "(" ) + 1, txt.indexOf( ")" ) );
	emailaddress += ";" ;
	
	$("#email_cc").val(  $("#email_cc").val() + " " + emailaddress  );
}


function add_bcc(){
	txt = $("#cc_coachid").val();
	emailaddress = txt.substring( txt.indexOf( "(" ) + 1, txt.indexOf( ")" ) );
	emailaddress += ";" ;
	
	$("#email_bcc").val(  $("#email_bcc").val() + " " + emailaddress  );
}
