function Delete_Cookie(name){
	document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;path=/;';
}

function extractCookieValue(val){
	if ((endOfCookie = document.cookie.indexOf(";", val)) == -1) {
		endOfCookie = document.cookie.length;
	}
	return unescape(document.cookie.substring(val,endOfCookie));
}

function ReadCookie(cookiename) {
	var nameEQ = cookiename + "=";
	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 createCookie(name, value, expiredays, path, domain, secure){
  var todayDate = new Date();
  todayDate.setDate(todayDate.getDate() + expiredays);
  document.cookie = name+"="+value+";expires="+todayDate.toGMTString()+";path=/;"
}

var allSEs = new Array();

allSEs[0] = "20search";
allSEs[1] = "alltheweb";
allSEs[2] = "altavista";
allSEs[3] = "aol";
allSEs[4] = "ask";
allSEs[5] = "bing";
allSEs[6] = "dogpile";
allSEs[7] = "excite";
allSEs[8] = "gigablast";
allSEs[9] = "google";
allSEs[10] = "iwon";
allSEs[11] = "joeant";
allSEs[12] = "lycos";
allSEs[13] = "mamma";
allSEs[14] = "msn";
allSEs[15] = "netscape";
allSEs[16] = "opendirectory";
allSEs[17] = "webcrawler";
allSEs[18] = "yahoo";
allSEs[19] = "ziplocal";
allSEs[20] = "nexis";
allSEs[21] = "thomasnet";
allSEs[22] = "infospace";
allSEs[23] = "hotbot";
allSEs[24] = "gigablast";
allSEs[25] = "live";
allSEs[26] = "looksmart";
allSEs[27] = "dmoz";
allSEs[28] = "cuil";
allSEs[29] = "go";
allSEs[30] = "galaxy";
allSEs[31] = "alexa";


// sample code: SCHOOL-PROGRAM-SEARCHENGINE-NSEARCH-SEO-NONEAVAIL



// page loads look at referring url
var SECOM = document.referrer.substring(document.referrer.indexOf("http://")+7, document.referrer.indexOf("/",document.referrer.indexOf("http://")+9));
var SE = SECOM.substring(SECOM.lastIndexOf(".",SECOM.lastIndexOf(".")-2)+1,SECOM.lastIndexOf("."));

var AllSEsString = allSEs.toString();

//DETERMINE IF REFERRER IS SEO OR SEM
var inType = "NSEARCH"; //default

if(AllSEsString.search(SE) > 0){
	if(SE == "google" || SE == "bing" || SE == "yahoo"){
		if(document.forms[0] && document.forms[0].Access_Code){
			//determine Access Code type
			if(document.forms[0].Access_Code.type == "select-one"){
				if(document.forms[0].Access_Code[1].value.length > 4){
					var actemp = document.forms[0].Access_Code[1].value.split("-");
				}else{
					var actemp = document.forms[0].Access_Code[2].value.split("-");
				}
				//edit select list
			}
			if(document.forms[0].Access_Code.type == "hidden"){
				var actemp = document.forms[0].Access_Code.value.split("-");
			}
			if(actemp[4] == "PPC" || actemp[4] == SE+"paidsearch"){
				inType = "PSEARCH";
			}else{
				inType = "NSEARCH";
			}
		}else{
			inType = "NSEARCH";
		}
	}else{
		inType = "NSEARCH";
	}
	var setTSE = inType+'-'+SE.toUpperCase();
	createCookie('search',setTSE,30,'/','','');
}else{
// current url
	var CURRPage = location.href;
	if(CURRPage.indexOf("thank_you.htm") > -1 || CURRPage.indexOf("request_processor.") > -1){
		var TYCOM = CURRPage.substring(document.referrer.indexOf("http://")+7, CURRPage.indexOf("/",CURRPage.indexOf("http://")+9));
		if(SECOM == TYCOM){
			delCookie();
		}
	}
}

userCookie = ReadCookie("search");
if (userCookie != null){
	checkAndMod();
}


function checkAndMod(){
	if(document.forms[0] && document.forms[0].Access_Code){
		//determine Access Code type
		userCookie = ReadCookie("search");
		if(userCookie != null){
			var inValue = userCookie.split("-");
			var inSType = inValue[0];
			var inSEngine = inValue[1];
		}

		//SAMPLE: SCHOOL-PROGRAM-SEARCHENGINE-NSEARCH-SEO-NONEAVAIL
		if(document.forms[0].Access_Code.type == "select-one"){
			for(s=0; s<document.forms[0].Access_Code.length; s++){
				if(document.forms[0].Access_Code[s].value.length > 4){
					var actemp = document.forms[0].Access_Code[s].value.split("-");
					if(inSEngine == actemp[2] || actemp[4] == "PPC" || actemp[3] == "NATSEA" || actemp[3] == "NATCON" || actemp[3] == "LOCSEA" || actemp[3] == "LOCCON" || actemp[3] == "REGSEA" || actemp[3] == "REGCON"){
						return false;
					}else{
						actemp[2] = inSEngine; //WHICH SE
						actemp[3] = inSType; //LEAD TYPE
						if(inSType == "NSEARCH"){
							document.forms[0].Access_Code[s].value = actemp[0]+"-"+actemp[1]+"-"+inSEngine.toLowerCase()+"naturalsearch";
						}
						if(inSType == "PSEARCH"){
							document.forms[0].Access_Code.value = actemp[0]+"-"+actemp[1]+"-"+inSEngine.toLowerCase()+"paidsearch";
						}
						//alert(document.forms[0].Access_Code[s].value);
					}
				}
			}
		}
		if(document.forms[0].Access_Code.type == "hidden"){
			var actemp = document.forms[0].Access_Code.value.split("-");
			if(inSEngine == actemp[2] || actemp[4] == "PPC" || actemp[3] == "NATSEA" || actemp[3] == "NATCON" || actemp[3] == "LOCSEA" || actemp[3] == "LOCCON" || actemp[3] == "REGSEA" || actemp[3] == "REGCON"){
				return false;
			}
			actemp[2] = inSEngine; //WHICH SE
			actemp[3] = inSType; //LEAD TYPE
			if(inSType == "NSEARCH"){
				document.forms[0].Access_Code.value = actemp[0]+"-"+actemp[1]+"-"+inSEngine.toLowerCase()+"naturalsearch";
			}
			if(inSType == "PSEARCH"){
				document.forms[0].Access_Code.value = actemp[0]+"-"+actemp[1]+"-"+inSEngine.toLowerCase()+"paidsearch";
			}
			//alert(document.forms[0].Access_Code.value);
		}
	}
//end checkAndMod
}
function delCookie(){
	Delete_Cookie('search');
}
