function $(id) { return document.getElementById(id); }

function firstPV() {
	var _gaPage = null;
	
	if (document.location.host == "checkout.netsuite.com") {
		if ($("newcust")) _gaPage = "/register";
		else if ($("login")) _gaPage = "/login";
		else if ($("address")) _gaPage = "/checkout-address";
		else if ($("shippingmethodtable")) _gaPage = "/checkout-shipping";
		else if ($("paymeth")) _gaPage = "/checkout-payment";
		else if (document.title == "Review and Submit Order - LittleMissMatched") _gaPage = "/checkout-review";
		else if (document.location.search.indexOf("category=thanks") > -1) _gaPage = "/checkout-thankyou";
	}
	else if ($("carttable"))
		_gaPage = "/cart";
		
	_gaPage == null ? _gaq.push(['_trackPageview']) : _gaq.push(['_trackPageview', _gaPage]);
}
_gaq.push(firstPV);
_gaq.push(['_trackPageLoadTime']);

//This function extracts the "_utma string" from the browser's cookies set by Google Analytics
function get_utma(l,n,s) {
	if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
	var i,i2,i3,c="-";
	i=l.indexOf(n);
	i3=n.indexOf("=")+1;
	
	if (i > -1) 
	{
	i2=l.indexOf(s,i); 
		if (i2 < 0) 
		{ 
		i2=l.length; 
		}
	c=l.substring((i+i3),i2);
	}
return c;
}
	 
//This function extracts the "visit count" value from the _utma cookie
function get_visit_count(str) {
	var i = str.lastIndexOf(".");
	i++;
	var vc = str.substring(i);
	return vc;
}

// This function was taken from the original urchin.js (aka Google Analytics v1).
function _uGC(l,n,s) {
   // used to obtain a value form a string of key=value pairs
   if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
   var i,i2,i3,c="-";
   i=l.indexOf(n);
   i3=n.indexOf("=")+1;
   if (i > -1) {
	  i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
	  c=l.substring((i+i3),i2);
   }
   return c;
}

// This code sets GA custom variable 1 through 3 to the first visit's source, medium, and keyword.
function cv123() {
	var utma = get_utma(document.cookie, '__utma=', ';');
	var visit_count = get_visit_count(utma);
	var b = _uGC(document.cookie, "__utmb=", ";");
	var b_parts = b.split(".");
	var pageview_count = b_parts[1];
	
	if ( (document.location.host == "www.littlemissmatched.com") && (visit_count == 1) && (pageview_count == 1) ) {
		var z = _uGC(document.cookie, "__utmz=", ";");
		var fv_source = _uGC(z,"utmcsr=", "|");
		var fv_medium = _uGC(z,"utmcmd=", "|");
		var fv_keyword = _uGC(z,"utmctr=", "|");
		var gclid = _uGC(z,"utmgclid=","|");
		if (gclid != "-") {
			fv_source = "google";
			fv_medium = "cpc";
		}
		_gaq.push(['_setCustomVar', 1, 'Source', fv_source, 1]);
		_gaq.push(['_setCustomVar', 2, 'Medium', fv_medium, 1]);
		_gaq.push(['_setCustomVar', 3, 'Keyword', fv_keyword, 1]);
		_gaq.push(['_trackPageview', '/vp/cv-code.htm']);
	}
}
_gaq.push(cv123);
// End custom variable code.
