|
Scripting the Modem Speed Converter (JavaScript)
function trimDecimals(num) {
num = num * 100; // times 100 for 2 decimal place
num = parseInt(num); // strip off decimals
num = num / 100; // return to original number with x decimal places
return num;
}
function convertSpeed(form) {
var Amt = form.txtAmount.value;
var Units = form.optUnits.selectedIndex;
if (Units == 0) { // kb/sec (kilobits)
form.txtBits.value = trimDecimals(Amt * 1000);
form.txtBytes.value = trimDecimals(Amt * 1000 / 8);
form.txtKbits.value = trimDecimals(Amt);
form.txtTimesFaster.value = trimDecimals(Amt / 50);
form.txtKbytes.value = trimDecimals(Amt / 8);
form.txtMbits.value = trimDecimals(Amt / 1000);
form.txtMbytes.value = trimDecimals(Amt / 1000 / 8);
}
if (Units == 1) { // KB/sec (Kilobytes)
form.txtBits.value = trimDecimals(Amt * 1000 * 8);
form.txtBytes.value = trimDecimals(Amt * 1000);
form.txtKbits.value = trimDecimals(Amt * 8);
form.txtTimesFaster.value = trimDecimals(Amt * 8 / 50);
form.txtKbytes.value = trimDecimals(Amt);
form.txtMbits.value = trimDecimals(Amt / 1000 * 8);
form.txtMbytes.value = trimDecimals(Amt / 1000);
}
if (Units == 2) { // mb/sec (megabits)
form.txtBits.value = trimDecimals(Amt * 1000000);
form.txtBytes.value = trimDecimals(Amt * 1000000 / 8);
form.txtKbits.value = trimDecimals(Amt * 1000);
form.txtTimesFaster.value = trimDecimals(Amt * 1000 / 50);
form.txtKbytes.value = trimDecimals(Amt * 1000 / 8);
form.txtMbits.value = trimDecimals(Amt);
form.txtMbytes.value = trimDecimals(Amt / 8);
}
if (Units == 3) { // MB/sec (Megabytes)
form.txtBits.value = trimDecimals(Amt * 1000000 * 8);
form.txtBytes.value = trimDecimals(Amt * 1000000);
form.txtKbits.value = trimDecimals(Amt * 1000 * 8);
form.txtTimesFaster.value = trimDecimals(Amt * 1000 * 8 / 50);
form.txtKbytes.value = trimDecimals(Amt * 1000);
form.txtMbits.value = trimDecimals(Amt * 8);
form.txtMbytes.value = trimDecimals(Amt);
}
calcDownloadTime(form);
return;
}
function calcDownloadTime(form) { // filesize should in terms of bytes
var Filesize = form.optFilesize.selectedIndex;
if (form.txtAmount.value == 0) return;
var DLSpeed = form.txtBytes.value;
if (Filesize == 0) { // 100 KB = 100,000 Bytes
var DLSize = 100000;
}
if (Filesize == 1) { // 250 KB = 250,000 Bytes
var DLSize = 250000;
}
if (Filesize == 2) { // 500 KB = 500,000 Bytes
var DLSize = 500000;
}
if (Filesize == 3) { // 1 MB = 1024 x 1000 Bytes
var DLSize = 1024000;
}
if (Filesize == 4) { // 100 MB = 100 x 1024 x 1000 Bytes
var DLSize = 102400000;
}
if (Filesize == 5) { // 250 MB = 250 x 1024 x 1000 Bytes
var DLSize = 256000000;
}
if (Filesize == 6) { // 500 MB = 500 x 1024 x 1000 Bytes
var DLSize = 512000000;
}
if (Filesize == 7) { // 1 GB = 1000 MB or 1 Billion Bytes
var DLSize = 1048576000;
}
var DLTime = DLSize / DLSpeed; // equal time in seconds
mod = DLTime % 3600;
form.txtHour.value = parseInt(DLTime / 3600);
form.txtMin.value = parseInt(mod/60);
form.txtSec.value = parseInt(DLTime % 60) + 1;
return;
}
|