﻿(function ($) {
    $.fn.emptySelect = function () {
        return this.each(function () {
            if (this.tagName == 'SELECT') this.options.length = 0;
        });
    }

    $.fn.loadSelect = function (optionsDataArray) {
        //alert('in load select');
        return this.emptySelect().each(function () {
            //alert(this.tagName);
            if (this.tagName == 'SELECT') {
                var selectElement = this;
                $.each(optionsDataArray, function (index, optionData) {
                    var option = new Option(optionData.optionDisplay,
			    optionData.optionValue);
                    //alert('in loop'+optionData.optionDisplay+optionData.optionValue);
                    if ($.browser.msie) {
                        selectElement.add(option);
                    }
                    else {
                        selectElement.add(option, null);
                    }
                });
            }
        });
    }
})(jQuery);

jQuery(document).ready(function () {

    $("#GroundToBeSponsoredName").val($("#GroundToBeSponsored :selected").text());
    $("#GroundToBeSponsored").change(function () {
        $("#GroundToBeSponsoredName").val($("#GroundToBeSponsored :selected").text());
    });

    //        if ($("#leagueteams").length > 0) {
    //            $.getJSON(
    //		        '/grounds/getteams/1',
    //		        {},
    //		        function(data){
    //			        if (data.length) {		
    //				        $("#leagueteams").loadSelect(data);
    //			        }
    //		        }
    //	        );
    //	    }    

    $('#LeagueInfo').change(function () {
        $.getJSON(
			    '/grounds/getteams/' + $(this).val(),
			    {},
			    function (data) {
			        if (data.length) {
			            $("#leagueteams,#PremierLeagueTeamInfo").loadSelect(data);
			        }
			    }
		    );
    });
});
