
//    Scarlet Nights'
//	written by Stephen Battey - September 1997
//	modified August 1999
//	 for DEBUGGED @ http://www.truplex.com



//------------------------------------------------
//		GLOBAL VARIABLES
//------------------------------------------------


PlayMode=0
//		0: No game started
//		1: Game on
//		2: Slow display active
//		3: Game quited

if ( navigator.appVersion.lastIndexOf('Win')!=-1 )
	NLC="\r\n"
else
	NLC="\n"


ExtraLines=0


DirText = new Array ( "N","NE","E","SE","S","SW","W","NW","UP","DOWN" )
DirName = new Array ( "north","north-east","east","south-east","south","south-west","west","north-west","up","down" )

CommandText="******************  COMMANDS  *******************"+NLC+"coins             down              drop <object>"+NLC+"e                 look              look <object>"+NLC+"look <person>     n                 ne"+NLC+"nw                poss              save"+NLC+"s                 score             se"+NLC+"sw                talk to <person>  take <object>"+NLC+"time              up                use <object>"+NLC+"w"

Vowels="aeiou"






//------------------------------------------------
//		LOW-LEVEL ROUTINES
//------------------------------------------------


function Round(number) {
//			function returns the extracted integer part
//			of a decimal number - Math.round is not used
//			since it is only availiable on a Unix platform

	v=number.toString()
	dp=v.indexOf(".")
	if ( dp==-1 )
		return number
	if ( ( dp==0 ) | ( v.substring(0,dp)=="-" ) )
		return 0
	return eval(v.substring(0,dp))
}



function NumText(v,width,use) {
//			returns the number 'v' in a text string of minimum
//			length 'width', preceeding spaces being filled with
//			the character specified in 'use'

	post=v.toString()
	pre=""
	uselen=width-post.length
	for (i=1; i<=uselen; i++) {
		pre+=use
	}
	return pre+post
}



function RandNo() {
//			Generates a random number between 0 and 9

	RightNow = new Date()
	ThoseSec=RightNow.getSeconds()
	return ( ThoseSec-(Round(ThoseSec/10)*10) )
}





function Counter() {
//			Tick up end of game scores

	ono = new Array ( 6,7,8,10,12,27,28 )

	quests=0
	for (i=0; i<7; i++) {
		if ( ObjRoom[ono[i]]==148 )
			quests++
	}
	qper=(quests/7)*100
	gper=(Score/1530)*100

	topv=qper
	if ( gper>qper )
		topv=gper

	for (perv=0; perv<=topv; perv++) {
		if ( qper>=perv )
			MWin.counters.qno.value=NumText(perv,3," ")+"%"
		if ( gper>=perv )
			MWin.counters.gno.value=NumText(perv,3," ")+"%"
		for (d=0; d<75; d--)
			d+=2
	}
}

function EndOfGame(pn) {
//			Ties up the procedures at the end of a game

	clearTimeout(TimesTimer)
	clearTimeout(ChecksTimer)
	PlayMode=3
	StoryTimer=window.setTimeout("EndStory("+pn+")",2000)
}

function EndStory(pn) {
//			Show the appropriate end story

	document.Main.comline.value=""
	document.Main.playtext.value=""
	if ( pn==1 ) {
		pictext=""
		eogt="Powerless to stop the second rebellion, the BAREE strike at the heart of CentralCity, killing or enslaving all of the enhabitants."
		eogt+="<p>As a prisoner, you are not taken captive, you are thrown out into the baron wastelands south of CentralCity."
		eogt+="<p>Cyberspace is soon taken hold of by the BAREE and so doomed for destruction."
	}
	if ( pn==2 ) {
		pictext="<p><br><img src='BadScene.jpg' width=200 height=150>"
		eogt="With no warning, the BAREE attack CentralCity.<br>Srakac and Xor take control of the city, and within weeks cyberspace is awash with evil BAREE."
		eogt+="<p>The peaceful GAREE are enslaved, their homes destroyed and the land left to rot.<br>BAREE control totally devestates the land."
		eogt+="<p>Human users find cyberspace links down and the land that they created, ruined and unmanageable. They decide to cut off all links with the BAREE people, and leave them to their own devices."
		eogt+="<p>Just one month into their rule, the BAREE find resources begin to run thin. This propmts Srakac to cut back on food and fuel given out to his own troops."
		eogt+="<br>The nature of the land becomes suffocating and people begin to die."
		eogt+="<p>Under pressure from his own people, Srakac decides to resign. Handing power over to you, he makes for the hills and ends the evil reign of BAREE rule."
		eogt+="<p>Despite large efforts to re-cultivate the land, you can do nothing to save it. Cyberspace rots away to nothing."
	}
	if ( pn==3 ) {
		pictext="<p><br><img src='GoodScene.jpg' width=200 height=150>"
		eogt="<br><br><br>Reporting the BAREE army back to CentralCity, the GAREE soldiers march on the underground lair."
		eogt+="<p>The BAREE are taken as prisoners, locked away safely on a floppy disc kept well out of harms way."
		eogt+="<p>The GAREE people are left to live their lives, in the safe knowledge that no danger lurks in baron wastelands that surround their towns and villages."
	}
	MessageWindow=window.open("","","toolbar=0,status=0,menubar=0,scrollbars=0,resizable=1,width=500,height=700")
	MWin=MessageWindow.document

	MWin.write("<html><title>ENDING</title><body bgcolor='#900000' text='#ffffff'>")
	MWin.write(pictext)
	MWin.write("<p><center>"+eogt+"<p><br><br>")
	MWin.write("<form name='counters'>")
	MWin.write("<font size=4 color='#00ffff'>QUESTS COMPLETED</font> &nbsp &nbsp <input type='text' name='qno' size=4 value=''><p>")
	MWin.write("<font size=4 color='#00ffff'>GAME COMPLETED</font> &nbsp &nbsp <input type='text' name='gno' size=4 value=''><p>")
	MWin.write("<br><br><input type='button' value=' FIN ' onClick='window.close()'>")
	MWin.write("</form></center>")
	MWin.write("</body></html>")

	MWin.close()
	CounterTimer=window.setTimeout("Counter()",2500)
}



function NewPage() {
//			Resets the textarea apge

	document.Main.playtext.value=""
	NoOfLines=0
}



function PlaceCursor() {
//			Returns the cursor to 'comline'

	document.Main.comline.focus()
}



function ShowText(text) {
//			Formats and displays the text to be shown

	if ( ExtraLines==0 )
		LineCount=0
	else
		LineCount=ExtraLines

	CurPos=0
	TextLen=text.length
	while ( CurPos<TextLen ) {
		CurPos+=60
		if ( CurPos<TextLen ) {

			while ( text.substring(CurPos,CurPos+1)!=" " ) {
				CurPos--
			}
		}
		LineCount++
	}
	if ( (NoOfLines+LineCount)>23 )
		NewPage()

	text+=NLC
	if ( PlayMode==1 ) {
		text+=NLC
		LineCount++
	}

	document.Main.playtext.value+=text
	NoOfLines+=LineCount
	CurTimer=window.setTimeout("PlaceCursor()",10)
	ExtraLines=0
}



function ListOfItems(room) {
//			Forms a list of items in ListStr which are in room 'room'
//			Counting tally is stored in 'Noi'

	ListStr=""
	LastItem=""
	Noi=0
	firstChar="A"
	for (i=1; i<40; i++) {
		if ( ObjRoom[i]==room ) {
			if ( Noi>0 ) {
				if ( Noi>1 )
					ListStr+=","
				ListStr+=LastItem
			}
			LastItem=""
			if ( i!=28 ) {
				LastItem+=firstChar
				if ( Vowels.indexOf(ObjName[i].substring(0,1))!=-1 )
					LastItem+="n"
			}
			firstChar=" a"
			LastItem+=" "+ObjName[i]
			Noi++
		}
	}
	if ( Noi>1 )
		ListStr+=" and"
	ListStr+=LastItem
}



function CurrTime() {
//			Returns a string containing the time in cyberspace

	HoursName = new Array ( "twelve","one","two","three","four","five","six","seven","eight","nine","ten","eleven","midnight","noon" )

	HourIn = new Array( 3,3,3,3,3,0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,3,3,3 )
	HoursN = new Array( "in the morning","in the afternoon","in the evening","at night" )

	foreStr=""
	aftStr=" "
	theHour=CyberHrs

	placement=HoursN[HourIn[CyberHrs]]

	if ( CyberMins==0 )
		if ( CyberHrs==0 )
			theHour=24
		else {
			if ( CyberHrs==12 )
				theHour=25
			else
				aftStr=" o'clock "
		}
	if ( CyberMins==15 )
		foreStr="quarter-past "
	if ( CyberMins==30 )
		foreStr="half-past "
	if ( CyberMins==45 ) {
		foreStr="quarter-to "
		theHour++
		if ( theHour==24 )
			theHour=0
	}

	if ( theHour>11 )
		theHour-=12
	if ( theHour>11 ) {
		placement=""
		aftStr=""
	}

	return ( foreStr+HoursName[theHour]+aftStr+placement+"." )


}






function GTimeUpdate() {
//			Ticks on the game time by one second

	GameTimeM++
	if ( GameTimeM==60 ) {
		GameTimeM=0
		GameTimeH++
	}

	TimesTimer=window.setTimeout("GTimeUpdate()",60000)
}



function BackGroundChecks() {
//			On-going checking system to update world

	CyberMins+=15
	if ( CyberMins==60 ) {
		CyberMins=0
		CyberHrs++
		if ( CyberHrs==24 )
			CyberHrs=0
	}

	ChecksTimer=window.setTimeout("BackGroundChecks()",22500)


	if ( ( CyberHrs>5 ) & ( CyberHrs<21 ) ) {
		FerryNo++
		if ( FerryNo==12 )
			FerryNo=0

		if ( FerryNo==1 ) {
			Exits[90]="0020202000"
			Exits[91]="0020000200"
			if ( ( CurRoom>89 ) & ( CurRoom<104 ) )
				ShowText("The ferry leaves the south dock, and sails north.")
		}

		if ( FerryNo==5 ) {
			Exits[100]="2002020000"
			Exits[104]="2000202000"
			if ( ( CurRoom>90 ) & ( CurRoom<105 ) )
				ShowText("The ferry arrives at AREE town harbour.")
		}

		if ( FerryNo==7 ) {
			Exits[100]="0002020000"
			Exits[104]="2000002000"

		if ( ( CurRoom>90 ) & ( CurRoom<105 ) )
				ShowText("The ferry leaves AREE town harbour, and sails south.")
		}

		if ( FerryNo==11 ) {
			Exits[90]="2020202000"
			Exits[91]="0020200200"
			if ( ( CurRoom>89 ) & ( CurRoom<104 ) )
				ShowText("The ferry arrives at the south dock.")
		}

	}


	if ( ( CyberHrs==17 ) & ( CyberMins==15 ) )
		ShowText("Night time begins to fall in cyberspace.")


	if ( ( CyberHrs==6 ) & ( CyberMins==15 ) )
		ShowText("A scarlet LED glow adorns the night sky.")


	if ( ( CyberHrs==18 ) & ( CyberMins==0 ) ) {
		ShowText("Night has fallen in cyberspace")

		Exits[37]="2000020000"
		if ( CurRoom==38 ) {
			ShowText("Truzy closes her shop for the night")
			CurRoom=37
			ShowRoom("You leave Truzy's shop."+NLC+NLC)
		}

		Exits[129]="2002020000"
		if ( CurRoom==130 ) {
			ShowText("CRASIN: We're closed!"+NLC+"Crasin chucks you out.")
			CurRoom=129
			ShowRoom("You land outside on the street."+NLC+NLC)
		}

		Exits[112]="0000200200"
		if ( ( CurRoom>111 ) & ( CurRoom<117 ) ) {
			ShowText("The spa centre ends its trade for the day.")
			if ( CurRoom>112 ) {
				CurRoom=112
				ShowRoom("You return outside onto the street."+NLC+NLC)
			}
		}
	}


	if ( ( CyberHrs==7 ) & ( CyberMins==0 ) ) {
		ShowText("Daylight returns to the electronic eden.")

		Exits[37]="2020020000"
		if ( CurRoom==37 )
			ShowText("Truzy opens up for business.")

		Exits[129]="2002020200"
		if ( CurRoom==129 )
			ShowText("Crasin opens his shop with a yawn.")

		Exits[112]="0020200200"
		if ( CurRoom==112 )
			ShowText("The spa centre opens it doors.")

		if ( ( ObjRoom[7]==0 ) & ( ( CurRoom>103 ) | ( CurRoom<101 ) ) & ( CurRoom!=81 ) ) {
			ObjRoom[7]=149
			ShowText("The sunlight melted the block of ice you were carrying.")
		}
		if ( ( ( ObjRoom[7]>103 ) | ( ObjRoom[7]<101 ) ) & ( ObjRoom[7]<148 ) ) {
			if ( CurRoom==ObjRoom[7] )
				ShowText("The block of ice melts benneath your feet.")
			ObjRoom[7]=149
		}

		if ( ( CurRoom>31 ) & ( CurRoom<37 ) ) {
			ShowText("You are caught within Area51 in broad daylight. You are taken away and jailed for treason.")
			EndOfGame(1)
			return
		}
	}

}



function ShowRoom(preText) {
//			Displays the room information

	ToShow=preText+DesStr[CurRoom]
	if ( CurRoom==110 ) {
		ToShow+=" The clock reads "+CurrTime()
	}
	if ( CurRoom==90 ) {
		if ( Exits[90].substring(0,1)=="2" )
			ToShow+=" The ferry is at the dock."
		else
			ToShow+=" The ferry is out of port."
	}
	if ( CurRoom==104 ) {
		if ( Exits[104].substring(4,5)=="2" )
			ToShow+=" The ferry is in AREE town harbour."
		else
			ToShow+=" The ferry is out at sea."
	}
	ToShow+=NLC+NLC
	ExtraLines+=2

	Noe=0
	EStr=" : "
	for (i=0; i<10; i++) {
		if ( Exits[CurRoom].substring(i,i+1)=="2" ) {
			if ( Noe>0 )
				EStr+=", "
			Noe++
			EStr+=DirName[i]
		}
	}
	preS="are"
	aftS="s"
	if ( Noe==1 ) {
		preS="is"
		aftS=""
	}
	if ( Noe==0 ) {
		NoeS="no"
		EStr=""

	} else

		NoeS=Noe.toString()

	ToShow+="There "+preS+" "+NoeS+" obvious exit"+aftS+EStr+"."+NLC

	ExtraLines++


	ListOfItems(CurRoom)

	aftS="is"

	if ( Noi>1 )

		aftS="are"

	if ( Noi>0 ) {

		ToShow+=NLC+ListStr+" "+aftS+" here."+NLC

		ExtraLines+=2

	}


	Nop=0

	PStr=""
	aftS="is"

	for (i=1; i<17; i++) {

		if ( PerRoom[i]==CurRoom ) {

			if ( Nop>0 ) {

				PStr+="and "

				aftS="are"

			}

			PStr+=PerName[i]+" "

			Nop++

		}

	}

	if ( Nop>0 ) {

		ToShow+=NLC+PStr+aftS+" here."+NLC

		ExtraLines+=2

	}

	ShowText(ToShow)

}



function SlowText() {
//			Shows a series of sentances with periodic time breaks
//			Works for speech 'SpNo'  sentance 'SeNo'

	SStr=SpText[SpNo][SeNo]

	if ( SStr.length<2 ) {
		PlayMode=1
		ExtraLines+=2
		if ( SStr=="*" )

			ShowRoom("")

		ChecksTimer=window.setTimeout("BackGroundChecks()",22500)
		TimesTimer=window.setTimeout("GTimeUpdate()",60000)
		document.Main.comline.value=""
		if ( SStr=="#" )
			EndOfGame(EndGameV)
		return
	}
	if ( SpText[SpNo][SeNo+1].length<2 )
		SStr+=NLC+NLC
	ShowText(SStr)
	SeNo++
	tdelay=(SStr.length*25)+1500
	SpeechTimer=window.setTimeout("SlowText()",tdelay)
	document.Main.comline.value="<<<  ...  ... >>>"
}





function BeginSpeech(sn) {
//			Sets up the Slow text function for speech number sn

	document.Main.comline.value="<<<  ...   ...   >>>"
	PlayMode=2
	SpNo=sn
	SeNo=1
	clearTimeout(ChecksTimer)
	clearTimeout(TimesTimer)
	SpeechTimer=window.setTimeout("SlowText()",150)
}



function TimeDiff(hrs,mins) {
//			Calculates the difference in minutes
//			from the current game time to the time given

	GameMins=(GameTimeH*60)+GameTimeM
	ThoseMins=(hrs*60)+mins
	if ( ThoseMins<0 )
		return -1
	else
		return (GameMins-ThoseMins)
}



function ScoreRoundOff(sv) {
//			Rounds off the score to a multiple of 10

	return Round(sv/10)*10
}



function RecieveObj(obno) {
//			Checks that an object recieved (not 'take'en)
//			is ok to have

	if ( (HoldingFac+HoldVal[obno])>6 ) {
		ShowText("You can't hold onto the "+ObjName[obno]+". You drop it on the floor.")
		ObjRoom[obno]=CurRoom
	} else
		ObjRoom[obno]=0
}




//------------------------------------------------
//		START GAME FUNCTIONS
//------------------------------------------------


function BeginGame() {
//			Sets general variables and preforms start-up routine

	PlayMode=1
	BanM=-1
	BanH=0
	ZanM=-1
	ZanH=0
	SinM=-1
	SinH=0

	for (j=1; j<1500; j+=2) {
		j--
	}

	NewPage()
	document.Main.comline.value=""

	ChecksTimer=window.setTimeout("BackGroundChecks()",22500)
	TimesTimer=window.setTimeout("GTimeUpdate()",60000)
}



function CheckStr(codeval) {
//			Produces a 2 digit check string

	tot=0
	for (pos=0; pos<68; pos++) {
		tot=tot+CodingStr.indexOf(codeval.substring(pos,pos+1))
	}
	ttot=0
	while ( tot>35 ) {
		tot-=36
		ttot++
	}
	utot=tot

	return ( CodingStr.substring(utot,utot+1) ) + ( CodingStr.substring(ttot,ttot+1) )
}



function Decode2dNo(nv,pos) {
//			Decode 2 digit value back to number

	tv=CodingStr.indexOf(nv.substring(pos,pos+1))
	bv=CodingStr.indexOf(nv.substring(pos+1,pos+2))
	return (tv*36)+bv
}

function OldGame() {
//			Perform checks on password

	if ( PlayMode!=0 ) {
		alert(NLC+"To begin another game, reload the page.")
		return
	}

	passStr=document.top.passtext.value
	if ( passStr.length!=69 ) {
		alert("Password is of incorrect length")
		return
	}

	CodingStr=""
	for (i=1; i<6; i++) {
		if ( passStr.substring(8,9)==CrackerStr[i].substring(0,1) )
			CodingStr=CrackerStr[i]
	}

	if ( CodingStr=="" ) {
		alert("Password is invalid")
		return
	}

	Code=passStr.substring(0,67)

	if ( CheckStr(Code)!=passStr.substring(67,69) ) {
		alert("Password is invalid")
		return
	}

	CurRoom=Decode2dNo(Code,0)
	Coins=(CodingStr.indexOf(Code.substring(2,3)))-30
	Score=Decode2dNo(Code,3)*10
	GameTimeH=CodingStr.indexOf(Code.substring(5,6))
	tmins=(CodingStr.indexOf(Code.substring(6,7)))-21
	umins=(CodingStr.indexOf(Code.substring(7,8)))-5
	GameTimeM=(tmins*10)+umins

	curp=9
	for (i=1; i<30; i++) {
		ObjRoom[i]=Decode2dNo(Code,curp)
		curp+=2
	}

	HoldingFac=0
	for (i=1; i<30; i++) {
		if ( ObjRoom[i]==0 )
			HoldingFac+=HoldVal[i]
	}

	TotalMins=(60*GameTimeH)+GameTimeM
	NoOfQ=Round((TotalMins*60000)/22500)

	CyberHrs=5
	while ( NoOfQ>95 ) {
		NoOfQ-=96
	}
	while ( NoOfQ>3 ) {
		NoOfQ-=4
		CyberHrs++
		if ( CyberHrs>23 )
			CyberHrs=0
	}
	CyberMins=NoOfQ*15

//		Perform start-up checks
	if ( CurRoom==38 )
		CurRoom=37
	if ( ObjRoom[22]==148 )
		Exits[31]="2020000000"
	if ( ObjRoom[25]==148 )
		Exits[55]="2020000000"
	if ( ObjRoom[18]==148 ) {
		Exits[137]="0000002002"
		DesStr[137]="You are inside a huge room, a dim light shines in from above. In the centre of the room is a large metal panel proped open over a hole by an iron bar. A set of ladders descends down into a smaller room below."
	}
	if ( ObjRoom[17]==138 ) {
		TakeStr[17]="The jug is too heavy."
		DescpStr[17]="The jug is full of water and keeps the stone aloft."
		Exits[138]="0020000020"
		DesStr[138]="You find yourself in a small room deep underground. A ladder ascends one wall allowing you to climb back out. A large stone sits hanging over a passage entrance on the eastern side."
	}
	if ( ObjRoom[14]==148 ) {
		Exits[120]="0020002010"
		Exits[122]="0020002000"
		DesStr[120]="The road from AREE town ends here. To the east it leads back to the town centre, whilst the entrance to the mine is over to the west. Above the entrance is a ledge, possibly where tools and equipment was stored."
		DesStr[122]="You stand just inside the mine. To the east you can see the light guiding the way out. To the west the tunnel plunges into total darkness."
	}
	if ( CurRoom==130)
		CurRoom=129
	if ( ( CurRoom>112 ) & ( CurRoom<117 ) )
		CurRoom=112
	if ( ObjRoom[8]>147 )
		PerRoom[9]=149
	if ( ObjRoom[25]==148 )
		PerRoom[6]=149
	if ( ObjRoom[3]==148 )
		PerRoom[1]=149
	if ( ObjRoom[28]==148 ) {
		PerRoom[1]=61
		PerDesStr[1]="Porin lies resting on a hospital bed. His broken limbs and wounds are well bandaged and the cuts on his face beginning to heal."
	}
	if ( ObjRoom[6]==148 )
		PerRoom[3]=149
	if ( ObjRoom[12]==148 ) {
		PerDesStr[16]="The baby girl lies fast asleep hugging her teddy bear."
		DesStr[111]="You stand inside an amusement arcade. This is where adult AREE send their children to so they can have a break from parenthood. That's why the room is filled with young AREE enjoying themselves, playing games like 'Zap the marshmellow'."
	}
	if ( ObjRoom[12]!=149 )
		DescpStr[39]="A large wooden closset."
	if ( ObjRoom[25]!=149 )

		DescpStr[37]="It has four drawers, one is locked, the other three are empty."
	if ( ObjRoom[26]!=149 )
		DescpStr[38]="It is made of SilicoWood, a heavy silicon compound. It has two drawers filled with documents addressed to the Governor."
	if ( ( CyberHrs>6 ) & ( CyberHrs<18 ) ) {
		Exits[37]="2020020000"
		Exits[129]="2002020200"
		Exits[112]="0020200200"
	}
	if ( ( CyberHrs>5 ) & ( CyberHrs<21 ) ) {
		TH=6
		TM=0
		FerryNo=1
		while ( !(( TH==CyberHrs ) & ( TM==CyberMins )) ) {
			FerryNo++
			if ( FerryNo==12 )
				FerryNo=0
			TM+=15
			if ( TM>45 ) {
				TM=0
				TH++
			}
		}

		if ( ( FerryNo>0 ) & ( FerryNo<11 ) ) {
			Exits[90]="0020202000"
			Exits[91]="0020000200"
		}
		if ( ( FerryNo>4 ) & ( FerryNo<7 ) ) {
			Exits[100]="2002020000"
			Exits[104]="2000202000"
		}
	} else
		FerryNo=0


	BeginGame()
	ShowRoom("")
}



function NewGame() {
//			Sets up a new game

	if ( PlayMode!=0 ) {
		alert(NLC+"To begin another new game, reload the page.")
		return
	}

	CurRoom=69
	Score=0
	Coins=0
	HoldingFac=0
	CyberMins=0
	CyberHrs=5
	GameTimeH=0
	GameTimeM=0
	FerryNo=0

	BeginGame()
	BeginSpeech(28)
}




//------------------------------------------------
//		CONTROL FUNCTIONS
//------------------------------------------------


function ComTime() {
//			Shows the player the time

	TimeText="The time in cyberspace is "+CurrTime()
	TimeText+=NLC

	TimeText+="This game is "
	if ( GameTimeH>0 ) {
		ha=""
		if ( GameTimeH!=1 )
			ha="s"
		TimeText+=GameTimeH+" hour"+ha+" and "
	}
	ma=""
	if ( GameTimeM!=1 )
		ma="s"
	TimeText+=GameTimeM+" minute"+ma+" old."
	TimeText+=NLC
	ExtraLines++
	ShowText(TimeText)
}



function ComPoss() {
//			Shows the player what is being carried

	ListOfItems(0)
	if ( Noi==0 )
		ShowText("You are empty handed.")
	else {
		ExtraLines++
		ShowText("You are carrying:"+NLC+ListStr+".")
	}
}



function ComDrop(item) {
//			Handles dropping of an object

	ObjNo=0
	for (i=1; i<40; i++) {
		if ( ( ObjRoom[i]==0 ) & ( ObjName[i].toUpperCase()==item ) )
			ObjNo=i
	}

	if ( ObjNo==0 )
		ShowText("You are not carrying "+item+".")
	else {
		ObjRoom[ObjNo]=CurRoom
		HoldingFac-=HoldVal[ObjNo]
		eS="the "
		if ( ObjNo==28 )
			eS=""
		ShowText("You drop "+eS+ObjName[ObjNo]+".")
	}
}



function ComTake(item) {
//			Handles taking of an object

	ObjNo=0
	for (i=1; i<40; i++) {
		if ( ( ObjRoom[i]==CurRoom ) & ( ObjName[i].toUpperCase()==item ) )
			ObjNo=i
	}

	if ( ObjNo==0 ) {
		ToShow="There is no "+item+" here."
		for (i=1; i<16; i++) {
			sStr="he"
			if ( ( i==2 ) | ( i==10 ) | ( i==11 ) | ( i==16 ) )
				sStr="she"
			if ( item==PerName[i].toUpperCase() )
				ToShow="You try to take "+PerName[i]+", but "+sStr+" resists strongly."
		}
		if ( item=="PORIN" )
			ToShow="He's too heavy to carry."
		if ( item=="XOR" )
			ToShow="You can't take yourself!"
		if ( item==PerName[16].toUpperCase() )
			ToShow="You thinks about taking the baby, but think that might be un-wise."
		ShowText(ToShow)
		return
	}

	if ( TakeStr[ObjNo]!="" ) {
		ShowText(TakeStr[ObjNo])
		return
	}

	if ( HoldingFac+HoldVal[ObjNo]>6 ) {
		ShowText("You are too heavy laiden to carry this item.")
	} else {
		ObjRoom[ObjNo]=0
		HoldingFac+=HoldVal[ObjNo]
		eS="the "
		if ( ObjNo==28 )
			eS=""
		ShowText("You take "+eS+ObjName[ObjNo]+".")
	}	

}



function ComLook(item) {
//			Handles looking at an object

	ObjNo=0
	for (i=1; i<40; i++) {
		if ( ( ( ObjRoom[i]==0 ) | ( ObjRoom[i]==CurRoom ) ) & ( ObjName[i].toUpperCase()==item ) )
			ObjNo=i
	}

	if ( ObjNo==0 ) {
		ToShow = "There is no "+item+" here."
		for (i=1; i<17; i++ )
			if ( PerRoom[i]==CurRoom & item==PerName[i].toUpperCase() )
				ToShow = PerDesStr[i]
		ShowText(ToShow)
	} else {
		eS="a "
		if ( ObjNo==28 )
			eS=""
		ToShow="You look at "+eS+ObjName[ObjNo]+NLC+DescpStr[ObjNo]
		NoOfLines++
		ShowText(ToShow)
		if ( ObjNo==37 ) {
			if ( ObjRoom[25]==149 ) {
				ObjRoom[25]=36
				DescpStr[37]="It has four drawers, one is locked, the other three are empty."
			}
		}
		if ( ObjNo==38 ) {
			if ( ObjRoom[26]==149 ) {
				ObjRoom[26]=56
				DescpStr[38]="It is made of SilicoWood, a heavy silicon compound. It has two drawers filled with documents addressed to the Governor."
			}
		}
		if ( ObjNo==39 ) {
			if ( DescpStr[39]!="A large wooden closset." ) {
				DescpStr[39]="A large wooden closset."
				ObjRoom[8]=43
				if ( ObjRoom[12]==149 )
					ObjRoom[12]=43
			}
		}
	}
}



function BuyItem() {
//			Handles use of coins

	if ( ( CurRoom==38 ) & ( ObjRoom[19]=149 ) & ( Coins>2 ) ) {
		ObjRoom[19]=0
		ShowText("You buy a Wizards Costume for 3 coins.")
		Coins-=3
		return
	}
	if ( ( CurRoom==130 ) & ( ObjRoom[13]=149 ) & ( Coins>0 ) ) {
		ObjRoom[13]=0
		ShowText("You buy a Surf Board for 1 coin.")
		Coins--
		return
	}
	if ( ( CurRoom==109 ) & ( ObjRoom[22]=149 ) & ( Coins>1 ) ) {
		ObjRoom[22]=0
		ShowText("You buy Antrif a drink for 2 coins.")
		Coins-=2
		BeginSpeech(49)
		return
	}
	if ( Coins>0 )
		ShowText("You waft your money about, but no-one pays any attention.")
	else
		ShowText("You dig deep into your pockets but find no cash.")
}



function ComUse(item) {
//			Handles use of an object

	if ( item=="COINS" ) {
		BuyItem()
		return
	}

	ObjNo=0
	for (i=1; i<40; i++) {
		if ( ( ObjRoom[i]==0 ) & ( ObjName[i].toUpperCase()==item ) )
			ObjNo=i
	}

	if ( ObjNo==0 ) {
		ShowText("You do not have "+item+".")
		return
	}

	if  ( ( UseRoom[ObjNo]!=CurRoom ) | ( ( ObjNo==5 ) & ( ObjRoom[6]!=11 ) ) ) {
		preS="the "
		if ( ObjNo==28 )
			preS=""
		ShowText("You use "+preS+ObjName[ObjNo]+", but nothing happens.")
		return
	}

	ShowText(UseStr[ObjNo])
	Score+=UsePts[ObjNo]
	if ( ( ObjNo!=19 ) & ( ObjNo!=24 ) ) {
		HoldingFac-=HoldVal[ObjNo]
		ObjRoom[ObjNo]=148
	}

	if ( ObjNo==1 ) {
		ObjRoom[3]=74
		ObjRoom[2]=148
	}
	if ( ObjNo==3 ) {
		RecieveObj(28)
		PerRoom[1]=149
	}
	if ( ObjNo==4 ) {
		CurRoom=2
		ObjRoom[4]=148
		BeginSpeech(1)
	}
	if ( ObjNo==5 ) {
		RecieveObj(6)
	}
	if ( ObjNo==6 ) {
		if ( ObjRoom[5]==148 ) {
			mdiff=TimeDiff(ZanH,ZanM)
			if ( mdiff>60 ) {
				BeginSpeech(48)
				ObjRoom[6]=149
				PerRoom[3]=149
				return
			}
			if ( mdiff==-1 )
				mdiff=15
			if ( mdiff<=2 )
				Score+=190
			else
				Score+=ScoreRoundOff(((60-mdiff)*190)/58)
			BeginSpeech(2)
			PerRoom[3]=149
			ObjRoom[6]=148
			return
		}
		BeginSpeech(3)
		ObjRoom[6]=149
		EndGameV=1
		return
	}
	if ( ObjNo==7 ) {
		mdiff=(GameTimeH*60)+GameTimeM
		if ( mdiff<=4 )
			Score+=120
		else {
			if ( mdiff>60 )
				Score+=20
			else
				Score+=ScoreRoundOff(((60-mdiff)*100)/56)+20
		}

		BeginSpeech(4)
		return
	}
	if ( ObjNo==8 ) {
		PerRoom[9]=149
		mdiff=TimeDiff(BanH,BanM)
		if ( mdiff>20 ) {
			ObjRoom[8]=149
			BeginSpeech(5)
			return
		}
		if ( mdiff==-1 )
			mdiff=8
		if ( mdiff<2 )
			Score+=160
		else
			Score+=ScoreRoundOff(((20-mdiff)*160)/18)
		if ( mdiff<10 )
			BeginSpeech(6)
		else
			BeginSpeech(7)
		CurRoom=50
		return
	}
	if ( ObjNo==9 ) {
		RecieveObj(10)
	}
	if ( ObjNo==10 ) {
		mdiff=TimeDiff(SinH,SinM)
		if ( mdiff>120 ) {
			ObjRoom[10]=149
			BeginSpeech(9)
			return
		}
		if ( mdiff==-1 )
			mdiff=25
		if ( mdiff<5 )
			Score+=190
		else
			Score+=ScoreRoundOff(((120-mdiff)*190)/115)
		BeginSpeech(8)
		return
	}
	if ( ObjNo==11 )
		RecieveObj(12)
	if ( ObjNo==12 ) {
		PerDesStr[16]="The baby girl lies fast asleep hugging her teddy bear."
		DesStr[111]="You stand inside an amusement arcade. This is where adult AREE send their children to so they can have a break from parenthood. That's why the room is filled with young AREE enjoying themselves, playing games like 'Zap the marshmellow'."
	}
	if ( ObjNo==13 ) {
		BeginSpeech(26)
		CurRoom=124
	}
	if ( ObjNo==14 ) {
		Exits[120]="0020002010"
		Exits[122]="0020002000"
		DesStr[120]="The road from AREE town ends here. To the east it leads back to the town centre, whilst the entrance to the mine is over to the west. Above the entrance is a ledge, possibly where tools and equipment were stored when miners worked here.."
		DesStr[122]="You stand just inside the mine. To the east you can see the light guiding the way out. To the west the tunnel plunges into total darkness."
	}
	if ( ObjNo==15 ) {
		ObjRoom[16]=128
	}
	if ( ObjNo==16 ) {
		Coins+=6
	}
	if ( ObjNo==17 ) {
		TakeStr[17]="The jug is too heavy."
		DescpStr[17]="The jug is full of water and keeps the stone aloft."
		ObjRoom[17]=138
		Exits[138]="0020000020"
		DesStr[138]="You find yourself in a small room deep underground. A ladder ascends one wall allowing you to climb back out. A large stone sits hanging over a passage entrance on the eastern side."
	}
	if ( ObjNo==18 ) {
		Exits[137]="0000002002"
		DesStr[137]="You are inside a huge room, a dim light shines in from above. In the centre of the room is a large metal panel proped open over a hole by an iron bar. A set of ladders descends down into a smaller room below."
	}
	if ( ObjNo==19 ) {
		CurRoom=144
		ShowRoom("")
	}
	if ( ObjNo==20 ) {
		ObjRoom[21]=116
	}
	if ( ObjNo==21 ) {
		RecieveObj(29)
		BeginSpeech(10)
		return
	}
	if ( ObjNo==22 ) {
		Exits[31]="2020000000"
	}
	if ( ObjNo==23 ) {
		ObjRoom[24]=23
	}
	if ( ObjNo==24 ) {
		CurRoom=35
		ShowRoom("")
	}
	if ( ObjNo==25 ) {
		if ( ObjRoom[29]==0 ) {
			BeginSpeech(12)
			PerRoom[6]=149
			Exits[55]="2020000000"
			return
		}
		BeginSpeech(11)
		EndGameV=1
		return
	}
	if ( ObjNo==26 ) {
		CurRoom=7
		ShowRoom("")
	}
	if ( ObjNo==28 ) {
		mdiff=(GameTimeH*60)+GameTimeM
		if ( mdiff>10 ) {
			BeginSpeech(14)
			ObjRoom[28]=149
			return
		}
		if ( mdiff<2 )
			Score+=200
		else
			Score+=ScoreRoundOff(((10-mdiff)*200)/8)
		BeginSpeech(13)
		PerRoom[1]=61
		PerDesStr[1]="Porin lies resting on a hospital bed. His broken limbs and wounds are well bandaged and the cuts on his face beginning to heal."
	}
}



function ComTalkTo(person) {
//			Handles conversations

	if ( person=="XOR" ) {
		BeginSpeech(50)
		return
	}

	PerNo=0
	for (i=1; i<17; i++) {
		if ( ( person.toUpperCase()==PerName[i].toUpperCase() ) & ( PerRoom[i]==CurRoom ) )
			PerNo=i
	}

	if ( PerNo==0 ) {
		ShowText(person+" is not here.")
		return
	}

	if ( PerNo==1 ) {
		if ( ObjRoom[28]==148 )
			BeginSpeech(47)
		else
			ShowText("  Porin is unconscious.")
	}
	if ( PerNo==2 ) {
		if ( ObjRoom[6]==11 )
			BeginSpeech(21)
		else
			BeginSpeech(22)
	}
	if ( PerNo==3 ) {
		if ( ObjRoom[6]==0 )
			BeginSpeech(29)
		else {
			if ( ObjRoom[4]==149 ) {
				BeginSpeech(30)

				RecieveObj(4)
				if ( ZanM<0 ) {
					ZanM=GameTimeM
					ZanH=GameTimeH
				}
			} else
				BeginSpeech(31)
		}
	}
	if ( PerNo==4 ) {
		if ( ObjRoom[6]==148 )
			ShowText("BREKON: Thanks for your help in solving Zandrik's case. Maybe you should become a PI?")
		else
			BeginSpeech(27)
	}
	if ( PerNo==5 ) {
		if ( ObjRoom[20]==149 ) {
			BeginSpeech(44)
			RecieveObj(20)
		} else {
			if ( ObjRoom[21]==148 )
				ShowText("MERLIN: Thanks for your assistance, fellow wizard.")
			else
				ShowText("MERLIN: Please help us, and defrost the artifact.")
		}
	}
	if ( PerNo==6 ) {
		if ( ( ObjRoom[13]==149 ) & ( Coins==0 ) ) {
			Coins++
			BeginSpeech(34)
		} else
			BeginSpeech(35)
	}
	if ( PerNo==7 ) {
		if ( ObjRoom[13]==149 )
			BeginSpeech(42)
		else {
			BeginSpeech(43)
			CurRoom=129
		}
	}
	if ( PerNo==8 ) {
		if ( ObjRoom[22]==149 )
			ShowText("ANTRIF: I is sou parched, I thin' I need anouther drink.")
		else
			ShowText("  Antrif is on the floor unconscious.")
	}
	if ( PerNo==9 ) {
		BeginSpeech(25)
		if ( BanM<0 ) {
			BanM=GameTimeM
			BanH=GameTimeH
		}
	}
	if ( PerNo==10 ) {
		if ( ObjRoom[19]==149 )
			BeginSpeech(15)
		else
			BeginSpeech(16)
	}
	if ( PerNo==11 ) {
		if ( ObjRoom[10]==148 )
			BeginSpeech(17)
		if ( ObjRoom[10]==149 )
			ShowText("SINGER: *SOB* My audition is a failiure, I'm no good.")
		if ( ObjRoom[10]<148 ) {
			ShowText("SINGER: Please help me, my singing act is ruined, I have nothing to shatter when I sing my high note.")
			if ( SinM<0 ) {
				SinM=GameTimeM
				SinH=GameTimeH
			}
		}
	}
	if ( PerNo==12 ) {
		if ( ObjRoom[26]==148 ) {
			ShowText("DOORMAN: Nice to see you again Governor. Go right through.")
			CurRoom=7
			ShowRoom("")
		} else
			ShowText("DOORMAN: Only performers and their guests are allowed backstage.")
	}
	if ( PerNo==13 ) {
		if ( ObjRoom[4]==148 ) {
			ShowText("BOUNCER: Oh, hello again. Be my guest, go right on in.")
			CurRoom=2
			ShowRoom("You enter the night club"+NLC+NLC)
		} else
			ShowText("BOUNCER: Sorry, no-one is allowed entry - not since the shooting of last week.")
	}
	if ( PerNo==14 ) {
		if ( ObjRoom[7]==148 )
			ShowText("HERMIT: Thank thee. I feel so much better now.")
		else
			BeginSpeech(33)
	}
	if ( PerNo==15 ) {
		if ( ObjRoom[22]==149 )
			BeginSpeech(38)
		else
			ShowText("MILANRO: Would you like a drink?")
	}
	if ( PerNo==16 ) {
		if ( ObjRoom[12]==148 )
			BeginSpeech(36)
		else
			BeginSpeech(37)
	}

}



function Encode2dNo(nv) {
//			Encodes a number into 2 digits

	tv=0
	while ( nv>35 ) {
		nv-=36
		tv++
	}
	bv=nv
	return ( CodingStr.substring(tv,tv+1) ) + ( CodingStr.substring(bv,bv+1) )
}

function ComSave() {
//			Presents user with a password and quits the game

	clearTimeout(ChecksTimer)
	clearTimeout(TimesTimer)

	CodingStr=CrackerStr[Round(RandNo()/2)+1]
	Code=""

	Code+=Encode2dNo(CurRoom)
	Code+=CodingStr.substring(Coins+30,Coins+31)
	Code+=Encode2dNo(Score/10)

	mins=GameTimeM
	tmins=0
	while ( mins>9 ) {
		mins-=10
		tmins++
	}
	umins=mins

	Code+=CodingStr.substring(GameTimeH,GameTimeH+1)
	Code+=CodingStr.substring(tmins+21,tmins+22)
	Code+=CodingStr.substring(umins+5,umins+6)
	Code+=CodingStr.substring(0,1)

	for (i=1; i<30; i++)
		Code+=Encode2dNo(ObjRoom[i])

	Code+=CheckStr(Code)

	window.scroll(0,0)
	document.top.passtext.value=Code

	ToShow=NLC+"The password for this point in the game is now shown in the password text box"+NLC
	ToShow+=NLC+"You should make a note of this so it can be re-entered when you return."+NLC+"You are strongly advised to copy, paste and save the password in a text file"+NLC+" on your computer so that no mistakes can be made when you re-enter it"+NLC
	ToShow+=NLC+"*************************************************************"+NLC+NLC+"                       Thanks for playing"
	ToShow+=NLC+"                        SCARLET NIGHTS"
	alert(ToShow)

	document.Main.playtext.value=""
	PlayMode=3
	document.top.passtext.select()
}



function MoveInDir(d) {
//			Handle player movement

	if ( eval(Exits[CurRoom].substring(d,d+1))>0 ) {
		en=eval(ExitStrRef[CurRoom].substring(d,d+1))
		esStr=ExitShowStr[en]
		if ( en==0 )
			esStr+=DirName[d]+"."
		esStr+=NLC+NLC
		ExtraLines++

		RStr=ExitRoom[CurRoom].substring(d*3,(d+1)*3)
		if ( RStr.substring(0,1)=="0" )
			RStr=RStr.substring(1,3)
		CurRoom=eval(RStr)

		ShowRoom(esStr)

		if ( ( CurRoom==32 ) & ( CyberHrs>7 ) & ( CyberHrs<18 ) ) {
			ShowText("You enter Area51 in broad daylight. You are caught and jailed for treason.")
			PlayMode=3
			EndOfGame(1)
			return
		}
		if ( ( CurRoom==96 ) & ( ObjRoom[7]==0 ) & ( CyberHrs>6 ) & ( CyberHrs<18 ) ) {
			ShowText("As you emerge into the sunlight, the block of ice melts.")
			ObjRoom[7]=149
		}

		if ( CurRoom==1 )
			BeginSpeech(18)

	} else
		ShowText("There does not appear to be an exit in that direction.")
}



//	Commands control procedures


function OtherComm(ComText) {
//			Handle functional commands

	if ( ComText=="COINS" ) {
		if ( Coins==0 )
			ShowText("You are totally skint.")
		else {
			sStr="s"
			if ( Coins==1 )
				sStr=""
			ShowText("You have "+Coins+" coin"+sStr+".")
		}
		return
	}
	if ( ComText=="TIME" ) {
		ComTime()
		return
	}
	if ( ComText=="SCORE" ) {
		if ( Score==0 )
			ShowText("So far your quest has been pointless.")
		else
			ShowText("You have scored "+Score+" points.")
		return
	}
	if ( ComText=="POSS" ) {
		ComPoss()
		return
	}
	if ( ComText=="LOOK" ) {
		ShowRoom("")
		return
	}
	if ( ComText=="SAVE" ) {
		ComSave()
		return
	}


//		More complex algorithms

	if ( ComText.substring(0,7)=="TALK TO" ) {
		subText=ComText.substring(8,ComText.length)
		ComTalkTo(subText)
		return
	}

	VerbText=ComText.substring(0,4)
	if ( VerbText=="USE " ) {
		subText=ComText.substring(4,ComText.length)
		ComUse(subText)
		return
	}
	subText=ComText.substring(5,ComText.length)
	if ( VerbText=="TAKE" ) {
		ComTake(subText)
		return
	}
	if ( VerbText=="LOOK" ) {
		ComLook(subText)
		return
	}
	if ( VerbText=="DROP" ) {
		ComDrop(subText)
		return
	}


	ShowText("You "+ComText.toLowerCase()+", but it does not help your cause.")
}



function ProCommand() {
//			Process a commands input by player

	ComText=document.Main.comline.value.toUpperCase()
	if ( ComText=="" )
		return

	if ( PlayMode==2 ) {
		document.Main.comline.value="Please Wait"
		return
	}
	if ( PlayMode!=1 ) {
		document.Main.comline.value="Press the NEW GAME button to begin."
		return
	}


	if ( CurRoom==1 ) {
		if ( ComText=="DEFECT" ) {
			BeginSpeech(19)
			EndGameV=2
		}
		if ( ComText=="DIE" ) {
			if ( ObjRoom[29]==0 ) {
				if ( ObjRoom[27]==0 ) {
					ObjRoom[27]=148
					BeginSpeech(20)
					EndGameV=3
				} else {
					BeginSpeech(24)
					EndGameV=2
				}
			} else {
				BeginSpeech(23)
				EndGameV=2
			}
		}
		return
	} else {

		ThisDir=-1
		for (i=0; i<10; i++) {
			if ( ComText==DirText[i] )
				ThisDir=i
		}

		if ( ThisDir>-1 )
			MoveInDir(ThisDir)
		else
			OtherComm(ComText)

	}

	document.Main.comline.value=""
}
