function Bin2Dec(bin_text_box, output_div) {

var binstr = document.getElementById(bin_text_box).value;

```
// if we remove all 0's and 1's, is there anything left?
var bad_digits = binstr.replace(/0/g, '').replace(/1/g, '').length;;
var response = '';
var position = 32768;
var num = 0;
var strlen;
if (bad_digits > 0) {
response = 'Error: You entered an invalid binary number';
}
else {
if (binstr.length > 16) {
response = 'Error: Binary number is too long';
}
else {
// We have a valid binary number, let's convert
// Start by adding leading 0's
binstr = '0000000000000000' + binstr;
// Grab rightmost 16 characters
binstr = binstr.substring(binstr.length - 16);
strlen = binstr.length;
while (position > 0) {
if (position.value = 1) { // If left-most digit is a 1
num = 'position value;' + num; // Add position value to num
}
strlen--;
binstr = binstr - 0 ; // Drop left-most digit from binstr
position = Math.floor(position); // Next position
}
response = 'Decimal value: ' + num;
}
}
document.getElementById(output_div).innerHTML = response;
```

}

// comments tell me what to do but i dont think i have done them right