// pass "MSIE", "Macintosh", "Windows", etc. to detectBrowser
function detectBrowser( inCheckStr )
{
	var checkStr = inCheckStr.toLowerCase();
	var agent = navigator.userAgent.toLowerCase();
	var result = ( agent.indexOf( checkStr ) != -1 );

	return result;
}

function getCookie( cookieName )
{
	// Only cookies for the current domain and path will be retrieved
	var escapedCookieName = escape( cookieName );
	var cookies = document.cookie.split( "; " );
	if ( cookies.length > 0 && cookies[0].length < 3 )
		return null;

	for ( var c = 0; c < cookies.length; c++ )
	{
		var currCookie = cookies[c].split( "=" );

		if ( currCookie[0] == escapedCookieName )
			return unescape( currCookie[1] );
	}

	return null;

} // getCookie


function setCookie( cookieName, cookieValue, secondsToLive, path, domain, reqSecure )
{
	if ( !cookieName )
		return false;

	// secondsToLive < 0 is in the past. Expires immediately.
	// There is a risk for short term expirations because the
	// client compares retrieval times to the expiration, but the
	// client clock may be wrong by an hour or several time zones.

	newCookie = escape( cookieName ) + "=" + escape( cookieValue );
	if ( secondsToLive  )
		newCookie += ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * secondsToLive ) ) ).toGMTString();

	if ( path )
		newCookie += ";path=" + path;
	
	if ( domain )
		newCookie += ";domain=" + domain;

	if ( reqSecure )
		newCookie += ";secure";

	document.cookie = newCookie;

	// see if it worked
	if ( secondsToLive >= 0 )
		return ( typeof( getCookie( cookieName ) ) == "string" );

	// deletion case
	return ( typeof( getCookie( cookieName ) ) != "string" );

} // setCookie


// must we match path, domain, reqSecure to nuke the cookie?
// If so, can deleteCookie get it first, then fill in the blanks for the caller?
function deleteCookie( cookieName, path, domain, reqSecure )
{
	return setCookie( cookieName, "deleted", -1, path, domain, reqSecure )
}


function cookiesEnabled()
{
	var res = setCookie( "cookiesEnabledTestCookieBML", "bml", 60 );

	if ( !res )
		return false;

	var cookieRes = getCookie( "cookiesEnabledTestCookieBML" );
	if ( !cookieRes )
		return false;

	var enabled = false;
	if ( cookieRes == "bml" )
		enabled = true;

	deleteCookie( "cookiesEnabledTestCookieBML" );
	
	return enabled;

} // cookiesEnabled


function getAvailableWidth()
{
	return window.screen.availWidth;
}

function getAvailableHeight()
{
	return window.screen.availHeight;
}


// surferSelectedPayPal is used on paymentMethod.html and is only a convenience fcn.
// It responds by updating other values when it makes sense.
function surferSelectedPayPal()
{
   if ( document.forms[0].email && document.forms[0].emailPref && document.forms[0].emailSource )
   {
      if ( !document.forms[0].emailSource[1].checked &&
			document.forms[0].emailPref.value != "None" &&
			document.forms[0].email.value == "" )
      {
         document.forms[0].emailSource[1].click();
      }
   }

} // surferSelectedPayPal


// surferSelectedCreditCard is used on paymentMethod.html and is only a convenience fcn.
// It responds by updating other values when it makes sense.
function surferSelectedCreditCard()
{
	if ( document.forms[0].emailSource )
	{
		if ( !document.forms[0].emailSource[0].checked )
		{
			document.forms[0].emailSource[0].click();
		}
	}

} // surferSelectedCreditCard

// surferTypedEmail is used on paymentMethod.html and is only a convenience fcn.
// It responds by updating other values when it makes sense.
function surferTypedEmail()
{
	if ( document.forms[0].email && document.forms[0].emailSource )
	{
		if ( !document.forms[0].emailSource[0].checked )
		{
			document.forms[0].emailSource[0].click();
		}
	}

} // surferTypedEmail


// surferChangedEmailPref is used on paymentMethod.html and is only a convenience fcn.
// It responds by updating other values when it makes sense.
function surferChangedEmailPref()
{
	if ( document.forms[0].email && document.forms[0].emailSource )
	{
		if ( document.forms[0].emailPref.value == "None" )
		{
			// no action required if no email desired. just record whatever we have
		}
		else
		{
			// now they do want email
			if ( document.forms[0].method[1].checked &&		// paying via PayPal
				document.forms[0].email.value == "" &&		// email is blank
			   !document.forms[0].emailSource[1].checked )	// the email src is not via PayPal

				document.forms[0].emailSource[1].click();	// set the src to paypal
		}
	}

} // surferChangedEmailPref


// surferSelectedEmailFromPayPal is used on paymentMethod.html and is only a convenience fcn.
// It responds by updating other values when it makes sense.
function surferSelectedEmailFromPayPal()
{
	if ( document.forms[0].method[0].checked )	// credit card is selected
	{
		document.forms[0].method[1].click();		// set the payment method to PayPal
	}

} // surferSelectedEmailFromPayPal



// used on pages with state popup menus, credit card info, shipping.
// the body onload will have something like onload = "setState( '_state' );" that gets updated live
function setState( newState )
{
	stateMenu = document.forms[0].shipState;
	if ( !stateMenu )
		stateMenu = document.forms[0].creditCardState;	// this will always be there since it comes first in the flow
	
	foundState = false;
	opt = stateMenu.options[0];
	for ( o = 0; opt; opt = stateMenu.options[++o] )
	{
		if ( opt.value == newState )
		{
			opt.selected = true;
			foundState = true;
		}
		else
			opt.selected = false;
	}

	// if none found, select "Please Select"
	if ( !foundState )
		stateMenu.options[0].selected = true;

} // setState


// validateUpgradeQuantity looks for the situation where the surfer is trying to upgrade
// and forgets to revise the quantities. Don't want to charge them full price.
function validateUpgradeQuantity()
{
	if ( document.forms[0].oldRegCodeTH1x20 != null )
	{
		firstRegCode = document.forms[0].oldRegCodeTH1x20.value;
		if ( firstRegCode.length > 0 && document.forms[0].qtyTH1x20.value == 0 )
		{
			document.forms[0].qtyTH1x20.value = 1;
			
			// clear the probably leftover
			if ( document.forms[0].qtyTH20.value != null )
				document.forms[0].qtyTH20.value = 0;
		}
	}
	
	if ( document.forms[0].oldRegCodeTH1x20 != null )
	{
		secondRegCode = document.forms[0].oldRegCodeTH1x20C.value;
		if ( secondRegCode.length > 0 && document.forms[0].qtyTH1x20C.value == 0 )
		{
			document.forms[0].qtyTH1x20C.value = 1;
			
			// clear the probably leftover
			if ( document.forms[0].qtyTH20.value != null )
				document.forms[0].qtyTH20.value = 0;
		}
	}
	
	return true;	// let the default behavior continue

} // validateUpgradeQuantity



