/* Functions for the Sites by Joe multi-picker box */


// found this function online to de-dupe my array of college ids
Array.prototype.unique = function () {
    var r = new Array();
    o:for(var i = 0, n = this.length; i < n; i++)
    {
        for(var x = 0, y = r.length; x < y; x++)
        {
                if(r[x]==this[i])
                {
                				//alert('this is a DUPE!');
                        continue o;
                }
        }
        r[r.length] = this[i];
    }
    return r;
}

jQuery.fn.extend({
    
    removeFromArray: function(value) {
        return this.filter(":input").val(function(i, v) {
           return $.grep(v.split(','), function(val) {  
                    return val != value;
                  }).join(',');
        }).end();
    }
});

/* this runs through all the checkboxes and styles and checks them as necessary */
function check_checkboxes() {
    
    var checked_items = $('#temp_list').val().split(",");
   
    //console.log(checked_items);
   
    $(".multi_picker input[type='checkbox']").each(function() {
        for (i = 0; i < checked_items.length; i++) {
		        if ($(this).attr('id') == checked_items[i]) {
		        		//console.log('checked');
		        		$(this).attr('checked', true);
		        }
		        
		        if ($(this).attr('checked')) {
		        		$(this).parent().css('backgroundColor', '#EEF1F9');
		        } else {
		        		$(this).parent().css('backgroundColor', 'transparent');
		        }
		        //console.log($(this));
        }
    });
    
    checked_items = null;
}

/* this clears out the temp field so it can be reused */
function clear_all_items() {
     
     $('#temp_list').val(null);  
     //$('input[name="scholarship_colleges"]').val(null);
}

/* this function repopulates the multipicked items if the form was sent back */
function repopulate_multipicked_items(field_name, destination, url)
{
		if ($(field_name).val().length > 0) {

		    // get the college names to match the ids            
		    content = $.ajax({
		        url: url,
		        global: false,
		        type: "POST",
		        data: ({ ids : $(field_name).val() }),
		        dataType: "html",
		        async:false,
		        success: function(msg) { 
		            //alert(msg);
		        }
		    }).responseText;
        
        // stick them under the college link as a string
        $(destination).empty();
        $(destination).append(content);
		}
}

/* eof */
