/*
*******************************************
--- JavaScript Download Time Calculator ---
-------------- Version 1.7 ---------------

-- INFO --
Calculates the time it takes to download a file, based on file size and connection speed.
This script was originally written with BBEdit Lite 4.1, and was edited with BBEdit Lite 4.6, 6.1.2 and TextWrangler 3.5.3.
This script requires JavaScript 1.2.

-- WEB SITE --
http://download.stormloader.com/

-- CONTACT --
derek_tsang (a t) logicbbs (d o t) org
Please type "DOWNLOAD TIME CALCULATOR" in the subject line in all correspondence.

-- HISTORY --
May 30, 2000  -- Version 1.00 -- First release.
May 31, 2000  -- Version 1.05 -- Added: form-based comments, comment() function, form fill-in verification.
June 1, 2000  -- Version 1.06 -- Changed name of script to 'Download Time Calculator'.
June 22, 2000 -- Version 1.07 -- Corrected '1 minutes' to '1 minute' (added if{} conditional); minor cosmetic touches.
July 9, 2001 -- Version 1.08 -- Changed script to reset 'ntime' when the connection speed is changed.
December 29, 2003 -- Version 1.5b -- Added cookie functionality so that the calculator will remember the last-selected connection speed. This beta version is posted in /test/ for private testing. Tweaked connection speeds. Added Legal section. Fixed a bug to do with overlapping names, element comment and function comment(), which caused loss of functionality in Mozilla and Safari. Originally released as 1.09b.
December 30, 2003 -- Version 1.5 -- More tweaks (changed "note" text, changed main page). Originally released as 1.09.
December 30, 2003 -- Version 1.51 -- Added satellite option
April 14, 2004 -- Version 1.6 -- Added support for custom bandwidth units (bps, Kbps, Mbps, Gbps); fixed some redundant code; tweaked connection speeds
April 14, 2004 -- Version 1.61 -- Reports times in hours if time is >120 minutes.
April 25, 2004 -- Version 1.62 -- Added support for terabytes.
November 1, 2004 - Version 1.63 -- Added more cable speeds. Multiplied broadband speeds by 0.9*cable and 0.95*DSL to reflect the non-ideal nature of connections. Removed the "small file size" qualifier.
November 21, 2004 - Version 1.63b -- Removed netfirms.com mirror (offline) and updated contact information.
February 8, 2012 - Version 1.7 -- Updated speeds to reflect 2012 broadband services

-- LEGAL --
Copyright 2000-2012 Derek Tsang.
No part of this script may be reproduced on any type of media, including, but not limited to, other web sites without explicit permission from the author (Derek Tsang).
An exception shall be granted to only the built-in caching functions of internet web browsers and HTTP proxy servers.

Parts of this code were written by Duncan Crombie - dcrombie (a t) chirp.com.au. These sections of code are clearly indicated using JavaScript comments and inquiries regarding their use shall be directed to the preceding e-mail address.

*******************************************
*/

// creating variables
var nfilesize;
var nfilesizeKB;
var nmdmspd_bps;
var ntime;
var tunit;
var tunitsl;
var tcntype;
var tcnctntype;
var tcomment;
var bremember;
var ncookiespeed;
var tmdmspdunit;
var tmdmspdunitl;

// ridding 'undefined' by nulling tcomment
tcomment = ""

function calculate() {

// assigning variables
nfilesize = document.calc.filesize.value;
nmdmspd_bps = document.calc.mdmspd.value;
tmdmspdunitl = document.calc.mdmspdunit.selectedIndex;
tmdmspdunit = document.calc.mdmspdunit.options[tmdmspdunitl].value;
tcntype = document.calc.cnctntype.selectedIndex;
tcnctntype = document.calc.cnctntype.options[tcntype].value;
tunitsl = document.calc.filesizeunit.selectedIndex;
tunit = document.calc.filesizeunit.options[tunitsl].value;

if(nfilesize == ""){
alert("Please enter the size of the file download.")
document.calc.filesize.focus()

return false;
}

// converting filesize to kilobytes
if(tunit == "Bytes") {
nfilesizeKB = nfilesize / 1024
}

if(tunit == "Kilobytes") {
nfilesizeKB = nfilesize * 1
}

if(tunit == "Megabytes") {
nfilesizeKB = nfilesize * 1024
}

if(tunit == "Gigabytes") {
nfilesizeKB = nfilesize * 1048576
}

if(tunit == "Terabytes") {
nfilesizeKB = nfilesize * 1073741824
}


if(tunit == "") {
alert("Please select a unit size.")
document.calc.filesizeunit.focus()

return false;
}

// converting nmdmspd_bps to bits per second based on tmdmspdunit
if(tmdmspdunit == "kbps"){
nmdmspd_bps = nmdmspd_bps * 1024;
}

if(tmdmspdunit == "mbps"){
nmdmspd_bps = nmdmspd_bps * 1048576;
}

if(tmdmspdunit == "gbps"){
nmdmspd_bps = nmdmspd_bps * 1073741824;
}

if(tcnctntype == "144") {
nmdmspd_bps = 14400
}

if(tcnctntype == "288") {
nmdmspd_bps = 28800
}

if(tcnctntype == "336") {
nmdmspd_bps = 33600
}

if(tcnctntype == "56") {
nmdmspd_bps = 57600
}

if(tcnctntype == "64") {
nmdmspd_bps = 64000
}

if(tcnctntype == "128") {
nmdmspd_bps = 115200
}

if(tcnctntype == "DSL") {
nmdmspd_bps = 1572864 * 0.90
}

if(tcnctntype == "fDSL") {
nmdmspd_bps = 3145728 * 0.90
}

if(tcnctntype == "f1DSL") {
nmdmspd_bps = 6291456 * 0.87
}

if(tcnctntype == "f2DSL") {
nmdmspd_bps = 12582912 * 0.86
}

if(tcnctntype == "f3DSL") {
nmdmspd_bps = 26214400 * 0.85
}

if(tcnctntype == "cable") {
nmdmspd_bps = 2097152 * 0.96
}

if(tcnctntype == "f2cable") {
nmdmspd_bps = 5242880 * 0.95
}

if(tcnctntype == "f3cable") {
nmdmspd_bps = 10485760 * 0.92
}

if(tcnctntype == "f4cable") {
nmdmspd_bps = 20971520 * 0.90
}

if(tcnctntype == "f5cable") {
nmdmspd_bps = 52428800 * 0.85
}

if(tcnctntype == "sat") {
nmdmspd_bps = 250000
}

if(tcnctntype == "fsat") {
nmdmspd_bps = 1000000
}

if(tcnctntype == "T1") {
nmdmspd_bps = 1154000
}

if(tcnctntype == "T3") {
nmdmspd_bps = 3462000
}

if(tcnctntype == "LAN") {
nmdmspd_bps = 15000000
}

// verifying that speed/type of connection fields are filled in
if(nmdmspd_bps == "" && tcnctntype == ""){
alert("Please fill in your connection speed or type.")
document.calc.mdmspd.focus()
document.calc.mdmspd.select()

return false;
}

// converting bps to KBps
nmdmspd_KBps = nmdmspd_bps / 8192;

// setting time to download in seconds
ntime = nfilesizeKB / nmdmspd_KBps;


// for times less than or equal to 120 min
if (ntime <= 7200){
// converting time to minutes
ntime /= 60

// conditional to check ntime value in minutes and adjusting result display
if (ntime > 1){
// rounding
ntime = Math.round(ntime)

if (ntime >= 2){
ntime += " minutes"
}

if (ntime == 1) {
ntime = "1 minute"
}
}

if (ntime < 1){
ntime = "Less than one minute"
}

}

// for times greater than 120 min
if (ntime > 7200){
ntime /= 3600;
ntime = Math.round(ntime*10)/10;
ntime += " hours";
}

// setting result field to ntime
document.calc.time.value = ntime;

document.calc.comment.value = "NOTE: Times are approximate. Internet slowdowns may affect actual download time."

// Runs cookie functions with properties of the cookie embedded
setCookie("speed", nmdmspd_bps)

}


// Cookie code
function setCookie(name, value){
bremember = document.calc.remember.checked;

// Code to write cookie when box is checked with a proper expiry date and data value (speed)
if (bremember == true){

// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.
var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000); // plus 365 days
document.cookie = name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
// End borrowed code

}

// Code to destroy cookie if box is not checked
if (bremember == false){
// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.
var today = new Date();
var expiry = new Date(today.getTime() - 365 * 24 * 60 * 60 * 1000); // minus 365 days
document.cookie = name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
// End borrowed code
}

}


// function to insert comments for ISPs with varying speeds and to reset time box upon speed change
function txtcomment(){

// resetting comments
tcomment = ""

tcntype = document.calc.cnctntype.selectedIndex;
tcnctntype = document.calc.cnctntype.options[tcntype].value;

var cmt;
cmt = " connection speeds may vary widely between different service providers."

if (tcnctntype == "DSL" || tcnctntype == "fDSL"){
tcomment = "NOTE: DSL"+ cmt
}

if (tcnctntype == "cable" || tcnctntype == "fcable"){
tcomment = "NOTE: Cable modem"+cmt
}

if (tcnctntype == "LAN"){
tcomment = "NOTE: Ethernet network"+cmt
}

document.calc.comment.value = tcomment

// setting value of the time box to be blank upon change of connection speed
ntime = "";
document.calc.time.value = ntime


}

// See if checkbox was checked last time; if so, then re-check the box. Also reloads cookie value
function check(){

if (document.cookie != ""){
document.calc.remember.checked=true;

// Parses cookie value to extract numerical speed
// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.
var bikky = document.cookie;

function getCookie(name) { // use: getCookie("name");
var index = bikky.indexOf(name + "=");
if (index == -1) return null;
index = bikky.indexOf("=", index) + 1; // first character
var endstr = bikky.indexOf(";", index);
if (endstr == -1) endstr = bikky.length; // last character
return unescape(bikky.substring(index, endstr));
}
// End borrowed code

// Function returns value of speed, so I'm putting this value to a variable ncookiespeed
ncookiespeed = getCookie("speed")

// Write cookie speed to the field on the form
document.calc.mdmspd.value = ncookiespeed
document.calc.mdmspdunit.selectedIndex = 0

}
}

