﻿// JScript File
//
//Author: Sunhee Miller
//Name: Base number converter
//copyright@2006
//developed as one of Math tools for smifzone.org
//
//
var otherBase;

function findSelected(){

var baseSel = document.getElementById('baseSelect1');
var baseSys = baseSel.options[baseSel.selectedIndex].text;
var baseN = Number(baseSys);

otherBase = baseN;

}


function evaluateDecimal(){

var baseSel = document.getElementById('baseSelect1');
var baseSys = baseSel.options[baseSel.selectedIndex].text;

var decV = document.getElementById('input1').value;
var n = Number(decV);
var str1 = "";
var len=0;

if (isNaN(decV)){
  alert("Sorry, I can't process any non-numeric values.  Enter a number!");
  return;
}

if (isNaN(baseSys)){
  alert("You have to choose a base system!");
  return;
}

len = decV.length;
if (len > 10){
  alert("For the efficiency matters, no more than 10 digits are allowed!");
  return;
}

if (IsInteger(decV)){
  str1 = n.toString(otherBase);
  document.getElementById('DecToOther').value = str1;
}
else
{  alert("Enter only positive integer numbers, please!");
   return;
}


}


function IsInteger(d){

var numStr = d;
var isInteger = true;
var len = numStr.length;
var i, sym;

sym = 0;  
for(i=0;((i<len) && (isInteger==true));i++){
  ch = numStr.charAt(i);
  if (ch == '.')
    isInteger = false;
}

return isInteger;
}


function evaluateOther(){

var baseSel = document.getElementById('baseSelect2');
var baseSys =  Number(baseSel.options[baseSel.selectedIndex].text);

var otherV = document.getElementById('input2').value;
var n = Number(otherV);
var str1 = "";
var len;

//if (isNaN(otherV)){
  //alert("Sorry, I can't process any non-numeric values.  Enter a number!");
  //return;
//}

if (isNaN(baseSys)){
  alert("You have to choose a base system!");
  return;
}

if (n < 0){
  alert("We're only working with positive integer values that is greater than 0.");
  return;
}

len = otherV.length;

if (len > 15){
  alert("For the efficiency matters, no more than 15 digits are allowed!");
  return;
}


if (IsComp(baseSys, otherV)==true){
  str1 = convertToDecimal(baseSys, otherV);
  document.getElementById('OtherToDec').value = str1;
}
else
{
  alert("Please be sure " + otherV + " is " + " for the base " + base1);
}

}


function IsComp(b, k){

var baseN = b;
var tempK = k;
var len = tempK.length;

var AlphaRange = "";
var base2 = "01";
var base3 = "012";
var base4 = "0123";
var base5 = "01234";
var base6 = "012345";
var base7 = "0123456";
var base8 = "01234567";
var base9 = "012345678";
var base16 = "0123456789ABCDEFabcdef";

var inBase = true;
var sym, i, ch;

if (baseN == 2)
  AlphaRange = base2;
if (baseN == 3)
  AlphaRange = base3;
if (baseN == 4)
  AlphaRange = base4;
if (baseN == 5)
  AlphaRange = base5;
if (baseN == 6)
  AlphaRange = base6;
if (baseN == 7)
  AlphaRange = base7;
if (baseN == 8)
  AlphaRange = base8;
if (baseN == 9)
  AlphaRange = base9;
if (baseN == 16)
  AlphaRange = base16;
  
sym = 0;  
for(i=0;((i<len) && (inBase==true));i++){
  ch = tempK.charAt(i);
  sym = AlphaRange.indexOf(ch);
  if (sym == -1)
    inBase = false;
}

return inBase;

}


function convertToDecimal(b, v){

var numStr = v;
var baseStr = b;
var baseN = Number(b);
var len = numStr.length;
var i, ch, sym, p;
var n = 0, total = 0;

p = len - 1;
for(i=0;i<len;i++){
  ch = numStr.charAt(i);
  
  if ((ch == 'a') || (ch == 'A'))
    ch = '10';
  if ((ch == 'b') || (ch == 'B'))
    ch = '11';
  if ((ch == 'c') || (ch == 'C'))
    ch = '12';
  if ((ch == 'd') || (ch == 'D'))
    ch = '13';
  if ((ch == 'e') || (ch == 'e'))
    ch = '14';
  if ((ch == 'f') || (ch == 'F'))
    ch = '15';
    
  n = Number(ch);
  total = total + Math.pow(baseN, p)*n;
  p--;
  
}

return total;

}



function resetDecToOther(){

document.getElementById('input1').value = "";
document.getElementById('DecToOther').value = "";

}


function resetOtherToDec(){

document.getElementById('input2').value = "";
document.getElementById('OtherToDec').value = "";

}

