/*
js_script.js
Software Version: Affedit 1.1
Sends referrer info to database and 
inserts id in tracking variables
Copyright: Peter Cartwright of affedit.com  -   2006
*/



function insertId(h)
{
  // buy.at
  if (h.indexOf('LID=') != -1)
    return h.replace('LID=','LID='+ref_id+'_');
    
  // affiliate future  
  else if (h.indexOf('affiliatefuture.com') != -1)
    return h.replace('tracking=','tracking='+ref_id+'_');    
  
 // custom manskincareproducts 
  else if (h.indexOf('site=') != -1)
    return h.replace('tracking=','tracking='+ref_id+'_manskincareproducts');   


    
  else return h;  
}

// displays text in status bar
function statusChange()
{
  window.status = "mysite.com";
  return true;
}

function statusChangeBack()
{
  window.status = '';
  return true;
}

//--------------------------------------------------------------
//            CHECK README.TXT BEFORE CHANGING ANYTHING BELOW
//--------------------------------------------------------------



var httpReq;  
var ref_id;


// add trim spaces method
String.prototype.trim = function()
{
  return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

function stateChangeHandler() 
{ 
  if(httpReq.readyState == 4) 
  { 
    if(httpReq.status == 200) 
    { 
      ref_id = httpReq.responseText.trim();
      writeCookie("ref_id",ref_id,30);
      // assign id to links      
      assignId();
    }
  } 
} 



function getRowId(url) 
{  
  // Mozilla etc 
  if (window.XMLHttpRequest) 
  { 
    httpReq = new XMLHttpRequest(); 
    if (httpReq) 
    { 
      httpReq.onreadystatechange = stateChangeHandler; 
      httpReq.open("GET", url, true);       
      httpReq.send(null); 
    }       
  }
  // IE version 
  else if (window.ActiveXObject) 
  { 
    httpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
    if (httpReq) 
    { 
      httpReq.onreadystatechange = stateChangeHandler; 
      httpReq.open("GET", url, true);       
      httpReq.send();     
    } 
  } 
}

// set cookie name, value and expiry period
function writeCookie(cookieName, cookieValue, factor)
{
  var period=new Date();
  var day = 1000*3600*24;
  period.setTime(period.getTime()+factor*day);
  myCookie=cookieName+"="+cookieValue+";expires="+period.toGMTString()+";path=/";
  document.cookie=myCookie;
}

// gets value of named cookie
function readCookies(cookieName)
{
  if(document.cookie)
  {
    var theCookie=document.cookie;
    var cookieSemicolon=theCookie.split(";");
    var cookieValue=getNamedVar(cookieName,cookieSemicolon);
    if(cookieValue) return cookieValue;
    else return false;
  }
}

// extracts value of named variables from array elements delimitable by "="
function getNamedVar(varName,arrName)
{
  for (i=0;i<arrName.length;i++)
  {
    var arrEqual=arrName[i].split('=');
    if(arrEqual[0].trim() == varName) return arrEqual[1];
  }
  return false;
}

// get value of 'ad' from url
function getAd(x)
{
  var queryString = window.location.search.substring(1);
  var ampSplit = queryString.split('&');  
  return getNamedVar(x,ampSplit);  
}

function getLogPath()
{
  var linkSrc = document.getElementById("logPath").src;
  return linkSrc.replace('js_script.js','');
}

// assign row id and event handlers to links
function assignId()
{  
  // do links
  var a = document.getElementsByTagName("a");
  for (var i = 0; i < a.length; i++)
  {
     a[i].onmouseover = statusChange;
     a[i].onmouseout = statusChangeBack;     
     
     // inserts id into affiliate links
     a[i].href =  insertId(a[i].href);    
  }
  
  // do iframes  
 /*
  var f = document.getElementsByTagName("iframe");
  for (var i = 0; i < f.length; i++)
  {
    f[i].src = insertId(f[i].src);
  }
  */  
}


window.onload = function()
{ 
    var ad = getAd("ad")? getAd("ad"): "";
    var urlStr="";
    ref_id = readCookies("ref_id");

    // get path to affedit folder from <script> element
    var logPath = getLogPath();

    var docRef =  encodeURIComponent(document.referrer);

    // new visitor
    if(!ref_id && (document.referrer || ad))
    {       
      urlStr = "?jsref="+docRef+"&ad="+ad;

      getRowId(logPath+"ref_logger_js.php"+urlStr);
    }
    // old visitor
    else assignId();
}




