/*
	/includes/help.js
	jquery functions for help system
	
	created July 4, 2009 (PC)
	
*/


	$(document).ready(function () {
	
		$("#accordion").accordion({ header: 'h3' });
		
		$("#replytoggle").click(function() {
		 	$("#replyform").show();
		});
		$("#replycancel").click(function() {
		 	$("#replyform").hide();
		});
		
		
		$("#closebttn").click(function() {
			
			if(!$("#ticketid").val()) {
				alert('Ticket ID is not defined.');
				return;
			}
			
			$("#closebttn").attr('disabled', true);
			$("#closebttn").html('<img src="/images/icons/ajax_waiting.gif" height="24" width="24" align="absmiddle" /> submitting request...');
			$.ajax({
				url: 'ajax/help.php',
				data: 'cmd=close_ticket&id='+ $("#ticketid").val(),
				dataType: 'json',
				type: 'post',
				success: function (j) {
					// put the 'msg' field from the $resp array from check_username (php code) in to the validation message
					$("#closebttn").html(j.msg);
					
				} // end success
			}); // end .ajax
			
		}); // end closebttn click
		
		
		$("#adminbttn").click(function() {
		
			var msg = $("#message").val();
			var sbj = $("#subject").val();
			
			if(!msg) {
				alert('Please Enter a message.');
				$("#message").focus();
				return;
			}
			
			if(!sbj) {
				alert('Please enter a subject.');
				return;
			}
			
			$("#adminbttn").attr('disabled', true);
			$("#adminform").html('<img src="/images/icons/ajax_waiting.gif" height="24" width="24" align="absmiddle" /> submitting request...');
			$.ajax({
				url: 'ajax/help.php',
				data: 'cmd=contact_admin&subject='+ sbj + '&message=' + msg,
				dataType: 'json',
				type: 'post',
				success: function (j) {
					// put the 'msg' field from the $resp array from check_username (php code) in to the validation message
					$("#adminform").html(j.msg);
					
				} // end success
			}); // end .ajax
				
		}); // end adminbttn click
		
		
		
		$("#replybttn").click(function() {
		
			var msg = $("#message").val();
			
			if(!msg) {
				alert('Please Enter a message.');
				$("#message").focus();
				return;
			}
			
			if(!$("#ticketid").val()) {
				alert('Ticket ID is not defined.');
				return;
			}
			
			$("#replybttn").attr('disabled', true);
			$("#replyform").html('<img src="/images/icons/ajax_waiting.gif" height="24" width="24" align="absmiddle" /> submitting request...');
			$.ajax({
				url: 'ajax/help.php',
				data: 'cmd=reply&id='+ $("#ticketid").val() + '&message=' + msg,
				dataType: 'json',
				type: 'post',
				success: function (j) {
					// put the 'msg' field from the $resp array from check_username (php code) in to the validation message
					$("#replyform").html(j.msg);
					
				} // end success
			}); // end .ajax
				
		}); // end replybttn click
		
	
		$("#cform").validate({
		
			
			submitHandler: function(form) {
				
				var code = $("#code").val();
				var subj = $("#subject").val();
				var dept = $("#dept").val();
				var pri  = $("#priority").val();
				var msg  = $("#message").val();
				var name = $("#name").val();
				var email = $("#email").val();
			
				$("#cbttn").attr('disabled', true);
				$("#cform_holder").html('<img src="/images/icons/ajax_waiting.gif" height="24" width="24" align="absmiddle" /> submitting request...');
				$.ajax({
					url: 'ajax/help.php',
					data: 'cmd=create_ticket&subject='+ subj + '&dept=' + dept + '&priority=' + pri + '&message=' + msg  + '&name=' + name  + '&email=' + email + '&code=' + code,
					dataType: 'json',
					type: 'post',
					success: function (j) {
						// put the 'msg' field from the $resp array from check_username (php code) in to the validation message
						$("#cform_holder").html('<div id="cform_msg"></div>');
						$("#cform_msg").html(j.msg);
						if(j.ok == true) {
							
						}
					} // end success
				}); // end .ajax
				
			}
			
   		}) // end validate
	
		
    }); // end document ready
