function replace(Content, SearchFor, ReplaceWith) {

   var tmpContent = Content;
   var tmpBefore = new String();   
   var tmpAfter = new String();
   var tmpOutput = new String();
   var intBefore = 0;
   var intAfter = 0;

   if (SearchFor.length == 0)
      return;


   while (tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase()) > -1) {
   
      // Get all content before the match
      intBefore = tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase());
      tmpBefore = tmpContent.substring(0, intBefore);
      tmpOutput = tmpOutput + tmpBefore;

      // Get the string to replace
      tmpOutput = tmpOutput + ReplaceWith;


      // Get the rest of the content after the match until
      // the next match or the end of the content
      intAfter = tmpContent.length - SearchFor.length + 1;
      tmpContent = tmpContent.substring(intBefore + SearchFor.length);

   }

   return tmpOutput + tmpContent;

}

function generateGuid()
{
var result, i, j;
result = '';
for(j=0; j<32; j++)
{
if( j == 8 || j == 12|| j == 16|| j == 20)
result = result + '-';
i = Math.floor(Math.random()*16).toString(16).toUpperCase();
result = result + i;
}
return result
}

function setCookie(c_name,value,expiredays)
{
	var exdate = new Date();
	var today = new Date(); 
	var zero_date = new Date(0,0,0); 
	var expires_date = new Date( today.getTime() + (15*60*1000)); 
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+expires_date.toGMTString());
} 


function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 ;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return ""
}
var uid;
var re
if (window.name.length > 0 ) 
{
	if(window.name.indexOf("tracking|")>=0)
	{
		uid=window.name.split("|")[1];
		setCookie('uid',uid,350000);
	}else{
		if (getCookie('uid')=='')
		{
			uid = generateGuid();
			setCookie('uid',uid,350000);
			window.name = 'tracking|' + uid;
		}else{
			uid = getCookie('uid');
			window.name = 'tracking|' + uid;
		}
	}
}
else
{
	if (getCookie('uid')=='')
	{
		uid = generateGuid();
		setCookie('uid',uid,350000);
		window.name = 'tracking|' + uid;
	}else{
		uid = getCookie('uid');
		window.name = 'tracking|' + uid;
	}
}


function getUrlVars(url){   
	var vars = [], hash;    
	var hashes = url.slice(url.indexOf('?') + 1).split('&');     
	for(var i = 0; i < hashes.length; i++)    
		{        hash = hashes[i].split('=');
		vars.push(hash[0]);        
		vars[hash[0]] = hash[1];   
		}     
	return vars;
} 
var hash = getUrlVars(window.location.href);
var cbuid;
cbuid='';
var referrer;
referrer='';
if (document.referrer!= '')
{
referrer=document.referrer;
}

if (hash['lgn']) {
	cbuid=hash['lgn'];
}
if (hash['LGN']) {
	cbuid=hash['LGN'];
}
if(cbuid==''){
	hash = getUrlVars(referrer);
	if (hash['lgn']) {
		cbuid=hash['lgn'];
	}
	if (hash['LGN']) {
		cbuid=hash['LGN'];
	}
}
document.write("<img style='top:-10000;position:absolute' src='http://www.coupon-bug.com/tracking/track.asp?uid=" + uid +"&page=" + replace(document.location.href,'&','~') + "&cbuid=" + cbuid + "&ref=" + replace(referrer,'&','~') + "'/>");

