// Init
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------

var map_has_been_created=0;

$(document).ready(function(){	
		
	// Navigation Rollovers 
	setupNav();	
	
	flipA=0;
	flipB=0;
	
	// Setup Read More button
	$('#read_more_measure_text_button').click( function(){ $('#read_more_measure_text').toggle('fast', function(){ flipA++; button = (flipA%2)? $('#read_more_measure_text_button').attr('src', 'assets/buttons/read_less.gif') : $('#read_more_measure_text_button').attr('src', 'assets/buttons/read_more.gif'); } ); return false; } );
	
	// Setup Legend Display link
	$('#legend_link').click( function(){ $('#legend').toggle('slow', function(){ flipB++; button = (flipB%2)? $('#legend_link').text('Hide Legend') : $('#legend_link').text('What do these numbers mean?'); } ); return false; } );
	
	// Setup Compare Button
	$('#compare_button').click(function(){ sendToCompare($('#category').val(), $('#sub_category').val()); });
	
	// Depending on the page, perform the appropriate data pull		
	if($("#page").val() == 'home' || $("#page").val() == 'index' || $("#page").val() == ''){			
		getMeasureList('dropdown', $('#sf').val());
	}
	
	if($("#page").val() == 'our_reports'){		
		getMeasureList('sidebar', $('#sf').val());		
		getMeasureData($('#sf').val(), $('#category').val(), $('#sub_category').val()); 
	}
	
	if($("#page").val() == 'search'){		
		getMeasureList('sidebar', $('#sf').val());		
		getSearchResults($('#sf').val(), $('#category').val(), $('#sub_category').val(), $('#search_phrase').val()); 
	}
	
	if($("#page").val() == 'compare'){
		getMeasureList('sidebar', $('#sf').val());		
		getComparisonData($('#sf').val(), $('#category').val(), $('#sub_category').val(), $('#compare').val()); 
	}
	
	if($("#page").val() == 'profile'){
		getMeasureList('sidebar', $('#sf').val());
		getProfileInfo($('#sf').val(), $('#category').val(), $('#sub_category').val(), $('#name_id').val());
	}

	if($("#page").val() == 'cost_reports'){
		getMeasureData($('#sf').val(), $('#category').val(), $('#sub_category').val()); 
	}	
	
});


// Setup
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------

function setupNav(){
	$('#nav_link_home').mouseover( function(){ old = this.src; this.src = 'assets/nav/home_mo.gif'; }).mouseout( function(){ this.src = old; });
	$('#nav_link_our_reports').mouseover( function(){ old = this.src; this.src = 'assets/nav/our_reports_mo.gif'; }).mouseout( function(){ this.src = old; });
	$('#nav_link_learn_more').mouseover( function(){ old = this.src; this.src = 'assets/nav/learn_more_mo.gif'; }).mouseout( function(){ this.src = old; });
}


/* ----- Send to Compare 
------------------------------------------------------------------------------------- */

function sendToCompare(category, sub_category){
	
	var sf = $('#sf').val();
	var search_phrase = $('#search_phrase').val();
	
	// Get all of the compare check boxes
	compare_check_boxes = $('.compare_checkbox');
	
	// Build a string of the checked boxes
	checked_boxes = '';
	for(i=0; i<compare_check_boxes.length; i++){
		checked_boxes = (compare_check_boxes[i].checked)? checked_boxes+compare_check_boxes[i].value+"," : checked_boxes+"";
	}
	
	// Send checked boxes along with category/sub_category to comparison page (if 2 or 3 entries have been checked)
	if( (checked_boxes.split(",")).length !=3 && (checked_boxes.split(",")).length !=4 ){ alert("Please choose either 2 or 3 entries for comparision."); }
	else {
		window.location = "compare.php?sf="+sf+"&category="+category+"&sub_category="+sub_category+"&compare="+checked_boxes+"&search_phrase="+search_phrase+"";
	}
	
	return false;
}


// Getting Feeds
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------


/* ----- Get Search Results
------------------------------------------------------------------------------------- */

function getSearchResults(sf, category, sub_category, search_phrase){
	
	if(!search_phrase){ window.location = "our_reports.php?sf="+sf+"&category="+category+"&sub_category="+sub_category+""; }
	else {
		// Strip of all but first word until a more robust search has been implemented
		//search_phrase = search_phrase.split(' ');
		//search_phrase = search_phrase[0];
		
		search_phrase = search_phrase.replace(/ /g, '_');
		
		feed = (sf=='group')? 'feed=group_search/'+search_phrase+'/' : 'feed=clinic_search/'+search_phrase+'/';
		feed = (category!='all')? feed + ''+category+'' : feed + '';
				
		$('#data_table_loading').html("<div id='data_table_loading_inner'><img alt='Loading...' src='assets/loading.gif' /></div>");
		
		$.ajax({
			type: "POST", url: "actions/get_xml.php",
			data: feed,
			success: 	function(xml){ populateDataList(sf, category, sub_category, xml); },		
			error: 		function(xml){ $('#error_feedback').append(""); populateDataList(sf, category, sub_category, xml); },
			complete: 	function(){ $('#data_table_loading').remove(); }
		});
	}	
}


/* ----- Get Measure List 
------------------------------------------------------------------------------------- */

function getMeasureList(type, sf){		
	$.ajax({
		type: "POST", url: "actions/get_xml.php",
		data: 'feed=measure_list/'+sf+'',
		success: 	function(xml){	
						if(type=='dropdown'){ populateCategoryDropdown(xml); }	
						if(type=='sidebar'){ populateCategorySidebar(xml); }
					},		
		error: 		function(){ $('#error_feedback').append(""); }	
	});
}


/* ----- Get Measure Data 
------------------------------------------------------------------------------------- */

function getMeasureData(sf, category, sub_category){
	
	feed = (category == 'all')? 'feed=medical_group' : 'feed=measure_data_current/'+category+'';
	feed = (sub_category)? 'feed=measure_component_current/'+sub_category+'' : feed;
	
	$('#data_table_loading').html("<div id='data_table_loading_inner'><img alt='Loading...' src='assets/loading.gif' /></div>");
	
	$.ajax({
		type: "POST", url: "actions/get_xml.php",
		data: feed,
		success: 	function(xml){ populateDataList(sf, category, sub_category, xml); },		
		error: 		function(){ $('#error_feedback').append(""); },
		complete: 	function(){ $('#data_table_loading').remove(); }
	});
}


/* ----- Get Comparison Data 
------------------------------------------------------------------------------------- */

function getComparisonData(sf, category, sub_category, compare){
	$('#data_table_loading').html("<div id='data_table_loading_inner'><img alt='Loading...' src='assets/loading.gif' /></div>");
	compare_ids = compare.split(',');	
	
	row = 1;
	
	if(category == 'all'){
		
		cats = new Array();
		cat_names = new Array();
		
		// if category = all
		// Find array of categories
		feed = 'feed=measure_list/'+sf+'';	
		$.ajax({
			type: "POST", url: "actions/get_xml.php",
			data: feed,
			success: 	function(xml){ 
				$('measure-category',xml).each(function(i){
					id = $(this).find('id:first').text();
					cats.push(id);
					name = $(this).find('public-name:first').text();
					cat_names.push(name);
				});
				
				// loop through each category, set row to j, add the new row "after" previous row, and category = category
				for(j=0; j<cats.length; j++){
					row_color = (j%2)? " odd " : " even ";
					$('#compare_row_'+(row-1)+'').after(
						"<tr id='compare_row_"+row+"'>"+
							"<td id='compare_row_"+row+"_category' class='compare_sidebar_mid'>"+cat_names[j]+"</td>"+
							"<td id='compare_row_"+row+"_col_1_data' class='compare_main_mid graph_area "+row_color+"'></td>"+
							"<td id='compare_row_"+row+"_col_2_data' class='compare_main_mid graph_area "+row_color+"'></td>"+
							"<td id='compare_row_"+row+"_col_3_data' class='compare_main_mid graph_area "+row_color+"'></td>"+
						"</tr>"
					);
					
					// loop through the compare ids and send to getComparisonData2 for each, set col = i and row = 1
					for(i=0; i<(compare_ids.length-1); i++){ getComparisonData2(i, row, sf, cats[j], sub_category, compare_ids[i]); }	
					
					row++;
				}
				
			},		
			error: 		function(){ $('#error_feedback').append(""); },
			complete: 	function(){ }
		});		
		
	}
	else {
		
		$('#compare_row_0').after(
			"<tr id='compare_row_1'>"+
				"<td id='compare_row_1_category' class='compare_sidebar_mid'></td>"+
				"<td id='compare_row_1_col_1_data' class='compare_main_mid graph_area odd'></td>"+
				"<td id='compare_row_1_col_2_data' class='compare_main_mid graph_area odd'></td>"+
				"<td id='compare_row_1_col_3_data' class='compare_main_mid graph_area odd'></td>"+
			"</tr>"
		);
		
		// If a single category is selected
		// loop through the compare ids and send to getComparisonData2 for each, set col = i and row = 1
		for(i=0; i<(compare_ids.length-1); i++){ getComparisonData2(i, row, sf, category, sub_category, compare_ids[i]); }	
	}
	
}

function getComparisonData2(i, row, sf, category, sub_category, compare_id){	
	feed = (sf=='group')? 'feed=group_measure_history/'+category+'/'+compare_id+'' : 'feed=clinic_measure_history/'+category+'/'+compare_id+''
	if(sub_category!=''){
		feed = (sf=='group')? 'feed=group_component_history/'+sub_category+'/'+compare_id+'' : 'feed=clinic_component_history/'+sub_category+'/'+compare_id+'';
	}
	$.ajax({
		type: "POST", url: "actions/get_xml.php",
		data: feed,
		success: 	function(xml){ populateComparison(1, compare_id, i, row, sf, category, sub_category, xml); },		
		error: 		function(xml){ populateComparison(0, compare_id, i, row, sf, category, sub_category, xml); },
		complete: 	function(){  }
	});	
}


/* ----- Get Profile Info / Data 
------------------------------------------------------------------------------------- */

function getProfileInfo(sf, category, sub_category, name_id){
		
	feed = (sf=='group')? 'feed=group_profile/'+name_id+'' : 'feed=clinic_profile/'+name_id+'';
	$('#data_table_loading').html("<div id='data_table_loading_inner'><img alt='Loading...' src='assets/loading.gif' /></div>");

	$.ajax({
		type: "POST", url: "actions/get_xml.php",
		data: feed,
		success: 	function(xml){ populateProfileInfo(sf, name_id, category, xml); getProfileData(sf, category, sub_category, name_id); },		
		error: 		function(){ $('#error_feedback').append(""); },
		complete: 	function(){ $('#data_table_loading').remove(); }
	});	
}

function getProfileData(sf, category, sub_category, name_id){
	
	$('#profile_data_table_loading').html("<div id='data_table_loading_inner'><img alt='Loading...' src='assets/loading_white.gif' /></div>");
	$('#profile_category_data').append("<table id='profile_category_data_table'></table>");
	
	if(category=='all'){
		
		cats = new Array();
		cat_names = new Array();
		
		// if category = all
		// Find array of categories
		feed = 'feed=measure_list/'+sf+'';	
		$.ajax({
			type: "POST", url: "actions/get_xml.php",
			data: feed,
			success: 	function(xml){ 
				$('measure-category',xml).each(function(i){
					id = $(this).find('id:first').text();
					cats.push(id);
					name = $(this).find('public-name:first').text();
					cat_names.push(name);					
				});
				
				for(j=0; j<cats.length; j++){
					feed = (sf=='group')? 'feed=group_measure_history/'+cats[j]+'/'+name_id+'' : 'feed=clinic_measure_history/'+cats[j]+'/'+name_id+'';
					
					$.ajax({
						type: "POST", url: "actions/get_xml.php",
						data: feed,
						success: 	function(xml){ populateProfileData(1, sf, name_id, cats[j], sub_category, '', xml); },		
						error: 		function(xml){ populateProfileData(0, sf, name_id, cats[j], sub_category, '', xml); },
						complete: 	function(){ $('#profile_data_table_loading').remove(); }
					});	
				}
				
			},		
			error: 		function(){ $('#error_feedback').append(""); },
			complete: 	function(){ }
		});
	}
	else {
		feed = (sf=='group')? 'feed=group_measure_history/'+category+'/'+name_id+'' : 'feed=clinic_measure_history/'+category+'/'+name_id+'';
		if(sub_category!=''){
			feed = (sf=='group')? 'feed=group_component_history/'+sub_category+'/'+name_id+'' : 'feed=clinic_component_history/'+sub_category+'/'+name_id+'';
		}
		$.ajax({
			type: "POST", url: "actions/get_xml.php",
			data: feed,
			success: 	function(xml){ populateProfileData(1, sf, name_id, category, sub_category, '', xml); },		
			error: 		function(xml){ populateProfileData(0, sf, name_id, category, sub_category, '', xml); },
			complete: 	function(){ $('#profile_data_table_loading').remove(); }
		});	
	}
}


// Doing things with Feeds
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------


/* ----- Populate Measure Dropdown 
------------------------------------------------------------------------------------- */

function populateCategoryDropdown(xml){

	$('#measure_list_dropdown').empty();
	$('#measure_list_dropdown').append("<option value='all'>All</option>");
	
	// Parse XML and append data to the dropdown
	$('measure-category',xml).each(function(i){		
		// Find top level (non component) data
		id = $(this).find('id:first').text();			
		name = $(this).find('public-name:first').text();
		$('#measure_list_dropdown').append("<option value='"+id+"'>"+name+"</option>");
	});
}

/* ----- Populate Measure Sidebar 
------------------------------------------------------------------------------------- */

function populateCategorySidebar(xml){
	
	var page = $('#page').val();
	var sf = $('#sf').val();
	var category = $('#category').val();
	var sub_category = $('#sub_category').val();
	var name_id = $('#name_id').val();
	var compare = $('#compare').val();
	var search_phrase = $('#search_phrase').val();
	
	// Parse XML and append data to the sidebar
	$('measure-category',xml).each(function(i){
	
		// Find top level (non component) data
		id = $(this).find('id:first').text();			
		name = $(this).find('public-name:first').text();
		
		selected = (category == id)? " id='cat_"+id+"' class='selected' " : "";
		$('#sb_nav').append("<li "+selected+"><a class='sb_link' id='sb_nav_"+id+"' href='"+page+".php?sf="+sf+"&amp;search_phrase="+search_phrase+"&amp;category="+id+"&amp;name_id="+name_id+"&compare="+compare+"'>"+name+"</a></li>");
		
		// If this is the selected Category
		if(category == id){
			
			$("#cat_"+id+"").append("<ul id='sb_sub_nav'></ul>");
			
			// Find and Populate Component Measures
			$('component', $(this)).each(function(i){
				sub_cat_id = $(this).find('id').text();			
				name = $(this).find('public-name').text();
				
				sub_category_selected = (sub_category == sub_cat_id)? "class='selected'" : "";
				
				$("#sb_sub_nav").append("<li "+sub_category_selected+"><a id='sb_sub_nav_"+sub_cat_id+"' href='"+page+".php?sf="+sf+"&amp;category="+id+"&amp;sub_category="+sub_cat_id+"&amp;name_id="+name_id+"&compare="+compare+"'>"+name+"</a></li>");
			});
		}
		
	});
}

/* ----- Populate Data List 
------------------------------------------------------------------------------------- */

function populateDataList(sf, category, sub_category, xml){
	
	var count=0;

	// IF Category is ALL
	// --------------------------------------------------------------------------------	
	if(category == 'all'){
		
		$('#sort_by_rate').html('');
		
		// IF ON THE SEARCH PAGE
		// --------------------------------------------------------------------------------
		if( $('#page').val() == 'search' ){ 
			
			object_label = (sf=='group')? 'medical-group' : 'clinic';
			id_label = (sf=='group')? 'medical-group-identifier' : 'id';

			// Parse XML and append data to the dropdown
			$(object_label,xml).each(function(i){
				cat_id = $('#category').val();
				this_name_id = $(this).find(id_label).text();
				name = $(this).find('name:first').text();
				city = $(this).find('city:first').text();
				zipcode = $(this).find('zip-code:first').text();
				if(name){				
					$('#data_table_tbody').append(
						"<tr class='colbody_top'>"+
							"<td class='name'><a class='data_list_name' href='profile.php?sf="+sf+"&amp;category="+cat_id+"&amp;name_id="+this_name_id+"'>"+name+"</a></td>"+
							"<td class='secondary'>"+city+"</td>"+
							"<td class='secondary'>"+zipcode+"</td>"+
							"<td class='graph_area'></td>"+
							"<td class='view_details'><a href='profile.php?sf="+sf+"&amp;category="+cat_id+"&amp;name_id="+this_name_id+"'>view profile ></a></td>"+
							"<td class='compare_button_cell'><div class='compare_button'><input class='compare_checkbox' type='checkbox' name='' value='"+this_name_id+"' /></div></td>"+							
						"</tr>"
					);				
				}				
				count++;				
			});
		}	
		// IF NOT ON THE SEARCH PAGE
		// --------------------------------------------------------------------------------
		else {
			
			var object_label = (sf=='group')? 'medical-group' : 'active-clinic'; 
			var id_label = (sf=='group')? 'medical-group-identifier' : 'id'; 
			
			// Parse XML and append data to the dropdown
			$(object_label,xml).each(function(i){
				cat_id = $('#category').val();
				this_name_id = $(this).find(id_label).text();
				name = $(this).find('name:first').text();
				city = $(this).find('city:first').text();
				zipcode = $(this).find('zip-code:first').text();
				has_data = $(this).find('has-dds-data:first').text();
				
				if( ( sf=='group' && name ) || ( sf=='clinic' && has_data=='true' && name ) ){				
					$('#data_table_tbody').append(
						"<tr class='colbody_top'>"+
							"<td class='name'><a class='data_list_name' href='profile.php?sf="+sf+"&amp;category="+cat_id+"&amp;name_id="+this_name_id+"'>"+name+"</a></td>"+
							"<td class='secondary'>"+city+"</td>"+
							"<td class='secondary'>"+zipcode+"</td>"+
							"<td class='graph_area'></td>"+
							"<td class='view_details'><a href='profile.php?sf="+sf+"&amp;category="+cat_id+"&amp;name_id="+this_name_id+"'>view profile ></a></td>"+
							"<td class='compare_button_cell'><div class='compare_button'><input class='compare_checkbox' type='checkbox' name='' value='"+this_name_id+"' /></div></td>"+							
						"</tr>"
					);				
				}
				count++;		
			});	
		}
	
	}

	// IF Category is NOT ALL
	// --------------------------------------------------------------------------------		
	else {

		var id_label = (sf=='group')? 'medical-group-identifier' : 'clinic-id'; 
		var name_label = (sf=='group')? 'medical-group-name' : 'clinic-name';
		
		// Parse XML and append data to the dropdown
		$('public-measure-summary',xml).each(function(i){
			clinic_name = $(this).find('clinic-name').text();
		
			cat_id = $(this).find('measure-id:first').text();
			this_name_id = $(this).find(id_label+':first').text();
			name = $(this).find(name_label+':first').text();
			city = (sf=='group')? $(this).find('medical-group-city:first').text() : $(this).find('clinic-city:first').text();
			zipcode = (sf=='group')? $(this).find('medical-group-zip:first').text() : $(this).find('clinic-zip:first').text();
			rate = Math.round($(this).find('rate:first').text() *100);
			overall_rate = Math.round($(this).find('overall-rate:first').text() * 100);
			
			data_source = $(this).find('data-source:first').text();
			
			// Display asterisk if not DDS Data and viewing Diabetes or Vascular data
			if( ( (category == '1' || category == '2') && data_source == 'DDS') || ( category !='1' && category !='2' ) ){
				data_area = "<div class='rate_num left'>"+rate+"%</div>"+
							"<div class='average_bar left' style='position:relative; left: "+(overall_rate+3)+"px; line-height: 0px; width: 3px; height: 13px;'></div>"+
							"<div class='left'>"+
								"<div class='graph'>"+
									"<div class='rate' style='width: "+rate+"px;'></div></div>"+
								"</div>"+
							"<div class='clear'></div>";
			}
			else {
				data_area = "<div class='rate_num asterisk'>*</div>";
			}
			
			
			if( (sf=='clinic' && name) || (sf=='group' && name && clinic_name=='') ){			
				$('#data_table_tbody').append(
					"<tr class='colbody_top'>"+
						"<td class='name'><a class='data_list_name' href='profile.php?sf="+sf+"&amp;category="+cat_id+"&amp;name_id="+this_name_id+"'>"+name+"</a></td>"+
						"<td class='secondary'>"+city+"</td>"+
						"<td class='secondary'>"+zipcode+"</td>"+
						"<td class='graph_area'>"+
							data_area+
						"</td>"+
						"<td class='view_details'><a href='profile.php?sf="+sf+"&amp;category="+cat_id+"&amp;name_id="+this_name_id+"'>view profile ></a></td>"+
						"<td class='compare_button_cell'><div class='compare_button'><input class='compare_checkbox' type='checkbox' name='' value='"+this_name_id+"' /></div></td>"+
					"</tr>"
				);				
			}			
			count++;
		});
		
		// Footnotes
		if(sf=='group' && (category == '1')){
			$('#data_table_tfoot').append(
				"<tr class='colbody_top'>"+
					"<td colspan='6' class='footnote'>"+
						"* HealthScores shown here for diabetes are based on data provided by the medical group. No data was provided by this medical group. To view this medical group's score based on health plan data, click on the medical group's View Profile button."+
					"</td>"+
				"</tr>"
			);
		}
		if(sf=='group' && (category == '2')){
			$('#data_table_tfoot').append(
				"<tr class='colbody_top'>"+
					"<td colspan='6' class='footnote'>"+
						"* HealthScores shown here for vascular disease are based on data provided by the medical group. No data was provided by this medical group. To view this medical group's score based on health plan data, click on the medical group's View Profile button."+
					"</td>"+
				"</tr>"
			);		}
		
	}

	// If there were no results
	// --------------------------------------------------------------------------------	
	if(!count){
		$('#data_table_tbody').append(
			"<tr class='colbody_top'>"+
				"<td colspan='6' class='no_results'>"+
					"<b>We're sorry, there were no results found for your search.</b><br />"+
					"Here are a few ideas that will hopefully improve your search results:<br /><br />"+
					"1. Double check the spelling of your search.<br />"+
					"2. Try using fewer words.<br />"+
					"3. Try using just part of the word that you are looking for.<br />"+
				"</td>"+
			"</tr>"
		);
	}
	
}

/* ----- Populate Comparison Table 
------------------------------------------------------------------------------------- */

function populateComparison(success, compare_id, col, row, sf, category, sub_category, xml) {
		
	$('#data_table_loading').remove();
	
	col = col+1;
	
	if(success){
		
		shown=0;
		
		// Parse XML and append data to the dropdown
		$('public-measure-summary',xml).each(function(i){
			
			data_source = $(this).find('data-source').text();
			
			measure_period_id = $(this).find('measure-period-id').text();
			current_measure_period_id = $(this).find('current-measure-period-id').text();
			
			//alert(measure_period_id+" : "+current_measure_period_id);
			
			//measure_period_name = $(this).find('measure-period-name').text();			
			//year = measure_period_name.split('Report');
			//year = year[0].replace(/^\s*|\s*$/g,''); // Remove white space
			
			cat_id = $(this).find('measure-id:first').text();
			cat_name = $(this).find('measure-name:first').text();
			if(sub_category!=''){ cat_name = $(this).find('component-name:first').text(); }
			this_name_id = (sf=='group')? $(this).find('medical-group-identifier:first').text() : $(this).find('clinic-id:first').text()
			name = (sf=='group')? $(this).find('medical-group-name:first').text() : $(this).find('clinic-name:first').text()
			rate = Math.round($(this).find('rate:first').text() *100);
			
			
			if(measure_period_id == current_measure_period_id){
				shown=1;
				$('#compare_row_'+row+'_category').html(cat_name);			
				$('#compare_col_'+col+'_title').html(name);
				$('#compare_row_'+row+'_col_'+col+'_data').html("<div class='rate_num left'>"+rate+"%</div><div class='graph left'><div class='rate' style='width: "+rate+"px;'></div></div><div class='clear'></div>");
			}
			
			// If data is not available for the current year (2008)
			if( measure_period_id != current_measure_period_id && shown==0 ){
				$('#compare_row_'+row+'_category').html(cat_name);			
				$('#compare_col_'+col+'_title').html(name);
				$('#compare_row_'+row+'_col_'+col+'_data').html("<div class='rate_num'>n/a</div>");
			}
			
			
		}
		);
	}
	else{
		
		//$('#compare_row_1_category').html('');
		
		feed = (sf=='group')? 'feed=group_profile/'+compare_id+'' : 'feed=clinic_profile/'+compare_id+'';
		
		$.ajax({
			type: "POST", url: "actions/get_xml.php",
			data: feed,
			success: 	function(xml){ 
				object_label = (sf=='group')? 'medical-groups' : 'clinic';
				$(object_label,xml).each(function(i){
					name = $(this).find('name:first').text();
					$('#compare_col_'+col+'_title').html(name);
				});
			},		
			error: 		function(){ $('#error_feedback').append(""); },
			complete: 	function(){ }
		});	
		
		$('#compare_row_'+row+'_col_'+col+'_data').html("<div class='rate_num'>n/a</div>");
	}
	
}

/* ----- Populate Profile Info 
------------------------------------------------------------------------------------- */

function populateProfileInfo(sf, name_id, category, xml){	
		
		//alert($('#show_affiliations').html());
		//aff_text = (sf=='group')? 'Clinics' : 'Medical Group';		
		//$('#show_affiliations').html(aff_text);
		
		$('#show_overview').click(function(){
			$('#profile_affiliations').hide(); 	$('#show_affiliations').removeClass('selected');	
			$('#profile_location').hide();		$('#show_location').removeClass('selected');
			$('#profile_overview').show();		$('#show_overview').addClass('selected');
			
			$('#profile_category_data').show();
			
			return false;
		});
		$('#show_affiliations').click(function(){
			$('#profile_location').hide();		$('#show_location').removeClass('selected');
			$('#profile_overview').hide();		$('#show_overview').removeClass('selected');
			$('#profile_affiliations').show();	$('#show_affiliations').addClass('selected');

			$('#profile_category_data').hide();

			return false;
		});
		$('#show_location').click(function(){
			$('#profile_overview').hide();		$('#show_overview').removeClass('selected');
			$('#profile_affiliations').hide();	$('#show_affiliations').removeClass('selected');
			$('#profile_location').show();		$('#show_location').addClass('selected');

			$('#profile_category_data').hide();

			if(!map_has_been_created){
				showMap(sf, name_id, category, xml);
			}
			return false;
		});		


	var object_label = (sf=='group')? 'medical-groups' : 'clinic'; 


	// Parse XML and append data to the dropdown
	$(object_label, xml).each(function(i){				
		
		name = $(this).find('name:first').text();
		
		addressline1 = $(this).find('address-line1:first').text();
		addressline2 = $(this).find('address-line2:first').text();
		city = $(this).find('city:first').text();
		state = $(this).find('state:first').text();
		zipcode = $(this).find('zip-code:first').text();

		web = $(this).find('website').text();		
		web_format = web.split('.');
		web = (web_format[0]=='www')? "http://"+web : web;		
		web = (web!='')? "Web: <a target='_blank' href='"+web+"'>"+web+"</a>" : "";
		
		main_phone = $(this).find('main-phone').text();		
		main_phone = main_phone.replace(/-/g,'');
		main_phone = main_phone.split('');
		main_phone = (main_phone!='')? "Main Phone: "+main_phone[0]+main_phone[1]+main_phone[2]+"-"+main_phone[3]+main_phone[4]+main_phone[5]+"-"+main_phone[6]+main_phone[7]+main_phone[8]+main_phone[9] : "";	
		
		tf_phone = $(this).find('toll-free-phone').text();
		tf_phone = tf_phone.replace(/-/g,'');
		tf_phone = tf_phone.split('');
		tf_phone = (tf_phone!='')? "Toll Free: "+tf_phone[0]+tf_phone[1]+tf_phone[2]+"-"+tf_phone[3]+tf_phone[4]+tf_phone[5]+"-"+tf_phone[6]+tf_phone[7]+tf_phone[8]+tf_phone[9] : "";
		

		// 
		// Overview
		// -------------------------------------------------------		
		$('#profile_name').append(""+name+"");
		$('#profile_overview').append(
			"<table>"+
			"<tr>"+
				"<td colspan='2'>"+web+"</td>"+""+
			"</tr>"+
			"<tr>"+
				"<td>"+main_phone+"</td>"+"<td>"+tf_phone+"</td>"+
			"</tr>"+		
			"</table>"	
		);
		
		
		// 
		// Affiliations
		// -------------------------------------------------------		
		affiliations_title = (sf=='group')? "<h3 class='affiliation_title'>Affiliated Clinics:</h3>" : "<h3 class='affiliation_title'>Affiliated Group:</h3>";
		
		$('#profile_affiliations').append(affiliations_title+"<table id='profile_affiliation_table'></table>");
		
		// 
		// If viewing a GROUP PROFILE, 
		// find all "active clinics" and report them as affiliations
		// -------------------------------------------------------
		$('active-clinic', xml).each(function(j){
			
			id = $(this).find('id').text();			
			name = $(this).find('name').text();
			city = $(this).find('city').text();
			state = $(this).find('state').text();
			zipcode = $(this).find('zip-code').text();
			county = $(this).find('county-name').text();
			
			$('#profile_affiliation_table').append(
				"<tr>"+
					"<td class='profile_affiliation_name'><a href='profile.php?sf=clinic&category="+category+"&name_id="+id+"'>"+name+"</a></td>"+
					"<td class='profile_affiliation_link'><a href='profile.php?sf=clinic&category="+category+"&name_id="+id+"'>view profile ></a></td>"+
				"</tr>"+	
				"<tr style='background: #E1EAE6;'>"+
					"<td class='profile_affiliation_info' colspan='2'>"+city+", "+state+" "+zipcode+"</td>"+
				"</tr>"
			);			
		});

		// 
		// If viewing a CLINIC PROFILE, 
		// find the "medical-group-name" and report it as this clinic's affiliation
		// -------------------------------------------------------
		$('clinic', xml).each(function(j){
			
			id = $(this).find('medical-group-identifier').text();			
			name = $(this).find('medical-group-name').text();
			city = $(this).find('city').text();
			state = $(this).find('state').text();
			zipcode = $(this).find('zip-code').text();
			county = $(this).find('county-name').text();
			
			$('#profile_affiliation_table').append(
				"<tr>"+
					"<td class='profile_affiliation_name'><a href='profile.php?sf=group&category="+category+"&name_id="+id+"'>"+name+"</a></td>"+
					"<td class='profile_affiliation_link'><a href='profile.php?sf=group&category="+category+"&name_id="+id+"'>view profile ></a></td>"+
				"</tr>"+	
				"<tr style='background: #E1EAE6;'>"+
					"<td class='profile_affiliation_info' colspan='2'>"+city+", "+state+" "+zipcode+"</td>"+
				"</tr>"
			);			
		});		

	});		
}

/* ----- Populate Profile Data 
------------------------------------------------------------------------------------- */

function populateProfileData(success, sf, name_id, category, sub_category, category_name, xml) {
		
	if(success){	
		count=0;
		// Parse XML and append data to the dropdown
		$('public-measure-summary',xml).each(function(i){
			count++;
					
			data_source = $(this).find('data-source').text();
			measure_period_name = $(this).find('measure-period-name').text();
			
			current_measure_period_id = $(this).find('current-measure-period-id').text();
			measure_period_id = $(this).find('measure-period-id').text();
			
			//alert(current_measure_period_id+" : "+measure_period_id);
			
			year = measure_period_name.split('Report');
			year = year[0];
			
			cat_id = $(this).find('measure-id:first').text();
			cat_name = (sub_category=='')? $(this).find('measure-name:first').text() : $(this).find('component-name:first').text();
			this_name_id = $(this).find('medical-group-identifier:first').text();
			name = $(this).find('medical-group-name:first').text();
			overall_rate = Math.round($(this).find('overall-rate:first').text() *100);
			rate = Math.round($(this).find('rate:first').text() *100);
	
			//alert(count+" - "+measure_period_name);
			
			if(name && count==1){			
				$('#profile_category_data_table').append(
					"<tr><td colspan='2' class='category'><h2>"+cat_name+"</h2></td></tr>"+
					"<tr>"+
					"<td class='current_report_year_title'><h4>Current Report Year</h4></td>"+
					"<td class='historic_report_years_title'><h4>Historic Report Years</h4></td>"+
					"</tr>"
				);
				
				// IF VIEWING CURRENT REPORT YEAR
				if(current_measure_period_id == measure_period_id){
					$('#profile_category_data_table').append(
						"<tr class='colbody_top'>"+
						"<td class='current_report_year'><!-- Current Report Year -->"+
							"<div class='graphs'>"+
								"<table><tr>"+
								"<td style='width: 90px;'>"+
									"<span class='graphs_small_text'>Rate:</span> "+rate+"%<br />"+
									"<div style='line-height: 0px; position: relative; top: "+(104-overall_rate)+"px; left: 66px;'>*</div>"+
									"<div style='background: #000; line-height: 0px; font-size: 0px; height: 3px; position: relative; top: "+(100-overall_rate)+"px; margin-right: 30px;'></div>"+
									"<div style='border: 1px solid #4E6751; margin-right: 30px; height: 100px; background: #5E7761;'>"+
										"<div style='background: #fff; height: "+(100-rate)+"px;'></div>"+
									"</div>"+
									"<div class='year'>"+year+"</div>"+
									"<span class='graphs_small_text'>(* Avg: "+overall_rate+"%)</span><br />"+
								"</td>"+
								"</tr></table>"+
							"</div>"+
						"</td>"+
						"<td class='historic_report_years'>"+
							"<div class='graphs'>"+
								"<table class='category_historic_table'><tr id='"+cat_id+"_historic'></tr></table>"+
							"</div>"+	
						"</td>"+
						"</tr>"
					);
				}
				else {
					$('#profile_category_data_table').append(
						"<tr class='colbody_top'>"+
						"<td class='current_report_year'><!-- Current Report Year -->"+
							"<div class='graphs'>"+
								"<table><tr>"+
								"<td style='width: 90px;'>"+
								"</td>"+
								"</tr></table>"+
							"</div>"+
						"</td>"+
						"<td class='historic_report_years'>"+
							"<div class='graphs'>"+
								"<table class='category_historic_table'><tr id='"+cat_id+"_historic'></tr></table>"+
							"</div>"+	
						"</td>"+
						"</tr>"
					);
					$("#"+cat_id+"_historic").append(
						"<td style='width: 90px;'>"+
							"<span class='graphs_small_text'>Rate:</span> "+rate+"%<br />"+
							"<div style='line-height: 0px; position: relative; top: "+(104-overall_rate)+"px; left: 66px;'>*</div>"+
							"<div style='background: #000; line-height: 0px; font-size: 0px; height: 3px; position: relative; top: "+(100-overall_rate)+"px; margin-right: 30px;'></div>"+
							"<div style='border: 1px solid #4E6751; margin-right: 30px; height: 100px; background: #5E7761;'>"+
								"<div style='background: #fff; height: "+(100-rate)+"px;'></div>"+
							"</div>"+
							"<div class='year'>"+year+"</div>"+
							"<span class='graphs_small_text'>(* Avg: "+overall_rate+"%)</span><br />"+
						"</td>"
					);					
				}
				
				/*
				$('#profile_category_data_table').append(
					"<tr class='colbody_top'>"+
						"<td class='category_average'>"+
							"<h3 class='category_data'>Average: "+overall_rate+"%</h3>"+
							"<div class='graph'>"+
								"<div style='height: "+(100-overall_rate)+"px; background: #fff;'></div>"+
							"</div>"+
							"<div class='year'>"+year+"</div>"+
						"</td>"+
						"<td class='category_current'>"+
							"<h2 class='category_data'>"+cat_name+" Rate: "+rate+"%</h2>"+
							"<div class='graph'>"+
								"<div style='height: "+(100-rate)+"px; background: #fff;'></div>"+
							"</div>"+
							"<div class='year'>"+year+"</div>"+
						"</td>"+
						"<td class='category_historic'>"+
							"<table class='category_historic_table'><tr id='"+cat_id+"_historic'></tr></table>"+
						"</td>"+
					"</tr>"
				);
				*/
			}
			else {
				$("#"+cat_id+"_historic").append(
					"<td style='width: 90px;'>"+
						"<span class='graphs_small_text'>Rate:</span> "+rate+"%<br />"+
						"<div style='line-height: 0px; position: relative; top: "+(104-overall_rate)+"px; left: 66px;'>*</div>"+
						"<div style='background: #000; line-height: 0px; font-size: 0px; height: 3px; position: relative; top: "+(100-overall_rate)+"px; margin-right: 30px;'></div>"+
						"<div style='border: 1px solid #4E6751; margin-right: 30px; height: 100px; background: #5E7761;'>"+
							"<div style='background: #fff; height: "+(100-rate)+"px;'></div>"+
						"</div>"+
						"<div class='year'>"+year+"</div>"+
						"<span class='graphs_small_text'>(* Avg: "+overall_rate+"%)</span><br />"+
					"</td>"
				);
				/*
				$("#"+cat_id+"_historic").append(
					"<td class='historic_graph'>"+
						"<h3 class='category_data year'>"+rate+"%</h3>"+
						"<div style='line-height: 0px; font-size: 0px; height: 3px; background: #000; position: relative; top: "+(100-overall_rate)+"px; margin-right: 11px; margin-left: 1px;'></div>"+
						"<div class='graph'>"+
							"<div style='height: "+(100-rate)+"px; background: #fff;'></div>"+
						"</div>"+
						"<div class='year'>"+year+"</div>"+
					"</td>"
				);
				*/
			}
		});
	}
	else {
		// If the return was in error
		/*
		$('#profile_category_data_table').append(
			"<tr class='colbody_top'>"+
				"<td class='category_current'>"+
					"<h2 class='category_data'>"+category_name+" Rate N/A</h2>"+
				"</td>"+
				"<td class='category_average'>"+
					"<h3 class='category_data'></h3>"+
				"</td>"+
				"<td class='category_historic'>"+
					"<h3 class='category_data'></h3>"+
				"</td>"+
			"</tr>"
		);
		*/
	}
}

function showMap(sf, name_id, category, xml){
	
	map_has_been_created=1;
	
	var object_label = (sf=='group')? 'medical-groups' : 'clinic'; 

	// Parse XML and append data to the dropdown
	$(object_label, xml).each(function(i){				
		
		name = $(this).find('name:first').text();
		
		addressline1 = $(this).find('address-line1:first').text();
		addressline2 = $(this).find('address-line2:first').text();
		city = $(this).find('city:first').text();
		state = $(this).find('state:first').text();
		zipcode = $(this).find('zip-code:first').text();

		web = $(this).find('website').text();
		main_phone = $(this).find('main-phone').text();
		tf_phone = $(this).find('toll-free-phone').text();


		// 
		// Create Map
		// -------------------------------------------------------
		//alert(addressline1+' '+addressline2+' '+city+' '+state+' '+zipcode);
		
		showAddress(addressline1+' '+addressline2+' '+city+' '+state+' '+zipcode);
		$('#map_link').append("<br /><a target='_blank' href='http://maps.google.com/maps?q="+addressline1+" "+addressline2+" "+city+" "+state+" "+zipcode+"'>Find this Location on Google Maps >> </a><br /><br />");
		
	});
}