
//Function: Get club names from database
function getClubNames () {

	jQuery.post(jQuery('#ajax_url').val(), {
		action: "getclubnames",
		value: jQuery('input[name=Contact-TeamName]').val()
	}, function(data) {
		jQuery('#clubnames').html(data);
	});

}

//Function: Ajax update summary field
function getSummary () {

	jQuery.post(jQuery('#ajax_url').val(), {
		action: "summary",
		value: jQuery('#submissionForm').serialize()
	}, function(data) {
		jQuery('#summary-table').html(data);
	});
}

//Function: Change club name
function changeClubName ( clubname ) {

	jQuery('input[name=Contact-TeamName]').val(clubname);

}

//Function: Reamove a team-row from the list
function removeTeam ( element ) {
	if(jQuery(element).parent().parent().hasClass('team')) {
		jQuery(element).parent().parent().remove();
	}
	
}

//Function: Reamove a allergy-row from the list
function removeAllergy ( element ) {
	if(jQuery(element).parent().parent().hasClass('allergy')) {
		jQuery(element).parent().parent().remove();
	}
	
}

//Function: On focus of a input field
function focusInput( elem ) {

	jQuery(elem).addClass('active');
	if(jQuery(elem).parent().children('.description').length > 0) {
		if(jQuery(elem).parent().children('.description').children('p').html() != "") {
			jQuery(elem).parent().children('.description').css('left' , ( jQuery(elem).width() + jQuery(elem).position().left + 15 ) + 'px');
			jQuery(elem).parent().children('.description').css('visibility','visible');
		}
	}

}

//Function: On blur of a input field
function blurInput( elem ) {

	jQuery(elem).removeClass('active');
	if(jQuery(elem).parent().children('.description').length > 0) {
		jQuery(elem).parent().children('.description').css('visibility','hidden');
	}

}

jQuery(document).ready(function($){
	$('input[name=Contact-TeamName]').keyup(function(){
		getClubNames();
	}).focus(function(){
		$('#clubnames').slideDown('fast');
	}).blur(function(){
		$('#clubnames').animate({ opacity: 1 },100,'',function(){ $(this).slideUp('fast'); });
	});

	$('input').focus(function(){
		focusInput(this);
	});
	$('input').blur(function(){
		blurInput(this);
	});

	jQuery('#summary-button').click(getSummary);

	function sum_count_guests(strWho) {
		intSum = 0;
		if (parseInt(jQuery( "[name=Lodging-school_" + strWho + "_girls]").attr("value")) > 0)
		    intSum += parseInt(jQuery( "[name=Lodging-school_" + strWho + "_girls]").attr("value"));
		if (parseInt(jQuery( "[name=Lodging-school_" + strWho + "_boys]").attr("value")) > 0)
		    intSum += parseInt(jQuery( "[name=Lodging-school_" + strWho + "_boys]").attr("value"));
		jQuery( "#count_" + strWho ).html(intSum + ' st');
	}

	jQuery("input.count_youth").change(function () {sum_count_guests('youth');});
	jQuery("input.count_youth").ready(function () {sum_count_guests('youth');});

	jQuery("input.count_senior").change(function () {sum_count_guests('senior');});
	jQuery("input.count_senior").ready(function () {sum_count_guests('senior');});

	jQuery('#addteam').click(function(){
	    var clonedRow = jQuery( "#teams tr:last" ).clone();
	    intRowId = parseInt(jQuery( "#teamID", clonedRow ).attr("value"));
	    intNewRowId = 1 + intRowId;
	    jQuery( "#teamID", clonedRow ).attr("value", intNewRowId );
	    jQuery( "[name^='Team-ClassID-']", clonedRow).attr("name", "Team-ClassID-" + intNewRowId);
	    jQuery( "[name^='Team-JerseyColor-']", clonedRow).attr("name", "Team-JerseyColor-" + intNewRowId);
	    jQuery( "[name^='Team-Players-']", clonedRow).attr("name", "Team-Players-" + intNewRowId);
	    jQuery( "[name^='Team-Leaders-']", clonedRow).attr("name", "Team-Leaders-" + intNewRowId);
	    jQuery( "[name^='Team-ID-']", clonedRow).attr("name", "Team-ID-" + intNewRowId).attr("value", "0");
	    jQuery( "[name^='Team-Active-']", clonedRow).attr("name", "Team-Active-" + intNewRowId);
	    jQuery( "#teams" ).append( clonedRow );

	    jQuery('input', clonedRow).focus(function(){
		focusInput(this);
	    });
	    jQuery('input', clonedRow).blur(function(){
		blurInput(this);
	    });

	    jQuery('button.red', clonedRow).css('visibility','visible');
	});
	jQuery('#addallergy').click(function(){
	    var clonedRow = jQuery( "#allergies tr:last" ).clone();
	    intRowId = parseInt(jQuery( "#maxAllergyID", clonedRow ).attr("value"));
	    intNewRowId = 1 + intRowId;
	    jQuery( "#maxAllergyID", clonedRow ).attr("value", intNewRowId );
	    jQuery( "[name^='Allergy-Type-]", clonedRow).attr("name", "Allergy-Type-" + intNewRowId);
	    jQuery( "[name^='Allergy-Info-']", clonedRow).attr("name", "Allergy-Info-" + intNewRowId);
	    jQuery( "#allergies" ).append( clonedRow );

	    jQuery('input', clonedRow).focus(function(){
		focusInput(this);
	    });
	    jQuery('input', clonedRow).blur(function(){
		blurInput(this);
	    });

	    jQuery('button.red', clonedRow).css('visibility','visible');
	});

	jQuery("#submissionForm").validate({
	    errorClass: "form-error",
	    errorElement: "p",
	    wrapper: "div class='form-error'"
	});

});