numeric_custom=function(a,b,order){ 

	a = replacechar(replacechar(a,",",""),"$","");
	b = replacechar(replacechar(b,",",""),"$","");
	
	if (isNaN(a) || isNaN(b))
	{
		return ((isNaN(a) ? 0 : 1) > (isNaN(b) ? 0 : 1)?1:-1)*(order=="asc"?1:-1);
    }
    else
    {
        return (parseFloat(a)>parseFloat(b)?1:-1)*(order=="asc"?1:-1);
    }
      
}

function replacechar(input, find, replacement)
{
	return input.split(find).join(replacement);
}

function CollegeGrid(id,GetDataURL,ComparisonHandler)
{
	
	this.id = id;
	
	this.DataURL = GetDataURL;
	this.currentWidth = $("#SchoolDataGrid").width();
	
	if (ComparisonHandler) {this.ComparisonHandler = ComparisonHandler}
	
	this.Load = function(){
	
		var objreference = this

		if (this.grid) this.grid.destructor()
		
		this.grid = new dhtmlXGridObject(this.id);
		
		this.grid.enableAutoHeight(true,550,false);
		
		this.grid.enableAutoWidth(true,this.currentWidth,965);
		
		this.grid.setImagePath("_inc/client/js/dhtmlxGrid/codebase/imgs/");
					
		this.grid.setSkin("collegeresults");
		this.grid.setAwaitedRowHeight(26);
		this.grid.enableSmartRendering(true);
	
		$("#" + this.id).mask("Loading Colleges...");
		
		//this.grid.init();

		this.grid.load(this.DataURL(), function(){DataLoaded.call(objreference)})

	};
		
	function DataLoaded()
	{

		// Set Pre-defined Sorting for college column since complex data resides in college column
	    this.grid.attachEvent("onBeforeSorting",function(ind,type,direction){
            if (ind == this.getUserData("","collegeColumnIndex")){                                      // if sorting for a problematic column
                this.sortRows(this.getColumnsNum() - 1,type,direction);         // sort grid by the column with prepared values
                this.setSortImgState(true,ind,direction);    // set a correct sorting image
                return false;                                          // block default sorting
            }
            return true;   
        });
        
		this.grid.sortRows(this.grid.getUserData("","sortColumnIndex"),"int","desc");
		$("#resultCount").html(this.grid.getUserData("","recordCount") + " College(s)");
		$("#outcomeMeasure").html(this.grid.getUserData("","outcomeMeasure"));
		
		// stop propogation of click event when 'more info' link is clicked in header cell
		$(".hdrcell a").bind("click", function(e){e.stopPropagation()});
		
		$("#" + this.id).unmask();
	};
	
	//********************************
	// School Comparison
	//********************************
	function displayInstitutionMaximumAlert()
	{
		alert("NO MORE THAN 100 COLLEGES CAN BE COMPARED\n=========================================\nThere are currently " + $("input[name='chkGroupInstitution']:checked").length + " colleges that have been selected for comparison. Please reduce the number of colleges you wish to compare to 100 or fewer colleges.");
	}
		
	this.compareSelectedSchools = function(){
		
		if ($("input[name='chkGroupInstitution']").length > 0){
		

			if ($("input[name='chkGroupInstitution']:checked").length < 2)
			{		
				alert("You must select at least two colleges to compare.");
			}
			else if ($("input[name='chkGroupInstitution']:checked").length > 100)
			{		
				displayInstitutionMaximumAlert();
			}
			else
			{
			
				var arrSelectedInstitutions = new Array();
				
				$("input[name='chkGroupInstitution']:checked").each(
					function()
					{
						arrSelectedInstitutions[arrSelectedInstitutions.length] = this.value;
					}
				);
				
				$("#txtInstIDs").val(arrSelectedInstitutions.join(","));
				
				if (this.ComparisonHandler)
				{
					this.ComparisonHandler(arrSelectedInstitutions.join(","));
				}
				else
				{
					this.Load();
				}

			}

		}
		
	}

	this.resetSelectedSchools = function()
	{
		$("#txtInstIDs").val($("#txtOrigInstIDs").val());
		this.Load();
	}

	this.selectInsitutions = function(bSelectAll){

		$("input[name='chkGroupInstitution']").each(
			function()
			{
				this.checked = bSelectAll;
			}
		);
					
	}
	    
}

$(function(){


	/********************************************
		Handles Category Selection from Menu
	********************************************/
	$("#SchoolDataMenu a").click(function()
	{
	
		var categoryId = this.id.split("_")[1];

		// check whether tab is already selected
		if (categoryId != $("#txtSelectedCategory").val())
		{
			// select tab
			$(this).addClass("selected");
			
			// unselect all other tabs
			$("#SchoolDataMenu a:not(#" + this.id + ")").each(function(){
				$(this).removeClass("selected");
			})
			
			$("#txtSelectedCategory").val(categoryId);
			
			mygrid.Load();
			
		}
		
	
	});

	/********************************************
		Handles click of Update table Data button
	********************************************/	
	$("#btUpdateGrid").click(function(){
		$("#outcomeMeasure").removeClass("expanded").addClass("expandable");
		$("#outcomeMeasureSelection").slideUp();
		mygrid.Load();
	});
	
})

