/* UPDATED: 01/08/11 */
/* VERSION: 3 */

//document.write('loading<br>');
var isAC = false;
var ACTag = ""; //Access Code tag
var ACTagVal = ""; //Access Code value, if any
var setTSE = ""; // Value being stored in the cookie
var actemp;
var inValue; // cookie value array
var inSType = ""; // cookie Search Type
var inSEngine; // cookie Search Engine
var isCAC; // cookie AC code
var CURRPage = location.href; // current page URL
var isNewPPC = false;
// 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("."));

//document.write('secom: ' + SECOM + '<br>');

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";

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){
	SE = "EMAIL";
}

// get pre-flight data
//Delete_Cookie("search"); // Delete cookie
userCookie = ReadCookie("search"); // Check cookie

checkIsAC(); // Check if there's a form on this page
checkIsNewPPC(); // Check to see if it's the new PPC string

if(isAC == true){ // If there IS a form
	getACVal(); //get current AC value
	if(document.forms[0].Will_you_be_needing_a_student_loan && document.forms[0].Will_you_be_needing_a_student_loan.value == "r "){
		document.forms[0].Will_you_be_needing_a_student_loan.value = "r " + document.referrer;
	}
	if(document.forms[0].Best_time_to_call){
		document.forms[0].Best_time_to_call.value = ACTagVal;
	}else{
		document.write('<input type="hidden" name="Best_time_to_call" value="' + ACTagVal + '">');
	}
}

// main script
if(AllSEsString.search(SE) > -1 && SECOM != ""){ // If referring IS a SE and referrer is not blank 
	if(isNewPPC == false && ACTagVal.indexOf("REMARKETING") == -1){ // if it's NOT the new PPC code and it's NOT remarketing
		if (userCookie != null){ // If there IS a cookie
			if(isAC == true){ // If there IS a form
				getCookieAC(); // get the originating code from the cookie
				acMod(); // replace the current AC with the one from the cookie
			}
		}else{ // If there is NOT a cookie
			if(isAC == true){
				actemp = ACTagVal.split("-");
				var toCookie = actemp[0]+"-"+actemp[1]+"-";
			}else{
				var toCookie = "XX-XXX-";
			}
			//determine NS or PS
			if(SE != "google" && SE != "bing" && SE != "yahoo"){ // If SE is NOT G, Y, or B
				inType = "NSEARCH"; //this is NS for sure
				ACTagVal = toCookie+SE.toLowerCase()+"naturalsearch";
			}else{
				checkForPS(); // determine if the AC is a PPC AC
				if(inSType == "NSEARCH"){
					ACTagVal = toCookie+SE.toLowerCase()+"naturalsearch";
				}
			}
			isCAC = ACTagVal; // ac value being put into cookie
			setTSE = inSType+'-'+SE.toUpperCase()+'-'+isCAC; //set cookie value
			createCookie('search',setTSE,30,'/','','');//set cookie to paid search with current ac
			if(isAC == true){
				acMod();
			}
		}
	}
}else{ // if incoming IS NOT a SE
	if (userCookie != null){ // If there IS a cookie
		if(isAC == true){ // If there IS a form
			getCookieAC(); // get the originating code from the cookie
			acMod(); // replace the current AC with the one from the cookie
		}else{
			// If there is NOT a form on this page
			if(CURRPage.indexOf("thank_you") > -1 || CURRPage.indexOf("request_processor") > -1){ // Check to see if it's a thank you page
				var TYCOM = CURRPage.substring(document.referrer.indexOf("http://")+7, CURRPage.indexOf("/",CURRPage.indexOf("http://")+9));
				if(SECOM == TYCOM){ // If it IS a thank you page
					Delete_Cookie('search'); // delete the cookie
				}
			}
		}
	}
}


//------------------------| FUNCTIONS |------------------------//
function checkIsNewPPC(){
	if(CURRPage.indexOf("kwd=") > -1 && CURRPage.indexOf("kwdmt=") > -1){
		isNewPPC = true;
	}
}

function checkIsAC(){
	if(document.forms[0] && (document.forms[0].Access_Code || document.forms[0].Access_code || document.forms[0].access_code)){
		isAC = true;
		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");
		}
		getACVal();
	}
// end isAC;
}

function getACVal(){
	if(ACTag.type == "hidden"){ //get value for single value
		ACTagVal = ACTag.value;
	}
	if(ACTag.type == "select-one"){ //get sample value for select list
		for(s=0; s<ACTag.length; s++){
			if(ACTag[s].value.indexOf("-") > 1){
				ACTagVal = ACTag[s].value;
			}
		}
	}
	if(ACTagVal.indexOf("REMARKETING") > -1){
		return false;
	}
	actemp = ACTagVal.split("-");
	if(ACTagVal == "INSERT" || ACTagVal == ""){ // if they're not there or dynamically inserted
		ACTagVal = ""; // not sure what to do here yet
	}
// getAC
}

function checkForPS(){
	actemp = ACTagVal.split("-");
	if(actemp[0] == "EK"){
		actemp.shift();
	}
	if(CURRPage.indexOf("kwd=") > -1 && CURRPage.indexOf("kwdmt=") > -1){
		inType = "PSEARCH";
		isNewPPC = true;
	}
	if(window.location.href.indexOf('?gclid=') > -1){
		inType = "PSEARCH";
		isNewPPC = true;
	}
	if(actemp.length == 3 && ACTagVal.lastIndexOf("sem") > -1){
		inType = "PSEARCH";
		isNewPPC = true;
	}
	if(actemp.length == 3){ // for 3 part ACs
		if(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 || actemp[2].indexOf("ADCENTER") > -1){
			inSType = "PSEARCH"
			isNewPPC = true;
		}
	}
	if(actemp.length > 3){ // for 4+ part ACs
		if(actemp[2].indexOf("GOOGLE") > -1 || actemp[2].indexOf("YAHOO") > -1 || actemp[2].indexOf("MSN") > -1 || actemp[2].indexOf("ADCENTER") > -1){
			inSType = "PSEARCH"
			isNewPPC = true;
		}
		if(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[3].indexOf("ADCENTER") > -1){
			inSType = "PSEARCH"
		}
		if(actemp[4] == "PPC"){
			inSType = "PSEARCH"
			isNewPPC = true;
		}
	}
	if(inSType != "PSEARCH"){
		inSType = "NSEARCH";
	}
//end checkForPS
}

function getCookieAC(){
	if(userCookie != null){
		inValue = userCookie.split("-");
		if(inValue.length > 2){
			var tempCAC = inValue.slice(2);
			isCAC = tempCAC.join("-");
		}
	}
	if(isCAC == ""){
		if(inValue[0] == "NSEARCH"){
			isCAC = actemp[0]+"-"+actemp[1]+"-"+inValue[1].toLowerCase()+"naturalsearch";
		}
		if(inValue[0] == "PSEARCH"){
			isCAC = actemp[0]+"-"+actemp[1]+"-"+inValue[1].toLowerCase()+"paidsearch";
		}
	}
//end getCookieAC
}


function acMod(){
	if(ACTagVal.indexOf("REMARKETING") > -1){
		return false;
	}
	cacTemp = isCAC.split("-");
	if(cacTemp[0] == "XX" && cacTemp[1] == "XXX"){
		var noACVal = ACTagVal.split("-");
		cacTemp[0] = noACVal[0];
		cacTemp[1] = noACVal[1];
	}
	if(isAC == true){
		if(ACTag.type == "select-one"){
			for(s=0; s<ACTag.length; s++){
				if(ACTag[s].value != "" && ACTag[s].value != "none" && ACTag[s].value != "INSERT"){
					var actemp = ACTag[s].value.split("-");
					if(actemp[0] == "EK"){
						actemp.shift();
					}
					ACTag[s].value = actemp[0]+'-'+actemp[1]+'-'+cacTemp[2];
				}
			}
		}
		if(ACTag.type == "hidden"){
			ACTag.value = cacTemp.join("-");
			//document.write('actagval: ' + ACTag.value + '<br>');
		}
	}
//end acMod
}

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=/;"
}

