// Copyright (c) 2000 Kevin Cole (kjcole@well.com). See the files
// COPYING and COPYRIGHT for the GNU General Public License.

var subtotal = 0;
var tax      = 0;
var payment  = 0;

/*****************************************************
 * This script is written by Eric (Webcrawl@usa.net) *
 * For full source code, installation instructions,  *
 * 100's more DHTML scripts, and Terms Of Use, visit *
 * http://dynamicdrive.com/                          *
 *****************************************************/

function printit() {
  if (window.print) {
      window.print() ;
  }
  else {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, 2); //Use a 1 vs. a 2 for a prompting dialog box
    WebBrowser1.outerHTML = "";
  }
}

// The money(cents) function assumes input is given as an integer
// representing a monetary value expressed in cents.  It returns
// a string representing a monetary value in dollars.  This function is
// necessary because JavaScript cannot display trailing zeros after the
// decimal point.

function money(cents)
{
  var sense = cents.toString();
//return sense.slice(0,-2)+"."+sense.slice(-2);
  return sense.substring(0,sense.length-2)+"."+sense.substr(sense.length-2,sense.length);
}

// The calculate() function is called by the event handlers in the form.

function calculate(row)
{
  // Get the user's input from the form. Assume it is all valid.
  var dishes = detail.document.menu;
  var check  = summary.document.bill;
  var cost = item[row].cost;
  var qty  = dishes.elements[row*2].value;
  item[row].qty = qty;

  // Compute item total.
  var itemtotal = cost * qty;
  item[row].total = itemtotal;
  dishes.elements[(row*2)+1].value = money(item[row].total);
  subtotal = 0;
  for (sum = 0 ; sum < item.length ; sum++)
    subtotal = subtotal + item[sum].total;
  tax = Math.floor(subtotal * 0.10);
  payment = subtotal + tax;
  check.subtotal.value = money(subtotal);
  check.tax.value      = money(tax);
  check.payment.value  = money(payment);
}

function itemize()
{
  var d = detail.document;

  d.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n');
  d.write('    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n');
  d.write('<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">\n<head>\n');
  d.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n');
  d.write('<title>Finalize order</title>\n</head>\n\n');
  d.write('<\!\-\- Copyright (c) 2000 Kevin Cole (kjcole@well.com). See the files \-\->\n');
  d.write('<\!\-\- COPYING and COPYRIGHT for the GNU General Public License.      \-\->\n\n');
//d.write('<body background="images/background.jpg"\n');
//d.write('style="background: url(/images/background.jpg) center no-repeat fixed;">\n');
  d.write('<body>\n');
  d.write('<form name="menu" id="menu">\n');
  d.write('<table>\n');
//d.write('<tr><td colspan="4" align="center"><big><strong><font color="#ff0000">Appetizers</font></strong></big></td></tr>\n');
  d.write('<tr>\n');
  d.write('<th>Qty</th>\n');
  d.write('<th>Description</th>\n');
  d.write('<th align="right">Price</th>\n');
  d.write('<th>Total Price</th>\n');
  d.write('</tr>\n');

  for(row = 0 ; row < item.length ; row++)
  {
    d.write('<tr>\n');
    d.write('<td valign="top" align="right">');
    d.write('<input name="dish'+row+'" type="text" size="2" onChange="top.calculate('+row+')" /></td>\n');
    d.write('<td valign="top"><strong>'+item[row].dish+'</strong><br />\n');
    d.write('<i>'+item[row].desc+'</i></td>\n');
    d.write('<td valign="top" align="right"><strong>'+money(item[row].cost)+'</strong></td>\n');
    d.write('<td valign="top" align="right">');
    d.write('<input type="text" size="6" onFocus="dish'+(row+1)+'.focus()" /></td>\n');
    d.write('</tr>\n');
  }
  d.write('</table>\n');
  d.write('</form>\n');
  d.write('</body>\n');
  d.write('</html>\n');
}

function summarize()
{
  var s = summary.document;
  var d = detail.document;

  s.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n');
  s.write('    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n');
  s.write('<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">\n<head>\n');
  s.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n');
  s.write('<title>Finalize order</title>\n');
  s.write('</head>\n\n');
  s.write('<\!\-\- Copyright (c) 2000 Kevin Cole (kjcole@well.com). See the files \-\->\n');
  s.write('<\!\-\- COPYING and COPYRIGHT for the GNU General Public License.      \-\->\n\n');
  s.write('<body>\n');
  s.write('<form name="bill" id="bill">\n');
  s.write('<div align="right">Subtotal: <input name="subtotal" type="text" size="6" onFocus="this.form.subtotal.blur();" /><br />\n');
  s.write('Tax: <input name="tax" type="text" size="6" onFocus="this.form.tax.blur();" /><br />\n');
  s.write('Payment: <input name="payment" type="text" size="6" onFocus="this.form.payment.blur()" /></p>\n');
  s.write('\n');
  s.write('<center><p><big><strong>Review Your Order<br />\n\n');
  s.write('<input type="button" value="Continue" onClick="top.order()" /></strong></big>\n');
  s.write('</p></center>\n</form>\n');
  s.write('</body>\n');
  s.write('</html>\n');
}

/********************************************************************************************
 * Order overwrites the two frames with printable order/receipt form with name, address,    *
 * and payment options (credit card, etc) on the top.                                       *
 ********************************************************************************************/

function order()
{
  var d = detail.document.menu;
  var s = summary.document.bill;
//var fax = window.open('','page','resizable,scrollbars,width=640,height=480');

  var ordered = new Array();
  for(row = 0 ; row < item.length ; row++)
  {
    ordered[row] = item[row];
  }

  var subtotal = s.subtotal.value
  var tax      = s.tax.value
  var payment  = s.payment.value

  var f = top.document;

  f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n');
  f.write('    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n');
  f.write('<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">\n<head>\n');
  f.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n');
  f.write('<title>Finalize order</title>\n</head>\n\n');
  f.write('<\!\-\- Copyright (c) 2000 Kevin Cole (kjcole@well.com). See the files \-\->\n');
  f.write('<\!\-\- COPYING and COPYRIGHT for the GNU General Public License.      \-\->\n\n');
  f.write('<body bgcolor="#e0e0e0">\n');
//f.write('<p>DEBUG:'+item.length+'</p>');
  f.write('<center>\n');
  f.write('<strong>Taste of India</strong><br />\n');
  f.write('<strong>2621 Connecticut Avenue NW</strong><br />\n');
  f.write('<strong>Washington, DC 20008</strong><br />\n');
  f.write('<strong>(202) 483-1115</strong><br />\n');
  f.write('<strong>(202) 483-2358</strong><br />\n');
  f.write('<h1>Order Form</h1>\n');
  f.write('</center>\n');

//f.write('<form name="order" id="order">\n');
  f.write('<form name="order" id="order" method="post"\n');
  f.write('action="sendorder.php">\n');
//f.write('action="cgi-bin/printorder.py">\n');
  f.write('<table width="100%" cellpadding="2" cellspacing="0" border="0">\n');
  f.write('<tr><td align="right" width="20%"><strong>Name: </strong></td>');
  f.write('<td width="80%"><input name="fullname" type="text" size="30" /></td></tr>\n');
  f.write('<tr><td align="right" width="20%"><strong>Address: </strong></td>');
  f.write('<td width="80%"><input name="address" type="text" size="30" />');
  f.write(' <strong>Apt: </strong><input name="apt" type="text" size="4" />');
  f.write(' <strong>Zip: </strong><input name="zip" type="text" size="10" /></td></tr>\n');
  f.write('<tr><td align="right" width="20%"><strong>Phone: </strong></td>');
  f.write('<td width="80%"><input name="phone" type="text" size="20" /></td></tr>\n');
  f.write('<tr><td align="right" width="20%"><strong>E-mail: </strong></td>');
  f.write('<td width="80%"><input name="email" type="text" size="50" /></td></tr>\n');
  f.write('<tr><td align="right" width="20%"><strong>Pay by: </strong></td>');
  f.write('<td><input name="method" type="radio" value="Cash" checked="checked" /> Cash ');
  f.write('<input name="method" type="radio" value="Visa" /> Visa ');
  f.write('<input name="method" type="radio" value="MasterCard" /> MasterCard ');
  f.write('<input name="method" type="radio" value="Ameican Express" /> American Express</td></tr>');
  f.write('<tr><td align="right" width="20%"><strong>Credit Card #: </strong></td>');
  f.write('<td width="80%"><input name="ccnum" type="text" size="20" />');
  f.write(' <strong>Exp (mm/yy)</strong>: <input name="ccexp" type="text" size="5" /></td></tr>\n');
  f.write('<tr><td valign="top" align="right" width="20%"><strong>Comments: </strong></td>');
  f.write('<td valign="top" width="80%"><textarea name="comments" rows="5" cols="60"> </textarea></td></tr>\n');
  f.write('</table>\n');

// Add cookies for name, address, phone...

  var patron = new Cookie(document,'PatronInfo');
  patron.load();

  if (patron.fullname != null) f.order.fullname.value = patron.fullname;
  if (patron.address  != null) f.order.address.value  = patron.address;
  if (patron.phone    != null) f.order.phone.value    = patron.phone;
//  if (patron.visits == null) patron.visits = 0;
//  patron.visits++;

  f.write('<table width="100%" cellpadding="2" cellspacing="0" border="0">\n');
  f.write('<tr>\n');
  f.write('<tr><td colspan="3"><hr noshade="noshade" /></td></tr>\n');
  f.write('<th width="10%">Quantity</th>\n');
  f.write('<th width="80%">Item</th>\n');
  f.write('<th width="10%" align="right">Price</th>\n');
  f.write('</tr>\n');
  f.write('<tr><td colspan="3"><hr /></td></tr>\n');

  var cnt=0;
  for(row = 0 ; row < ordered.length ; row++)
  {
    var qty  = ordered[row].qty;
    var dish = ordered[row].dish;
    var cost = money(ordered[row].total);
    if (qty > 0)
    {
      f.write('<tr>\n');
      f.write('<td align="center">'+qty);
      f.write('<input type="hidden" name="qty'+cnt+'"  value="'+qty+'" /></td>\n');
      f.write('<td>'+dish);
      f.write('<input type="hidden" name="dish'+cnt+'" value="'+dish+'" /></td>\n');
      f.write('<td align="right">'+cost);
      f.write('<input type="hidden" name="cost'+cnt+'" value="'+cost+'" /></td>\n');
      f.write('</tr>\n');
      ++cnt;
    }
  }

  f.write('<tr><td colspan="2">&nbsp;</td><td><hr /></td></tr>\n');
  f.write('<tr><td colspan="2" align="right">Subtotal: </td>');
  f.write('<td align="right"><strong>'+subtotal+'</strong>');
  f.write('<input type="hidden" name="subtotal" value="'+subtotal+'" /></td></tr>\n');
  f.write('<tr><td colspan="2" align="right">Tax: </td>');
  f.write('<td align="right"><strong>'+tax+'</strong>');
  f.write('<input type="hidden" name="tax" value="'+tax+'" /></td></tr>\n');
  f.write('<tr><td colspan="2" align="right">Payment: </td>');
  f.write('<td align="right"><strong>'+payment+'</strong>');
  f.write('<input type="hidden" name="payment" value="'+payment+'" /></td></tr>\n');
  f.write('</table>\n');
  f.write('<input type="hidden" name="dishes" value="'+cnt+'" />\n');

  f.write('<center><p>\n');
  f.write('<input type="submit" value="Send order" name="Print" id="Print" />');
  f.write('&nbsp;&nbsp;&nbsp;&nbsp;');
//f.write('<input type="button" value="Print and FAX" name="Print" onClick="printit()" />');
  f.write('<input type="button" value="Cancel" onClick="self.close()" />');
  f.write('</p></center>\n');
  f.write('</form>\n</body>\n');
}

