/* UPDATED: 10/09/10a */
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] = "google";
allSEs[1] = "yahoo";
allSEs[2] = "bing";
allSEs[3] = "aol";
allSEs[4] = "ask";
allSEs[5] = "gigablast";
allSEs[6] = "excite";
allSEs[7] = "altavista";
allSEs[8] = "joeant";
allSEs[9] = "live";
allSEs[10] = "lycos";
allSEs[11] = "msn";
allSEs[12] = "netscape";
allSEs[13] = "cuil";
allSEs[14] = "infospace";
allSEs[15] = "alexa";
allSEs[16] = "looksmart";
allSEs[17] = "dogpile";
allSEs[18] = "search";
//allSEs[19] = "test";

//allSEs[16] = "opendirectory"; ?? information.com ??

//allSEs[10] = "iwon"; //ask
//allSEs[1] = "alltheweb"; //yahoo
//allSEs[29] = "galaxy"; // google
//allSEs[23] = "hotbot"; // multi-engine
//allSEs[0] = "20search"; //multi-engine
//allSEs[17] = "webcrawler"; //multi-engine
//allSEs[13] = "mamma"; //infospace

// 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();

// GET YAHOO/GOOGLE/BING FOR INTERNATIONALS
if(SECOM.indexOf("google") > -1){
	SE = "google";
}
if(SECOM.indexOf("yahoo") > -1){
	SE = "yahoo";
}
if(SECOM.indexOf("bing") > -1){
	SE = "bing";
}
if(SECOM.indexOf("mail") > -1 || SECOM == "" || SE == ""){
	SE = "EMAIL";
}
if(location.href.indexOf("test") > -1){
	//SE = "test";
}

//DETERMINE IF REFERRER IS SEO OR SEM
var inType = ""; //default

if(AllSEsString.search(SE) > -1){
	if(SE == "google" || SE == "bing" || SE == "yahoo" || SE == "test"){
		if(document.forms[0] && (document.forms[0].Access_Code || document.forms[0].Access_code || document.forms[0].access_code)){
			if(document.forms[0].Access_Code){
				ACTag = eval("document.forms[0].Access_Code");
			}
			if(document.forms[0].Access_code){
				ACTag = eval("document.forms[0].Access_code");
			}
			if(document.forms[0].access_code){
			ACTag = eval("document.forms[0].access_code");
			}
			//determine Access Code type
			if(ACTag.type == "select-one"){
				if(ACTag[1].value.length > 4){
					var actemp = ACTag[1].value.split("-");
				}else{
					var actemp = ACTag[2].value.split("-");
				}
				//edit select list
			}
			if(ACTag.type == "hidden"){
				var actemp = ACTag.value.split("-");
			}
			if(actemp[4] == "PPC" || actemp[4] == SE+"paidsearch"){
				inType = "PSEARCH";
			}else{
				inType = "NSEARCH";
			}
		}else{
			if(window.location.href.indexOf('?gclid=') > -1){
				//alert('test');
				inType = "PSEARCH";
			}else{
				inType = "NSEARCH";
			}
		}
	}else{
		inType = "";
	}
	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();
}

var ACTag = "";

function checkAndMod(){
	if(document.forms[0] && (document.forms[0].Access_Code || document.forms[0].Access_code || document.forms[0].access_code)){
		if(document.forms[0].Access_Code){
			ACTag = eval("document.forms[0].Access_Code");
		}
		if(document.forms[0].Access_code){
			ACTag = eval("document.forms[0].Access_code");
		}
		if(document.forms[0].access_code){
			ACTag = eval("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];
		}

		if(inSType == "PSEARCH"){
			//alert('test2');
			return false;
		}
		//SAMPLE: SCHOOL-PROGRAM-SEARCHENGINE-NSEARCH-SEO-NONEAVAIL
		//SAMPLE: NEC-MAPP-YAHOONATIONALSEARCH
		//SAMPLE: KSU-MMEGEN-GOOGLENATSEARCH
		if(ACTag.type == "select-one"){
			for(s=0; s<ACTag.length; s++){
				if(ACTag[s].value.length > 4){
					var actemp = ACTag[s].value.split("-");
					if(actemp[0] == "EK"){
						actemp[0] = actemp[1];
						actemp[1] = actemp[2];
					}
					if(actemp.length == 3 && (actemp[2].indexOf("GOOGLE") > -1 || actemp[2].indexOf("YAHOO") > -1 || actemp[2].indexOf("MSN") > -1 || actemp[2].indexOf("ADCENTER") > -1 || SE == "EMAIL" || actemp[2] == "onlinembasitesem" || actemp[2] == "GOOGLESEARCH" || actemp[2] == "remarketing" || actemp[2].indexOf("NATSEARCH") > -1 || actemp[2].indexOf("NATCONTENT") > -1 || actemp[2].indexOf("LOCCONTENT") > -1 || actemp[2].indexOf("LOCSEARCH") > -1 || actemp[2].indexOf("REGCONTENT") > -1 || actemp[2].indexOf("REGSEARCH") > -1)){
						//alert(actemp.length + " : " + ACTag[s].value);
						return false;
					}
					if(actemp.length > 3 && (actemp[4] == "PPC" || actemp[3] == "NATSEA" || actemp[3] == "NATCON" || actemp[3] == "LOCSEA" || actemp[3] == "LOCCON" || actemp[3] == "REGSEA" || actemp[3] == "REGCON" || actemp[3].indexOf("GOOGLE") > -1 || actemp[3].indexOf("YAHOO") > -1 || actemp[3].indexOf("MSN") > -1 || actemp[2].indexOf("GOOGLE") > -1 || actemp[2].indexOf("YAHOO") > -1 || actemp[2].indexOf("MSN") > -1 || actemp[2].indexOf("ADCENTER") > -1 || actemp[3].indexOf("ADCENTER") > -1 || SE == "EMAIL")){
						return false;
					}else{
						actemp[2] = inSEngine; //WHICH SE
						actemp[3] = inSType; //LEAD TYPE
						if(inSType == "NSEARCH"){
							if(document.href.indexOf('theonlinembasite.com') > -1){
								ACTag[s].value = actemp[0]+"-"+actemp[1]+"-onlinembasiteseo";
							}else if(document.href.indexOf('theonlinembasite.com') > -1){
								ACTag[s].value = actemp[0]+"-"+actemp[1]+"-northeasterndegreesseo";
							}else{
								ACTag[s].value = actemp[0]+"-"+actemp[1]+"-"+inSEngine.toLowerCase()+"naturalsearch";
							}
						}
						if(inSType == "PSEARCH"){
							ACTag[s].value = actemp[0]+"-"+actemp[1]+"-"+inSEngine.toLowerCase()+"paidsearch";
						}
						//alert(ACTag[s].value);
					}
				}
					//alert("apres : " + ACTag[s].value);
			}
		}
		if(ACTag.type == "hidden"){
			var actemp = ACTag.value.split("-");
			if(actemp[0] == "EK"){
				actemp[0] = actemp[1];
				actemp[1] = actemp[2];
			}
			if(inSEngine == actemp[2] || SE == "EMAIL"){
				return false;
			}
			if(actemp.length == 3 && (actemp[2].indexOf("GOOGLE") > -1 || actemp[2].indexOf("YAHOO") > -1 || actemp[2].indexOf("MSN") > -1 || actemp[2].indexOf("ADCENTER") > -1 || SE == "EMAIL" || actemp[2] == "onlinembasitesem" || actemp[2] == "GOOGLESEARCH" || actemp[2] == "remarketing" || actemp[2].indexOf("NATSEARCH") > -1 || actemp[2].indexOf("NATCONTENT") > -1 || actemp[2].indexOf("LOCCONTENT") > -1 || actemp[2].indexOf("LOCSEARCH") > -1 || actemp[2].indexOf("REGCONTENT") > -1 || actemp[2].indexOf("REGSEARCH") > -1)){
				//alert(actemp.length + " : " + ACTag.value);
				return false;
			}
			else if(actemp.length > 3 && (actemp[4] == "PPC" || actemp[3] == "NATSEA" || actemp[3] == "NATCON" || actemp[3] == "LOCSEA" || actemp[3] == "LOCCON" || actemp[3] == "REGSEA" || actemp[3] == "REGCON" || actemp[3].indexOf("GOOGLE") > -1 || actemp[3].indexOf("YAHOO") > -1 || actemp[3].indexOf("MSN") > -1 || actemp[2].indexOf("GOOGLE") > -1 || actemp[2].indexOf("YAHOO") > -1 || actemp[2].indexOf("MSN") > -1 || actemp[2].indexOf("ADCENTER") > -1 || actemp[3].indexOf("ADCENTER") > -1 || SE == "EMAIL")){
				return false;
			}else{
				actemp[2] = inSEngine; //WHICH SE
				actemp[3] = inSType; //LEAD TYPE
				if(inSType == "NSEARCH"){
					ACTag.value = actemp[0]+"-"+actemp[1]+"-"+inSEngine.toLowerCase()+"naturalsearch";
				}
				if(inSType == "PSEARCH"){
					ACTag.value = actemp[0]+"-"+actemp[1]+"-"+inSEngine.toLowerCase()+"paidsearch";
				}
			//alert(ACTag.value);
			}
		}
	}
//end checkAndMod
}
//alert(document.forms[0].Access_Code.value);

function delCookie(){
	Delete_Cookie('search');
}

