function log10(x) {
return(Math.log(x)/Math.log(10));

}

function round(number,X) {
// rounds number to X decimal places, defaults to 3
    X = (!X ? 3 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}


function evalchan(form)
{

var c21=470;
var frq;
var chan=form.channel.value;
if (!isNaN(parseInt(chan))) {
	if ((chan < 21) || (chan > 68)) {
        alert("channel must be between 21 and 68");
        form.channel.focus();
        }
        else {
	frq=(chan-21)*8+c21;
        form.freq.value=frq;
        populate(form);
        }
}
else{
alert("invalid entry");
form.channel.value="";
form.channel.focus();
}

}

function evalfreq(form)
{

var c21=470;
var frq=form.freq.value;
var chan=0;
if (!isNaN(parseInt(frq))) {
	if ((frq < 470) || (frq > 860)) {
        alert("channel must be between 470 and 860");
        form.freq.focus();
        }
        else {
        chan=(frq-c21)/8+21;
        form.channel.value=parseInt(chan);
        populate(form)
        }
}
else{
alert("invalid entry");
form.channel.value="";
form.channel.focus();
}

}




function lambda()
{
        alert("output field only \n wavelength is changed by frequency/channel");
        //form.channel.focus();
        return true;
}



function populate(frm)
{       // aim of this is to see if we have enough values and to populate the form if so
var freq=0;
var lambda=0;
var erp=0;
var dist=0;
var ploss=0;
var rxgain=0;
var mhgain=0;
var mhmax=85;           // max level out of masthead
var rxv=0;
var feedloss=0.18;      // CT100 18dB/100m


var p_l_offset=32.45;
var c=2.998E8;          // speed of light
var ready=true;         // ready to go until false
//alert ("parsing");
if (parseFloat(frm.erp.value) > 0 ) {
	erp=parseFloat(frm.erp.value)*1000;
        }
        else
        {
	ready=false;
        }
        //alert("dist");
if (parseFloat(frm.dist.value) > 0 ) {
	dist=parseFloat(frm.dist.value);
        }
        else
        {
	ready=false;
        }
        //alert("freq");
if (parseFloat(frm.rxgain.value) >= 0 ) {
	rxgain=parseFloat(frm.rxgain.value);
        //alert("Rxgain "+ Math.pow(10,rxgain/20));
        }
if (parseFloat(frm.mhgain.value) >= 0 ) {
	mhgain=parseFloat(frm.mhgain.value);
        }


if (parseFloat(frm.freq.value) > 0 ) {
        freq=parseFloat(frm.freq.value);
        lambda=c/(freq*1000000);
        // alert("lambda");
        frm.lambda.value=round(lambda,1);
        }
        else
        {
	ready=false;
        }
        //alert("plossc"+freq+" "+dist+"gh");
if (parseFloat(frm.feedlen.value) >= 0 ) {
	cabloss=parseFloat(frm.feedlen.value)*feedloss;
        }
        else
        {
        cabloss=0;
        frm.feedlen.value=0;
        }

if (freq*dist > 0) {            // we have enough to calculate path loss
//alert("ploss");
ploss=p_l_offset+20*log10(freq)+20*log10(dist);
frm.ploss.value=round(ploss,1);
}
if (dist*erp > 0) {         // can work out field strength
//alert("Field strength");
fieldstr=Math.sqrt(30*erp)/(dist*1000);
frm.fieldstr.value=round(fieldstr,6);
}

if (lambda*dist*erp > 0) {         // can work out antenna voltage
//alert("RX volts");
// alert this was the old RSGB method rxv=0.7071*lambda*fieldstr*Math.pow(10,rxgain/20)*Math.sqrt(75/73.2)/(2*3.1415926);
rxv=0.5*fieldstr*lambda/3.14145926*Math.pow(10,rxgain/20);
frm.antdbuv.value=round(20*log10(rxv*1000000),1);
}

if (rxv > 0) {
	if ((mhgain > 0) && (mhgain+20*log10(rxv*1000000) > mhmax)) {
                alert("You are pushing your masthead with \n an output level of " + round(mhgain+(20*log10(rxv*1000000)),1) + "dBuV\nCheck specification for max level");

        	}
        frm.rxdbuv.value=round(mhgain-cabloss+(20*log10(rxv*1000000)),1)

        }

}




function evalform(form) {
populate(form);
return false;
//form.frq.focus();
}