// JavaScript Document for
//Ken Fackler's NOTAM Abbreviations Game<

//build abbrev database
var abbr_data = new Array(

"ABN||airport beacon",
"ABV||above",
"ACC||area control center",
"ACCUM||accumulate",
"ACFT||aircraft",
"ACR||air carrier",
"ACT||active",
"ADJ||adjacent",
"ADZD||advised",
"AFD||airport facility directory",
"AGL||above ground level",
"ALS||approach lighting system",
"ALT||altitude",
"ALTM||altimeter",
"ALTN||alternate",
"ALTNLY||alternately",
"ALSTG||altimeter setting",
"AMDT||amendment",
"AMGR||airport manager",
"AMOS||automatic meteorological observing system",
"AP||airport",
"APCH||approach",
"AP LGT||airport lighting",
"APP||approach control",
"ARFF||aircraft rescue and fire fighting|aircraft rescue and firefighting",
"ARR||arrive|arrival",
"ASOS||automatic surface observing system",
"ASPH||asphalt",
"ATC||air traffic control",
"ATCCC||air traffic control command center",
"ATIS||automatic terminal information service",
"AUTH||authority",
"AVBL||available",
"AWOS||automatic weather observing system",
"AWY||airway|air way",
"AZM||azimuth",
"BA FAIR||braking action fair",
"BA NIL||braking action nil",
"BA POOR||braking action poor",
"BC||back course|backcourse",
"BCN||beacon",
"BLW||below",
"BND||bound",
"BRG||bearing",
"BYD||beyond",
"CAAS||class a airspace|class a air space",
"CAT||category",
"CBAS||class b airspace|class b air space",
"CBSA||class b surface area",
"CCAS||class c airspace|class c air space",
"CCLKWS||counterclockwise|counter clockwise|counter-clockwise",
"CCSA||class c surface area",
"CD||clearance delivery",
"CDAS||class d airspace|class d air space",
"CDSA||class d surface area",
"CEAS||class e airspace|class e air space",
"CESA||class e surface area",
"CFR||code of federal regulations",
"CGAS||class g airspace|class g air space",
"CHAN||channel",
"CHG||change|modification",
"CIG||ceiling|cieling",
"CK||check",
"CL||center line|centerline",
"CLKWS||clockwise|clock wise|clock-wise",
"CLR||clearance|clear|clears|cleared to",
"CLSD||closed",
"CMB||climb",
"CMSND||commissioned|commisioned",
"CNL||cancel",
"CNTRLN||centerline|center line",
"COM||communication|communications",
"CONC||concrete",
"CPD||coupled",
"CRS||course",
"CTC||contact",
"CTL||control",
"DALGT||daylight",
"DCMSN||decommission|decommision",
"DCMSND||decommissioned|decommisioned",
"DCT||direct",
"DEGS||degrees",
"DEP||depart|departure",
"DEP PROC||departure procedure",
"DH||decision height",
"DISABLD||disabled",
"DIST||distance",
"DLA||delay|delayed",
"DLT||delete",
"DLY||daily",
"DME||distance measuring equipment",
"DMSTN||demonstration",
"DP||dewpoint temperature|dew point temperature|dewpoint|dew point",
"DSPLCD||displaced",
"E||east",
"EB||eastbound|east bound",
"EFAS||en route flight advisory service|enroute flight advisory service",
"ELEV||elevation",
"ENG||engine",
"ENRT||en route|enroute",
"ENTR||entire",
"EXC||except",
"FAC||facility|facilities",
"FAF||final approach fix",
"FAN MKR||fan marker",
"FDC||flight data center",
"FI/T||flight inspection temporary|flight inspection/temporary",
"FI/P||flight inspection permanent|flight inspection/permanent",
"FM||from",
"FNA||final approach",
"FPM||feet per minute",
"FREQ||frequency",
"FRH||fly runway heading",
"FRI||friday",
"FRZN||frozen",
"FSS||flight service station",
"FT||foot|feet",
"GC||ground control",
"GCA||ground control approach|ground controlled approach",
"GCO||ground communications outlet|ground communication outlet",
"GOVT||government",
"GP||glide path|glidepath",
"GPS||global positioning system",
"GRVL||gravel",
"HAA||height above airport",
"HAT||height above touchdown|height above touch down",
"HDG||heading",
"HEL||helicopter",
"HELI||heliport|heli-port",
"HIRL||high intensity runway lights",
"HLDG||holding",
"HOL||holiday",
"HP||holding pattern",
"HR||hour",
"IAF||initial approach fix",
"IAP||instrument approach procedure",
"INBD||inbound",
"ID||identification",
"IDENT||identify|identifier|identification",
"IF||intermediate fix",
"ILS||instrument landing system",
"IM||inner marker",
"IMC||instrument meteorological conditions",
"IN||inch|inches",
"INDEFLY||indefinitely",
"INFO||information",
"INOP||inoperative",
"INSTR||instrument|instruments",
"INT||intersection",
"INTL||international",
"INTST||intensity",
"IR||ice on runway",
"KT||knots|knot",
"L||left",
"LAA||local airport advisory",
"LAT||latitude",
"LB||pound|pounds",
"LC||local control",
"LOC||local|locally|location",
"LCTD||located",
"LGT||light|lights|lighting",
"LGTD||lighted",
"LIRL||low intensity runway lights",
"LLWAS||low level wind shear alert system",
"LDG||landing",
"LLZ||localizer",
"LONG||longitude",
"LRN||long range navigation",
"LSR||loose snow on runway",
"LT||left turn",
"MAG||magnetic",
"MAINT||maintain|maintenance",
"MALS||medium intensity approach light system",
"MAPT||missed approach point",
"MCA||minimum crossing altitude",
"MDA||minimum descent altitude",
"MEA||minimum en route altitude|minimum enroute altitude",
"MED||medium",
"MIN||minute|minutes",
"MIRL||medium intensity runway lights",
"MKR||marker",
"MLS||microwave landing system",
"MM||middle marker",
"MNM||minimum",
"MNT||monitor|monitoring|monitored",
"MOC||minimum obstruction clearance",
"MON||monday",
"MRA||minimum reception altitude",
"MSA||minimum safe altitude|minimum sector altitude",
"MSAW||minimum safe altitude warning",
"MSG||message",
"MSL||mean sea level",
"MUNI||municipal",
"N||north",
"NA||not authorized",
"NAV||navigation",
"NB||northbound|north bound",
"NE||northeast|north east",
"NGT||night",
"NM||nautical mile|nautical miles",
"NMR||nautical mile radius",
"NONSTD||nonstandard|non-standard",
"NOPT||no procedure turn required",
"NR||number",
"NTAP||notice to airmen publication",
"NW||northwest|north west",
"OBSC||obscured|obscure|obscuring",
"OBST||obstruction|obstacle",
"OM||outer marker",
"OPR||operate|operator|operative",
"OPS||operation|operations",
"ORIG||original",
"OTS||out of service",
"OVR||over",
"PAEW||personnel and equipment working",
"PAX||passenger|passengers",
"PAPI||precision approach path indicator",
"PAR||precision approach radar",
"PARL||parallel",
"PAT||pattern",
"PCL||pilot controlled lighting",
"PERM||permanent",
"PJE||parachute jumping exercise",
"PLA||practice low approach",
"PLW||plow|plowed",
"PN||prior notice required",
"PPR||prior permission required",
"PRN||pseudo random noise|pseudo-random noise",
"PROC||procedure",
"PROP||propeller|propellor",
"PSR||packed snow on runway",
"PTCHY||patchy",
"PTN||procedure turn",
"PVT||private",
"RAIL||runway alignment indicator lights",
"RAMOS||remote automatic meteorological observing system",
"RCL||runway center line|runway centerline",
"RCLL||runway center line lights|runway centerline lights",
"RCO||remote communication outlet|remote communications outlet",
"REC||receive|receiver|recieve|reciever",
"REIL||runway end identifier lights",
"RELCTD||relocated",
"REP||report|reports",
"RMNDR||remainder",
"RMK||remark|remarks",
"RNAV||area navigation",
"RPLC||replace",
"RQRD||required",
"RRL||runway remaining lights",
"RSVN||reservation",
"RT||right turn",
"RTE||route",
"RTS||return to service",
"RUF||rough",
"RVR||runway visual range",
"RWY||runway",
"S||south",
"SA||sand|sanded",
"SAT||saturday",
"SB||southbound|south bound",
"SDF||simplified directional facility",
"SE||southeast|south east",
"SFL||sequence flashing lights",
"SIMUL||simultaneous|simultaneously",
"SKED||scheduled|schedule",
"SLR||slush on runway",
"SN||snow",
"SNGL||single",
"SPD||speed",
"SSALS||simplified short approach lighting system",
"SSR||secondary surveillance radar",
"STA||straight-in approach|straight in approach",
"STAR||standard terminal arrival",
"SUN||sunday",
"SVC||service",
"SVN||satellite vehicle number",
"SW||southwest|south west",
"T||temperature",
"TACAN||tactical air navigational aid",
"TAR||terminal area surveillance radar",
"TDWR||terminal doppler weather radar",
"TDZ||touchdown zone|touch down zone",
"TDZ LGT||touchdown zone lights|touch down zone lights",
"TEMPO||temporary|temporarily",
"TFC||traffic",
"TFR||temporary flight restriction|temporary flight restrictions",
"TGL||touch-and-go landings|touch and go landings",
"THN||thin",
"THR||threshold",
"THRU||through",
"THU||thursday",
"TIL||until",
"TKOF||takeoff|take off|take-off",
"TM||traffic management",
"TMPA||traffic management program alert",
"TRML||terminal",
"TRNG||training",
"TRSN||transition",
"TSNT||transient",
"TUE||tuesday",
"TWR||airport control tower|control tower|tower",
"TWY||taxiway|taxi way",
"UAV||unmanned air vehicles|unmanned air vehicle|unmanned aerial vehicles|unmanned aerial vehicle",
"UFN||until further notice",
"UNAVBL||unavailable",
"UNLGTD||unlighted",
"UNMKD||unmarked",
"UNMNT||unmonitored",
"UNREL||unreliable",
"UNUSBL||unusable",
"VASI||visual approach slope indicator",
"VDP||visual descent point",
"VICE||instead|versus",
"VIS||visibility",
"VMC||visual meteorological conditions",
"VOL||volume",
"W||west",
"WB||westbound|west bound",
"WED||wednesday",
"WEF||with effect from|effective from",
"WI||within",
"WIE||with immediate effect|effective immediately",
"WND||wind",
"WPT||waypoint|way point",
"WTR||water on runway",


//additions for decoder
"BTN||between",
"LGTS||lights",
"MAP||missed approach point",
"NNW||north-northwest|north northwest",
"WNW||west-northwest|west northwest",
"NNE||north-northeast|north northeast",
"ENE||east-northeast|east northeast",
"SSW||south-southwest|south southwest",
"WSW||west-southwest|west southwest",
"SSE||south-southeast|south southeast",
"ESE||east-southeast|east southeast",


//last item MUST NOT end with a comma
"WX||weather"
);

//debug assistant
function s(str) {
	alert(str);
}


//initialize variables
var d = document;
var q = new Array();
var myAbbrev = 500;
var ok = 1;
var notOk = 1;
var skip = 0;


//create Abbreviation entity
function Abbreviation(abbrev,def) {
	this.abbrev = abbrev;
	this.def = def;
}

//convert abbr_data array to Abbreviation entities
for (i=0; i<abbr_data.length; i++) {

	var tmp = abbr_data[i].split("||");
	q[i] = new Abbreviation( tmp[0], tmp[1] );

}


//Generate a random number between 0 and the total number
//of questions available
function getRand(qBank) {

	//set this var to upper limit of random numbers
	var randomNum = 350;
	while (randomNum >= qBank) {
		randomNum = Math.random() * qBank;
		randomNum = Math.round(randomNum);
	}
	return randomNum;
}


//build next query
function buildQuery(f) {

	//turn off Next button; turn on Check My Answer
	/*
	document.getElementById("next").disabled = true;
	document.getElementById("check").disabled = false;
	document.getElementById("check").focus();
	*/
	f.next.disabled = true;
	f.check.disabled = false;
	f.check.focus();
	


	//if unused free skips
	if (skip < 2) {
		//d.getElementById("show").disabled = false;
		f.show.disabled = false;
	}

	//clear the form
	/*
	d.getElementById("def").value = "";
	d.getElementById("def").style.color = "";
	d.getElementById("def").focus();
	*/
	f.def.value = "";
	f.def.style.color = "";
	f.def.focus();

	//get random number and set abbreviation to corresponding value
	var idx = getRand(q.length);
	//d.getElementById("abbrev").value = q[idx].abbrev;
	f.abbrev.value = q[idx].abbrev;

	return idx;
}

//evaluate response
function checkMe(abbr_num, f) {

	//turn off Check My Answer and Free Skip buttons; turn on Next
	f.check.disabled = true;
	f.next.disabled = false;
	f.show.disabled = true;

	//create RegEx from should be state
	var shouldBe = new RegExp('\\b' + q[abbr_num].def + '\\b', "i");
	
	//Compare should be state to what was typed
	if ( shouldBe.test( f.def.value ) ) {

		//if answer is good
		ok++;

		//display smileys
		if (ok > 1) {
			document.images[ok-1].style.visibility = "hidden";
		}

		document.images[ok].style.visibility = "visible";
		document.images[ok].src = "okay_v2.gif";

	}//end if
	
	else {
		
		//if answer not good
		notOk++;
		
		//get and show answer in red
		/*
		d.getElementById("def").value = q[myAbbrev].def;
		d.getElementById("def").style.color = "red";
		*/
		f.def.value = q[myAbbrev].def;
		f.def.style.color = "red";

		//massage answers with options
		//if ( d.getElementById("def").value.indexOf("|") > -1 ) {
		if ( f.def.value.indexOf("|") > -1 ) {
			var ken = /\|/g;
			//d.getElementById("def").value = d.getElementById("def").value.replace(ken, " -OR- ");
			f.def.value = f.def.value.replace(ken, " -OR- ");
		}

		//turn on frowns
		/*
		for (i=1; i<notOk+1; i++) {	
			d.getElementById( i.toString() + "_not_okay").style.visibility = "visible";
		}*/

		//show blinking frown face
		//d.getElementById( notOk.toString() + "_not_okay").src = "not_okay_v2.gif";
		
		document.images[notOk+11-1].style.visibility = "hidden";
		document.images[notOk+11].style.visibility = "visible";
		document.images[notOk+11].src = "not_okay_v2.gif";
		

	}//end else

	//check for end of game
	gameOver(f);
	
}

//end of game checker
function gameOver(fm) {

	//if finish line (end of game) reached by good answerws
	if (ok >= 11) {
		
		//show success message and successful landing graphic; turn off next button
		fm.def.value = "Congratulations! You landed safely.";
		document.images[0].src = "landed.gif";
		fm.next.disabled = true;
	}

	//if finish line (end of game) reached by bad answers
	if (notOk >= 11) {

		//show failure message and crash landing graphic; turn off next button
		fm.def.value = "Ouch!! You just CRASHED.";
		document.images[0].src = "crashed.gif";
		fm.next.disabled = true;
	}
	
	return;

}


//show answer when user clicks Free Skip button
function showMe(f) {

	//turn off Next and Free Skip buttons; turn on Check My Answer
	/*
	d.getElementById("next").disabled = false;
	d.getElementById("check").disabled = true;
	d.getElementById("show").disabled = true;
	*/
	
	f.next.disabled = false;
	f.check.disabled = true;
	f.show.disabled = true;
	
	
	//increment free skip counter; max of 2
	skip++;
	
	//get and show answer in blue
	/*
	d.getElementById("def").value = q[myAbbrev].def;
	d.getElementById("def").style.color = "blue";
	*/
	
	f.def.value = q[myAbbrev].def;
	f.def.style.color = "blue";

	//massage answers with multiple options
	//if ( d.getElementById("def").value.indexOf("|") > -1 ) {
	if ( f.def.value.indexOf("|") > -1 ) {
		var ken = /\|/g;
		//d.getElementById("def").value = d.getElementById("def").value.replace(ken, " -OR- ");
		f.def.value = f.def.value.replace(ken, " -OR- ");
	}

	
	//check for Free Skip limit met
	if (skip >= 2) {

		//turn off Free Skip button
		//d.getElementById("show").disabled = true;
		f.show.disabled = true;
		
	}

}

