function initializePage()
{
    createClientPropertyRegistrar();
    setSmileboxCookies(window.location.search);
}

function createClientPropertyRegistrar()
{
	var flashvars = { };
	var params = { allowScriptAccess:"always", allowNetworking:"all", swliveconnect:"true" };
	var attributes = { id:"ClientPropertyRegistrar" };
	swfobject.embedSWF("/flash/ClientPropertyRegistrar.swf", 
					   "HolderForClientPropertyRegistrarSwf",
					   "0",
					   "0",
					   "8.0.0",
					   false,
					   flashvars, params, attributes);
}

var myClientPropertyRegistrarIsReady = false;

function clientPropertyRegistrarIsReady()
{
	myClientPropertyRegistrarIsReady = true;
}

function isSmileboxPropertyRegistrarReady()
{
	return myClientPropertyRegistrarIsReady;
}

function setPartnerCode( aPartnerCode )
{
	if ( isSmileboxPropertyRegistrarReady() )
	{
		setSmileboxProperty( "partner", aPartnerCode );
	}
	else
	{
		setTimeout("setPartnerCode( '" + aPartnerCode + "' )", 250 );
	}
}

function setSmileboxProperty( aName, aValue )
{
	try
	{
		if( aName != undefined && aName.length > 0 && 
			aValue != undefined && aValue.length > 0 )
		{
			var theRegistrar = document.getElementById("ClientPropertyRegistrar");
			theRegistrar.setSmileboxProperty( aName, aValue );
		}
	}
	catch( e )
	{
	}
}

function getSmileboxProperty( aName )
{
	var theProperty = "";
	try
	{
		var theRegistrar = document.getElementById("ClientPropertyRegistrar");
		theProperty = theRegistrar.getSmileboxProperty( aName );
	}
	catch( e )
	{
	}
    return theProperty;
}

function buildNameValuePairIfBothDefined( aName, aValue, aPrependWithAmpersandFlag )
{
	if ( aName != undefined && aName.length > 0 && aValue != undefined && aValue.length > 0 )
	{
		if ( aPrependWithAmpersandFlag )
		{
			return '&' + aName + '=' + aValue;
		}
		else
		{
			return aName + '=' + aValue;
		}
	}
	
	return '';
}

function setAffiliateParametersIfPresent()
{
	try
	{
		var theRegistrar = document.getElementById("ClientPropertyRegistrar");
		var theUrlData = '';
		
		var thePartnerCode = theRegistrar.getSmileboxProperty( 'partner' );
		if ( thePartnerCode == 'commissionjunction' )
		{
			var theAidvalue = theRegistrar.getSmileboxProperty( 'AID' );
			var theSidvalue = theRegistrar.getSmileboxProperty( 'SID' );
			var thePidvalue = theRegistrar.getSmileboxProperty( 'PID' );
				
			theUrlData = buildNameValuePairIfBothDefined( 'AID', theAidvalue );
			theUrlData += buildNameValuePairIfBothDefined( 'SID', theSidvalue, true );
			theUrlData += buildNameValuePairIfBothDefined( 'PID', thePidvalue, true );
		}
		else
		{
			var theShareASaleParameter = theRegistrar.getSmileboxProperty( 'SSAID' );
			if ( theShareASaleParameter != undefined && theShareASaleParameter.length > 0 )
			{
				var theSsaidData = theRegistrar.getSmileboxProperty( 'SSAIDDATA' );
				
				theUrlData = buildNameValuePairIfBothDefined( 'userID', theShareASaleParameter );
				theUrlData += buildNameValuePairIfBothDefined( 'SSAIDDATA', theSsaidData, true );
			}
		}
	
		if ( theUrlData != undefined && theUrlData.length > 0 )
		{
			theRegistrar.setSmileboxProperty( 'affiliateParameters', theUrlData );
		}
	} 
	catch ( e )
	{
	}
}

function setPartnerCodeInBrowserCookie()
{
    var thePartnerCode = getSmileboxProperty( "partner" );
	if ( typeof SetCookie == 'function' )
	{
    	var theExpirationDate = new Date();
		theExpirationDate.setFullYear( 2 + theExpirationDate.getFullYear() );
		SetCookie( 'partner', thePartnerCode, theExpirationDate );
	} 
}


function setSmileboxCookies( aQueryString )
{
    if (isSmileboxPropertyRegistrarReady())
    {
        if (!aQueryString && isPartnerCookieSet())
        {
            return;
        }

        var theNameValuePairs = aQueryString.replace('?', '').split('&');
        for (i = 0; i < theNameValuePairs.length; i++)
        {
            var theNameValuePair = theNameValuePairs[i].split('=');
            setSmileboxProperty( theNameValuePair[0], theNameValuePair[1]);
        }

        if (!isPartnerCookieSet())
        {
            setSmileboxProperty( "partner", "smilebox" );
        }
		
		setPartnerCodeInBrowserCookie();
		setAffiliateParametersIfPresent();
    }
    else
    {
        setTimeout("setSmileboxCookies('" + aQueryString + "')", 250);
    }
}

function isPartnerCookieSet()
{
    var thePartnerCookiesValue = getSmileboxProperty( "partner" );
    return thePartnerCookiesValue && thePartnerCookiesValue != "";
}
