// orders.js
// js function defs for ordertaking/index.html

function verific () {

  var calcres;
  var checkres;

/*
// recall next line instead of 2nd line 25/12!!!
//  calcres = calccost ();
// calcres = 0;
document.order.small.value = 2;
document.order.extralarge.value = 100;
document.order.speed[2].checked = true; 
document.order.customer_name.value = "fred";
document.order.customer_address.value = "1b any road\nanywhere";
document.order.customer_phone.value = "01234 56789";
document.order.customer_email_addr.value = "me@you.com";
//  alert ( "calccost = " + calcres + "." );
// end recall */

  calcres = calccost ();
  if ( calcres ) return false;
  checkres = check_details ();
//checkres = 0;
  if ( checkres ) return false;

  var ans_no;
  var del;
  var sod;
  var items = 0;
  var total = 0;
  var item_lines = 0;

  desc  = new Array ( "small","medium","large","extra large");
  price = new Array ( 5,7.5,10,15 );
  qtty  = new Array ();

  answer = new Array ();
  answer [ 0 ] = document.order.small.value;
  answer [ 1 ] = document.order.medium.value;
  answer [ 2 ] = document.order.large.value;
  answer [ 3 ] = document.order.extralarge.value;
   
  window.scrollTo ( 0, 1000 );

  var params = "location=no, resizable=yes, left=70, top=55, ";
     params += "scrollbars=yes, width=460, height=300, ";
     params += "toolbar=no, status=no, copyhistory=no ";

  newWindow = window.open ( '', 'confirm', params );
  var lines = "<HTML><HEAD><TITLE>Checking Order Details</TITLE></HEAD>";
  lines += "<BODY TEXT='brown' BACKGROUND='verifbg.gif' >" 
  lines += "<TABLE><TR><TD height=20></TD></TR></TABLE>";
  lines += "<P>Please check these order details are correct:-</P>"
  newWindow.document.write ( lines );

  newWindow.document.write ( "<p><TABLE BORDER='0' BGCOLOR='white' " );
  /* next line changed 20feb03  
  newWindow.document.write ( " WIDTH=90% ><TR><TD> " ); */

  newWindow.document.write ( "><TR><TD></td><td><table><tr><td> " );
  var nam  = document.order.customer_name.value;
  lines = ( "<FONT COLOR='blue' >" + nam + "</TD></TR><TR><TD>" ); 
  newWindow.document.write ( lines );

  var addr  = document.order.customer_address.value;
  if ( ! addr ) addr = "Address Not Supplied";
  var i;
  newline = /\r\n/g;
  addr_lines = addr.split ( newline ); 
  lines = ( "</TD></TR><TR><TD><FONT COLOR='blue' >" ); 
  for ( i = 0; i < addr_lines.length; i++ )
  lines += ( addr_lines [ i ] + "<BR>" );
  newWindow.document.write ( lines );

  var phone  = document.order.customer_phone.value;
  if ( ! phone ) phone = "Not Supplied";
  lines = ( "</TD></TR><TR><TD>" ); 
  newWindow.document.write ( lines );
  lines = "<TABLE cellspacing='0' cellpadding='0'>";
  lines += "<TR><TD><FONT COLOR='black' >";
  lines += "Phone </TD><TD align='left'> ";
  lines += ( "<FONT COLOR='blue' >" + phone + "</TD></TR>" ); 
  newWindow.document.write ( lines );

  var eaddr  = document.order.customer_email_addr.value;
  lines = "<TR><TD><FONT COLOR='black' >";
  lines += "E_Mail &nbsp; </TD><TD align='left'> ";
  lines += ( "<FONT COLOR='blue' >" + eaddr + "</TD></TR></TABLE>" ); 
  lines += "</TD></TR></TABLE></td><td></td></tr></table></p>";
  newWindow.document.write ( lines );

  newWindow.document.write ( "<p><TABLE BGCOLOR='white' WIDTH='100%'>" );
  newWindow.document.write ( "<TR><td></td><td><table width='100%'><tr> " );
  lines  = "<TD HEIGHT=50 ALIGN='left'><FONT COLOR='black'> ";
  lines += "<U>Quantity</TD>";
  lines += "<TD ALIGN='left'><FONT COLOR='black'> <U>Description</TD>";
  lines += "<TD ALIGN='right'><FONT COLOR='black'> <U>Unit Price</TD>";
  lines += "<TD ALIGN='right'><FONT COLOR='black'> <U>Net Price</TD>";
  lines += "</TR><TR>";
  newWindow.document.write ( lines );
  lines = "";
  for ( ans_no = 0; ans_no < 4; ans_no++ ) {
    var temp = answer [ ans_no ].toString ();
    if ( temp.length == 0 ) temp = 0;
    qtty [ ans_no ] = parseFloat ( temp );
    if ( temp == 0 ) continue;
    lines += "<TD align='left'><FONT COLOR='blue'>";
    lines += ( qtty  [ ans_no ] + "</TD>" );
    lines += "<TD align='left'><FONT COLOR='blue'>";
    lines += ( desc  [ ans_no ] + "</TD>" );
    var this_price = make_money ( price [ ans_no ] );
    lines += "<TD align='right'><FONT COLOR='navy'>";
    lines += ( this_price + "</TD>" );
    var net_total = make_money ( qtty [ ans_no ] * price [ ans_no ] );
    lines += "<TD align='right'><FONT COLOR='navy'>";
    lines += ( net_total + "</TD></TR>" );
    items += qtty [ ans_no ];
    item_lines++;
    total += ( qtty [ ans_no ] * price [ ans_no ] );
  }
  newWindow.document.write ( lines );

  if ( item_lines > 1 ) {
    newWindow.document.write ( "<TR height=1 ><TD colspan=3>");
    newWindow.document.write ( "</TD><TD><HR size=1 noshade ></TD></TR>");
    lines  = "<TR><TD COLSPAN=3 align='right' ><FONT COLOR='black'>";
    lines += "Subtotal</TD><TD align='right' ><FONT COLOR='navy'>";
    lines += ( make_money ( total ) + "</TD></TR>" );
    newWindow.document.write ( lines );
  }

  // discount
  var discount;
  if ( items >= 10 ) {
    discount = total * 0.2;
    total   -= discount;

    lines  = "<TR><TD COLSPAN=3 align='right' ><FONT COLOR='black'>";
    lines += "Discount</TD><TD align='right' ><FONT COLOR='orangered'>";
    lines += ( make_money ( discount ) + "</TD></TR>" );
    newWindow.document.write ( lines );
    newWindow.document.write ( "<TR height=1 ><TD colspan=3>");
    newWindow.document.write ( "</TD><TD><HR size=1 noshade ></TD></TR>");
    lines  = "<TR><TD COLSPAN=3 align='right' ><FONT COLOR='black'>";
    lines += "Subtotal</TD><TD align='right' ><FONT COLOR='navy'>";
    lines += ( make_money ( total ) + "</TD></TR>" );
    newWindow.document.write ( lines );
  }
 
  // packing and delivery
  if ( document.order.speed[0].checked == true ) { 
    del =  5;
    sod = "3 Working Days";
  }
  if ( document.order.speed[1].checked == true ) { 
    del = 25; 
    sod = "Overnight";
  }
  if ( document.order.speed[2].checked == true ) { 
    del = 50;
    sod = "Same Day";
  }
  total += del;

  lines  = "<TR><TD COLSPAN=3 align='right' ><FONT COLOR='black'>";
  lines += "Packing &amp; Delivery</TD><TD align='right' >";
  lines += ( "<FONT COLOR='navy'>" + make_money ( del ) + "</TD></TR>" );
  newWindow.document.write ( lines );
  newWindow.document.write ( "<TR height=1 ><TD colspan=3>");
  newWindow.document.write ( "</TD><TD><HR size=1 noshade ></TD></TR>");
  lines  = "<TR><TD COLSPAN=3 align='right' ><FONT COLOR='black'>";
  lines += "Subtotal</TD><TD align='right' ><FONT COLOR='navy'>";
  lines += ( make_money ( total ) + "</TD></TR>" );
  newWindow.document.write ( lines );

  // vat
  var vat = total * 0.175;
  total  += vat;
  lines  = "<TR><TD COLSPAN=3 align='right' ><FONT COLOR='black'>";
  lines += "VAT</TD><TD align='right' ><FONT COLOR='navy'>";
  lines += ( make_money ( vat ) + "</TD></TR>" );
  newWindow.document.write ( lines );
  newWindow.document.write ( "<TR height=1 ><TD colspan=3>");
  newWindow.document.write ( "</TD><TD><HR size=1 noshade ></TD></TR>");
  lines  = "<TR><TD COLSPAN=3 align='right' ><FONT COLOR='black'><B><U>";
  lines += "Order Total</TD><TD align='right' ><FONT COLOR='navy'><B><U>";
  lines += ( make_money ( total ) + "</TD></TR>" );
  lines += "<TR><TD></TD></TR></table></td><td></td></tr>";
  newWindow.document.write ( lines );

  newWindow.document.write ( "</TABLE></p>" );

  newWindow.document.write ( "<p><TABLE BORDER=0 BGCOLOR='white' >" );
  newWindow.document.write ( "<tr><td></td><td><table>" );

  // colour chosen
  var colr  = document.order.colour;
  var colrv = colr.options [ colr.selectedIndex ].value;
  if ( colrv ) {
    lines = ( "<TR><TD><FONT COLOR='black'>" + "Colour of order" );
    lines += ( "</TD><TD></TD><TD><FONT COLOR='blue' >" + colrv ); 
    newWindow.document.write ( lines );
  }

  // Speed of Delivery
  lines = ( "<TR><TD><FONT COLOR='black'>" + "Speed of Delivery" );
  lines += ( "</TD><TD></TD><TD><FONT COLOR='blue' >" + sod ); 
  lines += "</TD></TR></TABLE></td><td></td></tr></table></p>";
  newWindow.document.write ( lines );


  // in place of vlog () cos sub page
  lines  = "<img height=\"1\" width=\"1\" src=\"/cgi-bin/vlog.pl\?";

/*
  in place of vlog () cos sub page
  var loc   = document.location;
  var rfrr  = document.referrer;
      line += "loc=";
      line += loc;
      line += "\&";
      line += "rfrr=";
      line += rfrr;
      line += "\" >";
*/

  var loc   = "~/ordertaking/confirm_details";
  var rfrr  = document.location;
  lines += "loc=";
  lines += loc;
  lines += "\&";
  lines += "rfrr=";
  lines += rfrr;
  lines += "\" >";
// alert ( lines );

  newWindow.document.write ( lines );

  newWindow.document.write ( " <p><TABLE WIDTH=90% ><TR><TD> " );
  newWindow.document.write ( " <input type='button' value='Order " );
  newWindow.document.write ( " Details Correct. Send Now' " );
  newWindow.document.write ( " onclick='opener.document.order.submit();" );
  // newWindow.document.write ( " opener.document.order.reset ();" ); 
  newWindow.document.write ( " self.close()'> " );
  newWindow.document.write ( " </TD><TD align='right'> " );
  newWindow.document.write ( " <input type='button' value='Back' " );
  newWindow.document.write ( " onClick='self.close()' >" );
  newWindow.document.write ( " </TD></TR></TABLE></p> " );
  newWindow.document.write ( " </BODY> " );
  newWindow.document.close();



  newWindow.focus();
  return true;
}

function check_details () {

  var nam  = document.order.customer_name.value;
  if ( ! nam ) {
    window.scrollTo ( 0, 790 );
    document.order.customer_name.focus ( );
    alert ( "Please Enter Name in Customer Details" );
    return 1;
  }  

  var phone = document.order.customer_phone.value;
  if ( phone ) {
    var phoneformat = /^[0-9][0-9\ \.\-]+$/;
    var res = phoneformat.test ( phone );
    if ( ! res ) {
      window.scrollTo ( 0, 790 );
      document.order.customer_phone.focus ( );
      alert ( "Please Enter Phone Number in digits e.g. 01234 5678 901" );
      return 1;
    }
  }  

  var eaddr = document.order.customer_email_addr.value;
  if ( ! eaddr ) {
    window.scrollTo ( 0, 790 );
    document.order.customer_email_addr.focus ( );
//    alert ( "Please Enter E-Mail Address" );
    return 1;
  }

  var emailformat = 
// is this regular expression longer than a DNA formula?
  /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  
  var res = emailformat.test ( eaddr );
  if ( ! res ) {
    window.scrollTo ( 0, 790 );
    document.order.customer_email_addr.focus ( );
    alert ( "Please Enter Correct E-Mail Address" );
    return 1;
  }

  return 0;
}

function make_money ( float_pounds ) {

  var whole_pounds;
  var pence;
  var pennies;
  var amount_float;
  var amount_string;

  amount_float = ( Math.round ( float_pounds * 100 ) ) / 100;
  pounds = amount_float;
  pence  = ( amount_float * 100 ) % 100;
  pence  = Math.round ( pence );
  whole_pounds = pounds - ( pence / 100 );
  whole_pounds = Math.round ( whole_pounds );
  pennies = pence;
  if ( pence < 10 ) pennies = ( "0" + pence );
  amount_string = ( "£ " + whole_pounds + "." + pennies );
  return amount_string;
}
/*
<script language="JavaScript" src="file1.js">
</script>
<script language="JavaScript" src="file2.js">
</script>

*/

function calccost() {
   answer = new Array ();
   price  = new Array ( 5, 7.5, 10, 15 );
   qtty   = new Array ();

   answer [ 0 ] = document.order.small.value;
   answer [ 1 ] = document.order.medium.value;
   answer [ 2 ] = document.order.large.value;
   answer [ 3 ] = document.order.extralarge.value;
   
   var del   = -1;    // del buttons response, negative until response found
   var items = 0;
   var total = 0;


   // test quantity answers
   var qtty_found = 0;
   var ans_no;
   for ( ans_no = 0; ans_no < 4; ans_no++ ) {

     // test quantity value for all blank spaces
     var temp = answer [ ans_no ].toString ();
     var len  = temp.length;
     var pos = 0;
     var c;
     for ( pos = 0; pos < len; pos++ ) {
       c = temp.substr ( pos, 1 );
       if ( c == " " ) continue;
       break;
     }
     if ( ( pos == len ) || ( answer [ ans_no ] == 0 ) ) {
       answer [ ans_no ] = 0;
       continue;
     }
     if ( isNaN ( temp ) ) {
       document.order.cost.value = "£";
       window.scrollTo ( 0, 250 );
       document.order.elements [ ans_no ].focus ( );
       alert ( "Please fill in quantity box(es) using numbers e.g. 24" );
       return 1;
     }

     if ( ( temp < 0.5 ) || ( temp > 999999 ) ) {
       var message;
       message  = "The minimum number of items that may be ordered ";
       message += "is one half ( 0.5 )\n";
       message += "The maximum number of items is 999999";
       document.order.elements [ ans_no ].focus ( );
       window.scrollTo ( 0, 250 );
       alert ( message );
       document.order.cost.value = "£";
       return 1;
     }
     ++qtty_found;
     qtty [ ans_no ] = eval ( temp );
     items += qtty [ ans_no ];
     total += ( qtty [ ans_no ] * price [ ans_no ] );
     
   } // end for ( ans_no = 0; ans_no < 4; ans_no++ )

   if ( qtty_found == 0 ) {
     window.scrollTo ( 0, 250 );
     document.order.elements [ 0 ].focus ( );
     document.order.elements [ 0 ].blur ( );
     alert ( "Please enter quantity" );
     document.order.cost.value = "£";
     return 1;
   }

   if ( document.order.speed[0].checked == true ) del =  5;
   if ( document.order.speed[1].checked == true ) del = 25;
   if ( document.order.speed[2].checked == true ) del = 50;
   if ( del < 0 ) {
     document.order.cost.value = "£";
     window.scrollTo ( 0, 490 );
     document.order.speed [ 0 ].focus ( );
     document.order.speed [ 0 ].blur  ( );
     alert ( "Please select Speed of Delivery" );
     return 1;
   }
//   money_size2 = tcost.length;   

   // calculate discount and vat
   if ( items >= 10 ) total *= 0.8;
   total += del;
   total *= 1.175;

   var tcost = make_money ( total );
   document.order.cost.value = tcost;
//   if ( tcost.length > money_size2 )
//     money_size2 = tcost.length;   

   return 0;
}

function askml () {
  var eaddr = document.order.customer_email_addr.value;
  if ( eaddr ) { if ( eaddr.length > 0 ) return 0; }
  confirm_question  = "\t    Message from AlphaSelect 2000\n\n";
  confirm_question += "If you would like to receive the standard ";
  confirm_question += "confirmation E-Mail after sending this \"Order\" ";
  confirm_question += "please enter your real E-Mail address after ";
  confirm_question += "clicking \"OK\" in this panel.";
  confirm_question += "\n\nOtherwise please click on \"Cancel\"  ";
  confirm_question += "\( Which will enter a fake E-Mail address. Just ";
  confirm_question += "change this to your own E-Mail address at any time";
  confirm_question += " if you wish \)"
  var emailreqd = confirm ( confirm_question );
  if ( emailreqd ) return 0;
  else {
    document.order.customer_email_addr.value = "not_reqd@this.time";
    var addr = document.order.customer_address.value;
    if ( addr )
      document.order.stat.focus ();
    else
      document.order.customer_address.focus ();
    return 1;
  }
}


function vlog () {
  
  var line  = "<img height=\"1\" width=\"1\" src=\"/cgi-bin/vlog.pl\?";
  var loc   = document.location;
  var rfrr  = document.referrer;
      line += "loc=";
      line += loc;
      line += "\&";
      line += "rfrr=";
      line += rfrr;
      line += "\" >";
  document.write ( line );

} // end function vlog ()

