//<script type="text/javascript" language="JavaScript">
//<!--
//returns 0-i, randomly
function rollDie(i) {
 day = new Date();
 sec = day.getSeconds();
 return Math.round(i*Math.random());
}

var thrust = 102;
var rap = 100;
var guard = 115;

var timeout = 0;
var bwarn = 0;

var pose = new Array("thrust", "rap", "guard");

var debug = 0;
var easteregg = 0;

var victory = 5;

var engard = "En Garde.";

var wins;
var losses;

var rec_win = 0;
var rec_los = 0;

function vic_count() {
 document.write(victory);
}

function changeVal() {
  if ((timeout > 0) || (window.event.keyCode != "undefined")) { return; }
  var b = window.event.keyCode;
  if (debug > 0) { document.dodo.key.value = b; }
  var compb = -1;
  var redraw = 0;

  if (b == thrust) { compb = 0; redraw = 1; }
  if (b == rap) { compb = 1; redraw = 1; }
  if (b == guard) { compb = 2; redraw = 1; }
  if (b == 109 && easteregg > 0) { document.you.src="lmoon.gif";
   document.dodo.yousay.value = "Hee hee!";
   document.dodo.hesay.value = "Hey!";
  }

  if (redraw == 1) {
   action(compb);
  }
}

function action(a) {
 if (timeout <= 0) {
	 document.you.src="l"+pose[a]+".gif";
	 clear_say();
	 var result = make_move(a);
	 if (result > 0) { wins++; document.dodo.hesay.value = rand_phrase("touche"); }
	 else if (result < 0) { losses++; document.dodo.yousay.value = rand_phrase("touche"); }
	 if (wins >= victory) { gameover(1); }
	 else if (losses >= victory) { gameover(-1); }
	 if (result != 0) { scores(); }
 }
}

function clear_say() {
   if (document.dodo.hesay.value == engard) { document.dodo.hesay.value = ""; }
   else if (document.dodo.hesay.value != "") { document.dodo.hesay.value = engard; }
   if (document.dodo.yousay.value == engard) { document.dodo.yousay.value = ""; }
   else if (document.dodo.yousay.value != "") { document.dodo.yousay.value = engard; }
}

function make_move(compb) {
 var nm = rollDie(299);
 nm = nm % 3;
 if (nm == 0) { document.nemesis.src="rthrust.gif"; }
 else if (nm == 1) { document.nemesis.src="rrap.gif"; }
 document.nemesis.src="r"+pose[nm]+".gif";
 var result = 0;
 if ((nm + 1) % 3 == compb) { result = -1; }
 else if ((compb + 1) % 3 == nm) { result = 1; }
 if (debug > 0) { document.dodo.key.value = result; }
 return result;
}

function gameover(aa) {
 var short_msg, msg = "";
 if (aa > 0) { short_msg = "You Win!";
  msg = "You have "+rand_phrase('defeated')+" your "+rand_phrase('fool')+" opponent on the field of honor.";
  document.nemesis.src="rdead.gif";
  easteregg = 1;
  rec_win++;
 }
 else { short_msg = "You Lose!";
  msg = "You are "+rand_phrase('defeated')+"...your dear "+rand_phrase('sister')+" weeps for your "+rand_phrase('soul')+".";
  document.you.src="ldead.gif";
  rec_los++;
 }
 timeout = 1;
 if (bwarn < 1) { setTimeout("gameover_2('"+msg+"');",800); }
 else { gameover_2(msg); }
}

function gameover_2(msg) {
 alert (msg);
 timeout = 0;
 init();
 return 100;
}

function rand_phrase(which) {
 if (which == "touche") { which = "Touche!"; }
 return which;
}

function scores() {
 document.dodo.wins.value = wins;
 document.dodo.losses.value = losses;
}

function init() {
 wins = 0;
 losses = 0;
 document.you.src = "lready.gif";
 document.nemesis.src = "rready.gif";
 document.dodo.yousay.value = "";
 document.dodo.hesay.value = "";
 document.dodo.record.value = rec_win + " & " + rec_los;

 document.dodo.yousay.focus();

 scores();
}

function browser_warning() {
 var browserName=navigator.appName; 
 if (browserName!="Microsoft Internet Explorer") {
  document.write("<br />This has only been tested in IE");
  bwarn = 1;
 }
}

function hi_there(a) {
 document.write(a);
}

function the_story() {
 var story = "Back in the day, fencing was a matter of honor.\n\nSometimes a man would fence for a lady.\n\nSometimes a man would fence because another man said he was a lady.\n\nEither way the rules were the same...\n\nSomeone... would die!\n\nOr really be about to die but then the other guy would let him live!\n\nThat was almost worse.\n\nNot really though.\n"; 
 alert(story);
}

function dramatis_personae() {
 var who = "In FENCING: A MATTER OF HONOR, you take on the role of the noble Marcus Redplume, pitting your shining rapier against the crooked foil of the despicable Cornelius Bluefeather for the myriad wrongs he has done your family.\n\nWho will triumph?  ONLY YOU can answer that question...\nyou and FENCING: A MATTER OF HONOR.";
 alert(who);
}

//-->
//</script>

