$(document).ready(function(){

	$("#newCampaign").change(function(e){
		if ( $('#newCampaign').val().length < 2 )
			$('#_createNewCampaign').attr('disabled', true);
		else
			$('#_createNewCampaign').attr('disabled', false);
    });

    // Capture the enter key in the campaign textbox
    $("#newCampaign").keydown(function(e){
	if (e.keyCode == 13 && $("#newCampaign").val().length > 0 ) {
            $("#_createNewCampaign").click();
            return false;
	}
	if ( e.keyCode == 8 && $('#newCampaign').val().length < 2 )
		$('#_createNewCampaign').attr('disabled', true);
	else
		$('#_createNewCampaign').attr('disabled', false);
    });

    //
    // Campaign
    //
    $("#_createNewCampaign").click(function(e){
        e.preventDefault();
        var campaignName = $("#newCampaign").val();
        var affId        = $("#affId").val();

        if (campaignName === '') {
            alert('You must enter a Campaign name!');
            $("#newCampaign").focus();
            return false;
        }

        $.getJSON(
            '/addcampaign/',
            {affId: affId, label: campaignName},
            function (data, textStatus) {
                if (data.error) {

                    // Select the already existing entity
                    if (data.error.indexOf('already exists')) {
                        $("#campaign option").each(function(index){
                            var optionValue       = this.text.toLowerCase();
                            var campaignNameLower = campaignName.toLowerCase();
                            if (optionValue == campaignNameLower) {
                                this.selected = true;
                                $("#newCampaign").blur();
                                $("#campaign").focus();
                                return false;
                            }
                        });
                    } else {
                        $("#newCampaign").focus();
                    }

                    $("#newCampaign").value = "";
                    $("#campaignCreateSuccess").hide();
                    $("#campaignCreateError").text(data.error).fadeIn("normal");
                    return false;
                }

                var campaign = data.campaign;
                $("#campaign")
                    .append('<option value="' + campaign.id + '">' + campaign.label + '</option>')
                    .val(campaign.id)
                    .focus();
                $("#newCampaign").val("").blur();
                $("#campaignCreateError").hide();
                $("#campaignCreateSuccess").fadeIn("normal", function(){
                    t = setTimeout(function(){
                        $("#campaignCreateSuccess").fadeOut();
                    }, 5000);
                });
            }
        );

        return false;
    });

    $("#affiliateCampaign").change(function(){
        $("#campaignCreateSuccess").fadeOut();
        $("#campaignCreateError").fadeOut();
    });

	if($('input[name=destination]:eq(1)').attr('checked') || $('#yourTheaterId').val() != 0) {
		$('#aebnTheaterId option:first').attr('selected', true);
		$('#aebnDestinationContainer').hide();
		$('input[name=destination]:eq(0)').removeAttr('checked');
		$('input[name=destination]:eq(1)').attr('checked', 'true');
	}
	else {
		$('#yourTheaterId option:first').attr('selected', true);
		$('#customTemplatesContainer').hide();
		$('input[name=destination]:eq(0)').attr('checked', 'true');
		$('input[name=destination]:eq(1)').removeAttr('checked');
	}

	$('#destinationSelection').change(function() {
		if(this.value != 0)
			$('#affiliateTheater').val($('#affiliateTheater option:first').val());
	});

	$('#affiliateTheater').change(function() {
		if(this.value != 0)
			$('#destinationSelection').val($('#destinationSelection option:first').val());
	});

	calibrate_options = function() {
		if((!$('#yourTheaterId').val() || $('#yourTheaterId').val() == "0") && (!$('#aebnTheaterId').val() || $('#aebnTheaterId').val() == "0")) {
			$('#category').css('background-color', '#f0f0f0');
			$('#category').css('border', '1px solid #b0b0b0');
			$('#category option').remove();
			$('#category').click(function() { alert('Please select a Destination Theater to see a list of categories.'); });
			$('#category').attr('readonly', true);
			$('#size').css('background-color', '#f0f0f0');
			$('#size').css('border', '1px solid #b0b0b0');
			$('#size option').remove();
			$('#size').attr('readonly', true);
			$('#size').click(function() { alert('Please select a Destination Theater and Category to see a list of sizes.'); });
		}
		else if(!$('#category').val() || $('#category').val() == "0") {
			$('#size').css('background-color', '#f0f0f0');
			$('#size').css('border', '1px solid #b0b0b0');
			$('#size option').remove();
			$('#size').attr('readonly', true);
			$('#size').click(function() { alert('Please select a Category to see a list of sizes'); });
		}
	}
	calibrate_options();
});

