//////////////////////////////////////////////////////////////////////////////
//																			//  
//		File: Wufoo-Source.js												//
// 																			//
//		Purpose: Add referrer to Wufoo e-mail and set 24 hour cookie 		//
//																			//
//		Author: vWorker.com user Fast_Websites								//
//																			//
//		Last Modified: January 23, 2011										//
//																			//
//////////////////////////////////////////////////////////////////////////////

MyCookie = GetCookieVal();
Source = GetRef();
	if ( Source )
	{
	var OurRefs = [];
	// our referrers: OurRefs["argument"] = "Textual Description";
	OurRefs["ibp-adgroup=adwords"] = "Google AdWords";
	OurRefs["ibp-adgroup=adcenter"] = "Microsoft adCenter";
	Source = OurRefs[Source];
	}
	else if (MyCookie) Source = MyCookie;
	else Source = 'Organic Listing';
	if (!MyCookie && Source != 'Organic Listing') SetCookie(Source);
// URL Encode field value
Source = URLencode(Source);

function GetRef()
{
var regex = new RegExp( "[\\?]([^&#]*)" );
var results = regex.exec( window.location.href );
	if ( results == null ) return null;
	else return results[1];
}

function SetCookie(Source)
{
// set 24 hour cookie
var ExpireDate = new Date();
ExpireDate.setTime(ExpireDate.getTime() + (24*60*60*1000));
document.cookie = 'Source=' + escape(Source) + "; expires=" + ExpireDate.toGMTString() + '; path=/';
}

function GetCookieVal()
{
	if (document.cookie.length>0)
	{
	  start = document.cookie.indexOf('Source=');
		if (start != -1)
		{
		start = start + "Source".length + 1;
		end = document.cookie.indexOf(";", start);
			if (end == -1) end = document.cookie.length;
		return unescape( document.cookie.substring( start, end) );
		}
	}
return "";
}

function URLencode(s)
{
return encodeURIComponent( s ).replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\*/g, '%2A' ).replace( /\~/g, '%7E' );
}
