
/**
 * The XMLHttpRequest object.
 */
var http = null;

/**
 * Calculate the fabric dimensions.
 *
 * This function requires the name of an HTML form and the IDs of
 * two HTML elements. The form must contain the following fields:
 * - ftype    Fabric type
 * - fcnt     Fabric thread count
 * - ocnt     "Over" count.
 * - dw       Design width
 * - dh       Design height
 * - fa       Framing allowance
 * The two HTML elements will receive the calculated fabric
 * width and height. The function accepts an optional fourth
 * parameter: the ID of an HTML element that is do be displayed
 * while the calculation is taking place and hidden when it is
 * complete. This can be used to display some sort of "progress"
 * dialog (barber pole, etc.)
 *
 * @param   string      HTML form name
 * @param   string      HTML element id; receives the fabric width.
 * @param   string      HTML element id; receives the fabric height.
 * @param   string      HTML element to display while calculating (optional).
 * @return  void
 */
function fabcalc( fnam, fwid, fhid, did, dbg  )
{
  var dialog = null;                            // "Dialog box" element object.
  var err = '';                                 // Error messages.
  var fh = document.getElementById( fhid );     // Fabric height element.
  var frm = document.getElementById( fnam );    // Form object.
  var fw = document.getElementById( fwid );     // Fabric width element.
  var rqst = '';                                // The URI.

  if( typeof did != 'undefined' ) dialog = document.getElementById( did );
  if( typeof dbg == 'undefined' ) var dbg = 0;

  //
  // Do some sanity checking - Make sure the specified
  // elements exist and have values.
  //
  if( undefined == frm )
  {
    err += 'Form "' + fnam + '" is not defined.\n';
  }
  else
  {
    if( typeof frm.dbg != 'undefined' ) dbg = frm.dbg.value;
    if( !frm.ftype ) err += 'Field "ftype" is not defined.\n';
    if( !frm.fcnt ) err += 'Field "fcnt" is not defined.\n';
    if( !frm.ocnt ) err += 'Field "ocnt" is not defined.\n';
    if( !frm.dw ) err += 'Field "dw" is not defined.\n';
    if( !frm.dh ) err += 'Field "dh" is not defined.\n';
    if( !frm.fa ) err += 'Field "fa" is not defined.\n';
  }
  if( !fw ) err += 'Element "' + fwid + '" is not defined.\n';
  if( !fh ) err += 'Element "' + fhid + '" is not defined.\n';
  if(( typeof did != 'undefined' ) && !dialog )
    err += 'Element "' + did + '" is not defined.\n';

  //
  // If we encountered any design-time errors inform the
  // developer and bail.
  //
  if( err != '' )
  {
    alert( "DESIGN ERRORS:\n" + err );
    return false;
  }

  //
  // Attempt to instantiate an HTTP request object.
  //
  if( http ) http = null;
  if( typeof XMLHttpRequest != "undefined" )
    http = new XMLHttpRequest();
  else
  {
      var v = ["Msxml3.XMLHTTP.7.0", 
               "Msxml3.XMLHTTP.6.0", 
               "Msxml3.XMLHTTP.5.0",
               "Msxml3.XMLHTTP.4.0", 
               "MSXML3.XMLHTTP.3.0", 
               "MSXML3.XMLHTTP", 
               "Msxml2.XMLHTTP.7.0", 
               "Msxml2.XMLHTTP.6.0", 
               "Msxml2.XMLHTTP.5.0",
               "Msxml2.XMLHTTP.4.0", 
               "MSXML2.XMLHTTP.3.0", 
               "MSXML2.XMLHTTP", 
               "Microsoft.XMLHTTP"];
      for( var ii = 0; ii < v.length; ii++ )
      {
        try
        {
          http = new ActiveXObject( v[ii] );
//          if( http ) window.ActiveX_XMLHttpRequest = v[ii];
          break;
        }
        catch( e ) { /* We simply ignore it and try the next version. */ }
      }
  }
  if( typeof http == "undefined" || http == null )
  {
    err += "Your browser does not support the XMLHTTP object. Please upgrade.";
  }
/*
  else if( typeof ActiveXObject != "undefined" )
    http = new ActiveXObject("MSXML2.XmlHttp");
  else
    err += "Please upgrade your browser.\r\nIt doesn't support the XMLHttpRequest object.";
*/

  //
  // If we encountered any errors inform the user and bail.
  //
  if( err != '' )
  {
    alert( err );
    return false;
  }

  //
  // Build the request and send it off.
  //
  if( dialog ) dialog.style.display = "block";
  rqst = "cgi/fabcalc.php"
       + "?ftype=" + getval( frm.ftype )
       + "&fcnt=" + getval( frm.fcnt )
       + "&ocnt=" + getval( frm.ocnt )
       + "&dw=" + getval( frm.dw )
       + "&dh=" + getval( frm.dh )
       + "&fa=" + getval( frm.fa )
  if( dbg ) rqst += "&dbg=1";
  http.open( "GET", rqst, false );
  http.send( null );

  //
  // Process the response.
  //
  if( dbg ) alert( http.responseText );
  eval( http.responseText );
  if( dialog ) dialog.style.display = "none";
  fw.innerHTML = fwidth;
  fh.innerHTML = fheight;

  return false;

} // fabcalc()

/**
 * Get the value of a form field.
 * Handles the differences between scalar fields
 * and array fields (e.g., radio buttons).
 *
 * @param   object    The field.
 * @return  mixed     The field value or undefined.
 */
function getval( fld )
{
  if( fld.value != undefined ) return fld.value;

  if( fld.length > 1 )
  {
    for( var ii = 0; ii < fld.length; ii++ )
    {
      if( fld[ii].checked ) return fld[ii].value;
    }
  }
  return undefined;
} // getval()
