/***********************************************
* Google search
***********************************************/
function cGoogleCheckout( strTargetControlID, strConfigKeyPrefix, strFormAction, strItemName, strItemPrice, strTaxRate )
{
	// Parameters
	this.m_strTargetControlID = strTargetControlID;
	this.m_strConfigKeyPrefix = strConfigKeyPrefix;
	this.m_strFormAction = strFormAction;
	this.m_strItemName = strItemName;
	this.m_strItemPrice = strItemPrice;
	this.m_strTaxRate = strTaxRate;

	// Local variables
	this.m_ctrlText = null;
	this.frm = null;
	this.m_strDescription = '';
	this.m_ctrlTargetControl = '';

	// Function that runs when the page loads
	//
	this.onLoad = function()
	{
		// Prevent postbacks with the main/default form
		var frmMain = document.forms[0];
		frmMain.action = 'javascript:var postback=false;';
		frmMain.onsubmit = '';

		this.m_ctrlTargetControl = eval( 'g_obj' + this.m_strTargetControlID );

		// 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', 'frmGoogleCheckout_' + this.m_strConfigKeyPrefix );
		this.frm.setAttribute( 'action', '' );
		this.frm.setAttribute( 'method', 'POST' );
		this.frm.setAttribute( 'accept-charset', 'utf-8' );
		
		this.frm.appendChild( this.getHiddenInput( 'item_name_1', this.m_strItemName ) );
		this.frm.appendChild( this.getHiddenInput( 'item_description_1', '' ) );
		this.frm.appendChild( this.getHiddenInput( 'item_quantity_1', '1' ) );
		this.frm.appendChild( this.getHiddenInput( 'item_price_1', this.m_strItemPrice ) );

		if( this.m_strTaxRate.length > 0 )
		{
			var defaultTaxPrefix = 'checkout-flow-support.merchant-checkout-flow-support.tax-tables.default-tax-table.tax-rules.default-tax-rule-1.';
			this.frm.appendChild( this.getHiddenInput( defaultTaxPrefix + 'shipping-taxed', 'true' ) );
			this.frm.appendChild( this.getHiddenInput( defaultTaxPrefix + 'rate', this.m_strTaxRate ) );
			this.frm.appendChild( this.getHiddenInput( defaultTaxPrefix + 'tax-areas.world-area-1', '' ) );

			var altTaxPrefix = 'checkout-flow-support.merchant-checkout-flow-support.tax-tables.alternate-tax-tables.alternate-tax-table-1.';
			var rateName = 'special_tax_rate';
			this.frm.appendChild( this.getHiddenInput( 'shopping-cart.items.item-1.tax-table-selector', rateName ) );
			this.frm.appendChild( this.getHiddenInput( altTaxPrefix + 'name', rateName ) );
			this.frm.appendChild( this.getHiddenInput( altTaxPrefix + 'standalone', 'true' ) );
			this.frm.appendChild( this.getHiddenInput( altTaxPrefix + 'alternate-tax-rules.alternate-tax-rule-1.rate', this.m_strTaxRate ) );
			this.frm.appendChild( this.getHiddenInput( altTaxPrefix + 'alternate-tax-rules.alternate-tax-rule-1.tax-areas.world-area-1', '' ) );
		}

		this.frm.appendChild( this.getHiddenInput( '_charset_', '' ) );

		// 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;
	};

	// Submit the search
	//
	this.performCheckout = function()
	{
		var ii;

		this.m_strDescription = this.m_ctrlTargetControl.getDescription();
		
		// Verify that the user entered some text
		var ary = this.m_strDescription.split( ',' );
		var blnTextEntered = false;
		for( ii = 0; ii < ary.length; ii++ )
		{
			// Empty would be '' (length 2)
			if( ary[ii].length > 2 )
			{
				blnTextEntered = true;
			}
		}
		if( !blnTextEntered )
		{
			alert( 'Insufficient item description.  Please enter data before going to the checkout.' );
			return false;
		}

		// 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 == 'item_description_1' )
				{
					this.frm.children[ii].value = this.m_strDescription;
				}
			}
		}
		else						// Firefox, etc.
		{
			this.frm.item_description_1.value = this.m_strDescription;
		}

		// Submit the form
		this.frm.action = this.m_strFormAction;
		this.frm.submit();
	};
	
	return this;
}
