var def_formbuild = "v1.0";	// The version of the formbuild.js file

var nextTab     = -1;		// Number of the next tab
var nextGroup   = -1;		// Number of the next group
var activeTab   = 1;		// Number of the active tab
var activeGroup = 1;		// Number of the active group
var helpMap     = new Array();
helpMap[2] = "help_user_form";
helpMap[6] = "help_user_form2";
helpMap[7] = "help_user_form3";
helpMap[8] = "help_user_form4";

var tabChangeCallback = null;

function log( logStr )
{
  document.forms[0].structure.value += logStr + "\n";
}

function isBlank( value )
{
  if( typeof( value ) == "undefined" ) return true;

  if( value == "" ) return true;

  if( typeof( value.search ) != "undefined" ) return !value.search( /^\s*$/ );

  var blank = true;

  for( var i = 0 ; i < value.length ; i++ )
  {
    var c = value.charAt( i );

    if( c != " " && c != "\r" && c != "\n" && c != "\t" )
    {
      blank = false;
      continue;
    }
  }

  return blank;
}

function moveSomething( type, ext, num, direct )
{
  var i;
  var lastTabIdx = -1;
  var theTab     = getElt( type + ext + num );
  var tabBlock   = theTab.parentNode;

  for( var j = 0 ; j < tabBlock.childNodes.length ; j++ )
  {
    i = ( ( direct == -1 ) ? tabBlock.childNodes.length - 1 - j : j );

    if( tabBlock.childNodes[i].id == type + ext + num )
    {
      break;
    }

    if( typeof( tabBlock.childNodes[i].className ) != "undefined" && tabBlock.childNodes[i].className.indexOf( type ) == 0 )
    {
      lastTabIdx = i;
    }
  }

  if( lastTabIdx != -1 )
  {
    if( direct == 1 )
    {
      tabBlock.insertBefore( theTab, tabBlock.childNodes[lastTabIdx] );
    }
    else
    {
      tabBlock.insertBefore( tabBlock.childNodes[lastTabIdx], theTab );
    }
  }
}

function moveTab( direct )
{
  moveSomething( "tab", "", activeTab, direct );
}
function activateTab( tabId )
{
  var tabNum = tabId.substring( 3 );
  var thePage = getElt( "page" + activeTab );

  helpId = helpMap[parseInt(tabNum)];
  closeTmpl("hlp");

  if( typeof( thePage ) != "undefined" )
  {
    getElt( "tab"  + activeTab ).className = "tab inactive";
    getElt( "page" + activeTab ).className = "page inactive";
  }

  getElt( "tab"  + tabNum ).className = "tab";
  getElt( "page" + tabNum ).className = "page";

  activeTab = tabNum;

  activateGroup();

  if( tabChangeCallback != null )
  {
    tabChangeCallback( tabId );
  }
}
function renameTab()
{
  var theTitle = getElt( "tabTitle" + activeTab );
  var titleText = theTitle.innerHTML;
  theTitle.titleText = titleText;
  theTitle.innerHTML = '<input type="text" class="tabRename" id="rename" value="' + titleText + '" onblur="setTabName()" onkeydown="checkKeyTab(event)">';
  getElt( "rename" ).select();
}
function checkKeyTab( ev )
{
  if( ev.keyCode == 13 )
  {
    setTabName();
  }
  else if( ev.keyCode == 27 )
  {
    resetTabName();
  }
}
function setTabName()
{
  var theTitle = getElt( "tabTitle" + activeTab );
  var theField = getElt( "rename" );

  if( typeof( theField ) != "undefined" )
  {
    var titleText = theField.value;

    if( isBlank( titleText ) )
    {
      resetTabName();
    }
    else
    {
      theTitle.innerHTML = titleText;
    }
  }
}
function resetTabName()
{
  var theTitle = getElt( "tabTitle" + activeTab );
  theTitle.innerHTML = theTitle.titleText;
}
function delTab()
{
  if( !confirm( "Are you sure you wish to delete this tab?" ) )
  {
    return;
  }

  var thePage = getElt( "page" + activeTab );

  for( var i = thePage.childNodes.length - 1 ; i >= 0  ; i-- )
  {
    if( typeof( thePage.childNodes[i].className ) != "undefined" && thePage.childNodes[i].className.indexOf( "group" ) == 0 )
    {
      activateGroup( thePage.childNodes[i].id );
      delGroup( true );
    }
  }

  thePage.parentNode.removeChild( thePage );

  var theTab = getElt( "tab" + activeTab );

  theTab.parentNode.removeChild( theTab );
}
function insTab()
{
  var tabTemplate  = getElt( "tabTemplate" );
  var pageTemplate = getElt( "pageTemplate" );

  var newTab  = tabTemplate.cloneNode(  true );
  var newPage = pageTemplate.cloneNode( true );

  newTab.id = "tab" + nextTab;
  newTab.innerHTML = newTab.innerHTML.replace( /TABID/g, nextTab );

  newPage.id = "page" + nextTab;
  newPage.innerHTML = newPage.innerHTML.replace( /PAGEID/g, nextTab );

  var theTabs = getElt( "tabs" );
  theTabs.insertBefore( newTab, null );

  var thePanel = getElt( "panel" );
  thePanel.insertBefore( newPage, thePanel.childNodes[thePanel.childNodes.length - 4] );

  activateTab( newTab.id );
  insGroup();

  nextTab--;
}

function moveGroup( id, direct )
{
  moveSomething( "group", "", id, direct );
}
function activateGroup( groupId )
{
  if( typeof( groupId ) == "undefined" )
  {
    var thePage = getElt( "page" + activeTab );

    for( var i = 0 ; i < thePage.childNodes.length ; i++ )
    {
      if( typeof( thePage.childNodes[i].className ) != "undefined" && thePage.childNodes[i].className.indexOf( "group" ) == 0 )
      {
        groupId = thePage.childNodes[i].id;
        break;
      }
    }

    if( typeof( groupId ) == "undefined" )
    {
      return;
    }
  }

  var theGroup = getElt( "group" + activeGroup );

  if( typeof( theGroup ) != "undefined" )
  {
    theGroup.className = "group inactive";
  }

  groupNum = groupId.substring( 5 );

  getElt( "group" + groupNum ).className = "group";

  activeGroup = groupNum;
}
function renameGroup()
{
  var theTitle = getElt( "groupTitle" + activeGroup );
  var titleText = theTitle.innerHTML;
  theTitle.titleText = titleText;
  theTitle.innerHTML = '<input type="text" class="tabRename" id="rename" value="' + titleText + '" onblur="setGroupName()" onkeydown="checkKeyGroup(event)">';
  getElt( "rename" ).select();
}
function checkKeyGroup( ev )
{
  if( ev.keyCode == 13 )
  {
    setGroupName();
  }
  else if( ev.keyCode == 27 )
  {
    resetGroupName();
  }
}
function setGroupName()
{
  var theTitle = getElt( "groupTitle" + activeGroup );
  var theField = getElt( "rename" );

  if( typeof( theField ) != "undefined" )
  {
    var titleText = theField.value;

    if( isBlank( titleText ) )
    {
      resetGroupName()
    }
    else
    {
      theTitle.innerHTML = titleText;
    }
  }
}
function resetGroupName()
{
  var theTitle = getElt( "groupTitle" + activeGroup );
  theTitle.innerHTML = theTitle.titleText;
}
function delGroup( noWarn )
{
  if( typeof( noWarn ) == "undefined" )
  {
    if( !confirm( "Are you sure you wish to delete this group?" ) )
    {
      return;
    }
  }

  var theGroup  = getElt( "group"    + activeGroup );
  var container = getElt( "elements" + activeGroup );

  for( var i = container.childNodes.length - 1 ; i >= 0 ; i-- )
  {
    if( typeof( container.childNodes[i].className ) != "undefined" && container.childNodes[i].className.indexOf( "element" ) == 0 )
    {
      delElement( container.childNodes[i].id.substring( 7 ) );
    }
  }

  theGroup.parentNode.removeChild( theGroup );
}
function insGroup()
{
  var groupTemplate = getElt( "groupTemplate" );

  var newGroup = groupTemplate.cloneNode( true );

  newGroup.id = "group" + nextGroup;
  newGroup.innerHTML = newGroup.innerHTML.replace( /GROUPID/g, nextGroup );

  var thePage = getElt( "page" + activeTab );
  thePage.insertBefore( newGroup, thePage.firstChild );

  activateGroup( newGroup.id );
  renameGroup();

  nextGroup--;
}

function moveElement( id, direct )
{
  moveSomething( "element", "", id, direct );
}
function delElement( id )
{
  var destination = getElt( "holdingPen" );
  var theElement  = getElt( "element" + id );
  destination.insertBefore( theElement, null );

  getElt( "field" + id ).className = "field";
}
function insElement( id )
{
  var destination = getElt( "elements" + activeGroup );
  var theElement  = getElt( "element" + id );

  if( theElement.parentNode.id == "elements" + activeGroup )
  {
    return false;
  }

  if( theElement.parentNode.id != "holdingPen" && !confirm( "This element is already placed in a group on this form.\n\nPlacing it here will remove it from its existing position.\n\nAre you sure you wish to continue?" ) )
  {
    return false;
  }

  destination.insertBefore( theElement, destination.childNodes[Math.max(destination.childNodes.length - 2,0)] );

  getElt( "field" + id ).className = "field used";

  return false;
}

function parseStructure()
{
  var tabId;
  var tabName;
  var thePage;
  var groupId;
  var groupName;
  var theElements;
  var elementId;

  var tabCount;
  var groupCount;
  var elementCount;

  var errors  = "";
  var theTabs = getElt( "tabs" );
  var theForm = document.forms[0];
  var textbox = theForm.structure;

  textbox.value = "";

  tabCount = 0;

  for( var i = 0 ; i < theTabs.childNodes.length ; i++ )
  {
    if( typeof( theTabs.childNodes[i].className ) == "undefined" || theTabs.childNodes[i].className.indexOf( "tab" ) < 0 ) continue;

    tabId = theTabs.childNodes[i].id.substring( 3 );

    tabName = getElt( "tabTitle" + tabId ).innerHTML;

    textbox.value += "tab|" + tabId + "|" + tabName + "\r\n";

    thePage = getElt( "page" + tabId );

    groupCount = 0;

    for( var j = 0 ; j < thePage.childNodes.length ; j++ )
    {
      if( typeof( thePage.childNodes[j].className ) == "undefined" || thePage.childNodes[j].className.indexOf( "group" ) < 0 ) continue;

      groupId = thePage.childNodes[j].id.substring( 5 );

      groupName = getElt( "groupTitle" + groupId ).innerHTML;

      textbox.value += "group|" + tabId + "|" + groupId + "|" + groupName + "\r\n";

      theElements = getElt( "elements" + groupId );

      elementCount = 0;

      for( var k = 0 ; k < theElements.childNodes.length ; k++ )
      {
        if( typeof( theElements.childNodes[k].className ) == "undefined" || theElements.childNodes[k].className.indexOf( "element" ) < 0 ) continue;

        elementId = theElements.childNodes[k].id.substring( 7 );

        textbox.value += "element|" + groupId + "|" + elementId + "\r\n";

        elementCount++;
      }

      if( elementCount == 0 )
      {
        errors += "Group '" + groupName + "' in tab '" + tabName + "' has no fields, either add some fields or delete this group.\n";
      }

      groupCount++;
    }

    if( groupCount == 0 )
    {
      errors += "Tab '" + tabName + "' has no field groups, either add some groups or delete this tab.\n";
    }

    tabCount++;
  }

  if( tabCount == 0 )
  {
    errors += "There are no tabs in this form.\n";
  }

  if( errors != "" )
  {
    alert( errors );
  }
  else
  {
    theForm.submit();
  }

  return false;
}
