// JS-Menu & Shopping Cart Scripts
// Copright © 1998 - 2003 By Joel Stead
// Magic Circle Shopping Cart® Version 3.03.1907 (rev 2)
// Last Updated: 19 July 2003 
// Site URL: http://magiccircle.iinet.net.au/
// -------------------------------------------------------

// Date:
// -----

function displaydate(){
  var curdate = new Date();
  var testdate = curdate+"";
  var a = testdate.substring(0,3);
  var b = testdate.substring(4,7);
  var c = testdate.substring(8,10);
  var d = testdate.substring(testdate.length - 4, testdate.length);
  if (a == "Mon") {a = "Monday, ";}
  if (a == "Tue") {a = "Tueday, ";}
  if (a == "Wed") {a = "Wednesday, ";}
  if (a == "Thu") {a = "Thursday, ";}
  if (a == "Fri") {a = "Friday, ";}
  if (a == "Sat") {a = "Saturday, ";}
  if (a == "Sun") {a = "Sunday, ";}
  if (b == "Jan") {b = " January ";}
  if (b == "Feb") {b = " February ";}
  if (b == "Mar") {b = " March ";}
  if (b == "Apr") {b = " April ";}
  if (b == "May") {b = " May ";}
  if (b == "Jun") {b = " June ";}
  if (b == "Jul") {b = " July ";}
  if (b == "Aug") {b = " August ";}
  if (b == "Sep") {b = " September ";}
  if (b == "Oct") {b = " October ";}
  if (b == "Nov") {b = " November ";}
  if (b == "Dec") {b = " December ";}
  userdate = a + c + b + d;
  return userdate;
}

// Cart Variables
// --------------

var idx = 0;
var items = new Array();

function item (a,b,c,d,e,f) {
  this.title  = a;               // Title
  this.author = b.toUpperCase(); // Author Name(s)
  this.price  = c;               // Price
  this.qty    = d;               // Quantity
  this.isbn   = e;               // ISBN/ASIN
  this.month  = f;               // Catalogue Month
}

// Copy details for Printable Order
function details(a,b,c,d,e,f,g) {
  this.name    = a; // Name
  this.address = b; // postal address
  this.phone   = c; // phone
  this.email   = d; // email
  this.payment = e; // payment method (postal)
  this.emailme = f; // email me with reply
  this.msg     = g; // msg: additional titles or info
}

// add an item to the shopping cart
function additem(title,author,price,qty,isbn,month){
  items[++idx] = new item(title,author,price,qty,isbn,month);
  alert(title + ' has been added to the shopping cart');
}

// remove an item from the shopping cart
function removeitem(i) {
  if(idx==1) { idx=0; }
  if(idx>1)  { 
    idx-=1;
    items.splice(i,1);
  }
}

// check qty value (member of display shopping cart)
function checkNumber(checkString,thisbook,thisidx){
  var newString="";
  for(i=0;i<checkString.length;i++){
    ch = checkString.substring(i,i+1);
    if(ch >= "0" && ch <= "9"){
      newString += ch;
    }
  }
  if (newString >= 3) {
    if(!confirm('You have selected to purchase ' + newString + ' copies of ' + thisbook + ', is this correct?')) {
        newString = "1";
        updateCart(thisidx,newString);
        return 1;
    }
  }
  if(newString=='' || newString=='0') {
    if(confirm('You have selected to delete ' + thisbook + ' from your order, is this correct?')) {
      removeitem(thisidx);
      if(idx<thisidx && idx!=0) {newString=1;}
      displaycart();
      return 1;
    }
  }
  updateCart(thisidx,newString);
}

// qty update function (member of display shopping cart)
function update(thisbook,i,dmp){
  if (dmp==1) { 
     items[i].qty += 1;
  }
  else
  {
    if(items[i].qty == 1) {
      if(confirm('You have selected to delete ' + thisbook + ' from your order, is this correct?')) {
        removeitem(i);
      }
    }
    else
    {
      items[i].qty -= 1;
    }
  }
  displaycart();
}

// Magic Circle DisplayCart®
// Copyright © 1998 - 2003 By Joel Stead
// Source Updated: 19 July 2003
// Version 3.03.1907 (rev 3)
// -------------------------------------

function displaycart() {
  var sxx=0.00;
  var txx=0.00;
  var dmp=0;
  with (main.document) {
    open();
    writeln("<HTML>");
    writeln("<head>");
    writeln('<meta name="Author" content="Joel Stead">');
    writeln('<link rel="stylesheet" href="/mcstyle.css">');
    writeln("</head>");
    writeln('<body bgcolor="#E8E8E8" >');
    writeln('<table width="560" height="70" border=0 CELLSPACING=0 CELLPADDING=0>');
    writeln('<tr align=center valign=center width="80"><td><img src="/images/mm_title2.jpg" width="70"></td><td>');
    writeln('<div class="mcinfohead">Magic Circle Bookshop</div>');
    writeln('<div class="minibanner"><a class="minibanner" href="http://magiccircle.iinet.net.au/index.html" target="_top">http://magiccircle.iinet.net.au/index.html</a></div>');
    writeln("</td></tr></table>");
    writeln('<hr width="560" align=left>');
    writeln('<div class="mcinfotitle">Order Form</div>');
    // Temporary Message from David 
    //writeln('<div class="mcinfo"><li style="color: red"><b>Important Notice</b></div>');
    //writeln('<div class="mcinfo"><li style="color: red">Dear Customers, Please note that the Bookshop Office will be closed from the 26th of September till the 5th of October as I am going to be on a holiday during that period.  A copy of the Catalogue will still be posted to customers and online orders via the website and email will still be accepted during that period but confirmation of the orders will not be sent till after I return. I apologise for any inconvenience this may cause. Regards David.</div>');
    // end message
    writeln('<hr width="560" align=left>');
    if(idx==0) {
      writeln('<div class="mcinfo">There are currently no books in the shopping cart.</div>');
      writeln('<div class="mcinfo">If you would like to order books or request a search for a title then please</div>');
      writeln('<div class="mcinfo">send an email to <a href="mailto:magicc@bigpond.net.au?subject=Orders%20or%20Search%20Request">Magic Circle Bookshop</a> with your order or request.</div>');
    }
    else {
      writeln('<div class="mcinfo">You have '+ idx + ' item(s) in your shopping cart.</div>');
      writeln('<div class="mcinfo">Order ID: '+ parent.menu.counter + ' &nbsp; &nbsp; &nbsp; &nbsp; Date: ' + displaydate() + '</div>');
      writeln('<div class="mcinfo">------------------------------------------------</div>');
      writeln('<FORM name=Form1 Method=post Action="http://magiccircle.iinet.net.au/formmail.pl">');
      writeln('<INPUT type=hidden name="recipient" value="magicc@bigpond.net.au">');
      writeln('<INPUT type=hidden name="subject" value="Orders: Magic Circle Bookshop">');
      writeln('<INPUT type=hidden name="required" value="Name,Phone">');
      writeln('<INPUT Type=hidden name="orderdate" value="'+displaydate()+'">');
      writeln('<INPUT Type=hidden name="orderid" value="'+parent.menu.counter+'">');
      writeln('<div class="mcinfo">Please take care entering the following details.</div>');
      writeln('<div class="mcinfo">Items marked with a star (*) are required items.</div>');
      writeln('<hr width="560" align=left>');
      writeln('<table border=0 width="560"><tr valign=top><td class="mcinfo">Name: (*)</td><td><input class="mcinfo" type=text name="Name" size=45></td></tr>');
      writeln('<tr valign=top><td class="mcinfo">Postal Address: (*)</td><td><textarea class="mcinfo" Name="Address" rows=2 Cols=45></textarea></td></tr>');
      writeln('<tr valign=top><td class="mcinfo">Phone: (*)</td><td><input class="mcinfo" type=text name="Phone" size=45></td></tr>');
      writeln('<tr valign=top><td class="mcinfo">Email: (*)</td><td><input class="mcinfo" type=text name="email" size=45></td></tr>');
      writeln('<tr valign=top><td class="mcinfo">Payment Method: (*)</td><td><select class="mcinfo" name="paymentmethod">');
      writeln('<option value="Visa">Visa</option>');
      writeln('<option value="MasterCard">Mastercard</option>');
      writeln('<option value="BankCard">BankCard</option>');
      writeln('<option value="MoneyOrder-Voucher">Money Order or Voucher</option>');
      writeln('<option value="Cheque">Cheque</option>');
      writeln('</select></td></tr>');
      writeln('<tr valign=top><td class="mcinfo">Additional Requests: </td><td class="mcinfo"><textarea class="mcinfo" name="msg" rows=2 Cols=45></textarea></td></tr></table>');
      writeln('<hr width="560" align=left>');
      writeln('<div class="mcinfo">Items Ordered (All items include 10% GST)</div>');
      writeln('<table border=0 width=560 cols=5>');
      writeln('<tr align=left><th class="mcinfo" width="45%">Title</th><th class="mcinfo" width="25%">Author</th><th class="mcinfo" width="10%">Price</th><th class="mcinfo" width="10%" >Quantity</th><th class="mcinfo" width="10%">Sub Total</th></tr>');
      for(i=1;i<=idx;i++){
        sxx = ((items[i].qty * items[i].price) * 100) +.1;
        sxx = Math.floor(sxx) / 100;
        txx += sxx;
        txx = (txx * 100) + .1;
        txx = Math.floor(txx) / 100;
        writeln('<tr><td class="mcinfo">' + items[i].title + '</td><td class="mcinfo">' + items[i].author + '</td><td class="mcinfo">' + items[i].price + '</td><td class="mcinfo" align=center><input type=button onclick="parent.update(\''+items[i].title+'\','+i+',0)" value="-">' + items[i].qty + '<input type=button onclick="parent.update(\''+items[i].title+'\','+i+',1)" value="+"></td><td class="mcinfo">' + sxx + '</td></tr>');
      }
      writeln('<tr><td colspan=4 class="mcinfo"  align=right><b>Total</b></td><td class="mcinfo">$ ' + txx + '</td></tr></table>');
      for(i=1;i<=idx;i++){
        sxx = ((items[i].qty * items[i].price) * 100) +.1;
        sxx = Math.floor(sxx) / 100;
        writeln('<input type=hidden name="title('+i+')" value="' + items[i].title + '"><input type=hidden name="author('+i+')" value="' + items[i].author + '"><input type=hidden name="price('+i+')" value="' + items[i].price + '"><input type=hidden name="qty('+i+')" value="' + items[i].qty + '"><input type=hidden name="isbn('+i+')" value="' + items[i].isbn + '"><input type=hidden name="month('+i+')" value="'+ items[i].month +'"><input type=hidden name="subtotal('+i+')" value="'+ sxx +'">');
      }
      writeln('<input type=hidden name="total" value="' + txx + '">');
      writeln('<input type="submit" value="Submit Order"><input type=button value="Print Order" onclick="javascript:print();"></form>');
      writeln('<hr width="560" align=left>');
      writeln('<table width="560" border=0 CELLSPACING=0 CELLPADDING=3 >');
      writeln('<tr align=Center valign=top><td>');
      writeln('<div class="minibanner">Copyright © 1998 - 2003 By Joel Stead &nbsp; &nbsp; (<a href="mailto:steadjl@iinet.net.au?subject=[Magic%20Circle]:%20Suggestion%20or%20Problem%20Report&body=Please%20no%20orders%20to%20this%20email%20address.">Contact Webmaster</a>)</div>');
      writeln('</td></tr></table>');
    }

    writeln("</body></html>");
    close();
  }
}

