﻿$(document).ready(function(){
	
	// Add some effects to the navigation
	$("#secondary-nav li a").each(function() {
		if ($(this).hasClass("active")) {
			$("#secondary-nav li a").not(".active").not("#secondary-nav li li a").addClass("inactive");
		};
	});
	
	// Add some functionality to the radio buttons when you choose where to meet
	// make the input field required if you select another meeting place
	$("#contact_other").click(function() {
		$("#contact_mylocation").addClass("required");
	});
	
	// Remove the required if you select something else
	$("#contact_ljungby").click(function() {
		$("#contact_mylocation").removeClass("required").removeClass("has-error");
	});
	
	
	// A reference to the correct form
	var formtovalidate = "";
	var init = false;
	
	// If its just a single form to submit and you dont choose between the two
	// set the correct form to submit and make the user able to submit it without checking a radio
	// button
	if ($(".form").hasClass("single")) {
		init = true;
		var formtovalidate = $(".form .content");
	}
	
	// When focusing on a field for the first time, switch the active field container
	// and trigger a click on the form selector
	$(".form .content input").focus(function() {
		if(!init) {
			var section = $(this).closest(".section");
			$(section).find(".selector input").attr("checked", true);
			$(section).find(".selector input:radio").trigger('click');
		}
	});
	
	$(".form .c-area").click(function() {
		var section = $(this).closest(".section");
		$(section).find(".selector input").attr("checked", true);
		$(section).find(".selector input:radio").trigger('click');
	});
	
	// Selects the correct form and deselects and disables the other form
	$(".selector input:radio").click(function() {
		
		init = true;
		
		// hide error messages
		$(".error-messages p").hide();
		
		// Deselect all form areas
		$(".form .c-area.selected").removeClass("selected");
		
		// Select the current form and remove the not-selected class
		var id = $(this).attr("id");
		$("#"+id+"-area").addClass("selected").removeClass("not-selected");
		$(".selected input").attr("disabled", false);
		
		// Add not-selected to the forms that are not select and disable the inputs
		$(".form .c-area").not(".selected").addClass("not-selected");
		$(".form .c-area.not-selected input").attr("disabled", true).removeClass("has-error");
		
		// Let the validator know which form we need to validate
		formtovalidate = $("#"+id+"-area");
	});
	
	// All input fields assigned with the class 'default' will
	// get their default values hidden when clicked, and reset again
	// if the user leaves the field empty or hasnt changed anything
	// The searchfields needs an accompanying hidden input with the
	// same id but suffixed "-default" that contains the default value.
	$("input.default, textarea.default").each(function() {
		var defaultVal = $(this).next(".defaultvalue").val();
		if (!$(this).val())
			$(this).val(defaultVal);
		$(this).focus(function() {
			if ($(this).val() == defaultVal)
				$(this).val("");
		});
		$(this).blur(function() {
			if ($(this).val() == "")
				$(this).val(defaultVal);
		});
	});
	
	// Change the color of the input and the textfield for the comment form
	$(".validate").click(function() {
		if(init) {
			var parentObj = $(".content").not(".not-selected");
			var validated = true;
			
			
			// Dont send anything if one of the fields are equal to the default value
			$(parentObj).find("input.text, textarea").each(function() {
				if($(this).val() == $(this).next(".defaultvalue").val()) {
					validated = false;
				}
			});
			
			$(parentObj).find(".required").each(function() {
				if($(this).val() == "" || $(this).val() == $(this).next(".defaultvalue").val()) {
					$(this).addClass("has-error");	
					validated = false;
				}
			});

			if (!validated) {
				$(".error-messages p.error").show();
				return false;
			}
			else if (validated) {
				$(".error-messages p.error").hide();				
				$("#contactform").submit();	
			}
		}
		else {
			$(".error-messages p.error").show();
			return false;
		}
	});

	// Clear error when correcting it
	$("input")
	.keydown(function() {
		if($(this).hasClass(".required")) {
			clearError($(this));
		}
	});
	
});

function clearError(obj) {
	if (obj.val()) {
		obj.removeClass("has-error");
	}
}
