function setCookie(c_name,value) {
	var exdate=new Date();
	exdate.setTime(exdate.getTime()+(1000*60*60*24*1000));
	
	document.cookie	=	c_name + "=" + escape(value)+";"		+
						"expires=" + exdate.toGMTString()+";"	+
						"path=/;"								+
						"domain="+domain+";";
} // END FUNCTION: setCookie

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1)
				c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} // end if
	} // END IF
	return "";
} // END FUNCTION: getCookie

function checkCookie(name) {
	value=getCookie(name);
	return value;
} // END FUNCTION: checkCookie

function getRef() {
	if (document.referrer != "")
	{
		return unescape(document.referrer);
	}
	else
	{
		return "None";
	}
} // END FUNCTION: getRef

function getSearchEngine(url) {

	searchEngine = "None";
	if (url == null){
		url = getRef();
	} // END IF

	switch(true){
		case(url.search(/.google./i) > -1):
			searchEngine = "Google";
			break;
		case(url.search(/.yahoo./i) > -1):
			searchEngine = "Yahoo";
			break;	
		case(url.search(/.msn./i) > -1):
			searchEngine = "MSN";
			break;	
		case(url.search(/.live./i) > -1):
			searchEngine = "Live";
			break;
	} // END SWITCH
	
	return searchEngine;
} // END FUNCTION: getSearchEngine

function getNaturalKeyword() {
	naturalKeyword = "None";
	rurl = getRef();
	rse = getSearchEngine(rurl);		
	switch(rse){
	case "Google":
		s_param = "q";
		break;
	case "Yahoo":
		s_param = "p";
		break;	
	case "MSN":
		s_param = "q";
		break;	
	case "Live":
		s_param = "q";
		break;		
	default:
		s_param = "None";
		break;
	}	
	rquery = rurl.substr(rurl.indexOf("?")+1);
	rarr = rquery.split("&");
	
	for(i=0;i<rarr.length;i++){
		$rhash = rarr[i].split("=");
		if($rhash[0].toLowerCase()==s_param){
			naturalKeyword = $rhash[1];
		}
	}
		
	return naturalKeyword;
} // END FUNCTION: getNaturalKeyword

if (imptest) {
	alert("Apogee CRM Cookie Functions Inclusion Successful!");
} // END IF

// writeCookie
// if the cookie has been written once before, don't touch it
tempref = checkCookie("ref");
if (
	(tempref == "") ||
	(tempref == "null") ||
	(tempref == null) ||
	(tempref == undefined)
) 
{	
	name = "ref";
	value = getRef();
	setCookie(name, value);
			
	name = "nse";
	value = getSearchEngine();
	setCookie(name, value);
	
	name = "nkw";
	value = getNaturalKeyword();
	setCookie(name, value);
} // END IF