/*
 * Functions related to saving and loading puzzles to/from the user DB.
 * Copyright Gaby Vanhegan <gaby@vanhegan.net> 20060416
 */

/*
 * States for the guesses, pencilmarks and clues
 */
var curClues		= "";
var curPencilmarks	= "";
var curGuesses		= "";

/*
 * Utility function to find an object in the various DOM trees, independant
 * of the browser or platform being used.  In the event of failure, false is
 * returned
 * 
 * Heads up to Tom Wiltshire and Chris Tebb for this one.  Cheers boys...
 */
function getElement ( objectId ) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  if ( document.getElementById && document.getElementById( objectId ) ) {
      return document.getElementById( objectId ); 
  }
  else if ( document.all && document.all( objectId ) ) {  
      return document.all(objectId).style;
  } 
  else if ( document.layers && document.layers[ objectId ] ) { 
      return document.layers[objectId];
  } 
  else {
      return false;
  }
}

/*
 * Select a given value in the given radio group
 * @param	string	elemSrc			The source object to find the ID in
 * @param	string	elemName		The ID of the radio group
 * @param	string	theValue		The radio value to select
 */
function radioSelect ( elemSrc, elemName, theValue ) {
	var elemTmp		= getElement( elemSrc );
	var elem		= elemTmp[ elemName ];
	if ( elem ) {
		for ( var i = 0 ; i < elem.length ; i++ ) {
			var subElem	= elem[ i ];
			if ( subElem.value == theValue ) {
				subElem.checked	= true;
			}
			else {
				subElem.checked	= false;
			}
		}		
	}
}

/*
 * Function called by Flash to store the values in the variables
 * @param	string	theName	The item to be changing, clues, guesses or pencilmarks
 * @param	string	theVal	The data
 */
function saveState ( theName, theVal ) {
	if ( theName == "guesses" ) { 
		curGuesses 		= theVal;
		}
	else if ( theName == "clues" ) { 
		curClues 		= theVal;
		}
	else if ( theName == "pencilmarks" ) { 
		curPencilmarks	= theVal;
		}
	}

/*
 * Open up the save window with the details from the puzzle
 * @param	string	source	The source for the data, flash or html
 */
function openSavePopup ( source ) {
	var theURL		= "save.popup.php"
					+ "?clues=" + curClues 
					+ "&guesses=" + curGuesses
					+ "&pencilmarks=" + curPencilmarks
					+ "";
	window.open( theURL, "Save", "width=400,height=300,scrollbars=no,resize=no" );
	return true;
}

/*
 * Open up the load puzzle window
 */
function openLoadPopup ( ) {
	var theURL		= "save.load.popup.php"
					+ "";
	window.open( theURL, "Save", "width=400,height=300,scrollbars=no,resize=no" );
	return true;
}