//----------------------------------------------------------------------
// Referral tracking system
// Copyright 2008 Reverb Marketing, Inc.
// http://www.reverb-marketing.com
// author: Dan Royer (dan@reverb-marketing.com)
//----------------------------------------------------------------------
var __swapRef = "referral_source";	
var __site_url= "talanproducts.com";

//----------------------------------------------------------------------
// __body_onload()
// the entry function called from html pages by delaring
//   <body onload='__body_onload();'>
//----------------------------------------------------------------------
function __body_onload() {
  if(document.referrer.toLowerCase() != "") {
    x=document.referrer.toLowerCase().indexOf("http://www."+__site_url);
    y=document.referrer.toLowerCase().indexOf("http://"+__site_url);
    if( (x<0 && y<0) || (x>5 && y<0) || (y>5 && x<0) ) {
      SetCookie(__swapRef,document.referrer.toLowerCase());
    }
  }

  // Look for cookie with Phone number
  x=GetCookie(__swapRef);
  if(x != null) {
    SwapValueInElemID(x, __swapRef);
  }
}


//----------------------------------------------------------------------
// SwapValueInElemID()
// value swap used to replace text
//----------------------------------------------------------------------
function SwapValueInElemID(sText, theID) {
  var swapObj = document.getElementById(theID);
  if (swapObj != null)
    swapObj.value = sText;
}


//----------------------------------------------------------------------
// SetCookie()
// name - name of the cookie
// value - value of the cookie
// [days] - expiration # days from today of the cookie
//   (defaults to end of current session)
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
//----------------------------------------------------------------------
function SetCookie(name, value, days) {
  var expires = "";
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000) );
    expires = "expires="+date.toGMTString()+";";
  }
  document.cookie = name+"="+value+"; "+expires+" path=/";
}


//----------------------------------------------------------------------
// GetCookie()
// name - name of the desired cookie
// return string containing value of specified cookie or null
// if cookie does not exist
//----------------------------------------------------------------------
function GetCookie(name) {
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
  return null;
}
