// JavaScript Document

// =======================================================================
// Normal validation
// =======================================================================
function handleOnClickSubmit()
{
  var guppyForm = document.getElementById("competition_form");
	guppyForm.action.value = "save";
  guppyForm.submit();
}

// =======================================================================
// Do not save validation
// =======================================================================
function handleOnClickSubmitNoSave()
{
  var guppyForm = document.getElementById("competition_form");
	guppyForm.action.value = "nosave";
  guppyForm.submit();
}

// =======================================================================
// Print report function
// =======================================================================
function handlePrintReport()
{
  var guppyForm = document.getElementById("palmares_form");
	guppyForm.action.value = "print";
  guppyForm.submit();
}

// =======================================================================
// Sumit validation with action
// =======================================================================
function submitWithAction(form, action)
{
  var guppyForm = document.getElementById(form);
	guppyForm.action.value = action;
  guppyForm.submit();
}

// =======================================================================
// Sumit validation with param
// =======================================================================
function submitWithParam(form, param)
{
  var guppyForm = document.getElementById(form);
	guppyForm.param.value = param;
  guppyForm.submit();
}

// =======================================================================
// Insert table row
// =======================================================================
function handleOnClickInsertRow(insertImg)
{
  var rowElement    = insertImg.parentNode.parentNode;
  var rowIndex      = rowElement.rowIndex;
  var componentName = getEnclosingComponentName(insertImg);

  doSubmitCommand("ajouterLigne", componentName, rowIndex, null);
}

// =======================================================================
// Delete table row
// =======================================================================
function handleOnClickDeleteRow(deleteImg)
{
  var rowElement    = deleteImg.parentNode.parentNode;
  var rowIndex      = rowElement.rowIndex;
  var componentName = getEnclosingComponentName(deleteImg);

  doSubmitCommand("supprimerLigne", componentName, rowIndex-1, null);  // Offset -1 for headings
}

// =======================================================================
// DOM traversal helper
// =======================================================================

function getEnclosingComponentName(element)
{
  while (element.tagName != "DIV")
    element = element.parentNode;

  return element.id;
}

// =======================================================================
// Form sumission
// =======================================================================
function doSubmitCommand(action, composant, ligneIndex, param)
{
//  if (!guppyEnsureSingleSubmit())
//    return;

  var guppyForm = document.getElementById("competition_form");

  guppyForm.action.value    	= action;
  guppyForm.composant.value 	= composant;
  guppyForm.ligneIndex.value  = ligneIndex;
  guppyForm.param.value    		= param;

    // Make sure all htmlArea 3.0 textareas are updated
    // - htmlArea 3.0 assumes we use a standard submit form, so we need to fake that.
	/*
  for (var fieldName in guppyHtmlEditors)
  {
    var html = guppyHtmlEditors[fieldName].getHTML();
    guppyForm[fieldName].value = html;
  }
	*/
  guppyForm.submit();
}


