// Math.js
//    This function returns the integer of a number.
//
function intr(num) {
  with (Math) {
    var n = floor(abs(num)) ; if (num < 0) n = n * -1
  }
  return n
}
//
//    This function returns a numeric value.
//
function NumFloat(num) {
  with (Math) {
    var temp = parseFloat(num)
    if (!(temp > 0 || temp < 0 || temp == 0)) temp = 0
  }
  return temp
}
//
//    This function rounds a number by specified decimals.
//
function rnd(num,num2) {
  with (Math) {
    num = round(num*pow(10,num2)) / pow(10,num2)
  }
  return num
}
//
//    This function return the sign of a number.
//
function sgn(num) {
  if (num < 0) return -1
  if (num > 0) return 1
  return 0
}
//
//  This function returns cubit root
//
function cbrt(x) {
  with (Math) {
    if (x > 0) return exp(log(x)/3)
    if (x < 0) return -cbrt(-x)
    return 0.0
  }
}
//
//    Decimal to radian
//
function dtor(num) {
  with (Math) {
    return num / 57.29577951
  }
}
//
//    Radian to decimal
//
function rtod(num) {
  with (Math) {
    return num * 57.29577951
  }
}
//
//    Decimal sine
//
function dsin(num) {
  with (Math) {
    return sin(dtor(num))
  }
}
//
//    Decimal cosine
//
function dcos(num) {
  with (Math) {
    return cos(dtor(num))
  }
}
//
//    Decimal tangent
//
function dtan(num) {
  with (Math) {
    return tan(dtor(num))
  }
}
//
//    Decimal arcsine
//
function dasn(num) {
  with (Math) {
    var y
    if (num == 1) {
      y = 1.570796327
      }
    else {
      y = atan(num/ sqrt(-1 * num * num + 1))
    }
  }
  return rtod(y)
}
//
//    Decimal arccosine
//
function dacs(num) {
  with (Math) {
    var y
    if (num == 1) {
      y = 0
      }
    else {
      y = 1.570796327 - atan(num / sqrt(-1 * num * num + 1))
    }
  }
  return rtod(y)
}
//
//    Decimal arctangent
//
function datan(num) {
  with (Math) {
    return rtod(atan(num))
  }
}
//
//    Decimal arctangent2
//
function datan2(y, x) {
  with (Math) {
    return rtod(atan2(y,x))
  }
}
//
//    DD.MMSS to degrees: HH.MMSS to HH.HHHHH
//
function deg(a) {
  with (Math) {
    var a1, a2, a3, mm, sign, ss
    sign = 1
    if (a < 0) {
      a = -1 * a
      sign = -1
    }
    a1 = intr(a)
    mm = (a - a1) * 100 ; mm = rnd(mm,6)
    a2 = intr(mm)
    ss = (mm - a2) * 100 ; ss = rnd(ss,6)
    a3 = ss
  }
  return sign * (a1 + a2 / 60 + a3 / 3600)
}
//
function dms(a) {
  with (Math) {
    var a1, a2, a3, mm, sign, ss
    sign = 1
    if (a < 0) {
      a = -1 * a
      sign = -1
    }
    a1 = intr(a)
    mm = (a - a1) * 60 ; mm = rnd(mm,6)
    a2 = intr(mm)
    ss = (mm - a2) * 60 ; ss = rnd(ss,6)
    a3 = intr(ss)
  }
  return sign * (a1 + a2 / 100 + a3 / 10000)
}
//
//    returns string version of dms(), degree format
//
function DMS(x, sf) {
  var temp = dms(Math.abs(x))
  var hr = intr(temp) ; temp = (temp - hr) * 100 ; temp = rnd(temp,6)
  var mn = intr(temp) ; temp = (temp - mn) * 100 ; temp = rnd(temp,6)
  if (mn < 10) mn = "0" + mn
  var sc = intr(temp) ; if (sc < 10) sc = "0" + sc
  var tmp = (sgn(x)==-1) ? "-" : ""
  var tmp2 = (sf == true) ? "&#034;" : '"'
  return tmp + hr + "°" + mn + "'" + sc + tmp2
}
//
//    returns string version of dms(), am/pm time format
//
function DMSt(x) {
  var temp = dms(Math.abs(x))
  var hr = intr(temp) ; temp = (temp - hr) * 100 ; temp = rnd(temp,6)
  var mn = intr(temp) ; temp = (temp - mn) * 100 ; temp = rnd(temp,6)
  var sc = intr(temp)
  var am = "am" ; if (hr >= 12) {hr = hr - 12 ; am="pm"}
  if (hr == 0) hr = 12 // DA fix 03/24/01
  if (mn < 10) mn = "0" + mn
  if (sc < 10) sc = "0" + sc
  return hr + ":" + mn + ":" + sc + " " + am
}
//
//    returns string version of dms(), time format
//
function DMST(x) {
  var temp = dms(Math.abs(x))
  var hr = intr(temp) ; temp = (temp - hr) * 100 ; temp = rnd(temp,6)
  var mn = intr(temp) ; temp = (temp - mn) * 100 ; temp = rnd(temp,6)
  if (mn < 10) mn = "0" + mn
  var sc = intr(temp) ; if (sc < 10) sc = "0" + sc
  var tmp = (sgn(x)==-1) ? "-" : ""
  return tmp + hr + ":" + mn + ":" + sc
}
//
//    returns string version of dms(), Right Ascension format
//
function DMSR(x) {
  var temp = dms(Math.abs(x))
  var hr = intr(temp) ; temp = (temp - hr) * 100 ; temp = rnd(temp,6)
  var mn = intr(temp) ; temp = (temp - mn) * 100 ; temp = rnd(temp,6)
  if (mn < 10) mn = "0" + mn
  var sc = intr(temp) ; if (sc < 10) sc = "0" + sc
  var tmp = (sgn(x)==-1) ? "-" : ""
  return tmp + hr + "<sup>h</sup>" + mn + "<sup>m</sup>" + sc +"<sup>s</sup>";
}
//
//  This function returns logarithm, base 10.
//
function log10(x) {
  return Math.LOG10E * Math.log(x)
}
//
//  This function normalizes the number to 360 degrees
//
function rev(x) {
  return  x - Math.floor(x/360.0)*360.0
}
//
//    Mathematic section exit.
