/*
* author DC Dalton - www.dcddesigns.com
* version 1.0 02/08/2005
* function checks if a cookie has already been set with the referrer 
* If it hasnt been set it will grab the referrer and put it into a cookie
* IF the page contains a form field with the ID of formField it will set its value to the referrer
* Cookie is set to expire when they close the browser window - if we didnt do it that way we could get someone coming back and get the last time they were referred
* Script tested and working without any js errors in IE 4 and up, Netscape 4x and UP, Mozilla (all versions), Firefox (all versions) and Opera 6x and up (Opera 5 completely chokes on it) 
*/
function checkCookie()	{
	var cookie = document.cookie;
	if (cookie.indexOf("referral") == -1)	{																		// OUR cookie doesnt yet exist so we create it, set the referrer into it and then write it to the form field IF it exists
		var ref = getReferrer();																						// get the referrer from the document object
		document.cookie = "referral="+escape(ref) + ";";												// escape it and add to the cookie

		if (document.getElementById && document.getElementById('formField'))	{			// test to see if the browser supports the DOM AND has the form field we need to fill in
			document.getElementById('formField').value = ref;											// if it does set the variable to the field
			}
		else if (document.forms[0])	 {																				// used for older browsers that dont support getElementById
			document.forms[0].formField.value = ref;														// write the form field using the OLD document.forms[0]
			}
		}
	else	{																													// cookie has already been set - we DONT want to override it with one of our own pages!
		var data = cookie.substring(cookie.indexOf("referral=") + 9, cookie.length - 1);
		if (document.getElementById && document.getElementById('formField'))	{			// test to see if the browser supports the DOM AND has the form field we need to fill in
			document.getElementById('formField').value = unescape(data);							// if it does set the variable to the field
			}
		else if (document.forms[0])	 {																				// used for older browsers that dont support getElementById
			document.forms[0].formField.value = unescape(data);										// write the form field using the OLD document.forms[0]
			}
		}
	}

/* 
* function does nothing more than retrieve the referrer property from the document object and return it to the calling function
*/
function getReferrer()	 {
	return document.referrer;
	}
