// JavaScript Documentdetails

function Get_Cookie(name) {
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
		path = "/";
		domain = "sunpowercorp.com";
    var cookieString = name + "=" +escape(value) +
       ( (expires) ? ";expires=" + expires.toGMTString() : "") +
       ( (path) ? ";path=" + path : "") +
       ( (domain) ? ";domain=" + domain : "") +
       ( (secure) ? ";secure" : "");
    document.cookie = cookieString;
}

function Delete_Cookie(name,path,domain) {
	domain = "sunpowercorp.com";
   if (Get_Cookie(name)) document.cookie = name + "=" +
      ( (path) ? ";path=" + path : "") +
      ( (domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}


var today = new Date();
//var zero_date = new Date(0,0,0);
//today.setTime(today.getTime() - zero_date.getTime());
var cookie_expire_date = new Date(today.getTime() + (86400000));

function setVisitorID() {
   if (Get_Cookie('VisitorID')) {
       var VisitorID = Get_Cookie('VisitorID');
   }else{
       Set_Cookie('VisitorID',today.getTime(),cookie_expire_date);
   }
}

function setSessionID() {
   if (!Get_Cookie('SessionID'))
       Set_Cookie('SessionID', today.getTime());
}

// QueryString
// Call function by x = querystring("variable") returns variable=x
function QueryString(key) {
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++) {
		if (QueryString.keys[i]==key) {
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse() {
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	for (var i=0;i<pairs.length;i++) {
		var pos = pairs[i].indexOf('=');
		if (pos >= 0) {
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;
		}
	}
}
QueryString_Parse(); 

var campaign = "";
var details = "";
var ckid = "";

var affiliate_source = "";
function OutputCampaignID() {
	var tracking_cookie = Get_Cookie('SPTracking');
	var tracking_array  = tracking_cookie.split("|");
	var affiliate = tracking_array[2];
	//document.write('<input class="input" type="hidden" name="campaignId" value="' + affiliate_source + '" />');
	document.write('<input class="input" type="hidden" name="campaignId" value="' + affiliate + '" />');
}

function OutputThankyouPgTags() {
	var tracking_cookie = Get_Cookie('SPTracking');
	var tracking_array  = tracking_cookie.split("|");
	var affiliate = tracking_array[2];
	if (affiliate == "AAYA-54N3ME") {
		// Adknowledge specific thank you page
		document.write('<input type="hidden" id="thankyouBase" name="thankyouBase" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-Affiliate-Thankyou.aspx" /><input type="hidden" id="thankyouPage" name="thankyouPage" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-Affiliate-Thankyou.aspx?status=true" /><input type="hidden" id="thankyouRedirect" name="thankyouRedirect" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-Affiliate-Thankyou.aspx" /><input type="hidden" id="thankyouEmailTemplate" name="thankyouEmailTemplate" value="EN-B2C-Status-True" />');
	}
	else  if (affiliate == "AAYA-54N3MI") {
		// Direct Agent specific thank you page
		document.write('<input type="hidden" id="thankyouBase" name="thankyouBase" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-Affiliate-Thankyou.aspx" /><input type="hidden" id="thankyouPage" name="thankyouPage" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-Affiliate-Thankyou.aspx?status=true" /><input type="hidden" id="thankyouRedirect" name="thankyouRedirect" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-Affiliate-Thankyou.aspx" /><input type="hidden" id="thankyouEmailTemplate" name="thankyouEmailTemplate" value="EN-B2C-Status-True" />');		
	}
	else  if (affiliate == "AAYA-54N3M6") {
		// Commission Junction specific thank you page
		document.write('<input type="hidden" id="thankyouBase" name="thankyouBase" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-AffiliateCJ-Thankyou.aspx" /><input type="hidden" id="thankyouPage" name="thankyouPage" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-AffiliateCJ-Thankyou.aspx?status=true" /><input type="hidden" id="thankyouRedirect" name="thankyouRedirect" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-AffiliateCJ-Thankyou.aspx" /><input type="hidden" id="thankyouEmailTemplate" name="thankyouEmailTemplate" value="EN-B2C-Status-True" />');		
	}
	else  if (affiliate == "AAYA-54N3MA") {
		// Kowabunga specific thank you page
		document.write('<input type="hidden" id="thankyouBase" name="thankyouBase" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-AffiliateKB-Thankyou.aspx" /><input type="hidden" id="thankyouPage" name="thankyouPage" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-AffiliateKB-Thankyou.aspx?status=true" /><input type="hidden" id="thankyouRedirect" name="thankyouRedirect" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-AffiliateKB-Thankyou.aspx" /><input type="hidden" id="thankyouEmailTemplate" name="thankyouEmailTemplate" value="EN-B2C-Status-True" />');		
	}
	else  if (affiliate == "AAYA-58HONE") {
		// Direct Response Taxes specific thank you page
		document.write('<input type="hidden" id="thankyouBase" name="thankyouBase" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_DRT_Thankyou.aspx" /><input type="hidden" id="thankyouPage" name="thankyouPage" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_DRT_Thankyou.aspx?status=true" /><input type="hidden" id="thankyouRedirect" name="thankyouRedirect" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_DRT_Thankyou.aspx" /><input type="hidden" id="thankyouEmailTemplate" name="thankyouEmailTemplate" value="EN-B2C-Status-True" />');		
	}
	else  if (affiliate == "AAYA-58HONI") {
		// Direct Response ROI  specific thank you page
		document.write('<input type="hidden" id="thankyouBase" name="thankyouBase" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_DRR_Thankyou.aspx" /><input type="hidden" id="thankyouPage" name="thankyouPage" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_DRR_Thankyou.aspx?status=true" /><input type="hidden" id="thankyouRedirect" name="thankyouRedirect" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_DRR_Thankyou.aspx" /><input type="hidden" id="thankyouEmailTemplate" name="thankyouEmailTemplate" value="EN-B2C-Status-True" />');
	}
	else  if (affiliate == "AAYA-58I526") {
		// Direct Response Electric Bills  specific thank you page
		document.write('<input type="hidden" id="thankyouBase" name="thankyouBase" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_DR3_Thankyou.aspx" /><input type="hidden" id="thankyouPage" name="thankyouPage" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_DR3_Thankyou.aspx?status=true" /><input type="hidden" id="thankyouRedirect" name="thankyouRedirect" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_DR3_Thankyou.aspx" /><input type="hidden" id="thankyouEmailTemplate" name="thankyouEmailTemplate" value="EN-B2C-Status-True" />');
	}
	
	else  if (affiliate == "AAYA-5CPRS5") {
		// Solio  specific thank you page
		document.write('<input type="hidden" id="thankyouBase" name="thankyouBase" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_Thankyou.aspx" /><input type="hidden" id="thankyouPage" name="thankyouPage" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_Thankyou.aspx?status=true" /><input type="hidden" id="thankyouRedirect" name="thankyouRedirect" value="http://www.sunpowercorp.com/Contact-Forms/NA_B2C_Thankyou.aspx" /><input type="hidden" id="thankyouEmailTemplate" name="thankyouEmailTemplate" value="EN-B2C-Status-True" />');
	}
	
	else {
		document.write('<input type="hidden" id="thankyouBase" name="thankyouBase" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-Thankyou.aspx" /><input type="hidden" id="thankyouPage" name="thankyouPage" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-Thankyou.aspx" /><input type="hidden" id="thankyouRedirect" name="thankyouRedirect" value="http://www.sunpowercorp.com/Landing-Pages/NA-B2C-Thankyou.aspx" /><input type="hidden" id="thankyouEmailTemplate" name="thankyouEmailTemplate" value="EN-B2C-Status-True" />');		
	}
}

function setTracking() {

	// get referrer and href
	var ref = document.referrer;
	var href = location.href;

    // Updated process to more closely match paid search/clickshift tracking
    // 1. Look for WT.srch on location.href. if found:
    // 2. - set ckid, set campaign to 'Paid Search', run _uRef to set details, set SPTracking and SPCSID cookies
    // 3. If WT.srch not found:
    // 4. - Check for existing SPTracking cookie. If not found:
    // 5. - - Check for ref(errer). If found:
    // 6. - - - Check for organic search via _uOrg. If organic:
    // 7. - - - - Set campaign and details
    // 8. - - - If not organic:
    // 9. - - - - Must be a referring site, set campaign and details
    // 10.- - If no ref(erer), check for WT.mc_id. If found:
    // 11.- - - Email Campaign, set campaign and details
    // 12.- - If WT.mc_id not found:
    // 13.- - - Must be direct entry, set campaign
    // 14.- - Set SPTracking cookie
    // 15.- - Set SPCSID if ckid is set

    // Look for WT.srch on location.href. if found:
	
	// start NO WEB TREND CODE NEEDED HERE
	/*
	if (href.indexOf("WT.srch") > 0 || href.indexOf("WT.medium=cpc") > 0) {
	
	    // If found, set details accordingly
		campaign = 'Paid Search';
		_uRef(ref);
		
		// set tracking cookie
		Set_Cookie('SPTracking', campaign+"|"+details,cookie_expire_date);
				
		// get clickshift id - 11/20/07 just use whole cshift_ck cookie
		// ckid = QueryString('cshift_ck').split("cs",1);
		ckid = QueryString('cshift_ck');
		
		// conditionally set clickshift ID
		if (ckid) {
			Set_Cookie('SPCSID', ckid, cookie_expire_date);
		} else {
			Set_Cookie('SPCSID', 'cpc', cookie_expire_date);
		}

		
	// Otherwise, if not paid search, check to see if tracking cookie has not been set			
	} else if (!Get_Cookie('SPTracking')) {
	*/
	// end NO WEB TREND CODE NEEDED HERE

    if (!Get_Cookie('SPTracking')) {
		
		// If there's a referrer, figure out which kind
		if (ref) {
			
			var lead_source_type = QueryString('LS');
			
			// is it organic search?
            if (_uOrg(ref)) {
				campaign = 'Organic Search';
				// _uOrg sets details
			}
			
			else if (lead_source_type == "PS") {
				campaign = 'Paid Search';
				_uRef(ref); // sets details
			}
			
			else if (lead_source_type == "WA") {
				campaign = 'Web Affiliate';	
				_uRef(ref); // sets details
			}
			
			else if (lead_source_type == "CPA") {
				campaign = 'CPA';	
				_uRef(ref); // sets details
			}
			
			// no, must be referral
			else {
				campaign = 'Referring Sites';
				_uRef(ref); // sets details
			}

		}

		// is it a custom, typically email, campaign link, i.e. http://www.sunpowercorp.com/?WT.mc_id=my_campaign?
		else if (href.indexOf("WT.mc_id") > 0) {
			campaign = 'Email Campaign';
			details = QueryString('WT.mc_id');
		}		
	
		// no referrer, no custom campaign, must be direct
		else {
			campaign = "Direct Entry";
		}

		
		Set_Cookie('SPTracking', campaign+"|"+details,cookie_expire_date); 
		/* start: get affiliate value from url argument and append to referring url in cookie */		 
		
		affiliate_source = QueryString('source');
		//console.log("test" + affiliate_source);
		//var bid = QueryString('bid');  // update - don't need banner id after all
		//var addldetails = details + "|" + affiliate_source + "|" + bid;
		if (affiliate_source != null && affiliate_source != "undefined")			
			var addldetails = details + "|" + affiliate_source;
		else
			var addldetails = details;
		Set_Cookie('SPTracking', campaign+"|"+ addldetails,cookie_expire_date);	
		
		/* end: affiliate name appended to referring url in cookie */
	
	// end else if (!Get_Cookie('SPTracking'))
	}
	
 
}


//-- Auto/Organic Sources and Keywords
var _uRno=new Array();
var _uOsr=new Array();
var _uOkw=new Array();
var _uOno=new Array();
_uOsr[0]="google";	_uOkw[0]="q";
_uOsr[1]="yahoo";	_uOkw[1]="p";
_uOsr[2]="msn";		_uOkw[2]="q";
_uOsr[3]="aol";		_uOkw[3]="query";
_uOsr[4]="aol";		_uOkw[4]="encquery";
_uOsr[5]="lycos";	_uOkw[5]="query";
_uOsr[6]="ask";		_uOkw[6]="q";
_uOsr[7]="altavista";	_uOkw[7]="q";
_uOsr[8]="search";	_uOkw[8]="q";
_uOsr[9]="netscape";	_uOkw[9]="s";
_uOsr[10]="cnn";	_uOkw[10]="query";
_uOsr[11]="looksmart";	_uOkw[11]="qt";
_uOsr[12]="about";	_uOkw[12]="terms";
_uOsr[13]="mamma";	_uOkw[13]="query";
_uOsr[14]="alltheweb";	_uOkw[14]="q";
_uOsr[15]="gigablast";	_uOkw[15]="q";
_uOsr[16]="voila";	_uOkw[16]="kw";
_uOsr[17]="virgilio";	_uOkw[17]="qs";
_uOsr[18]="live";	_uOkw[18]="q";
_uOsr[19]="baidu";	_uOkw[19]="wd";
_uOsr[20]="alice";	_uOkw[20]="qs";
_uOsr[21]="seznam";	_uOkw[21]="w";
_uOsr[22]="yandex";	_uOkw[22]="text";
_uOsr[23]="najdi";	_uOkw[23]="q";

function _uRef(_ur) {
 if (_ur=="0" || _ur=="" || _ur=="-") return "";
 var i=0,h,k,n;
 if ((i=_ur.indexOf("://"))<0) return "";
 h=_ur.substring(i+3,_ur.length);
 if (h.indexOf("/") > -1) {
  k=h.substring(h.indexOf("/"),h.length);
  if (k.indexOf("?") > -1) k=k.substring(0,k.indexOf("?"));
  h=h.substring(0,h.indexOf("/"));
 }
 h=h.toLowerCase();
 n=h;
 if ((i=n.indexOf(":")) > -1) n=n.substring(0,i);
 for (var ii=0;ii<_uRno.length;ii++) {
  if ((i=n.indexOf(_uRno[ii].toLowerCase())) > -1 && n.length==(i+_uRno[ii].length)) { _ufno=1; break; }
 }
 if (h.indexOf("www.")==0) h=h.substring(4,h.length);
 // alert("utmccn=(referral)|utmcsr="+_uEC(h)+"|"+"utmcct="+_uEC(k)+"|utmcmd=referral");
 details = _uEC(h);
}

function _uOrg(_ur,t) {
 if (_ur=="0" || _ur=="" || _ur=="-") return "";
 var i=0,h,k;
 if ((i=_ur.indexOf("://")) < 0) return "";
 h=_ur.substring(i+3,_ur.length);
 if (h.indexOf("/") > -1) {
 	h=h.substring(0,h.indexOf("/"));
 }
 for (var ii=0;ii<_uOsr.length;ii++) {
  if (h.toLowerCase().indexOf(_uOsr[ii].toLowerCase()) > -1) {
   if ((i=_ur.indexOf("?"+_uOkw[ii]+"=")) > -1 || (i=_ur.indexOf("&"+_uOkw[ii]+"=")) > -1) {
    k=_ur.substring(i+_uOkw[ii].length+2,_ur.length);
    if ((i=k.indexOf("&")) > -1) k=k.substring(0,i);
    for (var yy=0;yy<_uOno.length;yy++) {
     if (_uOno[yy].toLowerCase()==k.toLowerCase()) { _ufno=1; break; }
    }
    // if (t) return _uEC(k);
    // else alert("utmccn=(organic)|utmcsr="+_uEC(_uOsr[ii])+"|"+"utmctr="+_uEC(k)+"|utmcmd=organic");
		details = _uEC(_uOsr[ii])+": "+_uEC(k);
		// alert(_uEC(_uOsr[ii])+": "+_uEC(k));
		return true;
   }
  }
 }
 return false;
}


function _uEC(s) {
  var n="";
  if (!s || s=="") return "";
  for (var i=0;i<s.length;i++) {if (s.charAt(i)==" ") n+="+"; else n+=s.charAt(i);}
  return n;
}
	
var loaded_script = true; 


