
var isval = 0;
var oplast = 0;
var val1 = 0;
var val2 = "";
var val3 = "";
var op1 = "";
var op2 = "";
var op3 = "";
var ChangeVar = "";

function digit(d) {
  if (isval) {
    if (document.calculator.display.value == "0")
      document.calculator.display.value = d;
    else
      document.calculator.display.value += d;
  }
  else {
    document.calculator.display.value = d;
    isval = 1;
  }
  oplast = 0;
}


function decimal() {
  if (document.calculator.display.value == "0" || !isval)	
    document.calculator.display.value = "0,";
  else if (document.calculator.display.value.indexOf(',') != -1)   
    return;
  else   
    document.calculator.display.value += ',';

  isval = 1;
  oplast = 0;
}

function binary(op) {
  if (oplast) {
    op1 = op + " ";
    return;
  }
  
  //changeFormat2(1,document.calculator.display.value)			
  var temp = "" + ChangeVar; 

  if (op == '+' || op == '-') {
    if (op1) {
      if (op1 == "p")
        temp = Math.pow(val1, temp);
      else
        temp = eval(val1 + op1 + temp);

      if (op2) {
        temp = eval(val2 + op2 + temp);
        val2 = "";
        op2 = "";

        if (op3) {
          temp = eval(val3 + op3 + temp);
          val3 = "";
          op3 = "";
        }
      }
    }
  }

  else { /* x or / */
    if (op1) {
      if (op1 == "+ " || op1 == "- ") {
        val2 = "" + val1;
        op2 = "" + op1;
      }
      else {
        if (op1 == "p")
          temp = Math.pow(val1, temp);
        else
          temp = eval(val1 + op1 + temp);
      }

      if (op2) {
        if (op2 == "+ " || op2 == "- ") {
          val1 = "" + val2;
          op1 = "" + op2;
        }
        else {
          temp = eval(val2 + op2 + temp);
          val2 = "";
          op2 = "";
        }

        if (op3) { /* must be + or - */
          val2 = "" + val3;
          op2 = "" + op3;
          val3 = "";
          op3 = "";
        }
      }
    }
  }
  
	//changeFormat2(2,temp)
  val1 = "" + temp;
  op1 = op + " ";
  isval = 0;
  oplast = 1;
}

function pow() {
  if (oplast) {
    op1 = 'p';
    return;
  }
  
  //changeFormat2(1, document.calculator.display.value);
  var temp = "" + ChangeVar;

  if (op1) {
    if (op1 == 'p') {
      temp = Math.pow(val1, temp);
    }
    else {
      if (op2) {
        val3 = "" + val2;
        op3 = "" + op2;
      }
      val2 = "" + val1;
      op2 = "" + op1;
    }
  }
  //changeFormat2(2, temp);
  document.calculator.display.value = "" + ChangeVar;
  val1 = "" + temp;
  op1 = 'p';
  isval = 0;
  oplast = 1;
}

function equals() {
	//var s = new String(document.calculator.display.value);
	//			s = s.replace(",",".");
	//changeFormat2(1,document.calculator.display.value)			
  var temp = "" + ChangeVar; //s;

  if (op1) {
    if (op1 == 'p')
      temp = Math.pow(val1, temp);
    else
      temp = eval(val1 + op1 + temp);
    op1 = "";
    val1 = 0;

    if (op2) {
      temp = eval(val2 + op2 + temp);
      op2 = "";
      val2 = "";

      if (op3) {
        temp = eval(val3 + op3 + temp);
        op3 = "";
        val3 = "";
      }
    }
  }
	//var ss = new String(temp);
	//ss = ss.replace(".",",");
	//changeFormat2(2,temp)			
  document.calculator.display.value = "" + ChangeVar;
  isval = 0;
  oplast = 0;
}

function negate() {
  document.calculator.display.value = eval("- " + document.calculator.display.value);
}

function unary(func) {
//changeFormat2(1,document.calculator.display.value);
ChangeVar = func(ChangeVar);
//changeFormat2(2,ChangeVar);

  isval = 0;
  oplast = 0;
}

function square(x) {
  return x * x;
}
function log10(x) {
  return Math.log(x) / Math.LN10;
}

function pow10(x) {
  return Math.pow(10, x);
}

function inv(x) {
  return 1 / x;
}

function square(x) {
  return x * x;
}

function setconst(x) {
  document.calculator.display.value = x;
  isval = 1;
  oplast = 0;
}

function clear() {
  if (oplast) clear_all();
  document.calculator.display.value = 0;
  isval = 0;
}

function clear_all() {
  document.calculator.display.value = 0;
  
  isval = 0;
  oplast = 0;
  val1 = 0;
  val2 = "";
  val3 = "";
  op1 = "";
  op2 = "";
  op3 = "";
}

function parse_input() {
  document.calculator.display.blur();
  window.status = document.calculator.display.value;

  document.calculator.display.focus();
  return false;
}

//slutt her
//BMI kalkulator
function beregn(form)
			{
			var H = new String (form.TxtH.value);
			//H = H.replace(",",".");				
			var V = new String (form.TxtV.value);
			//V = V.replace(",",".");		
			
				Hd = (H)/100;  
				B = (V)/(Hd*Hd);	
				BB = "" + B;
				len = BB.length;
				//form.LblSvar.value = BB.substring(4,0) 
				
			//var s = new String(form.LblSvar.value);
			//s = s.replace(".",",");
			//form.LblSvar.value = s
			//changeFormat2(2,BB.substring(4,0));
			form.LblSvar.value = BB.substring(4,0);//ChangeVar;
			}

// Vis/Skjul kalkulator			
function hideAll(layer_ref1,layer_ref2,layer_ref3,layer_ref4) {
			
			if (document.all) { //IS IE 4 or 5 (or 6 beta)
			
			
			eval( "document.all." + layer_ref1 + ".style.visibility = 'hidden'");
			eval( "document.all." + layer_ref2 + ".style.visibility = 'hidden'");
			eval( "document.all." + layer_ref3 + ".style.visibility = 'hidden'");
			eval( "document.all." + layer_ref4 + ".style.visibility = 'hidden'");
			}
			}

function hideMe(layer_ref) {
			
				if (document.all) { //IS IE 4 or 5 (or 6 beta)
				eval( "document.all." + layer_ref + ".style.visibility = 'hidden'");
				}
				if (document.layers) { //IS NETSCAPE 4 or below
				document.layers[layer_ref].visibility = 'hidden';
				}	
			}
			
function showMe(layer_ref) {
			
				hideAll('Val','prosent','kalkulatoren','BMI')
				eval( "document.BMIForm.TxtH.value = ' '");
				eval( "document.BMIForm.TxtV.value = ' '");
				eval( "document.BMIForm.LblSvar.value = ' '");
				document.calculator.display.value = "";
				
				
				if (document.all) { //IS IE 4 or 5 (or 6 beta)
				eval( "document.all." + layer_ref + ".style.visibility = 'visible'");
				
				}
				if (document.layers) { //IS NETSCAPE 4 or below
				document.layers[layer_ref].visibility = 'visible';
				}		
			}
// Gamlekalkulator
var indec = 0
var memor = 0

function DisplaySym(number) {
			//if ( number == '×' )
			//	number == '*';
			document.calculator.display.value += number;
			//changeFormat2(2,document.calculator.display.value )
			//changeFormat();
			
			}

function BackSpace() {
			document.calculator.display.value = document.calculator.display.value.substring(0, document.calculator.display.value.length - 1);
			changeFormat();
			}

function Do_Math() {  
			var s = new String(document.calculator.display.value);
				for ( x = 0; x < s.length; x++ ) {
				s = s.replace(",",".");
				}
				document.calculator.display.value = s
			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;
				changeFormat();
			}
			}

function DisplayClear() {
			document.calculator.display.value = ""
			}
			                     
function NumberCheck(number2)  {
			y = 0;	
			number2= number2.replace('\u00F7',"/");				//engelsk tegn
			number2= number2.replace('\u00D7',"*");				//engelsk tegn
			
			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 != "("  && numcheck != ")"  ) {
					x = number2.length;
					y = 1;
				} 
				if ( numcheck == "^" ) {
					y = 2;
				}
				}
				}
				if ( y == 0 ) {	
					document.calculator.display.value=number2;
					Do_Math()
					
			}
				if ( y == 1 ) {
					alert("Ugyldig verdi.");
			}
				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); 
				changeFormat();
			}

function Higher_Math2()  {
				document.calculator.display.value =  Math.pow(document.calculator.display.value,2)  
				changeFormat();
				}

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)
			changeFormat();
			}

function Percent() {
			document.calculator.display.value = parseFloat(document.calculator.display.value) / 100;			
			changeFormat();
			}
function changeFormat()
			{			
				var s = new String(document.calculator.display.value);
			//	s = s.replace(".",",");
				document.calculator.display.value = s
			}
//Kalkulator: fra . til ,
function changeFormat2(t,display)

			{				
			if ( t == 1 ){		
				var s = new String(display);
				s = s.replace(",",".");
				}
			if ( t == 2 ) {
				var s = new String(display);
			//	s = s.replace(".",",");
				}				
				ChangeVar = s;	
				document.calculator.display.value 	= ChangeVar;		
			}			
//Valuta kalkulator
function restoreDefault() {
			for (var i = 0; i < document.valuta.fraValuta.length; i++) {
				if (document.valuta.fraValuta.options[i].defaultSelected == true) {
					document.valuta.fraValuta.options[i].selected=true;
				}
			}
			for (var i = 0; i < document.valuta.tilValuta.length; i++) {
				if (document.valuta.tilValuta.options[i].defaultSelected == true) {
					document.valuta.tilValuta.options[i].selected=true;
				}
			}
			document.valuta.utPenger.value = ' ';
			document.valuta.innPenger.value = 100;
			}

function regnUt(form) {
			
				EUR = 7.6672;
				USD = 6.1019;  
				BATH = 13.9400;
				GBP = 11.27600;
				
			
				valgtUt = form.tilValuta.selectedIndex;

				if (valgtUt == 0) veksleKurs = 1;

				if (valgtUt == 1) veksleKurs = EUR;
				if (valgtUt == 2) veksleKurs = USD;
				if (valgtUt == 3) veksleKurs = ( BATH / 100 )  ;
				if (valgtUt == 4) veksleKurs = GBP;
				
				var ss = new String(form.innPenger.value);
				ss = ss.replace(",",".");
				valgtInn = form.fraValuta.selectedIndex;
				harPenger = ss;//form.innPenger.value;

				if (valgtInn == 0) harPenger *= 1;
				if (valgtInn == 1) harPenger *= EUR;
				if (valgtInn == 2) harPenger *= USD;
				if (valgtInn == 3) harPenger *= ( BATH / 100 ) ;
				if (valgtInn == 4) harPenger *= GBP;
			    

			form.utPenger.value = (Math.floor (10000*harPenger / veksleKurs) /10000);
			//var s = new String(form.utPenger.value);
			//	s = s.replace(".",",");
			//	form.utPenger.value = s
			}
//Tilbake hoveside
function Show()
			{				
				x = event.clientX + document.body.scrollLeft;				
				y = event.clientY + document.body.scrollTop + 35;				
				Popup.style.display="block";				
				Popup.style.left = x;				
				Popup.style.top = y;
			}
function Hide()	{Popup.style.display="none";}

//Mal
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

