// Create, Store, and Retrieve cookies
function createCookie(name,value,days)
{
        if (days)
        {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++)
        {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}

function eraseCookie(name)
{
        createCookie(name,"",-1);
}

// Extract and Encode URL tag
function ExtractURLTagValue(URLtag)
{
	thispage=window.location.href;
	if (thispage.indexOf(URLtag)>0) 
		{
		prefix=thispage.substring(0,thispage.lastIndexOf(URLtag));
		suffix=thispage.substring(thispage.lastIndexOf(URLtag)+3,thispage.length);
			// alert('the subpage is:['+prefix+']['+suffix+']');
		return suffix;
		}
	else
		{
		return '';
		}
}

function StoreAffiliateCode(URLtag)
{
	var AffiliateCode = ExtractURLTagValue(URLtag);
	if (AffiliateCode == '')
	{
		AffiliateCode = readCookie(URLtag);
	} else {
		createCookie(URLtag,AffiliateCode,9999);
	}

	if (document.URL.indexOf("ID_HomePage.html") > 0)
	{
		document.Newsletter1.Registrant_Source.value = AffiliateCode;
		document.Newsletter2.Registrant_Source.value = AffiliateCode;
	} else {
		if (document.URL.indexOf("ID_GetStarted.html") > 0)
		{
			document.GetStarted.Registrant_Source.value = AffiliateCode;
		}
	}
}
