﻿
var version = '1.0002';


// this is to make sure the self refers are NOT
var a = document.URL.split("//"); // split at protocol
a = (a[1] ? a[1] : a[0]).split("/");

var selfDomain = 'http://' + a[0]
var docRefer = 'http://';
var handled = false;
var cleanKeywords = true;
var keyWords = '';
var keyDomain = '';


//See If we need to 
if (doReferSystem() == true)
{

    //alert(docRefer);
    //http://www.google
    
    //Reading the Query String from the Referer
    var querystring = getQueryString();
  
  
  //Google Tracking
    if(handled == false){
        if (Left(docRefer,17)== 'http://www.google'  || Left(docRefer,13) == 'http://google' ){
            keyDomain = "GOOGLE"
            keyWords = querystring['q'];
            handled = true;
        }
    }
    
    //MSN Live
    if(handled == false){
        if (Left(docRefer,16)== 'http://www.live.'  || Left(docRefer,12) == 'http://live.'  ||  Left(docRefer,19) == 'http://search.live.' ){
            keyDomain = "LIVE"
            keyWords = querystring['q'];
            handled = true;
        }
    }
    
    //Yahoo
    if(handled == false){
        if (Left(docRefer,17)== 'http://www.yahoo.'  || Left(docRefer,13) == 'http://yahoo.'  ||  Left(docRefer,20) == 'http://search.yahoo.' ){
            keyDomain = "YAHOO"
            keyWords = querystring['p'];
            handled = true;
        }
    }
    
    //Lycos
    if(handled == false){
        if (Left(docRefer,17)== 'http://www.lycos.'  || Left(docRefer,13) == 'http://lycos.'  ||  Left(docRefer,20) == 'http://search.lycos.' ){
            keyDomain = "LYCOS"
            keyWords = querystring['query'];
            handled = true;
        }
    }
    
    //Dont know , so will save Full URL
    if(handled == false){
            keyDomain = "UNKNOWN"
            keyWords = docRefer;
            handled = true;
            cleanKeywords = false;
    }

    //Removing the + and | signs from the keywords
    if(cleanKeywords == true){
        cleanKeywordsData();
    }

  if(handled == true)
  {
    //Reading the OLD cookie
    var oldCcookieinfo = readCookie("icreon_tracker")
    if(oldCcookieinfo){
    }
    else
    {
        oldCcookieinfo = '';
    }
    //Taking Old cookie value , adding new text
    //and creating a new cookie
    var cookieinfo = oldCcookieinfo + '|' + keyDomain + '|' + keyWords ;
    createCookie('icreon_tracker',cookieinfo)
    //alert(cookieinfo);
    //alert(keyDomain + '  ' + keyWords); 
  }
 
}
else
{
    //alert('same domain refere');
}
//

function cleanKeywordsData(){
    keyWords = keyWords.replace(/\+/gi, ' ');
    keyWords = keyWords.replace(/\|/gi, ' ');
}

function doReferSystem(){
    //docRefer = document.referrer;
    if (document.referrer && document.referrer!="")
        {
            docRefer =  document.referrer;
        }
    else
        {
            docRefer = selfDomain;
        }
    
    //Now Lets check if its the Same refers        
    if(docRefer.length >= selfDomain.length)
    {
        var lenofSelfDomain = selfDomain.length;
        var referRoot = Mid(docRefer,0,lenofSelfDomain)
        //Okay so Its Not the Same Domain at the Site
        if(referRoot != selfDomain)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else if(docRefer.lenght == selfDomain.lenght)
    {
        return false;
    }

       
}



function Mid(str, start, len)
{
    
// Make sure start and len are within proper bounds
    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}


function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}



function getQueryString ()
{
     var querystring = new Array;
     
     // parse current url into an array with the keys/values
     var q = String (docRefer).split ('?')[1];
     if (!q) return false;
     q = q. split ('&');

     for (var i = 0 ; i < q.length; i++)
     {
       // for each key/value, split them at the '='
       // and add them to the qerystring array
       var o = q[i].split('=');
       querystring[o[0]] = o[1];
     }

     // return the querystring
     return querystring;
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	value =  escape(value);
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	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 unescape(c.substring(nameEQ.length,c.length));
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
