var blankX=1, blankY=1, diffX, diffY, from, to, current_location=new Array(0, 3, 8, 7, 2, 6, 4, 1, 5), target_location=new Array(0, 1, 2, 3, 4, 5, 6, 7, 8), save_location, attempts=0;

//****************
function checkField(entry,message) {  // check if a field is blank and send a message if supplied 
//****************

var find=entry.search(/\w/)
var entryValid=true

if (find<0) {
  if (message!="") {
    alert(message);
  }
  entryValid=false 
}
return entryValid
}

// ************************
function validNumber(entry) {  // check if a field is a number
// ************************

var entryValue=entry
var floatValue

floatValue=parseFloat(entryValue)

if (isNaN(floatValue)) {
  return false
} 
else 
{
  return true	
}
}

//****************
function checkLogin(form) {  // check that the user number and password have been entered
//****************

var formValid=true

if (!checkField(form.userNumber.value,"Please enter a user number.")) {
  formValid=false;
}
else
{
  if (!checkField(form.userPassword.value,"Please enter a user password.")) {
    formValid=false;
  }
}
return formValid;
}


//****************
function checkLinkDetails(form) {  // check that the Link details have been entered
//****************

var formValid=true

if (!checkField(form.linkName.value,"Please enter a name.")) {
  formValid=false;
}
else
{
  if (!checkField(form.linkURL.value,"Please enter a URL.")) {
    formValid=false;
  }
  else
  {
    if (!checkField(form.linkDetails.value,"Please enter link details.")) {
      formValid=false;
    }
    else
    {
      if (!checkField(form.category.value,"Please enter a category.")) {
        formValid=false;
      }
      else
      {
        if (!checkField(form.subCategory.value,"Please enter a sub-category.")) {
          formValid=false;
        }
		else
        {
		  if (!checkField(form.linkDate.value,"Please enter a date in DD/MM/YYYY format.")) {
          formValid=false;
          }
		}
      }
    }
  }
}
return formValid;
}

//****************
function checkCategoryDetails(form) {  // check that the Category details have been entered
//****************

var formValid=true

if (!checkField(form.categoryDescription.value,"Please enter a description.")) {
  formValid=false;
}
else
{
  if (!checkField(form.categoryDetails.value,"Please enter category details.")) {
    formValid=false;
  }
}
return formValid;
}

//****************
function checkSubCategoryDetails(form) {  // check that the sub-category details have been entered
//****************

var formValid=true

if (!checkField(form.subCategoryDescription.value,"Please enter a description.")) {
  formValid=false;
}
else
{
  if (!checkField(form.subCategoryDetails.value,"Please enter sub-category details.")) {
    formValid=false;
  }
}
return formValid;
}


//****************
function checkUserDetails(form) {  // check that the user details have been entered
//****************

var formValid=true

if (!checkField(form.userEmail.value,"Please enter an email address.")) {
  formValid=false;
  }
  else
  {
  if (!checkField(form.userPassword.value,"Please enter a password.")) {
    formValid=false;
  }
}
return formValid;
}
 

//****************
function checkPageDetails(form) {  // check that the page details have been entered
//****************

var formValid=true

if (!checkField(form.pageDescription.value,"Please enter a description.")) {
  formValid=false;
}
else
{
  if (!checkField(form.pageAddress.value,"Please enter an address.")) {
    formValid=false;
  }
}
return formValid;
}

//****************
function todayStr() { // returns today's date
//****************
var today=new Date()
return today.getDate()+"/"+(today.getMonth()+1)+"/"+today.getYear() 

}

//****************
function checkMailingListEntry(form) {  // check that the user details have been entered
//****************

var formValid=true

if (!checkField(form.mailingListForename.value,"Please enter a forname.")) {
  formValid=false;
}
else
{
  if (!checkField(form.mailingListSurname.value,"Please enter a surname.")) {
    formValid=false;
  }
  else
  {
    if (!checkField(form.mailingListEmailAddress.value,"Please enter an e-mail address.")) {
      formValid=false;
    }
  } 
}
return formValid;
}

//****************
function checkVisitorsBookEntry(form) {  // check that the user details have been entered
//****************

var formValid=true

if (!checkField(form.visitorsBookForename.value,"Please enter a forname.")) {
  formValid=false;
}
else
{
  if (!checkField(form.visitorsBookSurname.value,"Please enter a surname.")) {
    formValid=false;
  }
  else
  {
    if (!checkField(form.visitorsBookLocation.value,"Please enter a location.")) {
      formValid=false;
    }
	else
    {
      if (!checkField(form.visitorsBookEntry.value,"Please enter a comment.")) {
        formValid=false;
      }
	}
  } 
}

if (formValid==false) {
  return formValid;
}  
 
for (var counter=0;counter<form.elements.length;counter++) {

  if ((form.elements[counter].type=="radio") && (form.elements[counter].value=="yes") && (form.elements[counter].checked)) {

    if (!checkField(form.visitorsBookEmailAddress.value,"Please enter an e-mail address.")) {
      formValid=false;
	  return formValid;
    }
  }	
}
}

// ************************
function checkAnswers_page1() {  // checks answers to page 1
// ************************

  var score=0;

  if (document.forms[0].question1[0].checked)score++;  
  if (document.forms[0].question2[1].checked)score++;  
  if (document.forms[0].question3[2].checked)score++;  
  if (document.forms[0].question4[1].checked)score++;  
  if (document.forms[0].question5[2].checked)score++;  

  attempts++ 
  
if (score==5) MM_setTextOfLayer('intro','','Congratulations! <a href="quiz_page2.htm" class="navLink">Try some more?</a>');

set_quiz_scoreboard(attempts, score)
} 

// ************************
function checkAnswers_page2() {  // checks answers to page 2
// ************************

  var score=0;

  if (document.forms[0].question6[2].checked)score++;  
  if (document.forms[0].question7[0].checked)score++;  
  if (document.forms[0].question8[2].checked)score++;  
  if (document.forms[0].question9[1].checked)score++;  
  if (document.forms[0].question10[2].checked)score++;  

  attempts++ 

if (score==5) MM_setTextOfLayer('intro','','Congratulations! <a href="quiz_page3.htm" class="navLink">Try some more?</a>');

set_quiz_scoreboard(attempts, score)
}

// ************************
function checkAnswers_page3() {  // checks answers to page 3
// ************************

  var score=0;

  if (document.forms[0].question1[0].checked)score++;  
  if (document.forms[0].question2[1].checked)score++;  
  if (document.forms[0].question3[2].checked)score++;  
  if (document.forms[0].question4[1].checked)score++;  
  if (document.forms[0].question5[2].checked)score++;  

  attempts++ 

if (score==5) MM_setTextOfLayer('intro','','Congratulations! <a href="quiz_page4.htm" class="navLink">Try some more?</a>');

set_quiz_scoreboard(attempts, score)
} 

// ************************
function checkAnswers_page4() {  // checks answers to page 3
// ************************

  var score=0;

  if (document.forms[0].question1[2].checked)score++;  
  if (document.forms[0].question2[1].checked)score++;  
  if (document.forms[0].question3[0].checked)score++;  
  if (document.forms[0].question4[2].checked)score++;  
  if (document.forms[0].question5[1].checked)score++;  

  attempts++ 

if (score==5) MM_setTextOfLayer('intro','','Congratulations! Check our quiz again in the future as the questions will be changed on a regular basis.');  

set_quiz_scoreboard(attempts, score)
} 

// ************************
function set_quiz_scoreboard (attempts, score) {// set the scoreboard
// ************************

if (attempts==1) MM_setTextOfLayer('scoreboard','','Your score from 1 attempt is:')
else MM_setTextOfLayer('scoreboard','','Your score from ' + attempts + ' attempts is:')
 
 for (var i=0;i<score;i++){
   if ((obj=MM_findObj("image_score"+(i+1)))!=null) obj.src="images/leaf.gif"
 }
 for (i;i<5;i++){
   if ((obj=MM_findObj("image_score"+(i+1)))!=null) obj.src="images/dummy.gif"
 }		
}

// ************************
function move(tileX, tileY) {  // 
// ************************
  
  diffX=tileX-blankX;
  diffY=tileY-blankY;
  to=((tileY-1)*3)+tileX-1;
  from=((blankY-1)*3)+blankX-1;
  
  if ((diffX==0) && (diffY==0)) alert("That's not a valid move!")
  else
    {
	if (((diffX==0) && ((diffY==1) || (diffY==-1))) || ((diffY==0) && ((diffX==1) || (diffX==-1)))) 
	{
	blankX=tileX;
	blankY=tileY;
	document.box[from].src=document.box[to].src;
    document.box[to].src="images/tile1.jpg";
	save_location=current_location[from];
	current_location[from]=current_location[to];
	current_location[to]=save_location;
    attempts++; 
	set_game_scoreboard();	
	}
	else alert("That's not a valid move!")
    }
	
  }

// ************************
function set_game_scoreboard() {  // 
// ************************
  
  var score=0;
  
  for (var i=0;i<9;i++){
    if (target_location[i]==current_location[i]) score++
  }
  if (score==9) MM_setTextOfLayer('intro','','Congratulations! You finished the game in ' + attempts + ' moves.')
  if (attempts==1) MM_setTextOfLayer('scoreboard','','You have had 1 move.')
  else MM_setTextOfLayer('scoreboard','','You have had ' + attempts + ' moves.')
}

// ************************
function MM_setTextOfLayer(objName,x,newText) { //v3.0
// ************************

  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (navigator.appName=='Netscape') {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}

// ************************
function checkfield(entry, message) {  // check if a field is blank and send a message if supplied 
// ************************

var entryValue=entry
var entryValid=true

if (entryValue=="") {
   if (message!="") {
   alert(message);
   }
 entryValid=false 
 }
return entryValid
}

// ************************
function extLink(selObj){ //v3.0 Go to a link
// ************************
  if (selObj.options[selObj.selectedIndex] != 0) {
    New=window.open();
  }
  eval("New.location='"+selObj.options[selObj.selectedIndex].value+"'");
  selObj.selectedIndex=0;
}

// ************************
function extLink(selObj){ //v3.0 Go to a link
// ************************
  if (selObj.options[selObj.selectedIndex] != 0) {
    New=window.open();
  }
  eval("New.location='"+selObj.options[selObj.selectedIndex].value+"'");
  selObj.selectedIndex=0;
}

// ************************
function intLink(selObj){ //v3.0
// ************************
    eval("parent.frames[1].location='"+selObj.options[selObj.selectedIndex].value+"'");
    selObj.selectedIndex=0;
}
