var speed;
var capacity;
var bth;

function conv_kt_to_mbt(x) {
	var tmp;
	var tmp2;
	tmp2=x;
	if (document.tapecalc.units[1].checked==true) { 
		tmp=Math.round((x/1024)*60*100);
		tmp2=tmp/100;
	};
	return tmp2;
};

function conv_mb_to_gb(x) {
	var tmp;
	tmp=x;
	if (document.tapecalc.mbgb[1].checked==true) {
		tmp=x/1024;
	};
	return tmp;
};

function redraw() {
	var tmp;
	sizebox=document.tapecalc.size;
	backuptimebox=document.tapecalc.backuptime;
	backuptimeboxc=document.tapecalc.backuptimec;
	tapesreqbox=document.tapecalc.tapesreq;
	tapesreqboxc=document.tapecalc.tapesreqc;
	notesbox=document.tapecalc.notes;
	speedbox=document.tapecalc.speed;
	capacitybox=document.tapecalc.capacity;
	tape = document.tapecalc.tape.options[document.tapecalc.tape.selectedIndex].value;
	tmp = tape.split("/");
	speed = tmp[1];
	capacity=tmp[0];
	notesbox.value="";
	recalc_units();
	return false;
};

function recalc_units() {
	if (speed === undefined) {
		redraw();
	};
	document.tapecalc.speed.value=conv_kt_to_mbt(speed);
	document.tapecalc.capacity.value=conv_mb_to_gb(capacity);
	return false;
};

function tcalc() {
	var tmp;
	redraw();
	if (!sizebox.value) {
		alert("No HD size entered")
	} else { 
		backuptimebox.value=stotime(sizebox.value*1024/speed);
		if (bth>7) {
			noteadd("Consider fitting a faster tape drive");
		};
		backuptimeboxc.value=stotime((sizebox.value*1024/speed)/2);
		tmp=treqcalc(capacity, sizebox.value);
		tapesreqbox.value=tmp;
		if (tmp > 1) {
	    	noteadd("You cannot use the individual file restore (TapeDisk) feature of SnapBack if Tape Spanning (backing up to more than one tape) is used.\nYou may want to consider getting a higher capacity tape drive"); 
		};
	tapesreqboxc.value=treqcalc((capacity*1.5), sizebox.value);
	};
	return false
};

function treqcalc(x, y) {
	//x=capacity of tape
	//y=size of HD
	var tmp;
	tmp=Math.round(y/x);
	if ( Math.round(y/x) < y/x ) {
		tmp=tmp+1
	};
	return tmp;
};

function stotime(x) {
	bts=x;
	//seconds to minutes and remaining seconds
	btm=Math.floor(bts/60);
	bts=Math.round(bts%60);
	//minutes to hours and remaining minutes
	bth=Math.floor(btm/60);
	btm=Math.round(btm%60);
	//hours to days and remaining hours
	btd=Math.floor(bth/24);
	bth=Math.round(bth%24);
	strng=btm+"m "+bts+"s";
	if (bth!=0) { strng=bth+"h "+strng };
	if (btd!=0) { strng=btd+"d "+strng };
	return strng;
}

function noteadd(x) {
	var tmp;
	tmp=notesbox.value;
	if (!tmp) { 
		notesbox.value=x;
	} else {
		tmp=notesbox.value+"\n"+x;
		notesbox.value=tmp;
	};
	return false;
};