document.onkeypress = keyPress;
aspect=16/9;
wratio= .8715 //some say .87
hratio= .4902  //some say .495
height=48;
width=85.33;
diag = round(Math.sqrt( Math.pow(width,2) + Math.pow(height,2) ),2);
document.screencalc.d.value=diag;
document.screencalc.w.value=width;
document.screencalc.h.value=height;
document.screencalc.h.focus();
curfocus = document.screencalc.h.name;

function focuschanged(item){
	curfocus=item.name;
}

function changeaspect(ratio){
 if (ratio=="16:9"){
  aspect=16/9;
  wratio= .8715 //some say .87
  hratio= .4902  //some say .495
 }
 if (ratio=="4:3"){
  aspect=4/3;
  wratio= .8 
  hratio= .6 
 }

 if (ratio=="2.35:1"){
  aspect=2.35;
  wratio= .9202 
  hratio= .3916 
 }
 calc();

}

function calc(){

  if (curfocus == "w"){
	wchanged();
  }

  if (curfocus == "h"){
	hchanged();
  }

  if (curfocus == "d"){
	dchanged();
  }

}

function hchanged(){
if( isNaN(document.screencalc.h.value)){
	alert("Height is not a number");
	return;
}

height = round(document.screencalc.h.value,2);
document.screencalc.h.value = height;

width = height * aspect;
diag =   Math.sqrt( Math.pow(width,2) + Math.pow(height,2));

document.screencalc.w.value = round(width,2);
document.screencalc.d.value = round(diag,2);

}


function wchanged(){
if( isNaN(document.screencalc.w.value)){
	alert("Width is not a number");
	return;
}

width = round(document.screencalc.w.value,2);
document.screencalc.w.value = width;
height = width/aspect;
diag = Math.sqrt( Math.pow(width,2) + Math.pow(height,2));
document.screencalc.h.value = round(height,2);
document.screencalc.d.value = round(diag,2);
}


function dchanged(){
if( isNaN(document.screencalc.d.value)){
	alert("Diagonal is not a number");
	return;
}
diag = round(document.screencalc.d.value,2); 
document.screencalc.d.value=diag;
width =  diag * wratio;
height = diag * hratio;
document.screencalc.h.value = round(height,2);
document.screencalc.w.value = round(width,2);
}

function hadd(){
document.screencalc.h.value = parseFloat(document.screencalc.h.value) + 1;
hchanged();
document.screencalc.h.focus();

}
function hsub(){
document.screencalc.h.value = parseFloat(document.screencalc.h.value) - 1;
hchanged();
document.screencalc.h.focus();

}


function wadd(){
document.screencalc.w.value = parseFloat(document.screencalc.w.value) + 1;
wchanged();
document.screencalc.w.focus();
}

function wsub(){
document.screencalc.w.value = parseFloat(document.screencalc.w.value) - 1;
wchanged();
document.screencalc.w.focus();
}

function dadd(){
document.screencalc.d.value = parseFloat(document.screencalc.d.value) + 1;
dchanged();
document.screencalc.d.focus();
}

function dsub(){
document.screencalc.d.value = parseFloat(document.screencalc.d.value) - 1;
dchanged();
document.screencalc.d.focus();
}

function round(rnum, rlength) {
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	return newnumber;
}

function keyPress(evt){
  var e = evt? evt : window.event; 
  if(!e) return; 
  var key = 0; 
  if (e.keyCode) {
    key = e.keyCode; 
  } // for moz/fb, if keycode==0 use 'which' 
  else if (typeof(e.which)!= 'undefined') { 
    key = e.which; 
  } 

 if(key==13){
	calc();
 }
}
