// V 1.0

// Proper pronoun support.
var character_gender 				= "female";
var cap_he_she 						= "She";
var he_she 							= "she";
var his_her 						= "her";
var him_her 						= "her";
var sex								= "woman";

// variables to store the story state.
var user_name 						= "Unchosen";
var user_name_2 					= "Unchosen";
var user_city 						= "Unchosen";
var user_lifestyle_adj 				= "Unchosen";
var user_lifestyle 					= "Unchosen";
var user_fight						= "Unchosen";
var user_study						= "Unchosen";
var user_learn						= "Unchosen";
var user_style_adj					= "Unchosen";
var user_style 						= "Unchosen";
var user_technique_adj 				= "Unchosen";
var user_technique 					= "Unchosen";
var user_relative 					= "Unchosen";
var user_relative_condition 		= "Unchosen";
var user_bad_guy 					= "Unchosen";
var user_showdown 					= "Unchosen";
var user_beating 					= "Unchosen";
var user_salvation					= "Unchosen";
var user_victory					= "Unchosen";


// Information about the user options.
// variablesFormName, variableCodeName;
varInfoList = new Array(
	"myName",				"user_name",
	"myName2",				"user_name_2",
	"myCity",				"user_city",
	"myLifeAdj",			"user_lifestyle_adj",
	"myLife",				"user_lifestyle",
	"myFight",				"user_fight",
	"myStudy",				"user_study",
	"myLearn",				"user_learn",
	"myStyleAdj",			"user_style_adj",
	"myStyle",				"user_style",
	"myTechniqueAdj",		"user_technique_adj",
	"myTechnique",			"user_technique",
	"myRelative",			"user_relative",
	"myRelativeCondition",	"user_relative_condition",
	"myBadGuy",				"user_bad_guy",
	"myShowdown",			"user_showdown",
	"myBeating",			"user_beating",
	"mySalvation",			"user_salvation",
	"myVictory",			"user_victory"
);


function ResetStoryState()
{
	user_name 						= "Unchosen";
	user_name_2 					= "Unchosen";
	user_city 						= "Unchosen";
	user_lifestyle_adj 				= "Unchosen";
	user_lifestyle 					= "Unchosen";
	user_fight						= "Unchosen";
	user_study						= "Unchosen";
	user_learn						= "Unchosen";
	user_style_adj					= "Unchosen";
	user_style 						= "Unchosen";
	user_technique_adj 				= "Unchosen";
	user_technique 					= "Unchosen";
	user_relative 					= "Unchosen";
	user_relative_condition 		= "Unchosen";
	user_bad_guy 					= "Unchosen";
	user_showdown 					= "Unchosen";
	user_beating 					= "Unchosen";
	user_salvation					= "Unchosen";
	user_victory					= "Unchosen";
}


function SetVarByFormName( formVarName, formVarValue )
{
	for( var i = 0; i < varInfoList.length; i+=2 )
	{
		if( formVarName == varInfoList[ i ] )
		{
			// Set the code variable.
			eval( varInfoList[ i + 1 ] + '="' + formVarValue + '";' );
		}
	}
}

function ExtractUserOptions()
{
	var shouldReset = false;

	// Make sure we are working with a clean slate.
	ResetStoryState();

	// Try to get the query value from the query string...
	var queryStringStripped = self.location.search.substring( self.location.search.lastIndexOf("?") + 1 )
	
	// Get an array name value pairs.
	var arrayOfNameValueStrings = queryStringStripped.split('&');

	// Handle each of the pairs.
	for( var i = 0; i < arrayOfNameValueStrings.length; ++i )
	{
		// Break the pair into two parts.
		var nameValuePairArray = arrayOfNameValueStrings[i].split('=');

		// Check if we should reset.
		if( ( nameValuePairArray[0] == "choice" ) &&
			( nameValuePairArray[1] == "Restart" ) )
		{
			// Flag that we should reset.
			shouldReset = true;
		}


		// Fix spaces that have come through as + signs.
		var value = "" + nameValuePairArray[1];
		var valueArray = value.split('+');
		var newValue = valueArray.join(' ');
		nameValuePairArray[1] = newValue;

		// Use the two parts to set the varuable to the value.
		SetVarByFormName( nameValuePairArray[0], nameValuePairArray[1] );
	}

	// Check if the user want to reset.
	if( shouldReset )
	{
		// Make sure we are working with a clean slate.
		ResetStoryState();		
	}
}


function SetGenderPronouns()
{
	if ( character_gender == 'male' )
	{
		cap_he_she = "He";
		he_she = "he";
		his_her = "his";
		him_her = "him";
		sex = "man";
	}else
	{
		cap_he_she = "She";
		he_she = "she";
		his_her = "her";
		him_her = "her";
		sex = "woman";
	}
}


function SetCharacterGender( user_name )
{	
	if ( user_name == 'Michelle')
	{
		character_gender = "female";
	}else
	{
		character_gender = "male";
	}
	SetGenderPronouns();
}


function WriteStorySoFar()
{
	ExtractUserOptions();

	document.write( '<form method="get" action="'+ thisDocument + '">' );

	if( !WriteStoryParts() )
	{
		document.write( '<p>' );
		document.write( '<input type="submit" name="choice" value="Continue">' );
		document.write( '<input type="submit" name="choice" value="Restart">' );
		document.write("</form>\n");
		document.write( '</p>' );
	}
	else
	{
		document.write( '<p>' );
		document.write( '<input type="submit" name="choice" value="Restart">' );
		document.write("</form>\n");
		document.write( '</p>' );
	}
}


function WriteStoryParts()
{
	if( !StoryName() ) return false;
	if( !StoryCity() ) return false;
	if( !StoryLifestyle() ) return false;
	if( !StoryFirstFight() ) return false;
	if( !StoryStudy() ) return false;
	if( !StoryLearn() ) return false;
	if( !StoryKungFuStyle() ) return false;
	if( !StoryTechnique() ) return false;
	if( !StoryConflict() ) return false;
	if( !StoryBadGuy() ) return false;
	if( !StoryShowdown() ) return false;
	if( !StoryBeating() ) return false;
	if( !StorySalvationAndVictory() ) return false;

	return true;
}


function StoryName()
{
	if( ( user_name != "Unchosen" ) && 
		( user_name_2 != "Unchosen" ) )
	{
		SetCharacterGender( user_name );
		document.write( '<p>' );
		document.write("You are an actor whose name is " + user_name + " " + user_name_2 + "." );
		document.write( '<input type="hidden" name="myName" value="'+ user_name +'">' );
		document.write( '<input type="hidden" name="myName2" value="'+ user_name_2 +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
		document.write("You are an actor whose name is ");
		document.write("<select name='myName' size='1'>");
			document.write("<option selected value='Michelle'>Michelle");
			document.write("<option value='Jackie'>Jackie");
			document.write("<option value='Jean Claude'>Jean Claude");
			document.write("<option value='Bruce'>Bruce");
			document.write("<option value='Chuck'>Chuck");
			document.write("<option value='Chow'>Chow");
			document.write("<option value='Steven'>Steven");
			document.write("<option value='Voltez'>Voltez");
			document.write("<option value='Wong'>Wong");
			document.write("<option value='Russel'>Russel");
			document.write("<option value='Chun'>Chun");
		document.write("</select>");
		document.write("<select name='myName2' size='1'>");
			document.write("<option selected value='Yeoh'>Yeoh");
			document.write("<option value='VanDamme'>VanDamme");
			document.write("<option value='Chan'>Chan");
			document.write("<option value='Lee'>Lee");
			document.write("<option value='Norris'>Norris");
			document.write("<option value='Yun-Fat'>Yun-Fat");
			document.write("<option value='Segal'>Segal");
			document.write("<option value='V.'>V.");
			document.write("<option value='Fei-Hong'>Fei-Hong");
			document.write("<option value='Wong'>Wong");
			document.write("<option value='Li'>Li");
		document.write("</select>");
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StoryCity()
{
	if( user_city != "Unchosen" )
	{
		document.write( '<p>' );
		document.write(cap_he_she + " plays a " + sex + " who was born in the city of " + user_city +".");
		document.write( '<input type="hidden" name="myCity" value="'+ user_city +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
		document.write(cap_he_she + " plays a " + sex + " who was born in the city of ");
		document.write("<select name='myCity' size='1'>");
			document.write("<option selected value='San Francisco'>San Francisco");
			document.write("<option value='Hong Kong'>Hong Kong");
			document.write("<option value='Prague'>Prague");
			document.write("<option value='Beijing'>Beijing");
			document.write("<option value='Paris'>Paris");
			document.write("<option value='Los Angeles'>Los Angeles");
		document.write("</select>");
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StoryLifestyle()
{
	if(	( user_lifestyle_adj != "Unchosen" ) &&
		( user_lifestyle != "Unchosen" ) )
	{
		document.write( '<p>' );
			document.write( cap_he_she + " is " + user_lifestyle_adj + " " + user_lifestyle + ".");
			document.write( '<input type="hidden" name="myLifeAdj" value="'+ user_lifestyle_adj +'">' );
			document.write( '<input type="hidden" name="myLife" value="'+ user_lifestyle +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
			document.write(cap_he_she + " is ");
			document.write("<select name='myLifeAdj' size='1'>");
				document.write("<option selected VALUE='an aspiring'>an aspiring");
				document.write("<option value='a lowly'>a lowly");
				document.write("<option value='a bumbling'>a bumbling");
				document.write("<option value='a great'>a great");
				document.write("<option value='an old'>an old");
			document.write("</select>");
			document.write("<select name='myLife' size='1'>");
				document.write("<option selected value='janitor'>janitor");
				document.write("<option value='lowlife'>lowlife");
				document.write("<option value='kung fu student'>kung fu student");
				document.write("<option value='ninja'>ninja");
				document.write("<option value='grandfather'>grandfather");
				document.write("<option value='grandmother'>grandmother");
				document.write("<option value='migrant worker'>migrant worker");
				document.write("<option value='drifter'>drifter");
				document.write("<option value='mercenary'>mercenary");
				document.write("<option value='shao-lin monk'>shao-lin monk");
				document.write("<option value='orphan'>orphan");
			document.write("</select>");
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StoryFirstFight()
{
	if( user_fight != "Unchosen" )
	{
		document.write( '<p>' );
		document.write(cap_he_she + " is forced to fight " + user_fight +", but gets beaten up.");
		document.write( '<input type="hidden" name="myFight" value="'+ user_fight +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
		document.write(cap_he_she + " is forced to fight ");
		document.write("<select name='myFight' size='1'>");
			document.write("<option selected value='because of getting picked on'>because of getting picked on");
			document.write("<option value='to avenge the past'>to avenge the past");
			document.write("<option value='by the cops'>by the cops");
			document.write("<option value='by the bad guys'>by the bad guys");
			document.write("<option value='through deception'>through deception");
		document.write("</select>");
		document.write(", but gets beaten up.");
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StoryStudy()
{
	if( user_study != "Unchosen" )
	{
		document.write( '<p>' );
		document.write( 'Later, ' + he_she + ' ' + user_study + ' and begins martial arts training.' );
		document.write( '<input type="hidden" name="myStudy" value="'+ user_study +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
		document.write( 'Later, ' + he_she + ' ' );
		document.write("<select name='myStudy' size='1'>");
			document.write("<option selected value='finds an ancient kung fu manual'>finds an ancient kung fu manual");
			document.write("<option value='goes to a karate school'>goes to a karate school");
			document.write("<option value='goes to a shaolin temple'>goes to a shaolin temple");
			document.write("<option value='goes to his grandfathers'>goes to his grandfathers");
			document.write("<option value='goes to the kind old neightbor'>goes to the kind old neightbor");
			document.write("<option value='is discovered by dying old man'>is discovered by dying old man");
			document.write("<option value='is discovered by drunk old man'>is discovered by drunk old man");
			document.write("<option value='watches others train through a hole in the wall'>watches others train through a hole in the wall");
		document.write("</select>");
		document.write(' and begins martial arts training.');
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StoryLearn()
{
	if( user_learn != "Unchosen" )
	{
		document.write( '<p>' );
		document.write( 'Where, ' + he_she + ' must ' + user_learn + ' for hours every day.' );
		document.write( '<input type="hidden" name="myLearn" value="'+ user_learn +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
		document.write( 'Where, ' + he_she + ' must ' );
		document.write("<select name='myLearn' size='1'>");
			document.write("<option selected value='clean'>clean");
			document.write("<option value='cook'>cook");
			document.write("<option value='balance'>balance");
			document.write("<option value='meditate'>meditate");
			document.write("<option value='endure torture'>endure torture");
			document.write("<option value='take terrible beatings'>take terrible beatings");
		document.write("</select>");
		document.write(' for hours every day.');
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StoryKungFuStyle()
{
	if(	( user_style_adj != "Unchosen" ) &&
		( user_style != "Unchosen" ) )
	{
		document.write( '<p>' );
				document.write(cap_he_she + " eventually learns the " + user_style_adj + " " + user_style + " style.");
			document.write( '<input type="hidden" name="myStyleAdj" value="'+ user_style_adj +'">' );
			document.write( '<input type="hidden" name="myStyle" value="'+ user_style +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
			document.write(cap_he_she + " eventually learns the ");
			document.write("<select name='myStyleAdj' size='1'>");
				document.write("<option selected value='White'>White");
				document.write("<option value='Black'>Black");
				document.write("<option value='Red'>Red");
				document.write("<option value='Golden'>Golden");
				document.write("<option value='Fire'>Fire");
				document.write("<option value='Spitting'>Spitting");
				document.write("<option value='Hopping'>Hopping");
				document.write("<option value='Spinning'>Spinning");
				document.write("<option value='Drunken'>Drunken");
			document.write("</select>");
			document.write("<select name='myStyle' size='1'>");
				document.write("<option selected value='Crane'>Crane");
				document.write("<option value='Tiger'>Tiger");
				document.write("<option value='Toad'>Toad");
				document.write("<option value='Dragon'>Dragon");
				document.write("<option value='Monkey'>Monkey");
				document.write("<option value='Hamster'>Hamster");
				document.write("<option value='Sloth'>Sloth");
				document.write("<option value='Praying Mantis'>Praying Mantis");
			document.write("</SELECT>");
			document.write(" style.");
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StoryTechnique()
{
	if(	( user_technique_adj != "Unchosen" ) &&
		( user_technique != "Unchosen" ) )
	{
		document.write( '<p>' );
			document.write("But becomes especially adept at the " + user_technique_adj + " " + user_technique + " technique.");
			document.write( '<input type="hidden" name="myTechniqueAdj" value="'+ user_technique_adj +'">' );
			document.write( '<input type="hidden" name="myTechnique" value="'+ user_technique +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
			document.write("But becomes especially adept at the ");
			document.write("<select name='myTechniqueAdj' size='1'>");
				document.write("<option selected value='Smashing'>Smashing");
				document.write("<option value='Crushing'>Crushing");
				document.write("<option value='Lightning'>Lightning");
				document.write("<option value='Flying'>Flying");
				document.write("<option value='Sitting'>Sitting");
				document.write("<option value='Dancing'>Dancing");
				document.write("<option value='Silent'>Silent");
				document.write("<option value='Death'>Death");
				document.write("<option value='Chicken'>Chicken");
				document.write("<option value='Flapping'>Flapping");
			document.write("</select>");
			document.write("<select name='myTechnique' size='1'>");
				document.write("<option selected value='Head'>Head");
				document.write("<option value='Forearm'>Forearm");
				document.write("<option value='Finger'>Finger");
				document.write("<option value='Foot'>Foot");
				document.write("<option value='Buddha'>Buddha");
				document.write("<option value='Fist'>Fist");
				document.write("<option value='Slap to the ears'>Slap to the ears");
				document.write("<option value='Chop'>Chop");
				document.write("<option value='Kick'>Kick");
			document.write("</select>");
			document.write(" technique.");
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StoryConflict()
{
	if(	( user_relative != "Unchosen" ) &&
		( user_relative_condition != "Unchosen" ) )
	{
		document.write( '<p>' );
			document.write("Unfortunately, " + his_her + " " + user_relative  + " " + user_relative_condition + " by ");
			document.write( '<input type="hidden" name="myRelative" value="'+ user_relative +'">' );
			document.write( '<input type="hidden" name="myRelativeCondition" value="'+ user_relative_condition +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
			document.write("Unfortunately, " + his_her + " ");
			document.write("<select name='myRelative' size='1'>");
				document.write("<option selected value='mother'>mother");
				document.write("<option value='father'>father");
				document.write("<option value='brother'>brother");
				document.write("<option value='sister'>sister");
				document.write("<option value='grandfather'>grandfather");
				document.write("<option value='grandmother'>grandmother");
				document.write("<option value='uncle'>uncle");
				document.write("<option value='cat'>cat");
				document.write("<option value='the family heirloom'>the family heirloom");
				document.write("<option value='the school'>the school");
				document.write("<option value='school's mystical object'>school's mystical object");
				document.write("<option value='aunt's brother's wife's mother-in-law'>aunt's brother's wife's mother-in-law");
			document.write("</select>");
			document.write("<select name='myRelativeCondition' size='1'>");
				document.write("<option value=' has been disgraced'> has been disgraced");
				document.write("<option selected value='is missing'>is missing");
				document.write("<option value='has been kidnapped'>has been kidnapped");
				document.write("<option value='is dying'>is dying");
				document.write("<option value='is being blackmailed'>is being blackmailed");
				document.write("<option value='was murdered'>was murdered");
				document.write("<option value='was raped'>was raped");
				document.write("<option value='was tortured and left for dead'>was tortured and left for dead");
				document.write("<option value='has been stolen'>has been stolen");
			document.write("</select>");
			document.write(" .");
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StoryBadGuy()
{
	if( user_bad_guy != "Unchosen" )
	{
		document.write( '<p>' );
		document.write( user_bad_guy );
		document.write( '<input type="hidden" name="myBadGuy" value="'+ user_bad_guy +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
			document.write("<select name='myBadGuy' size='1'>\n");
				document.write("<option selected value='organized crime'>organized crime\n");
				document.write("<option value='a rival school'>a rival school\n");
				document.write("<option value='an evil overlord'>an evil overlord\n");
				document.write("<option value='bandits'>bandits\n");
				document.write("<option value='a traitor'>a traitor\n");
				document.write("<option value='an ex-lover'>an ex-lover\n");
				document.write("<option value='a fellow student'>a fellow student\n");
				document.write("<option value='an old friend'>an old friend\n");
				document.write("<option value='their evil twin brother'>their evil twin brother\n");
				document.write("<option value='their evil twin sister'>their evil twin sister\n");
			document.write("</select>\n");
			document.write(" .\n");
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}



function StoryShowdown()
{
	if( user_showdown != "Unchosen" )
	{
		document.write( '<p>' );
		document.write( 'A final showdown occurs ' + user_showdown + '.' );
		document.write( '<input type="hidden" name="myShowdown" value="'+ user_showdown +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
		document.write( 'A final showdown occurs ' );
		document.write("<select name='myShowdown' size='1'>");
			document.write("<option selected value='in a graveyard'>in a graveyard");
			document.write("<option value='on a battlefield'>on a battlefield");
			document.write("<option value='in a castle'>in a castle");
			document.write("<option value='in an open field'>in an open field");
			document.write("<option value='in a stream'>in a stream");
			document.write("<option value='on top of a building'>on top of a building");
			document.write("<option value='on a boat in a river'>on a boat in a river");
			document.write("<option value='at a big championshiop'>at a big championshiop");
		document.write("</select>");
		document.write('.');
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StoryBeating()
{
	if( user_beating != "Unchosen" )
	{
		document.write( '<p>' );
		document.write( 'where ' + he_she + ' takes a ' + user_beating + ' beating.' );
		document.write( '<input type="hidden" name="myBeating" value="'+ user_beating +'">' );
		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
		document.write( 'where ' + he_she + ' takes a ' );
		document.write("<select name='myBeating' size='1'>");
			document.write("<option selected value='terrible'>terrible");
			document.write("<option value='on a awful'>awful");
			document.write("<option value='demeaning'>demeaning");
			document.write("<option value='brutal'>brutal");
		document.write("</select>");
		document.write(' beating.');
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}


function StorySalvationAndVictory()
{
	if( ( user_salvation != "Unchosen" ) &&
		( user_victory != "Unchosen" ) )
	{
		document.write( '<p>' );
		document.write( 'before ' + user_salvation + ' and then ' + user_victory + ' defeats the enemy.' );
		document.write( '<input type="hidden" name="mySalvation" value="'+ user_salvation +'">' );
		document.write( '<input type="hidden" name="myVictory" value="'+ user_victory +'">' );
		document.write( '</p>' );

		document.write( '<h2>' );
					document.write( "The end" );
		document.write( '</h2>' );

		document.write( '<p>' );

		document.write( 'Now you are ready to go off to a film studio with your exciting and original script! If you have any commnents or suggestions (especially for more options or parts of the story) please <a href="../contact.html" acceskey="9">contact</a> me.' );

		document.write( '</p>' );

		// Continue printing out the story.
		return true;
	}
	else
	{
		document.write( '<p>' );
		document.write( 'before ' );
		document.write("<select name='mySalvation' size='1'>");
			document.write("<option selected value='realizing inner strength'>realizing inner strength");
			document.write("<option value='remembering the masters teachings'>remembering the masters teachings");
			document.write("<option value='becoming like their spirit animal'>becoming like their spirit animal");
			document.write("<option value='the badguy becomes tired'>the badguy becomes tired");
			document.write("<option value='their friends arrive'>their friends arrive");
		document.write("</select>");
		document.write(' and then ');
		document.write("<select name='myVictory' size='1'>");
			document.write("<option selected value='gloriously'>gloriously");
			document.write("<option value='on a powerfully'>powerfully");
			document.write("<option value='instantly'>instantly");
		document.write("</select>");
		document.write(' defeats the enemy.');
		document.write( '</p>' );

		// Stop printing out the story.
		return false;
	}
}

