/********************************
*  AJAX                         *
*********************************/

// Set standard behavior for AJAX
$.ajaxSetup({

	// Default AJAX attributes
	cache: false,
	type: 'POST',
	dataType: 'json',

	// Success Event
	success: function(response) {
		ajax_notice(response);
	},

	// Error Event
	error: function() {
		ajax_error();
	}

});

// Handle AJAX response notices
function ajax_notice(response){

	// If the response came with an error
	if (response.error) {

		// There is an error, post notice
		if (response.error_message !== undefined) {
			display_notice(response.error_message, 'notice-error', false);
			return true;
		}

	// If the response came with a notice
	} else if (response.notice) {

		// There is a notice, post it
		if (response.notice !== undefined) {
			display_notice(response.notice, 'notice-info', true);
			return true;
		}

	}

	// No notice
	return false;

}

// Handle AJAX errors
function ajax_error(){
	display_notice('Connection Error', 'notice-error', true);
}



/********************************
*  NOTICE                       *
*********************************/

// Creates a notice
function display_notice(notice_message, notice_type, fadeout) {

	// If the error string has a value
	if (notice_message != "") {

		// Locate the notice div tag if available
		if ($('#notice').length > 0) {

			// Break apart error message
			parts = notice_message.split("\r\n");

			// Start error message string
			notice_messages = "";

			// Loop error messages and build error message string
			for (i = 0; i < parts.length; i++) {
				if (parts[i] != "") {
					notice_messages += "<span>" + parts[i] + "</span>";
				}
			}

			// Notice Messages
			notice_messages = $('<p>' + notice_messages + '</p>');

			// Add Notice HTML
			$('#notice').html(notice_messages);

			// Reset Notice Class
			$('#notice').removeClass();
			$('#notice').addClass('notice');

			// If set, add custom notice type
			if (notice_type !== undefined) {
				$('#notice').addClass(notice_type);
			}

			// Make the notice visible
			$('#notice').fadeIn(500);

			// Fade Out Notice
			if (fadeout) {
				setTimeout(function(){ $('#notice').fadeOut(1000)}, 3000);
			}

		} else {
			alert(notice_message);
		}

	}

}



/********************************
*  BROWSING                     *
*********************************/

// Submit a hidden form after asking question
function submit_form_request(form_id, question_id) {

	// Get the question from a hidden field in the form
	ask_question = $("#" + question_id).text();

	// Ask the question and force a YES response
	if (confirm(ask_question)) {
		$("#" + form_id).submit();
	}

}

// Submit a hidden form after asking question with checkbox selection
function submit_form_request_checkbox(form_id, source_form_id, question_id) {

	// Get the selected Checkboxes
	ids = get_selected_checkboxes(source_form_id);

	// If there were IDs selected
	if (ids != "") {

		// If no question was passed
		if (question_id == undefined) {

			// Submit the form
			$("#" + form_id + " [name='selected_ids']").val(ids);
			$("#" + form_id).submit();

		} else {

			// Get the question from a hidden field in the form
			ask_question = $("#" + question_id).text();

			// Ask the question and force a YES response
			if (confirm(ask_question)) {
				$("#" + form_id + " [name='selected_ids']").val(ids);
				$("#" + form_id).submit();
			}

		}

	} else {
		alert ("You did not select any checkboxes.");
	}

}

//Get selected check boxes of a form
function get_selected_checkboxes(container_id) {

	// Hold a comma seperated list of IDs
	ids = "";

	// Loop through each checkbox within our container
	$("#" + container_id + " :checked").not(".checkall").each( function () {
		ids += "," + $(this).val();
	});

	// Return list of IDs, remove beginning comma
	return ids.substr(1);

}

// Check or Uncheck checkboxes of a container
function check_uncheck_all(container_id, self_id) {
	$("#" + container_id + " :checkbox").attr('checked', $("#" + self_id).is(':checked'));
}


