// global variables

var feetA, inchA, feetB, inchB, feetC, inchC, angleA, angleB;

function checkIt(evt, field) 
{
  evt = (evt) ? evt : window.event;
  var charCode = (evt.which) ? evt.which : evt.keyCode;
  if( field.id == "alpha" || field.id == "beta")
  {
    if(charCode == 46)
    {
      pos = field.value.indexOf(".");
      if( pos != -1)
      {
        alert("This field accepts decimal only once");
        return false;
      }
    }

    if (charCode > 31 && (charCode < 46 || charCode > 57 || charCode == 47)) 
    {
      alert("This field accepts decimal numbers only");
      return false;
    }
  }
  else
  {
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    {
      alert("This field accepts numbers only.");
      return false;
    }      
  } 
  return true;
}

function set_defaults()
{
  document.getElementById('sidea_feet').value = "";
  document.getElementById('sidea_inch').value = "";
  document.getElementById('sideb_feet').value = "";
  document.getElementById('sideb_inch').value = "";  
  document.getElementById('sidec_feet').value = "";
  document.getElementById('sidec_inch').value = "";  
  document.getElementById('alpha').value = "";
  document.getElementById('beta').value = ""; 
}

function calculate_angle()
{
  var condition = "";
  var condition_matched=new Boolean(false);

  
  // User should enter either
  // a) 1 side and 1 angle OR
  // b) 2 sides
  // truth table for valid entries
  //
  // SideA SideB SideC AngleA AngleB
  //    1   1     0     0       0
  //    1   0     1     0       0
  //    0   1     1     0       0 
  //    1   0     0     1       0
  //    1   0     0     0       1
  //    0   1     0     1       0
  //    0   1     0     0       1
  //    0   0     1     1       0
  //    0   0     1     0       1
  
  var truth_table = new Array('11000','10100','01100','10010','10001','01010','01001','00110','00101');
  
  // generate the condition, so as to compare it against the truth table
  feetA = document.getElementById('sidea_feet').value;
  feetA = (feetA != 0) ? parseFloat(feetA) : 0;
  inchA = document.getElementById('sidea_inch').value;
  inchA = (inchA != 0) ? parseFloat(inchA) : 0;
  sideA = Math.round((parseFloat(feetA) + parseFloat(inchA/12)) * 100)/100;
  condition = (sideA != 0) ? (condition + '1') : (condition + '0');
    
  feetB = document.getElementById('sideb_feet').value;
  feetB = (feetB != 0) ? parseFloat(feetB) : 0;
  inchB = document.getElementById('sideb_inch').value;
  inchB = (inchB != 0) ? parseFloat(inchB) : 0;
  sideB = Math.round((parseFloat(feetB) + parseFloat(inchB/12)) * 100)/100;
  condition = (sideB != 0) ? (condition + '1') : (condition + '0');
  
  feetC = document.getElementById('sidec_feet').value;
  feetC = (feetC != 0) ? parseFloat(feetC) : 0;
  inchC = document.getElementById('sidec_inch').value;
  inchC = (inchC != 0) ? parseFloat(inchC) : 0;
  sideC = Math.round((parseFloat(feetC) + parseFloat(inchC/12)) * 100)/100;
  condition = (sideC != 0) ? (condition + '1') : (condition + '0');
  
  angle = document.getElementById('alpha').value; 
  angleA = (angle != 0) ? parseFloat(angle) : 0; 
  condition = (angleA != 0) ? (condition + '1') : (condition + '0');
  
  angle = document.getElementById('beta').value; 
  angleB = (angle != 0) ? parseFloat(angle) : 0;
  condition = (angleB != 0) ? (condition + '1') : (condition + '0'); 

  for (i = 0; i < 9; i++)
  {
    if (condition == truth_table[i])
    {
      condition_matched = true;
      break;
    }
  }  
  
  if(condition_matched == false)
  {
    alert("Enter two sides OR one side and one angle");
    return false;
  }
  
  switch(condition)
  {
    case '11000':  //find SideC and angles
      case11000();
      break;
    case '10100':  //find SideB and angles
      case10100();
      break;
    case '01100':  //find SideA and angles
      case01100();
      break;
    case '10010':  //find SideB & C and angle beta
      case10010();
      break;                    
    case '10001':  //find SideB & C and angle alpha
      case10001();
      break;
    case '01010':  //find SideA & C and angle beta
      case01010();
      break;                    
    case '01001':  //find SideA & C and angle alpha
      case01001();
      break;
    case '00110':  //find SideA & B and angle beta
      case00110();
      break;                    
    case '00101':  //find SideA & B and angle alpha
      case00101();
      break;                  
    default:
      alert("Enter two sides OR one side and one angle");
  }
  return true;
}

function case11000() //find SideC and angles
{
  sideC = Math.sqrt(Math.pow(sideA,2) + Math.pow(sideB,2));
  sideC = parseFloat(Math.round(sideC * 100)/100);
  // convert value into feet and inches
  feetC = findFeet(String(sideC));
  inchC = findInch(String(sideC));
  // find angles using radian to degree conversion
  angleA = Math.round((Math.asin(sideB/sideC) * 180/Math.PI)*100)/100;
  angleB = Math.round((90 - angleA)*100)/100;
  showResults();
  return true;
}

function case10100() //find SideB and angles
{
  if(sideC <= sideA)
  {
    alert("Side c should be the longest");
    return false;
  }
  sideB = Math.sqrt(Math.pow(sideC,2) - Math.pow(sideA,2));
  sideB = parseFloat(Math.round(sideB * 100)/100);
  // convert value into feet and inches
  feetB = findFeet(String(sideB));
  inchB = findInch(String(sideB));
  // convert value into feet and inches
  // find angles using radian to degree conversion
  angleA = Math.round((Math.asin(sideB/sideC) * 180/Math.PI)*100)/100;
  if (angleA == 0)
  { 
    angleB = 0;
  }
  else
  {
    angleB = Math.round((90 - angleA)*100)/100;
  }  
  showResults();
}

function case01100() //find SideA and angles
{
  if(sideC <= sideB)
  {
    alert("Side c should be the longest");
    return false;
  }
  sideA = Math.sqrt(Math.pow(sideC,2) - Math.pow(sideB,2));
  sideA = parseFloat(Math.round(sideA * 100)/100);
  // convert value into feet and inches
  feetA = findFeet(String(sideA));
  inchA = findInch(String(sideA));
  // find angles using radian to degree conversion
  angleA = Math.round((Math.asin(sideB/sideC) * 180/Math.PI)*100)/100;
  if (angleA == 0)
  { 
    angleB = 0;
  }
  else
  {
    angleB = Math.round((90 - angleA)*100)/100;
  }  
  showResults();
}

function case10010() //find SideB & C and angle beta
{
  sideC = sideA/(Math.sin((angleA * Math.PI)/180));
  sideC = parseFloat(Math.round(sideC * 100)/100);
  // convert value into feet and inches
  feetC = findFeet(String(sideC));
  inchC = findInch(String(sideC));
  
  sideB = sideC * Math.cos((angleA * Math.PI)/180);
  sideB = parseFloat(Math.round(sideB * 100)/100);
  // convert value into feet and inches
  feetB = findFeet(String(sideB));
  inchB = findInch(String(sideB));
  // find angle
  angleB = Math.round((90 - angleA)*100)/100;
 
  showResults();
}

function case10001() //find SideB & C and angle alpha
{
  sideC = sideA/(Math.cos((angleB * Math.PI)/180));
  sideC = parseFloat(Math.round(sideC * 100)/100);
  // convert value into feet and inches
  feetC = findFeet(String(sideC));
  inchC = findInch(String(sideC));
  
  sideB = sideC * Math.sin((angleB * Math.PI)/180);
  sideB = parseFloat(Math.round(sideB * 100)/100);
  // convert value into feet and inches
  feetB = findFeet(String(sideB));
  inchB = findInch(String(sideB));
  // find angle
  angleA = Math.round((90 - angleB)*100)/100;
 
  showResults();
}

function case01010() //find SideA & C and angle beta
{
  sideC = sideB/(Math.cos((angleA * Math.PI)/180));
  sideC = parseFloat(Math.round(sideC * 100)/100);
  // convert value into feet and inches
  feetC = findFeet(String(sideC));
  inchC = findInch(String(sideC));
  
  sideA = sideC * Math.sin((angleA * Math.PI)/180);
  sideA = parseFloat(Math.round(sideA * 100)/100);
  // convert value into feet and inches
  feetA = findFeet(String(sideA));
  inchA = findInch(String(sideA));
  // find angle
  angleB = Math.round((90 - angleA)*100)/100;
 
  showResults();
}

function case01001() //find SideA & C and angle alpha
{
  sideC = sideB/(Math.sin((angleB * Math.PI)/180));
  sideC = parseFloat(Math.round(sideC * 100)/100);
  // convert value into feet and inches
  feetC = findFeet(String(sideC));
  inchC = findInch(String(sideC));
  
  sideA = sideC * Math.cos((angleB * Math.PI)/180);
  sideA = parseFloat(Math.round(sideA * 100)/100);
  // convert value into feet and inches
  feetA = findFeet(String(sideA));
  inchA = findInch(String(sideA));
  // find angle
  angleA = Math.round((90 - angleB)*100)/100;
 
  showResults();
}

function case00110() //find SideA & B and angle beta
{
  sideA = sideC * (Math.sin((angleA * Math.PI)/180));
  sideA = parseFloat(Math.round(sideA * 100)/100);
  // convert value into feet and inches
  feetA = findFeet(String(sideA));
  inchA = findInch(String(sideA));
  
  sideB = sideC * Math.cos((angleA * Math.PI)/180);
  sideB = parseFloat(Math.round(sideB * 100)/100);
  // convert value into feet and inches
  feetB = findFeet(String(sideB));
  inchB = findInch(String(sideB));
  // find angle
  angleB = Math.round((90 - angleA)*100)/100;
 
  showResults();
}

function case00101() //find SideA & B and angle alpha
{
  sideA = sideC * (Math.cos((angleB * Math.PI)/180));
  sideA = parseFloat(Math.round(sideA * 100)/100);
  // convert value into feet and inches
  feetA = findFeet(String(sideA));
  inchA = findInch(String(sideA));
  
  sideB = sideC * Math.sin((angleB * Math.PI)/180);
  sideB = parseFloat(Math.round(sideB * 100)/100);
  // convert value into feet and inches
  feetB = findFeet(String(sideB));
  inchB = findInch(String(sideB));
  // find angle
  angleA = Math.round((90 - angleB)*100)/100;
 
  showResults();
}

function findFeet(value)
{
  if (value.indexOf('.',1) == -1)
  {
    return value;
  }
  else
  {
    return(value.substring(0, value.indexOf('.',1)));
  }  
}

function findInch(value)
{
  if (value.indexOf('.',1) == -1)
  {
    value = 0;
    return value;
  }
  else
  {
    return(Math.round(parseFloat(value.substring(value.indexOf('.',1), value.length)*12)));
  } 
}

function showResults()
{
  document.getElementById('sidea_feet').value = feetA;
  document.getElementById('sidea_inch').value = inchA;
  document.getElementById('sideb_feet').value = feetB;
  document.getElementById('sideb_inch').value = inchB;  
  document.getElementById('sidec_feet').value = feetC;
  document.getElementById('sidec_inch').value = inchC;  
  document.getElementById('alpha').value = angleA;
  document.getElementById('beta').value = angleB; 
}