// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
// 
// Created by B. F. B. Emson Esq.
// http://www.emson.co.uk
// 
// Create a single global variable to act as a new namespace
// This will avoid collisions and interference with other applications
var EMSON = {}


EMSON.submit_contact = function submitContact() {
	// relative URL to submit the form values to
	var url = '/visitors';
	// use the serialize JQuery function to gather all the input 
	// data and serialize it as text.
	var data = $("#contact_data :input").serialize();
	// use HTTP POST to post the data to the Rails resource
	// Note: we pass text from the contacts_controller.rb into the 
	// callback function using the 'html' variable.
	// Also we post this information as 'text' not 'JSON' or any other format.
	$.post(url, data, 
		function(text){
			$("#response-container").empty().append("<div id='response'>" + EMSON.process_response(text) + "</div>")
		}, 
		"text");
		
	return false;
}


EMSON.process_response = function process_response(text) {
	// remove any existing error messages, and markings
	EMSON.clean_up_error_styles();
	// determine if the text is an error or a success message
	if(text.indexOf(';') === -1) {
		return EMSON.process_success(text);
	} else {
		return EMSON.process_error(text);
	}
}


EMSON.clean_up_error_styles = function clean_up_error_styles() {
	var errorNodes = $(".with_error");
	errorNodes.empty();
	errorNodes.removeClass("with_error");
}


EMSON.process_error = function process_error(text) {
	// build error response message
	var response_message = "<div id='response_error' class='with_error'>";
	var errors = text.split(',');
	for(count in errors) {
		var line = errors[count];
		var items = line.split(';');
		var element = items[0];
		var message = items[1];
		if(element !== null && element !== undefined && element !== "") {
			// mark error fields
			var errorInputField = $("#" + element);
			errorInputField.addClass("with_error");
			// clear values from error fields
			errorInputField.val("");
			// add a label for the fields that have an error
			EMSON.add_error_label(element, message);
			// build error message
			response_message = response_message + " - "+ message + "<br/>";
		}
		// get the first error field and set it with the focus
		$("#" + errors[0].split(';')[0]).focus();
	}
	response_message = response_message + "</div>";
	return response_message;
}


EMSON.process_success = function process_success(text) {
	var response_message = "<div  id='response_success' class='with_success'>" + text + "</div>";
	// find all form input fields and clear out their contents
	$("#contact_data input").val("");
	$("input:first").focus();
	return response_message;
}


// finds the element and adds an error label node after it.
EMSON.add_error_label = function add_error_label(element, message) {
	elementId = "#" + element; 
	$(elementId).after("<br/><label class='with_error' for='" + element + "'>" + message + "</label> ");
}



