/**
*
* Main functions
*
**/

function AyreForm() {

	document.write('<table class="head" width="650" cellspacing="0">\n');
	document.write('<tr>\n');
	document.write('<td class="rht"><img src="images/aydb.GIF" width="25" height="26" name="ayD1"></td>\n');
	document.write('<td class="mid ctr" width="100"><span class="Hz" id="ayH">&nbsp;</span></td>\n');
	document.write('<td class="lft"><img src="images/aydb.GIF" width="25" height="26" name="ayD2"></td>\n');
	document.write('</tr>\n');
	document.write('</table><br>\n\n');

	document.write('<form name="ayref">\n');
	document.write('<input type=hidden name="trkno" value="7">\n');
	document.write('<input type=hidden name="clear" value="Y">\n');
	document.write('Select track 1 (a one minute glide tone) or track 7 (a five minute tone) using the buttons above.<br><br>\n');
	document.write('Then enter the time displayed\n');
	document.write('<input type=text name="mins" size=1 maxlength=1 value="1">\n');
	document.write('minutes\n');
	document.write('<input type=text name="secs" size=2 maxlength=2 value="30">\n');
	document.write('seconds\n');
	document.write('<input type=button value="Calculate" onClick="ayreFreq(document.ayref.mins.value, document.ayref.secs.value)">\n');
	document.write('or frequency\n');
	document.write('<input type=text name="freq" size=5 maxlength=5 value="5000">\n');
	document.write('<input type=button value="Calculate" onClick="ayreTime(document.ayref.freq.value)">\n');
	document.write('</form>\n');
}

function ayreFreq(mm,ss) {
	var ff;
	if (mm<0 || mm>5 || mm!=Math.round(mm) || ss<0 || ss>59 || ss!=Math.round(ss)) {
		ShowInvalid();
	} else {
		var ww = Math.round(mm*60) + Math.round(ss);
		var mx=ayLen();
		if (ww>mx) {
			ShowInvalid();
		} else {
			if (ww<5 || ww>(mx-10)) {
				ff=0;
			} else {
				ww = ((ww - 5) * aySweep()) + ayLog(5);
				ff = Math.round(Math.pow(10,ww));
			}
			mm = Math.round(mm);
			ss = Math.round(ss);
			if (ss < 10) ss = '0'+ss;
			var tt=mm+":"+ss;
			ShowResult(tt, ff);
		}
	}
}

function ayreTime(ff) {
	if (ff<5 || ff>20000) {
		ShowInvalid();
	} else {
		var ww = Math.round(((ayLog(ff) - ayLog(5)) / aySweep()) + 5);
		var mm = Math.floor(ww/60);
		var ss = Math.round(ww - (mm*60));
		if (ss < 10) ss = '0'+ss;
		var tt=mm+":"+ss;
		ShowResult(tt, ff);
	}
}

/**
*
* Calculation functions
*
**/

function ayLog(x) {
	return (Math.log(x)/Math.log(10))}

function aySweep() {
	if (document.ayref.trkno.value == '1') {
		var sw=0.08005;
	} else {
		var sw=0.0126388;
	}
	return (sw)}

function ayLen() {
	if (document.ayref.trkno.value == '1') {
		var ln=60;
	} else {
		var ln=300;
	}
	return (ln)}

/**
*
* Display functions
*
**/

function ChangeTrk() {
	ShowClear();
	$('ayH').innerHTML = '';
	if (document.ayref.trkno.value == '1') {
		document.ayT.src = "images/ayt7.JPG";
		document.ayref.trkno.value = '7';
	} else {
		document.ayT.src = "images/ayt1.JPG";
		document.ayref.trkno.value = '1';
	}
}

function ShowClear() {
	if (document.ayref.clear.value == 'N') {
		document.ayM.src  = "images/aymb.JPG";
		document.ayS1.src = "images/aysb.JPG";
		document.ayS2.src = "images/aysb.JPG";
		document.ayD1.src = "images/aydb.GIF";
		document.ayD2.src = "images/aydb.GIF";
		document.ayref.clear.value = 'Y';
	}
}

function ShowInvalid() {
	ShowClear();
	$('ayH').innerHTML = '!! Invalid !!';
}

function ShowResult(tim, frq) {
	var cc;
	cc = tim.substr(0,1);
	document.ayM.src = "images/aym"+cc+".JPG";
	cc = tim.substr(2,1);
	document.ayS1.src = "images/ays"+cc+".JPG";
	cc = tim.substr(3,1);
	document.ayS2.src = "images/ays"+cc+".JPG";
	$('ayH').innerHTML = frq+' Hz';
	if (frq == 0) {
		document.ayD1.src = "images/aydz.GIF";
		document.ayD2.src = "images/aydz.GIF";
	} else {
		if (frq < 700) {
			document.ayD1.src = "images/ayds.GIF";
			document.ayD2.src = "images/ayds.GIF";
		} else {
			document.ayD1.src = "images/aydf.GIF";
			document.ayD2.src = "images/aydf.GIF";
		}
	}
	document.ayref.clear.value = 'N';
}
