/*
 * CARES
 */

function showCare(url, width, height)
{
  fb.start({href: url, rev: "type:ajax;sameBox:true;width:" + width +";height:" + height});
}

function enableCare(id)
{
  $('iconCare' + id).src = window['image_care_enabled'];
  $('careLink' + id).style.fontWeight = 'bold';
  if(id != 0 && id != 14) // Not "first visit" and "further"
  {
    $('careDisableLink' + id).show();
  }
}

function disableCare(id)
{
  $('iconCare' + id).src = window['image_care_disabled'];
  $('careLink' + id).style.fontWeight = 'normal';
  if(id != 0 && id != 14) // Not "first visit" and "further"
  {
    $('careDisableLink' + id).hide();
  }
}


/*
 * SCHEMA
 */

function showTooth(number, types, suffix)
{
  $('toothBoxNumber' + suffix).innerHTML = number;
  fb.start($('toothBoxLink' + suffix));
  
  for(var i = 0 ; i < types.length ; i++)
  {
	if(window['teeth' + types[i] + suffix].contains(number))
	{
      $('tooth_' + types[i] + suffix).setAttribute('checked', 'checked');
      $('tooth_' + types[i] + suffix).setAttribute('defaultChecked', 'defaultChecked'); /* IE workaround */
      $('tooth_' + types[i] + suffix).checked = true;
      $('tooth_' + types[i] + suffix).defaultChecked = true;
	}
	else
	{
      $('tooth_' + types[i] + suffix).removeAttribute('checked');
      $('tooth_' + types[i] + suffix).removeAttribute('defaultChecked'); /* IE workaround */
      $('tooth_' + types[i] + suffix).checked = false;
      $('tooth_' + types[i] + suffix).defaultChecked = false;
	}
  }
}

function saveTooth(number, types, suffix)
{
  for(var i = 0 ; i < types.length ; i++)
  {
	if($('tooth_' + types[i] + suffix).checked)
	{
	  addTooth(number, types[i], suffix);
	}
	else
	{
      removeTooth(number, types[i], suffix);
      
      /* Hide tooth marker if tooth is no longer selected */
      $('tooth' + types[i] + number + suffix).hide();
	}
  }
  
  fb.end();
  generateFormValues(types, suffix);
}

function addTooth(number, type, suffix)
{
  if(!(window['teeth' + type + suffix]).contains(number))
  {
    (window['teeth' + type + suffix]).push(number);
  }
}

function removeTooth(number, type, suffix)
{
  for(var i = 0 ; i < (window['teeth' + type + suffix]).length ; i++)
    {
      if((window['teeth' + type + suffix])[i] == number)
      {
        (window['teeth' + type + suffix]).array_cut(i);
      }
    }
}


function generateFormValues(types, suffix)
{
  for(var i = 0 ; i < types.length ; i++)
  {
	var data = window['teeth' + types[i] + suffix];
	
    var value = '';
    for(var j = 0 ; j < data.length ; j++)
    {
      if(value != '') 
      {
        value += ',';
      }
      value += data[j];
      
      /* Show tooth marker */
      $('tooth' + types[i] + data[j] + suffix).show();
    }
    
    /* Update hidden input fields */
    $('teeth_to_' + types[i] + suffix).value = value;
  }
}
