var bitA = 1;
var bitB = 2;
var bitC = 4;
var bitHypot = 8;
var bitWidth = 16;
var bitHeight = 32;

function degFromRad(rad)
{
	return (rad * 180 / Math.PI);
}

function radFromDeg(deg)
{
	return (deg * Math.PI / 180);
}
/*
function clearAllFields()
{
	document.forms[0].txtA.value = "";
	document.forms[0].txtB.value = "";
	document.forms[0].txtHypot.value = "";
	document.forms[0].txtWidth.value = "";
	document.forms[0].txtHeight.value = "";
}
*/
function clearFieldsWithZero()
{
	if( document.forms[0].txtA.value == 0 )
		document.forms[0].txtA.value = "";
	if( document.forms[0].txtB.value == 0 )
		document.forms[0].txtB.value = "";
	if( document.forms[0].txtHypot.value == 0 )
		document.forms[0].txtHypot.value = "";
	if( document.forms[0].txtWidth.value == 0 )
		document.forms[0].txtWidth.value = "";
	if( document.forms[0].txtHeight.value == 0 )
		document.forms[0].txtHeight.value = "";
}

function getUnknown()
{
	var unknown = 0;

	clearFieldsWithZero();

	if( document.forms[0].txtA.value == "" )
		unknown |= bitA;
	if( document.forms[0].txtB.value == "" )
		unknown |= bitB;
	if( document.forms[0].txtHypot.value == "" )
		unknown |= bitHypot;
	if( document.forms[0].txtWidth.value == "" )
		unknown |= bitWidth;
	if( document.forms[0].txtHeight.value == "" )
		unknown |= bitHeight;
	return unknown;
}

function calcUnknown()
{
	var unknown = getUnknown();
	var known = 63 - unknown;
	var stillUnknown = 0;

	var A = radFromDeg(+document.forms[0].txtA.value);
	var B = radFromDeg(+document.forms[0].txtB.value);
	var C = radFromDeg(90.0);
	var hypot = +document.forms[0].txtHypot.value;
	var width = +document.forms[0].txtWidth.value;
	var height = +document.forms[0].txtHeight.value;

do
{
	unknown = getUnknown();
	known = 63 - unknown;
	stillUnknown = 0;

	if( unknown & bitHypot )
	{
		if( (known & bitHeight) && (known & bitWidth) )
			hypot = Math.sqrt((height * height) + (width * width));
		else if( (known & bitHeight) && (known & bitB) )
			hypot = height / Math.sin(B);
		else if( (known & bitWidth) && (known & bitB) )
			hypot = width / Math.cos(B);
		else if( (known & bitHeight) && (known & bitA) )
			hypot = height / Math.cos(A);
		else if( (known & bitWidth) && (known & bitA) )
			hypot = width / Math.sin(A);
		else
			stillUnknown |= bitHypot;

		if( (stillUnknown & bitHypot) == 0 )
			document.forms[0].txtHypot.value = Math.round(hypot * 100) / 100;
	}
	if( unknown & bitHeight )
	{
		if( (known & bitHypot) && (known & bitB) )
			height = hypot * Math.sin(B);
		else if( (known & bitWidth) && (known & bitB) )
			height = width * Math.tan(B);
		else if( (known & bitHypot) && (known & bitA) )
			height = hypot * Math.cos(A);
		else if( (known & bitWidth) && (known & bitA) )
			height = width / Math.tan(A);
		else
			stillUnknown |= bitHeight;

		if( (stillUnknown & bitHeight) == 0 )
			document.forms[0].txtHeight.value = Math.round(height * 100) / 100;
	}
	if( unknown & bitWidth )
	{
		if( (known & bitHypot) && (known & bitB) )
			width = hypot * Math.cos(B);
		else if( (known & bitHeight) && (known & bitB) )
			width = height * Math.tan(B);
		else if( (known & bitHypot) && (known & bitA) )
			width = hypot * Math.sin(A);
		else if( (known & bitHeight) && (known & bitA) )
			width = height * Math.tan(A);
		else if( (known & bitHypot) && (known & bitHeight) )
			width = Math.sqrt(Math.pow(hypot, 2) - Math.pow(height, 2));
		else
			stillUnknown |= bitWidth;

		if( (stillUnknown & bitWidth) == 0 )
			document.forms[0].txtWidth.value = Math.round(width * 100) / 100;
	}
	if( unknown & bitA )
	{
		if( known & bitB )
			A = C - B;
		else if( (known & bitHypot) && (known & bitHeight) )
			A = Math.acos(height / hypot);
		else
			stillUnknown |= bitA;

		if( (stillUnknown & bitA) == 0 )
			document.forms[0].txtA.value = Math.round(degFromRad(A) * 100) / 100;
	}
	if( unknown & bitB )
	{
		if( known & bitA )
			B = C - A;
		else if( (known & bitHeight) && (known & bitHypot) )
			B = Math.asin(height / hypot);
		else
			stillUnknown |= bitB;

		if( (stillUnknown & bitB) == 0 )
			document.forms[0].txtB.value = Math.round(degFromRad(B) * 100) / 100;
	}
} while( unknown != stillUnknown );

	return true;
}

$(document).ready(function() {
	$('#appClose').click(function() {
		window.location = '../';
	});
	$('#clear').click(function() {
		$("input[type='text']").val('');
	});
	$('#clear').click();
});
