function ajaxHelperClearErrors(errorPanelId, hide)
{
  var panel = $(errorPanelId);
  if (panel)
  {
    if (hide || panel.getAttribute('was-display-none') == 1) panel.style.display = 'none';
    panel.innerHTML = '';
  }
}

function ajaxHelperShowError(msg, target, errorPanelId)
{
	var count = 1;
	if (target)
	{
		if (target.errorCount > 0) target.errorCount++;
		else target.errorCount = 1;
		count = target.errorCount;
	}
	var panel = $(errorPanelId);
	if (panel)
	{
    if (panel.style.display == 'none' || panel.offsetHeight == 0) panel.setAttribute('was-display-none',1); // used by LiveInput
    panel.style.display = 'block';
		panel.innerHTML = (panel.innerHTML ? (panel.innerHTML + '<br/>') : '') + 
                      (count > 1 ? (count + '. ') : '') + msg;
  }
  else
  {
    alert(msg.replace(/<\/?[^>]+>/gi, ''));
  }
}

function ajaxHelperOnError(ob, ex, target, errorPanelId, startTime, info)
{
	var z = '';
	if (typeof(ex) == 'string')
	{
		// Handle badly formatted JSON messages.  Special case is
		// IIS error HTML.
		var detail = ex;
		var seek = '<li>Error Type: <br/>';
		var i = detail.indexOf(seek);
		if (i > 0)
		{
			detail = detail.substr(i + seek.length);
			i = detail.indexOf('</li>');
			if (i > 0) detail = detail.substr(0,i);
		}
		z = detail;
	}
	else for(var n in ex)
	{
		if (typeof(ex[n]) != 'function' && typeof(ex[n]) != 'object')
			z += n + ': ' + ex[n] + '<br/>\n';
	}
	
	var msg = '<b>ERROR (Ajax Exception)</b>:<br/>';
  if (startTime)
  {
    var now = (new Date()).getTime();
    var elapse = now - startTime;
    msg += 'Timer: ' + (new Number(elapse/1000.0)).toFixed(2) + '<br/>';
  }
  if (info) z += 'info: ' + info + '<br/>\n';
	ajaxHelperShowError(msg + z, target, errorPanelId);
}

function ajaxHelperLoadJson(json)
{
	// Special error handling: not in JSON format
	if (json && (json.indexOf('[') != 0 && json.indexOf('{') != 0))
		throw 'Malformed JSON: ' + json;
  var code = '(' + json + ')';
  if (code == '()') return null;
  var ob = eval(code);
  if (ob && typeof(ob.error) == 'string') throw ob.error;
  return ob;
}
