var oCurrentPuzzle;
var oPuzzleName;
var oPuzzleGrade;
var oPuzzleFormat;
var oEula;
var sHelpList = "Main Help\r\nRegistration\r\nFiling\r\nSudoku play\r\n";
var sOptionsList = "r1c1 Format\r\nR1 Format\r\nC1 Format\r\n";
var nIndex = 0;

function DebugCallback(sIn)
{
	if (sIn == "test")
		ResultBool = true;
	else
		ResultBool =  false;
}

function LoadPage(sName, sResult)
{
	switch (sName.substr(0,4).toLowerCase())
	{
		case "main":
		{
			document.getElementById("IFrame1").src = "HelpHome.htm";
			nIndex = 0;
			break;
		}
		case "regi":
		{
			document.getElementById("IFrame1").src = "HelpRegistration.htm?result=" + sResult;
			nIndex = 1;
			break;
		}
		case "fili":
		{
			document.getElementById("IFrame1").src = "HelpFiler.htm";
			nIndex = 2;
			break;
		}
		case "sudo":
		{
			document.getElementById("IFrame1").src = "HelpSudokuPlay.htm";
			nIndex = 3;
			break;
		}
		case "eula":
		{
			document.getElementById("IFrame1").src = "HelpEULA.htm";
			nIndex = 3;
			break;
		}
	}
}

function pageLoadFunctions()
{
    setBGColor("#1E4CA1");
		setZoom();
	  checkSVP();
		loadingSpan.style.display = "none";
		mainSpan.style.display = "block";
		var sURL = document.URL;
		var nLoc = sURL.indexOf("page");
		var sPage = "main";
		if (nLoc>0 && ((nLoc+5) < sURL.length))
		{
			sPage = sURL.substr(nLoc+5);
		}
    oCurrentPuzzle = GetCookie('current');
    if (oCurrentPuzzle == null)
			currrentPuzzle = "";
    oPuzzleName = GetCookie('name');
    if (oPuzzleName == null)
			oPuzzleName = "";
    oPuzzleGrade = GetCookie('grade');
    if (oPuzzleGrade == null)
			oPuzzleGrade = "";
    oPuzzleFormat = GetCookie('format');
    if (oPuzzleFormat == null || oPuzzleFormat == "")
			oPuzzleFormat = "RN";
		LoadPage(sPage);
		LoadTable(help, sHelpList, true);
    oEula = GetCookie('eula');
    if (oEula == null || oEula != "accepted")
    {
	    buttonBack.innerText = "I Accept Agreement";
	  }
	  else
	  {
			LoadTable(options, sOptionsList, true);
	  }
    setArray();
    startFocus();
    if (oEula != null && oEula == "accepted")
    {
			var aLables = sHelpList.split("\r\n");
			ToggleSelect(aLables.slice(nIndex, nIndex+1), "help");
			switch (oPuzzleFormat)
			{
				case "RC":
				{
					ToggleSelect("r1c1 Format", "options");
					break;
				}
				case "RN":
				{
					ToggleSelect("R1 Format", "options");
					break;
				}
				case "CN":
				{
					ToggleSelect("C1 Format", "options");
					break;
				}
			}
		}
    if (store.Registered.indexOf("Online") != 0)
    {
			regName.value	= store.Registered;
			regCode.value = "Registered";
		}
		window.status = oPuzzleName + " [" + oPuzzleGrade + "]";
		TextEntryControl1.style.display = "none";
    checkSVP();
		checkArrows();
}
	
function onFocusChange()
{
    // find element that has actual focus
//        var oFocusObj = document.activeElement;
    var oFocusObj = oCurFocus;
    
    if (IsLoadingList)
			return;
    
    if	(oFocusObj.name == "options")
    {
			var sFormat = oFocusObj.innerText; 
			var nIndex = oFocusObj.parentNode.rowIndex;
    }
}

function doSelect()
{
/* This function determines what your buttons do when they are selected
(navigate, call a function, etc.). This function will get called whenever user
clicks a focusable item, or selects it with "OK" button on remote. Make sure to
include a case below for each focusable item on the page */
		var result = false;
		var nIndex = 0;
		if (oCurFocus.name == "options")
		{
			var sFormat	= oCurFocus.innerText;
			nIndex = oCurFocus.parentNode.rowIndex;
			ToggleSelect(sFormat, "options");
			switch (nIndex)
			{
				case 0:
				{
					SetCookie("format", "RC");
					break;
				}
				case 1:
				{
					SetCookie("format", "RN");
					break;
				}
				case 2:
				{
					SetCookie("format", "CN");
					break;
				}
			}
			document.getElementById("IFrame1").src = "HelpFormat.htm";
		}
		else if (oCurFocus.name == "help")
		{
			// Help List
			LoadPage(oCurFocus.innerText, "");
			checkArrows();
			ToggleSelect(oCurFocus.innerText, "help");
		}
		else
		{
			switch(oCurFocus.id)
			{
					case "buttonReg":
					{
						if (regName.value == "" || regCode.value == "" || regCode.value == "Registered")
						{
							LoadPage("regi", "Empty");
							checkArrows();
							ToggleSelect(oCurFocus.innerText, "regi");
						}
						else
						{
							if (store.Register(regName.value, regCode.value) == true)
							{
								LoadPage("regi", "OK");
								regCode.value = "Registered";
								regCode.style.color = "black";
							}
							else
							{
								LoadPage("regi", "Fail");
								regCode.style.color = "red";
							}
							checkArrows();
							ToggleSelect(oCurFocus.innerText, "regi");
						}
						break;
					}
					case "buttonBack":
					{
						if (oEula != "accepted")
						{
							SetCookie("eula", "accepted")
						}
            			window.navigate("SudokuMain.htm");
            				
						break;
					}
			}
    }
    return true;
}

function doOnFocus(keyChar)
{
    // find element that has actual focus
    var oFocusObj = document.activeElement;
		Result = false;

    // if focus is on iFrame and user hits up or down arrow ...
    if (oFocusObj.id == "IFrame1")
    {
        if (document.frames(oFocusObj.id).document.activeElement.id != 'body')
        {
            document.frames(oFocusObj.id).onRemoteEvent(keyChar)
						Result = true;
            return true
        }
        /*Call the scrollIFrame function (in IFrameScrolling.js) and Pass the keycode of the key pressed
        That function will return true only if user has pressed up arrow, down arrow pg-up or pg-down, and if scrolling actually takes place.
        If return value of scrollIFrame function = true, return true in this function
        that tells the onRemoteEvent function that the keypress has been handled, so it will not do any other navigation */
        if (scrollIframe(keyChar) == true)
        {
            // don't do anything else; just return true
						Result = true;
            return true;
        }
    }

		if (keyChar == -100 || keyChar == 112 || keyChar == 47)
		{
			// Don't pass on Help Key
			Result = true;
			return true;
		}
    
    // If focus is on a text input control, leave function, navigate normally
    if (oFocusObj.className != 'textControl')
			return false;

    // capture keypress for keyboard BACK button
    if (oFocusObj.className == 'textControl' && keyChar == 8)
    {
        // call function to update info in text box; first give control enough time to erase character
        setTimeout('controlKeyUp(document.activeElement)',1);
    }

    //if any arrow key pressed
    if (keyChar == 38 || keyChar == 40 || keyChar == 37 || keyChar == 39)
		{
				return false;
		}
    
    // call function to update info in text box;
    // this is needed just to capture event from remote control

    controlKeyUp(oFocusObj);

		return false;
}

function doOnBlur(oBlurring)
{
	if (oBlurring.className == 'textControl')
	{
		oBlurring.style.display = 'none';
		restore(regName);
	}
}

function inputText(oWhichItem)
{
    // Variable for correct text entry control
    var oCtrl;
    // Skip because no triple-tap
    if (false && oWhichItem == regName)
    {
        // update variable for correct text entry control
        oCtrl = TextEntryControl1;
        // Show control for entering name
        oCtrl.style.display = 'block';
				// focus on control
        oCtrl.focus();
        // call control's method for setting focus on its input box
        // (which is hidden from user)
        try
        {
					oCtrl.SetInputFocus();
				}
				catch (e)
				{
				}
    }
    // call function to hilite text box; first give control enough time
    setTimeout('hilite(oCurFocus)',1);
}

function controlKeyUp(oCtrl)
{
    if (oCtrl.id == "TextEntryControl1")
    {
        // update display in visible input box
        regName.value = oCtrl.GetInputText();
    }
    if (oCtrl.id == "ListBox1")
    {
        // update selection in visible input box
        regName.value = oCtrl.Text;
    }
}
