/***********************************************
* Google search
***********************************************/
function cGoogleSearch( strTextID, strCx, strCof )
{
	// Parameters
	this.m_strTextID = strTextID;
	this.m_strCx = strCx;
	this.m_strCof = strCof;

	// Local variables
	this.m_strTargetWindowName = 'googleSearchResults';
	this.m_ctrlText = null;
	this.frm = null;

	// Function that runs when the page loads
	//
	this.onLoad = function()
	{
		this.m_ctrlText = document.getElementById( this.m_strTextID );

		// Prevent postbacks with the main/default form
		var frmMain = document.forms[0];
		frmMain.action = 'javascript:var postback=false;';
		frmMain.onsubmit = '';

		// Insert a submit form for this control
		this.appendSubmitForm();
	};

	// Append the google form submit stuff to the page
	//
	this.appendSubmitForm = function()
	{
		// Append the submit form
		var tmpDiv = document.createElement( 'div' );

		this.frm = document.createElement( 'form' );
		this.frm.setAttribute( 'name', 'frmGoogleSearch' );
		this.frm.setAttribute( 'action', '' );
		this.frm.setAttribute( 'method', 'get' );
		
		this.frm.appendChild( this.getHiddenInput( 'q', 'UTF-8' ) );
		this.frm.appendChild( this.getHiddenInput( 'cx', this.m_strCx ) );
		this.frm.appendChild( this.getHiddenInput( 'cof', this.m_strCof ) );
		this.frm.appendChild( this.getHiddenInput( 'sa', 'Search' ) );
		this.frm.appendChild( this.getHiddenInput( 'hl', 'en' ) );
		this.frm.appendChild( this.getHiddenInput( 'client', 'google-coop' ) );

		// This code prevents the form from posting back to the server
		this.frm.action = 'javascript:var postback=false;';
		this.frm.onsubmit = '';

		tmpDiv.appendChild( this.frm );

		document.body.appendChild( tmpDiv );
	};

	// Create a hidden input field dom object	
	//
	this.getHiddenInput = function( strName, strValue )
	{
		var elInputHidden = document.createElement( 'input' );
		
		elInputHidden.setAttribute( 'type', 'hidden' );
		elInputHidden.setAttribute( 'name', strName );
		elInputHidden.setAttribute( 'value', strValue );

		return elInputHidden;
	};

	// When the user clicks the search text entry field, highlight all the text
	//
	this.textGetFocus = function()
	{
		this.m_ctrlText.focus();
		this.m_ctrlText.select();
		this.m_ctrlText.value = "";
	};

	// Test each keystroke, looking for enter to bring up the search results
	//
	this.detectKeyPress = function(e)
	{
		var keynum;
		var keychar;
		var numcheck;
		if( window.event ) // IE
		{
			keynum = e.keyCode;
		}
		else 
		{
			if( e.which ) // Netscape/Firefox/Opera
			{
				keynum = e.which;
			}
		}
		if( keynum == 13 )
		{
			this.submitSearch();
		}
	};

	// Submit the search
	//
	this.submitSearch = function()
	{
		var ii;

		// Stuff the user-entered value into the hidden form
		if( this.frm.children )		// MSIE
		{
			for( ii=0; ii < this.frm.children.length; ii++ )
			{
				if( this.frm.children[ii].name == 'q' )
				{
					this.frm.children[ii].value = this.m_ctrlText.value;
				}
			}
		}
		else						// Firefox, etc.
		{
			this.frm.q.value = this.m_ctrlText.value;
		}

		// Submit the form
		this.frm.method = 'get';
		this.frm.action = 'http://www.google.com/cse';
		this.frm.target = this.m_strTargetWindowName;

		this.frm.onsubmit = window.open('', this.m_strTargetWindowName, 
			'width=816,height=600,status=no,resizable=yes,scrollbars=yes');
		this.frm.submit();

		// Reset it
		this.frm.action = 'javascript:var postback=false;';
		this.frm.onsubmit = '';
	};
	
	return this;
}

