// Script - ReSum.js

/* This page does all the magic for applying
 * Ajax principles to a registration form.
 * Several Scripts are included for ease.
 * recalc_tuit() will re-calculate the tuition.
 * recalc_matl() will re-calculate the materials charges.
 * recalc_serv() will re-calculate the services charges.
 * All three will update the total owed, as well.
 */

// Function that starts the Ajax process for Tuition:
function recalc_tuit( tuition )
{
  // Confirm that the object is usable:
  if ( ajax )
  {
    // Call the PHP script.
    // Use the GET method.
    // Pass the tuition type in the URL.
    ajax.open( 'get', 'ReTuit.php?tu=' + tuition );

    // Function that handles the response:
    ajax.onreadystatechange = change_tuit;

    // Send the request:
    response = ajax.send( null );

    if ( ajax.responseText != '' )
    {
        document.getElementById( 'tottuit' ).innerHTML = ajax.responseText;
    }
  }
  else
  {
    // Can't use Ajax!
    document.getElementById( 'tottuit' ).innerHTML = 'The tuition will be calculated upon form submission.';
  }
}  // end of recalc_tuit() function.


// Function that starts the Ajax process for Total Owed:
function total( tu, ac, bc, cc, dc, ec, fc, le, os, oa, hc, am )
{
  // Confirm that the object is usable:
  if ( ajax )
  {
    // Save Tuition into tui item
    document.getElementById( 'tui' ).innerHTML = tu;
    
    // Call the PHP script.
    // Use the GET method.
    // Pass the tuition type in the URL.
    ajax.open( 'get', 'SumTotal.php?tuit=' + tu + '&acls=' + ac + '&bcls=' + bc + '&ccls=' + cc + '&dcls=' + dc + '&ecls=' + ec + '&fcls=' + fc + '&leaboamt=' + le + '&oschol=' + os + '&oallow=' + oa + '&hcexp=' + hc + '&amtpd=' + am );
    // Function that handles the response:
    ajax.onreadystatechange = change_all_costs;

    // Send the request:
    response = ajax.send( null );

    if ( ajax.responseText != '' )
    {
      document.getElementById( 'owed' ).innerHTML = ajax.responseText;
    }
  }
  else
  {
    // Can't use Ajax!
    document.getElementById( 'owed' ).innerHTML = 'The cost will be calculated upon form submission.';
  }
}  // end of recalc_tuit() function.


// Function that starts the Ajax process for Total Owed:
function retotal( tu, ma, le, os, oa, hc, am )
{
  // Confirm that the object is usable:
  if ( ajax )
  {
    // Call the PHP script.
    // Use the GET method.
    // Pass the tuition type in the URL.
    ajax.open( 'get', 'ReSum.php?tuit=' + tu + '&matl=' + ma + '&leaboamt=' + le + '&oschol=' + os + '&oallow=' + oa + '&hcexp=' + hc + '&amtpd=' + am );

    // Function that handles the response:
    ajax.onreadystatechange = change_total_cost;

    // Send the request:
    response = ajax.send( null );

    if ( ajax.responseText != '' )
    {
      document.getElementById( 'owed' ).innerHTML = ajax.responseText;
    }
  }
  else
  {
    // Can't use Ajax!
    document.getElementById( 'owed' ).innerHTML = 'The cost will be calculated upon form submission.';
  }
}  // end of recalc_tuit() function.


// Function that starts the Ajax process for Music Reading Class:
function chngcls( classaid, classbid, classcid, classdid, classeid, classfid )
{
  var tuit = 0.00;

  // Confirm that the object is usable:
  if ( ajax )
  {
    // Call the PHP script.
    // Use the GET method.
    // Pass the tuition type in the URL.
    ajax.open( 'get', 'ClsCostChk.php?sesacls=' + classaid + '&sesbcls=' + classbid + '&sesccls=' + classcid + '&sesdcls=' + classdid + '&sesecls=' + classeid + '&sesfcls=' + classfid );

    // Function that handles the response:
    ajax.onreadystatechange = change_matls;

    // Send the request:
    response = ajax.send( null );

    if ( ajax.responseText != '' )
    {
      document.getElementById( 'totmatl' ).innerHTML = ajax.responseText;
    }
  }
  else
  {
    // Can't use Ajax!
    document.getElementById( 'totmatl' ).innerHTML = 'The cost will be calculated upon form submission.';
  }
}  // end of recalc_tuit() function.


// Function that starts the Ajax process for Music Reading Class:
function total_costs( tucost, macost )
{
  // Confirm that the object is usable:
  if ( ajax )
  {
    // Call the PHP script.
    // Use the GET method.
    // Pass the tuition type in the URL.
    ajax.open( 'get', 'ReTotCost.php?tu=' + tucost + '&ma=' + macost );

    // Function that handles the response:
    ajax.onreadystatechange = change_cost;

    // Send the request:
    response = ajax.send( null );

    if ( ajax.responseText != '' )
    {
      document.getElementById( 'totmatl' ).innerHTML = ajax.responseText;
    }
  }
  else
  {
    // Can't use Ajax!
    document.getElementById( 'totmatl' ).innerHTML = 'The cost will be calculated upon form submission.';
  }
}  // end of recalc_tuit() function.


// Function that handles the response from the tuit PHP script:
function change_tuit()
{
  if ( ( ajax.readyState == 4 ) && ( ajax.status == 200 ) )
  {
    // Assign the returned value to a document element:
    document.getElementById( 'tottuit' ).innerHTML = ajax.responseText;
  }
}  // End of change_tuit() function.

// Function that handles the response from the tuit PHP script:
function change_matls()
{
  var fstsp = 0;
  var str, str2, str3, str4, str5, str6, str7;

  if ( ( ajax.readyState == 4 ) && ( ajax.status == 200 ) )
  {
    str = ajax.responseText;
    fstsp = str.indexOf(",");
    document.getElementById( 'totmatl' ).innerHTML = str.slice( 0, fstsp );
    str2 = str.substr( fstsp+1 );
    fstsp = str2.indexOf(",");
    document.getElementById( 'acost' ).innerHTML = str2.slice( 0, fstsp );
    str3 = str2.substr( fstsp+1 );
    fstsp = str3.indexOf(",");
    document.getElementById( 'bcost' ).innerHTML = str3.slice( 0, fstsp );
    str4 = str3.substr( fstsp+1 );
    fstsp = str4.indexOf(",");
    document.getElementById( 'ccost' ).innerHTML = str4.slice( 0, fstsp );
    str5 = str4.substr( fstsp+1 );
    fstsp = str5.indexOf(",");
    document.getElementById( 'dcost' ).innerHTML = str5.slice( 0, fstsp );
    str6 = str5.substr( fstsp+1 );
    fstsp = str6.indexOf(",");
    document.getElementById( 'ecost' ).innerHTML = str6.slice( 0, fstsp );
    str7 = str6.substr( fstsp+1 );
    fstsp = str7.indexOf(",");
    document.getElementById( 'fcost' ).innerHTML = str7;
    document.getElementById( 'fcost2' ).innerHTML = str7;
  }
}  // End of change_tuit() function.

// Function that handles the response from the total PHP script:
function change_cost()
{
  if ( ( ajax.readyState == 4 ) && ( ajax.status == 200 ) )
  {
    // Assign the returned value to a document element:
    document.getElementById( 'totcost' ).innerHTML = ajax.responseText;
  }
}  // End of change_total_cost() function.

// Function that handles the response from the total PHP script:
function change_total_cost()
{
  if ( ( ajax.readyState == 4 ) && ( ajax.status == 200 ) )
  {
    // Assign the returned value to a document element:
    document.getElementById( 'owed' ).innerHTML = ajax.responseText;
  }
}  // End of change_total_cost() function.


// Function that handles the response from the SumTotal PHP script:
function change_all_costs()
{
  var compos = 0;
  var strt = 0;
  var str1 = '';

  if ( ( ajax.readyState == 4 ) && ( ajax.status == 200 ) )
  {
    compos = ajax.responseText.indexOf( ",", strt );
    str1 = ajax.responseText.slice( strt, compos );
    document.getElementById( 'totmatl' ).innerHTML = str1;
    strt = compos + 1;
    compos = ajax.responseText.indexOf( ",", strt );
    str1 = ajax.responseText.slice( strt, compos );
    document.getElementById( 'tottuit' ).innerHTML = str1;
    strt = compos + 1;
    compos = ajax.responseText.indexOf( ",", strt );
    str1 = ajax.responseText.slice( strt, compos );
    document.getElementById( 'totcost' ).innerHTML = str1;
    strt = compos + 1;
    compos = ajax.responseText.indexOf( ",", strt );
    str1 = ajax.responseText.slice( strt, compos );
    document.getElementById( 'owed' ).innerHTML = str1;
  }
}  // End of change_tuit() function.

