help needed for a budding scripter.

admin

Administrator
Staff member
I am a budding scripter and am interested to know if anyone understands this code, and if they are willing to each line as it goes along, in english, not programmer language.

I know it's a calculator, but how does it work?

<form name="form1">
<script language="JavaScript">

function createArray(size) {
for (var i=1; i < size; i++) {
this = 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 Choose", 0.00);
}

var category = new createArray(1);


color1 = "Gold";
color2 = "Orange";


// ******* -- Product Catalogue section to be edited *******

category[1] = new Category("Frame");
category[1].product[1] = new Product("Frame1",629.00);

// ***** End of Product Range - Thats It!.... ***********

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.product[j] != null)
j++;
category.product.length = j;
}
}

SetLengths();

function writeTableRow(i) {
document.write('<tr bgcolor="' + ((i%2 == 0) ? color1 : color2) + '">');
document.write('<td>' + category.title.toUpperCase() + ':<br>'
+ '<select size="1" name="menu' + i + '" onChange="update(' + i + ')">');
len = category.product.length;
for (j=0; j<len; j++) {
if (j != 0)
document.write('<option>' + category.product[j].description
+ ' - ? + fix(category.product[j].price) + '</option>');
else
document.write('<option selected value=http://www.webdeveloper.com/forum/archive/index.php/" ">Please Choose</option>');
}
document.write('</select></td><td valign=bottom>'
+ '<input type="text" value=http://www.webdeveloper.com/forum/archive/index.php/"0.00" name="price' + i + '" '
+ 'size=12 maxlength=12 onFocus="document.form1.price' + i + '.blur()">'
+ '</td></tr>');
}

function writeTable() {
document.write('<table cellspacing="1" cellpadding="7" border="10">');
for (i=1; i<category.length; i++)
writeTableRow(i);
document.write('<tr bgcolor="' + ((category.length%2==0) ? color1 : color2)
+ '"><td align=center>Total:<br> (Built with DT spokes, Clim8 cables, Panaracer tubes) </td><td><input type="text" '
+ 'name="total" size=12 maxlength=12 value=http://www.webdeveloper.com/forum/archive/index.php/"50.00"></td></tr></table>');
}

function update(num) {
eval('selected = document.form1.menu' + num + '.selectedIndex;');
cost = fix(category[num].product[selected].price);
eval('document.form1.price' + num + '.value = cost;');
var grand_total = 50.00;
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;
}

</script><div align="center">
<p align="center"> <br>
<br>
<script language="JavaScript">
writeTable(); // writes all categories
document.write('<br><table cellspacing=10 cellpadding=10 '
+ 'bgcolor="' + color1 + '">');

</script>
</form>
</body>
</html>
 
Back
Top