// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function scorecard_toggle(divName) {
  $('scorecard_data').hide();
  $('scorecard_chart_otd').hide();
  $('scorecard_chart_ppm').hide();
  $(divName).show();
};

function scorecard_refresh(supplier_id) {
	new Ajax.Updater('scorecard_container', "/suppliers/scorecard/" + supplier_id, {
		method: 'get',
		onFailure: function(transport)
		{
			$('errorExplanation').innerHTML = "Error.  Could not complete the refresh";
		}
	});
}

// Functions for inline edit of scorecards
////////////////////////////////////

function replace_with_input(domID)
{
  return;
	oldVal = $(domID).innerHTML;
	// Note gets special processing...
	$($(domID).parentNode).innerHTML = "<input id=\"" + domID + "\" type=\"text\" size=\"5\" name=\"" + domID + "\" value=\"" + oldVal + "\" onBlur=\"replace_with_text('" + domID + "', '" + oldVal + "')\" />";
	$(domID).select();
	$(domID).focus();
}

function replace_with_text(domID, original)
{
	oldVal = $(domID).value;
	id = $($($($(domID).parentNode).parentNode).parentNode).id;
	entry = $(domID).id;
	
	if(oldVal == "0")
	{
		Effect.Fade($($($($(domID).parentNode).parentNode).parentNode).id);
		//$($($(domID).parentNode).parentNode).remove()
		destroy_record(id);
		return;
	}
	// Note gets special processing...
	if(oldVal == "") oldVal = "<center><small><i>-edit note-</i></small></center>";
	$($(domID).parentNode).innerHTML = "<span id=\"" + domID + "\" onClick=\"replace_with_input('" + domID + "')\">" + oldVal + "</span>";
	if(oldVal == original) return;
	update_record(id, entry, oldVal);
}

function update_record(domID, entry, value)
{
	id = extract_id(domID);
	to_update = extract_entry(entry, id);
	params = to_update + "=" + value;
	new Ajax.Request("/supplier_ratings/update_inline/" + id, {
		parameters: params,
		onSuccess: function(transport) {
			$("purchase_item_" + id + "_error").hide();
			update_totals();
		}
	});
}

function extract_id(extract)
{
	id = 0;
	index = extract.lastIndexOf('_') + 1;
	id = extract.substr(index);
	return id;
}

function extract_entry(entry, inNum)
{
	var ret = "";
	index = entry.lastIndexOf(inNum) + inNum.length + 1;
	ret = entry.substr(index);
	return ret;
}