/* Convert from dBm to milliwatts
Function by Rex Frobenius, October 2006
Copyright 2006, Besser Associates, Inc. All Rights Reserved.
*/


function convertFromdBm(theValue){
	var value = parseInt(theValue);
	var multiplier = 1;
	var largePrefix = new Array();
	var smallPrefix = new Array();
	var conversion = new Array();
	var result = new Array();
	var label;
	var HTMLtext;
	
	largePrefix[0] = "milliwatts";
	largePrefix[1] = "watts";
	largePrefix[2] = "kilowatts";
	largePrefix[3] = "megawatts";
	largePrefix[4] = "gigawatts";
	
	smallPrefix[0] = "milliwatts";
	smallPrefix[1] = "microwatts";
	smallPrefix[2] = "nanowatts";
	smallPrefix[3] = "picowatts";
	smallPrefix[4] = "femtowatts";
	
	conversion[0] = "<tr><td>0</td><td colspan='2' align='center'>Memorize this one</td><td>1</td></tr>";
	conversion[1] = "<tr><td>1</td><td>10-3-3-3</td><td>10/2/2/2</td><td>1.25</td></tr>";
	conversion[2] = "<tr><td>2</td><td>5-3</td><td>3/2</td><td>1.5</td></tr>";
	conversion[3] = "<tr><td>3</td><td colspan='2'>Memorize this one</td><td>2</td></tr>";
	conversion[4] = "<tr><td>4</td><td>10-3-3</td><td>10/2/2</td><td>2.5</td></tr>";
	conversion[5] = "<tr><td>5</td><td colspan='2'>Memorize this one</td><td>3</td></tr>";
	conversion[6] = "<tr><td>6</td><td>3+3</td><td>2*2</td><td>4</td></tr>";
	conversion[7] = "<tr><td>7</td><td>10-3</td><td>10/2</td><td>5</td></tr>";
	conversion[8] = "<tr><td>8</td><td>5+3</td><td>3*2</td><td>6</td></tr>";
	conversion[9] = "<tr><td>9</td><td>3+3+3</td><td>2*2*2</td><td>8</td></tr>";
	conversion[10] = "<tr><td>10</td><td colspan='2'>Memorize this one</td><td>10</td></tr>";
	
	result[0] = 1;
	result[1] = 1.25;
	result[2] = 1.5;
	result[3] = 2;
	result[4] = 2.5;
	result[5] = 3;
	result[6] = 4;
	result[7] = 5;
	result[8] = 6;
	result[9] = 8;
	result[10] = 10;
	
	//Step 1: determine watts, kilowatts, etc.
	
	if (value > 150 || value < -120){
		HTMLtext = "Please enter a value greater than -120dBm and less than 150dBm";
		
		document.getElementById("answer").innerHTML = HTMLtext;
		return;
	}
	
	HTMLtext = "<h2>Your Example</h2><p>We wish to convert ";
	HTMLtext = HTMLtext + value;
	HTMLtext = HTMLtext + " dB worth of milliwatts into an expression of kilowatts, watts, picowatts, etc. </p><h2>Step 1</h2>";
	var i=0;
	
	if (value >0){
		HTMLtext = HTMLtext + "<p>We see how many times we can subtract 30 from the number without going negative. Each time we move up in power level</p><blockquote>";
		HTMLtext = HTMLtext + value + "dB worth of milliwatts <br/>";
		while (value >=30){
			i += 1;
			value -=30;
			HTMLtext = HTMLtext + "-30dB = " + value + "dB worth of " + largePrefix[i] + "<br/>";
				
		}
		label = largePrefix[i];
		
	}
	else {
		HTMLtext = HTMLtext + "<p>We see how many times we need to add 30 to the number in order to make it positive. Each time we move down in power level </p><blockquote>";
		HTMLtext = HTMLtext + value + "dB worth of milliwatts <br/>";
		
		while (value <0){
			i += 1;
			value +=30;
			HTMLtext = HTMLtext + "+30dB = " + value + "dB worth of " + smallPrefix[i] + "<br/>";
				
		}
		label = smallPrefix[i];
	}
	
	HTMLtext = HTMLtext + " <br/>At the end of this step we have " + value + "dB worth of <b>" + label + "</b></blockquote><h2>Step 2</h2><p>Check if we can subtract 20 or 10 without going negative.</p>";
	
	if (value > 19) {
		multiplier = 100;
		
		HTMLtext = HTMLtext + "<p>Since we can subtract 20 from the number without going negative, the result will be multiples of 100</p>";
		HTMLtext = HTMLtext + "<p>We have " + value + " - 20 = ";
		value -= 20;
		HTMLtext = HTMLtext + value + "dB worth of 100's of " + label + "</p>";
	}
	
	else if (value > 9) {
		multiplier = 10;
		HTMLtext = HTMLtext + "<p>Since we can subtract 10 from the number without going negative, the result will be multiples of 10</p>";
		HTMLtext = HTMLtext + "<p>We have " + value + " - 10 = ";
		value -= 10;
		HTMLtext = HTMLtext + value + "dB worth of 10's of " + label + "</p>";
	}
	
	else {
		HTMLtext = HTMLtext + "<p>Since the leftover value is less than 10 the result will be simply multiplied by 1:</p><p>We have ";
		
		HTMLtext = HTMLtext + value + "dB worth of " + label + "</p>";
	}
	
	HTMLtext = HTMLtext + "<h2>Step 3</h2><p>Figure out how many hundreds, tens, or ones we have using our memorized values</p><table>";
	HTMLtext = HTMLtext + "<tr><td>DB Value</td><td>Sum/Difference of 10,5,3</td><td>Equivalent multiplication and division</td><td>Result</td></tr>";
	HTMLtext = HTMLtext + conversion[value];
	HTMLtext = HTMLtext + "</table>";
	HTMLtext = HTMLtext + "<p><b>The answer is " + result[value] + " x " + multiplier + " = " ;
	HTMLtext = HTMLtext + result[value]*multiplier;
	HTMLtext = HTMLtext + " " + label + "</b></p>";
	
	document.getElementById("answer").innerHTML = HTMLtext;
}
