var indec = 0
var memor = 0

function DisplaySym(number) {
  document.calculator.display.value += number;
}

function BackSpace() {
  document.calculator.display.value = document.calculator.display.value.substring(0, document.calculator.display.value.length - 1);
}

function Do_Math() {  
  if ( document.calculator.display.value != "" && document.calculator.display.value != parseInt(document.calculator.display.value) ) {
    var hold  = eval(document.calculator.display.value);
    document.calculator.display.value = hold;
}
}

function DisplayClear() {
  document.calculator.display.value = ""
}
                     
function NumberCheck(number2)  {
  y = 0;
  for ( x = 0; x < number2.length; x++ ) {
  var numcheck = number2.substring(x, x+1)
      if ( numcheck < "0" || numcheck > "9" ) {
      if ( numcheck != "/" && numcheck != "*" && numcheck != "-" && numcheck != "+"  && numcheck != "." && numcheck != "("  && numcheck != ")"  ) {
          x = number2.length;
          y = 1;
     } 
     if ( numcheck == "^" ) {
          y = 2;
     }
     }
     }
     if ( y == 0 ) {
          Do_Math()
}
     if ( y == 1 ) {
          confirm("Se over innskrevne data, det finnes et ugyldig tegn i utregningen!");
}
     if ( y == 2 ) {
          Higher_Math3(number2)
}
}

function Higher_Math(process)  {
if ( document.calculator.Deg[0].checked ) {
    if ( process == "sin" || process == "cos" || process == "tan" ) {
    temp = eval("(" + document.calculator.display.value + "/" + "180) * Math.PI");
    } 
    } else {
    temp = parseFloat(document.calculator.display.value)
    }
    var m = "Math." + process + "(" + temp + ")";
    document.calculator.display.value =  eval(m); 
}

function Higher_Math2()  {
    document.calculator.display.value =  Math.pow(document.calculator.display.value,2)  
}

function Higher_Math3(number3)  {
  for ( x = 0; x < number3.length; x++ ) {
  var numcheck = number3.substring(x, x+1)
      if ( numcheck == "^" ) {
      number4 = number3.substring(0,x);
      number5 = number3.substring(x+1,number3.length);
      x=number3.length;
}
}
document.calculator.display.value = Math.pow(number4,number5)
}

function Memory(indec, memhold)  {
var memhold2 = document.calculator.display.value

  if ( indec == 0 ) {
     memhold = "";
     document.calculator.display.value = memhold;
     memor = memhold;
}
  if ( indec == 1 ) {
     memhold = document.calculator.display.value; 
     document.calculator.display.value = memhold;
     memor = memhold;
}
  if ( indec == 2 ) {
     memhold = parseFloat(memhold) + parseFloat(memhold2);
     document.calculator.display.value = memhold;
     memor = memhold;
}
  if ( indec == 3 ) {
     memhold = parseFloat(memhold) - parseFloat(memhold2);
     document.calculator.display.value = memhold;
     memor = memhold;
}
  if ( indec == 4 ) {
     document.calculator.display.value = memhold;
}
}

function Percent() {
  document.calculator.display.value = parseFloat(document.calculator.display.value) / 100;
}

function DispPi()  {
  document.calculator.display.value += Math.PI
}

function Posneg() {
  temp =   document.calculator.display.value
  document.calculator.display.value = -temp
}
