// Functions for Javascript page processing

// Focus on this input when loaded
function initedit()
{
  document.getElementById( 'first_edit' ).focus();
  return;
}

// Update total ticket cost
function mult( cntid, cstid, totid, oweid )
{
  var tot = new Number();

//  alert( "CntID: " + cntid + ", CstID: " + cstid + ", TotID: " + totid + ", Owed: " + oweid );
  cst = document.getElementById( cstid );
//  alert( "Step 1" );
  cnt = document.getElementById( cntid );
  cost = cst.innerHTML;
//  alert( "Step 2" );
  owe = document.getElementById( oweid );
//  alert( "Step 3" );
  count = cnt.value;
//  alert( "Step 4" );
  tot = count * cost;
//  alert( "Step 5" );
  out = document.getElementById( totid );
//  alert( "Step 6" );
  out.innerHTML = tot.toFixed( 2 );
  owe.value = tot;
//  alert( "Step 7" );
//  alert( "Cost: " + cost + ", Count: " + count + ", Total: " + tot.toFixed( 2 ) + ", Owe: " + owe.value );
}


// Update a given ID with a given value
function update_val( id, v )
{
  ch = document.getElementById( id );
  ch.innerHTML = v;
}


// Update amount owed when either amount paid or total cost changes
function retot( tc )
{
  var o = new Number( 0.00 );
  pd = document.getElementById( "amtpd" );
  ow = document.getElementById( "owed" );
  o = tc - pd.value;
  ow.value = o.valueOf();
  ow.innerHTML = o.toFixed( 2 );
}

// Set Insert Point
function set_entry( ths )
{
  ths.focus();
}

// Validate Input Provided for an input field and
//    send alert text if not provided
// Caution: Does Not Check correctness of input
function validate_required( field, alerttxt )
{
  with (field)
  {
    if ( value == null || value == "" )
    {
      alert( alerttxt );
      return false;
    }
    else
    {
      return true;
    }
  }
}

// Announce Valid Input Required for two semi-required (one or the other) inputs
function validate_semi_required( field1, field2, alerttxt )
{
  var failed = false;

  with ( field1 )
  {
    if ( value != null && value != "" )
    {
      return true;
    }
  }
  with ( field2 )
  {
    if ( value != null && value != "" )
    {
      return true;
    }
  }
  alert( alerttxt );
  return false;
}

// Validate the Registration form
function validate_reg_form( thisform )
{
  with ( thisform )
  {
    if ( validate_required( lname, "Last Name Required!" ) == false )
    {
      lname.focus();
      return false;
    }
    if ( validate_required( fname, "First Name Required!" ) == false )
    {
      fname.focus();
      return false;
    }
    if ( validate_required( addr1, "Address Required!" ) == false )
    {
      addr1.focus();
      return false;
    }
    if ( validate_required( city, "City Name Required!" ) == false )
    {
      city.focus();
      return false;
    }
    if ( validate_required( stsel, "Select a State Required!" ) == false )
    {
      stsel.focus();
      return false;
    }
    if ( validate_required( zip, "Zip or Postal Code Required!" ) == false )
    {
      zip.focus();
      return false;
    }
    if ( validate_semi_required( phone, email, "Phone or Email Required!" ) == false )
    {
      if ( phone != '' )
      {
        email.focus();
      }
      else
      {
        phone.focus();
      }
      return false;
    }
    exb = badges.value - 1;
    for ( ieb=0; ieb<exb; ieb++ )
    {
      nmstr = " Name Required in Extra Registrant " + ( ieb + 1 ) + "!";
      if ( validate_required( exlname[ieb], ( "Last" + nmstr ) ) == false )
      {
        exbadges[ieb].focus();
        return false;
      }
      if ( validate_required( exfname[ieb], ( "First" + nmstr ) ) == false )
      {
        exfname[ieb].focus();
        return false;
      }
    }
    if ( validate_required( paymnt, "Method of Payment Selection Required!" ) == false )
    {
      paymnt.focus();
      return false;
    }
  }
}
