/***************************************************************************
*   Copyright (C) 2006, Paul Lutus                                        *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

addEvent(window,"load",computePoly);
// function to add an event listener
function addEvent(o,e,f) {
  if (o.addEventListener) {
    o.addEventListener(e,f,false);
    return true;
  }
  else if (o.attachEvent) {
    return o.attachEvent("on"+e,f);
  }
  else {
    return false;
  }
}
function computePoly() {
  var error = "Invalid Entry";
  var area = error;
  var side = error;
  var inradius = error;
  var vertex_angle = error;
  var sides = document.data.sides.value;
  if (isNaN(sides)) {
    sides = 0;
  }
  else {
    var si = Math.floor(sides);
    if(si != sides) {
      document.data.sides.value = si;
    }
    sides = si;
  }
  var radius = document.data.radius.value;
  if(sides > 0 && radius > 0) {
    vertex_angle = 360 / sides;
    side = 2 * radius * Math.sin(Math.PI / sides);
    inradius = radius * Math.cos(Math.PI / sides);
    area = side * side * sides / (4.0 * Math.tan(Math.PI / sides));
  }
  document.getElementById("area").innerHTML = area;
  document.getElementById("side").innerHTML = side;
  document.getElementById("inradius").innerHTML = inradius;
  document.getElementById("vertex_angle").innerHTML = vertex_angle;
}
