/*
404 Source Code - Based on Perfect 404 by Ian Lloyd (accessify.com)
*/
/**
  Initialisation 
  ----------------------------------------------------------
  Place all loads as input strings into the system event 
  chain
  ----------------------------------------------------------*/

if(sysLoadChain) {
 sysLoadChain[sysLoadChain.length] = 'Show404Message();';
}

function Show404Message(){
  var msg = GetErrorString();
  var d404 = document.getElementById('d404::input');
  if(d404 == null) {
  alert(msg);
    return;
  }
  d404.innerHTML = msg;
}
function SayThanks(){
  var input  = document.getElementById('d404::input');
  var output = document.getElementById('d404::output');
  if(input == null || output == null) {
    alert("Thanks for reporting the missing page to us.");
  } else {
    input.className = "unseen";
	output.className = "seen";
  }
  InitExternalLinks();
}
/**
  */

function GetReferrer(){
  var strReferrer=document.referrer.toLowerCase();
  if (location.href.indexOf("noreferrer=true")>=0) strReferrer="";
  return strReferrer;
}

function GetErrorString(){
  var strReferrer = GetReferrer();
  var str='';
  if (strReferrer.length==0)  { // No Referrer
	str+= "<p>Usually it is because of one of the following reasons:</p>\n";
	str+= "<ul>";
	str+= "  <li>An <strong>out-of-date</strong> bookmark/favourite</li>\n";
	str+= "  <li>A search engine that has an <strong>out-of-date listing</strong> for our site</li>\n";
	str+= "  <li>A <strong>mis-typed address</strong></li>\n";
	str+= "</ul>";
	str+= "We think you will find one of the following links useful:</p>\n";
	str+= GetOptionsString()
  } else {
    var blnSearchReferral = ((strReferrer.indexOf(".looksmart.co")>0)||
		 (strReferrer.indexOf(".ifind.freeserve")>0)||
		 (strReferrer.indexOf(".ask.co")>0)||
		 (strReferrer.indexOf("google.co")>0)||
		 (strReferrer.indexOf("altavista.co")>0)||
		 (strReferrer.indexOf("msn.co")>0)||
		 (strReferrer.indexOf("yahoo.co")>0));
		 
	var blnInsiteReferral = ((strReferrer.indexOf("ghanakeyboards.com")>=0));
	var strSite = "";
	if ( blnSearchReferral ) {  // referrer is a known search engine	
	  var arrSite=strReferrer.split("/");
	  var arrParams=strReferrer.split("?");
	  var strSearchTerms = arrParams[1];
	  arrParams=strSearchTerms.split("&");
	  strSite=arrSite[2];
	  var sQryStr="";		

	  //define what search terms are in use
	  var arrQueryStrings = new Array();
	  arrQueryStrings[0]="q=";	//google, altavista, msn
	  arrQueryStrings[1]="p=";	//yahoo
	  arrQueryStrings[2]="ask=";	//ask jeeves
	  arrQueryStrings[3]="key=";	//looksmart
	  for (i=0;i<arrParams.length;i++)	{//loop through all the parameters in the referring page's URL
		for (q=0;q<arrQueryStrings.length;q++)  {
		  sQryStr = arrQueryStrings[q];
		  if (arrParams[i].indexOf(sQryStr)==0)  {//we've found a search term!
			strSearchTerms = arrParams[i];
			strSearchTerms = strSearchTerms.split(sQryStr);
			strSearchTerms = strSearchTerms[1];
			strSearchTerms = strSearchTerms.replace("+", " ");
		  }
		}
	  }
	  str =  "<p>You did a search on <strong><a href='" + strReferrer;
	  str += "' class='offsite' rel='external' target='_blank'>" + strSite + "</a></strong> for \"<strong>\n";
	  str += strSearchTerms + "</strong>\". However, their index appears to be out of date.</p>\n";
	  str += "<h2>All is not lost!</h2><p>We think that the following are related to \n";
	  str += "your search query:</p>\n";
	  str += "<ul><li><a href='index.html'>history page</a></li>";
	  if ((strSearchTerms.indexOf("keyboard")>=0) ||
 	      (strSearchTerms.indexOf("download")>=0))	{
        str+= "<li><a href='/downloads/index.html'>downloadable keyboards</a></li>\n";
	  }
	  if(strSearchTerms.indexOf("help")>=0)  {
		str+= "<li><a href='/support/index.html'>support</a></li>\n";
	  }
	  str += "</ul>";
	  str += "<p>However, you can also check-out one of these pages:</p>\n";
	  str += GetOptionsString();
	}  else { /* blnSearchReferral == false*/
	  if( ! blnInsiteReferral) { /* coming from some other broken referrer*/
	    strSite = strReferrer;
		strSite = strSite.split("/");
		strSite = strSite[2];
		str =  "<p>You were incorrectly referred to this page by: <strong><a href='\n";
		str += strReferrer + "' class='offsite' rel='external'>" + strSite + "</a>. \n";
		str += "We suggest you try one of the links below:</p>\n";
	    str += GetOptionsString();
	    str += "<p>In order to improve our service, you can inform us that someone \n";
		str += "else has an incorrect link to our site. We will do our best to notify \n";
		str += "them that this page has moved and ask them to update the link. \n";
		str += "<strong>Simply click the button below!</strong></p>\n";
	  } else { /* we are at fault */
		str += "<h2>Please help us to help you ...</h2>\n";
		str += "<p>It looks like one of our own links is broken - we're very sorry about \n"
		str += "this, and we will ensure that this is passed on so that the links can be \n"
		str += "fixed. All you need to do is click on the button below.</p>\n";
	  }
	  var message = "A broken link has been spotted. Referrer = " + strReferrer;
	  message += " and page location = " + location.href;
	  str += "<form method='post' action='/cgi-bin/feedback.php' onsubmit='SayThanks();'>\n";
	  str += "  <input type='hidden' name='name'    value='missing-link' />\n";
	  str += "  <input type='hidden' name='email'   value='noreply@hostname.dom' />\n";
	  str += "  <input type='hidden' name='message' value=" + message +"' />\n";
	  str += "  <input type='submit' name='' class='button-0' value='Report broken link &raquo;'/>\n";
	  str += "</form>\n";
	}
  }
  return str;
}  
function GetOptionsString(){
  var str = "<ul>"
  str += "<li><a href='/' title='Go to GhanaKeyboards.Com home page'>home page</a></li>\n";
  str += "<li><a href='/downloads/index.html'title='Downloads'>downloadable keyboards</a></li>\n";
  str += "<li><a href='/support/index.html' title='Support'>support</a></li>\n";
  str += "<li><a href='/cgi-bin/search.php' title='Search GhanaKeyboards.Com'>search</a></li></ul>";
  return str;
}