//
// $Id: example.js,v 1.2 2008/10/10 19:34:39 pwh Exp $
//
// A script to implement interactive Sudoku examples.
//

function nextStep ( ex )

{
   if ( exActive ) {

	ex.nextStep ()
   }
}

function prevStep ( ex )

{
   if ( exActive ) {

	ex.prevStep ()
   }
}

function exGetCell ( rc )

{
   return ( rc.charAt ( 1 ) + rc.charAt ( 3 ) )
}

function exNext ()

{
   if ( this.step < this.lastStep ) {

	var action

	if ( this.step == 0 ) {

		document.getElementById ( "prevButton" + this.name ).disabled
								= false
		document.getElementById ( "prevStep" + this.name ).style.color
								= "black"
	}

	if ( ++this.step == this.lastStep ) {

		document.getElementById ( "nextButton" + this.name ).disabled
								= true
		document.getElementById ( "nextStep" + this.name ).style.color
								= "gray"
	}

	document.getElementById ( "commentary" + this.name ).innerHTML
						= this.comments [this.step]

	for ( action in this.actions[this.step] ) {

		var parts = this.actions [this.step][action].split( " " )

		document.getElementById ( this.name
				+ this.getCell ( parts [0] )
				+ parts [2] ).className = parts [1]
	}
   }
}

function exPrev ()

{
   if ( this.step > 0 ) {

	var action

	if ( this.step != 2 ) {

		for ( action in this.actions[this.step] ) {

			var parts
				= this.actions [this.step][action].split( " " )

			document.getElementById ( this.name
				+ this.getCell ( parts [0] )
				+ parts [2] ).className = "blank"
		}
	}

	if ( this.step == this.lastStep ) {

		document.getElementById ( "nextButton" + this.name ).disabled
								= false
		document.getElementById ( "nextStep" + this.name ).style.color
								= "black"
	}

	if ( --this.step == 0 ) {

		document.getElementById ( "prevButton" + this.name ).disabled
								= true
		document.getElementById ( "prevStep" + this.name ).style.color
								= "gray"
	}

	if ( this.step == 1 ) {

		for ( action in this.actions[this.step] ) {

			var parts
				= this.actions [this.step][action].split( " " )

			document.getElementById ( this.name
				+ this.getCell ( parts [0] )
				+ parts [2] ).className = parts [1]
		}
	}

	document.getElementById ( "commentary" + this.name ).innerHTML
						= this.comments [this.step]
   }
}

function Example ( name, comments, actions )

{
   this.name = name
   this.comments = comments
   this.actions = actions
   this.step = 0
   this.lastStep = comments.length - 1
   this.nextStep = exNext
   this.prevStep = exPrev
   this.getCell = exGetCell
}

