var wSrch = window.location.search;
browsername=navigator.appName;

var StrID;

function getCookie(sName)
{// function to check for cookie name compared to name passed into function. Then adds string to URL if found - if not found initiates create cookie method

	if (document.cookie.length>0)
	{
	  c_start=document.cookie.indexOf(sName + "=");
		  if (c_start==-1)
			{
			createCookie(sName);
			}
	  
	  else{ alert("Check but not create cookie");
		  var aCookie = document.cookie.split("; ");
		  for (var i=0; i < aCookie.length; i++)
		  {
			// a name/value pair (a crumb) is separated by an equal sign
				
				
			var aCrumb = aCookie[i].split("=");
			if (sName == aCrumb[0]) {  
			
			if (window.location.search == ""){
				window.location.search = "?StrID=" + aCrumb[1];
			}
			else{
			if (wSrch.indexOf("StrID=")==-1){
				window.location.search = window.location.search + "&StrID=" + aCrumb[1];}
				}
			}
		  
		  }
	  }
	}
}
function createCookie(sName){
//first checks if search string is null - if y then no parent, no q str, if yes check q string for parent, if n then process variables in q string to qAdd
	if (wSrch=="") {document.cookie="Parent=Null"; var parID = null;}
	else {

	if
		(wSrch.indexOf("StrID")==-1)
		{var qAdd = "";
		document.cookie="Parent=Null";
		var parID = null;
		alert('Hitting search str but no parent');
		var questSplit = wSrch.split("?");
		var qStrs = questSplit[1].split("&");
			for ( var i=0; i < qStrs.length; i++)
			{
			var qAdd = qAdd + qStrs[i] + "_"
			alert(qAdd);
			}
		}
		
		else 
		{alert('Parent Check');
		var parSelf = wSrch.indexOf("StrID=");
		var parSplit = wSrch.substring(parSelf + 6, parSelf + 16);
		var parID = parSplit;
		document.cookie="Parent=" + parID;
		var qAdd = "NULL";
		}
		
				
			
	}
	alert('past parent cookie checkpoint');

function makeid()
		{
			var text = "";
			var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

			for( var i=0; i < 10; i++ )
				text += possible.charAt(Math.floor(Math.random() * possible.length));

			return text;
		}
//returns random ID, defines cookie, adds random ID to search str
alert('past makeID');
		var ref = document.referrer;
		if (ref =="") {ref = "NULL";}
		var host = window.location.hostname;
		var test2 = makeid();
		document.cookie= sName + "=" + test2;
		
		
		window.location.search = "?StrID=" + test2;
		//alert('Set ID');
		
		
		
			
		//else{
		//if (wSrch.indexOf("StrID=")==-1){
		//window.location.search = window.location.search + "&StrID=" + test2;}
		//}
		
		//window.location.search = "?StrID=" +test2;		
		
//packages and sends httpopen data to server for insertion to DB			
		alert('past cookie');
		var htReq = "http://stage.catalysis.com/STrack/api.php?StrID="+ test2 + "&ParentID=" + parID + "&host=" + "XDR" + "&referrer=" + ref + "&Date=11990" + "&qstring=" + qAdd;
		alert(htReq);
		
		
if(XMLHttpRequest)
{
  var request = new XMLHttpRequest();
  if (browsername.indexOf("Microsoft")==-1)
  {
  alert('FF3!');
   // Firefox 3.5 and Safari 4
   request.open('post', htReq, true);
  request.onreadystatechange=function() {
			if (request.readyState==4) {
			alert(request.responseText)
			}
		}
		request.send(null)
		}
  else if (XDomainRequest)
  {
   // IE8
   alert('IE8!');
   var xdr = new XDomainRequest();
   xdr.open("POST", htReq);
   xdr.send();
 
   // handle XDR responses -- not shown here :-)
  }
 }
 // This version of XHR does not support CORS  
 // Handle accordingly

		
		//JSONRequest.post(htReq, {name:"Tracking!"}, function(sn, result, error){alert([sn, result, error])});	
		
		

alert('past httpopen');



		
}


function trackClick(action, dest)
{

alert("ClickTrack Open");
var tracked = "http://stage.catalysis.com/STrack/clickapi.php?StrID=" + StrID + "&Action=" + action + "&url=" + dest;
alert(tracked);

if(XMLHttpRequest)
{
  var request = new XMLHttpRequest();
  if (browsername.indexOf("Microsoft")==-1)
  {
  alert('FF3!');
   // Firefox 3.5 and Safari 4
   request.open('post', htReq, true);
  request.onreadystatechange=function() {
			if (request.readyState==4) {
			alert(request.responseText)
			}
		}
		request.send(null)
		}
  }
  else if (XDomainRequest)
  {
   // IE8
   var xdr = new XDomainRequest();
   xdr.open("POST", tracked);
   xdr.send();
 
   // handle XDR responses -- not shown here :-)
  }
 
 // This version of XHR does not support CORS  
 // Handle accordingly
}
//xmlhttp = new XMLHttpRequest();
//alert('xml http opened');
//xmlhttp.open('post', tracked ,true);
//xmlhttp.onreadystatechange=function() {
	//		if (xmlhttp.readyState==4) {
		//	alert(xmlhttp.responseText)
			//}
		//}
//xmlhttp.send(null)
//alert("ClickTrackSent");
}



