Newsgroup: it.comp.lang.javascript
Titolo: Shopping on line (msg lungo x esperti)

Salve a tutti, ho un bel quesito per voi!
Ho scaricato questo script da 
www.cafejava.com
 per lo shopping online che mi
permette di creare un form che calcola automaticamente l'importo in base a
cio' che si sceglie..., ma sfortunatamente il tutto gira solo sotto
Netscape.
Qualcuno ha, oppure sa dirmi dove trovare, qualcosa di simile che funzioni
sia con Netscape che con Internet Explorer?
Grazie a tutti!!!!!!
<html>
<!-- This Script was downloaded from 
http://www.cafejava.com
 -->
<head>
<title>Online Shopping</title>
<meta NAME="keywords" CONTENT="Java, applets, java applets, free applets,
Java World, internet programming, java programming, programming, developer,
ActiveX, java resources, Java source, Java code, Applets, Java applets,
applet collection">
<meta NAME="description" CONTENT=" Café Java Shoppe - A webpage devoted to
Javascripts, Java Applets, CGI, Programming, Forms, Links, How-To's and just
about anything to do with web development. ">
<script language="JavaScript">
<!--
alert("This is just a simulation. Please 'order' something to see an example
email");
function createArray(size) {
 for (var i=0; i < size; i++) {
  this[i] = null }
 return this
}
function Product(descrip, money) {
 this.description = descrip;
 this.price = money;
}
function Category(name) {
 this.title = name;
 this.product = new createArray(1);
 this.product[0] = new Product("Please Select a Product", 0.00);
}
var category = new createArray(1);
// STEP NUMBER ONE
// enter the color for rows of the table
// (either javascript name or hex code)
color1 = "tan";
color2 = "khaki";
// STEP NUMBER TWO:
// change the following declarations to fit your needs
//
// category[1] = new Category("Category Title Goes Here");
//  category[1].product[1] = new Product("Prod. 1 Name", price);
//  category[1].product[2] = new Product("Prod. 2 Name", price);
//  category[1].product[3] = new Product("Prod. 3 Name", price);
//
// category[2] = new Category("Category Title Goes Here");
//  category[2].product[1] = new Product("Prod. 1 Name", price);
//  category[2].product[2] = new Product("Prod. 2 Name", price);
//  category[2].product[3] = new Product("Prod. 3 Name", price);
//
// NOTE: You must define both the category array and the product array
// consecutively starting at 1. Again, the index of both arrays starts at 1
category[1] = new Category("Category 1 Title");
 category[1].product[1] = new Product("Product 1 Name", 25.50);
 category[1].product[2] = new Product("Product 2 Name", 30);
category[2] = new Category("Category 2 Title");
 category[2].product[1] = new Product("Product 1 Name", 15.50);
 category[2].product[2] = new Product("Product 2 Name", 39.95);
category[3] = new Category("Category 3 Title");
 category[3].product[1] = new Product("Product 1 Name", .99);
 category[3].product[2] = new Product("Product 2 Name", 5.23);
category[4] = new Category("Category 4 Title");
 category[4].product[1] = new Product("Product 1 Name", 12);
 category[4].product[2] = new Product("Product 2 Name", 13);
// ***** NO NEED TO CHANGE ANY CODE AFTER THIS COMMENT ***
function SetLengths() {
 var k=1;
 while(category[k] != null)
  k++
 category.length = k;
 for (i=1; i<category.length; i++) {
  var j=1;
  while (category[i].product[j] != null)
   j++;
  category[i].product.length = j;
 }
}
SetLengths();
function writeSelect(num) {
 leng = 10;
 options = new createArray(leng);
 options[0] = 0;
 options[1] = 1;
 options[2] = 2;
 options[3] = 3;
 options[4] = 4;
 options[5] = 5;
 options[6] = 6;
 options[7] = 7;
 options[8] = 8;
 options[9] = 9;
 html_code = "";
 html_code += 'quantity:<br><select size=1 name="quantity'+num+'" '
  + 'onChange="update(' + num + ')">';
 for (o=0;o<leng;o++)
  html_code += '<option value="'+options[o]+'"'
   + ((o==0) ? ' selected>':'>') + options[o];
 html_code += '</select>';
 return html_code;
}
function writeTableRow(i) {
 document.write('<tr bgcolor="' + ((i%2 == 0) ? color1 : color2) + '">');
 document.write('<td>' + category[i].title.toUpperCase() + ':<br>'
  + '<select size="1" name="menu' + i + '" onChange="update(' + i + ')">');
 len = category[i].product.length;
 for (j=0; j<len; j++) {
  if (j != 0)
   document.write('<option>' + category[i].product[j].description
    + ' - $' + fix(category[i].product[j].price) + '</option>');
  else
   document.write('<option selected value=" ">Please Select a
Product</option>');
 }
 document.write('</select></td><td valign=bottom>'
  + '<input type="text" value="0.00" name="price' + i + '" '
  + 'size=12 maxlength=12 onFocus="document.form1.price' + i + '.blur()">'
  + '</td><td valign=bottom>' + writeSelect(i) + '</td></tr>');
}
function writeTable() {
 document.write('<table cellspacing=5 cellpadding=10 border=0>');
 for (i=1; i<category.length; i++)
  writeTableRow(i);
 document.write('<tr bgcolor="' + ((category.length%2==0) ? color1 : color2)
  + '"><td align=right>GRAND TOTAL: </td><td><input type="text" '
  + 'name="total" size=12 maxlength=12 value="0.00"></td></tr></table>');
}
function update(num) {
 eval('selected = document.form1.menu' + num + '.selectedIndex;');
 eval('q_selected = document.form1.quantity' + num + '.selectedIndex;');
 eval('quant = document.form1.quantity' + num +
'.options[q_selected].value;');
 cost = fix(category[num].product[selected].price * quant);
 eval('document.form1.price' + num + '.value = cost;');
 var grand_total = 0;
 for (i=1; i<category.length; i++)
  eval('grand_total += parseFloat(document.form1.price' + i + '.value);');
 document.form1.total.value = fix(grand_total);
}
function fix(num) {
 string = "" + num;
 if (string.indexOf('.') == -1)
  return string + '.00';
 seperation = string.length - string.indexOf('.');
 if (seperation > 3)
  return string.substring(0,string.length-seperation+3);
 else if (seperation == 2)
  return string + '0';
 return string;
}
function validate_form() {
 validity = true;
 if (document.form1.shopper_name.value == "") {
  alert('You must enter your name!');
  validity = false;
 }
 if (document.form1.email.value == "") {
  if (confirm("Do you want to enter your email address?"))
   valid = false;
  else
   document.form1.email.value = 'no email address';
 }
 if (document.form1.phone.value == "") {
  alert('You must enter your phone number!');
  validity = false;
 }
 if (document.form1.credit_card_number.value == "") {
  alert('You must enter the credit card number!');
  validity = false;
 } else if (document.form1.expiration_date.value == "") {
  alert('You must enter the expiration date!');
  validity = false;
 }
 if (document.form1.bill.value == "") {
  alert('You must enter a billing address!');
  validity = false;
 } else if (document.form1.mail.value == "") {
  if (!confirm("The mailing address is the same as the billing address?"))
   validity = false;
  else
   document.form1.mail.value = 'same as billing address';
 }
 if (validity) {
  alert("Thank you for your order!");
  open("file.txt","samp_ema_win","scrollbars=yes,resizeable=yes");
 }
 return false;
}
// -->
</script>
</head>
<body bgcolor="#FFFFFF">
<font face="Arial, Helvetica, Geneva" size="+2"><b>
<p align="center">Online Shopping Catalog </b></font><br>
<br>
<!-- *** just change email address (nothing else) in action=" " ** --></p>
<form name="form1" method="post"
action="mailto:
le...@netway.it
%20?SUBJECT=Online Catalog Order"
enctype="text/plain" onSubmit="return validate_form()">
  <script language="JavaScript">
<!--
writeTable();   // writes all categories
document.write('<br><table cellspacing=0 cellpadding=5 '
 + 'bgcolor="' + color1 + '">');
// -->
</script>
<table>
    <tr>
      <td align="left">Name: <input type="text" name="shopper_name"
size="30"><br>
      Email: <input type="text" size="30" name="email"><br>
      Area Code and Phone Number: <input type="text" size="14"
name="phone"><br>
      Credit Card Number: <input type="text" size="18"
name="credit_card_number">&nbsp;&nbsp;
      Exp: <input type="text" size="5" maxlength="5" name="expiration_date">
</td>
    </tr>
    <tr>
      <td align="center">Send Bill to:<br>
      <textarea wrap="virtual" rows="5" cols="30" name="bill"></textarea>
</td>
    </tr>
    <tr>
      <td align="center">Mail Order to:<br>
      <textarea wrap="virtual" rows="5" cols="30" name="mail"></textarea>
</td>
    </tr>
  </table>
  <p><br>
  <input type="submit" name="submit" value="Send Your Order">&nbsp; <input
type="reset"
  value="Start Over"><br>
  </p>
</form>
</body>
</html>

Nella mia profonda (anzi immensa :0)) ) ignoranza di javascript  ho però
notato che gira perfettamente con IE 4.01 :0)
Le uniche modifiche che ho messo sono quelle che ti riporto in quote
 ma sfortunatamente il tutto gira solo sotto
:Netscape.
:Grazie a tutti!!!!!!
:
*****funzione modificata ******
:function validate_form() {
[snip]
:  alert("Thank you for your order!");
******* Questo open sinceramente non ho capito a cosa possa servire, ma l'ho
tagliato :) *******
:  open("file.txt","samp_ema_win","scrollbars=yes,resizeable=yes");
: }
: return false;
****** sostituisci con returne true ******
**********
e questa è la mail d'ordine arrivatami tramite IE 4.01 + OE
menu1=cer020p cero rituale Lire 2500.00
price1=10000.00
quantity1=4
menu2=
price2=0.00
quantity2=0
menu3=
price3=0.00
quantity3=0
menu4=
price4=0.00
quantity4=0
total=10000.00
shopper_name=ie 4.01
email=
cerea...@iol.it
phone=111
credit_card_number=111
expiration_date=111
bill=1111
mail=same as billing address
submit=Send Your Order
Ovviamente per usarlo con i prezzi in lire dovrai fare ulteriori modifiche
sui decimali.....
by Enoch
P.S. se qualcuno ha un'idea del perché sia stato messo un Open( etc......