jQuery.extend(
  jQuery.expr[ ":" ], 
  { reallyvisible : function (a) { return !(jQuery(a).is(':hidden') || jQuery(a).parents(':hidden').length); }}
);

jQuery("#participantsRegistration").ready(function(){
	
	// Define hidden html template for participants
	var registrationGeneral			= jQuery("fieldset.registrationGeneral");
	var participantTemplate			= jQuery("fieldset.participant.template");
	var participants					= jQuery("div.participants");
	var participantsCount			= 0;
	
	
	function addRemoveErrorClass (event) {
		if (jQuery(this).val() == '')
			jQuery(this).addClass("error");
		else
			jQuery(this).removeClass("error");
	}
	
	registrationGeneral.find("input, select, textarea").change(addRemoveErrorClass);
	
	// Assign add participant action
	jQuery("input.addParticipant").click(function(event){
		
		participantsCount++;
		
		var newParticipant = participantTemplate.clone().appendTo(participants);
		var participantTerminTemplate = newParticipant.find(".seminar.template");
		
		newParticipant.attr("participantNumber", participantsCount);
		newParticipant.show("slow");
		newParticipant.removeClass("template");
		newParticipant.find("span.number").html(" #"+participantsCount);
		newParticipant.find("input, select").each(function () {
			var field = jQuery(this);

			if (field.attr("name"))
			{
				var str = field.attr("name");
				field.attr("name", str.replace("[x]", "["+participantsCount+"]"));
			}
        });
		
		// Assign remove participant handler on new 
		newParticipant.find("input.removeParticipant").click(function(event){
			jQuery(this).parent().parent().hide("slow", function () {
				jQuery(this).remove();
			});
		});
		
		newParticipant.find("input, select, textarea").change(addRemoveErrorClass);
		
		// Assign remove participant handler on new 
		newParticipant.find("input.participantAddTermin").click(function(event){
			
			var newParticipantTermin = participantTerminTemplate.clone().appendTo(jQuery(this).parent().parent());
			newParticipantTermin.show("slow");
			newParticipantTermin.removeClass("template");
			
			newParticipantTermin.find("input.participantRemoveTermin").click(function(event){
				jQuery(this).parent().hide("slow", function () {
					jQuery(this).remove();
				});
			});
			
			newParticipantTermin.find("select.trening").change(function(event){
				if (jQuery(this).val() != '')
				{
					var sel = jQuery(this).val();
					
					jQuery(this).parent().find("span.price").
						html(termini[sel].price > 0 ? termini[sel].price+" €" : "").
						show("slow");
					
					jQuery(this).parent().find("select.termin").
						removeOption(/./).
						addOption(termini[sel].date, false).
						show("slow");
					
				}
				else {
					jQuery(this).parent().find("select.termin").
						hide("slow");
					
					jQuery(this).parent().find("span.price").
						html("").
						hide("slow");
				}
			});
			
			newParticipantTermin.find("input, select, textarea").change(addRemoveErrorClass);
			
		});
		
		newParticipant.find("input.participantAddTermin").click();
	});

	jQuery("input.addParticipant").click();

	
	jQuery("#participantsRegistration").submit ( function() {
		
		var errorsFound = 0;
		

		
		jQuery("#participantsRegistration input:reallyvisible, #participantsRegistration select:reallyvisible").each(function(i){
			
			if (jQuery(this).val() == '')
			{
				errorsFound++;
				jQuery(this).addClass("error");
			}
			
		});
		
		if(jQuery("#participantsRegistration fieldset.participant:visible").size() <= 0)
		{
			alert('Minimalni broj sudionika po prijavi je jedan. Molimo dodajte barem jednog sudionika u prijavu!');
			return false;
		}
		
		if (errorsFound >= 1)
		{
			alert('Molimo popunite potrebna polja!');
			return false;
		}
		else {
			
			if (!jQuery('#policy').is(':checked')) {
				alert('Molimo prihvatite "Opće uvjete" ukoliko želite poslati prijavnicu!');
				return false;
			} else 
				return true;
		}
		
	});
	
});