function getCookieVal (offset) {
  var s
  var d
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;

  s = document.cookie.substring(offset, endstr);
  var clen = s.length;
  var i = 0;
  d = ''
  while (i < clen) {
    d += (s.substring(i, i+1) == '+') ? ' ' : s.substring(i, i+1) ;
    i = i + 1
  }

  return unescape(d);
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return '';
}

// like ReplaceString( '<A href=x.asp?affiliate=[affiliate]>', '[affiliate]', 'icanjam' )
//    returns <A href=x.asp?affiliate=icanjam>
function ReplaceString( src, name, value ) {
  var ret
  var i = 0
  var pos = 0
  var substr = ''

//document.write("<P>replacing " + name + " with " + value + " in " + src )
//document.write( "<BR>src.length=" + src.length )
//document.write( "<BR>name.length=" + name.length )
  // find the position in the string then do the replace
  while (i < src.length -name.length) {
    pos = i + name.length
    substr = src.substring(i, pos)
//document.write("<BR>src.substring("+ i +", "+ pos +")=" + substr)
    if (name == substr) {
      ret = src.substring( 0, i ) + value + src.substring( pos, src.length ) ;
//document.write("<BR>created ret=" + ret)
      return( ReplaceString( ret, name, value ) );
      break ;
    }
    i = i +1 ;
  }
  return( src )
}

function aAffiliateLink( str ) {
  var s
  if (GetAffiliate()) {
    s = ReplaceString( 
        '<A href=[affiliateurl]>[affiliatesname]</A>', 
        '[affiliateurl]', 
        GetCookie('affiliateurl') 
      )
    s = ReplaceString(
        s,
        '[affiliatesname]',
        GetCookie('affiliatesname') 
      )
  } else {
    s = '' ;
  }
  return s 
}


function aAffiliateLogo( str ) {
  var s
  if (GetAffiliate()) {

	if (GetCookie('affiliatelogo')) {
	    s = '<A href=' + GetCookie('affiliateurl') + '>' 
	    s = s + '<img src=' + GetCookie('affiliatelogo') + ' border=0>' 
		s = s + '</A>'
	} else {
	    s = ReplaceString( 
		'<A href=[affiliateurl]>[affiliatesname]</A>', 
		'[affiliateurl]', 
		GetCookie('affiliateurl') 
	      )
	    s = ReplaceString(
		s,
		'[affiliatesname]',
		GetCookie('affiliatesname') 
	      )
	}
  } else {
    s = '' ;
  }
  return s 
}


function aAffiliateLogoOLD( str ) {
  var s
  if (GetAffiliate()) {
        s = ReplaceString( 
		'<A href=[affiliateurl]><img src=[affiliatelogo] border=0></A>', 
		'[affiliateurl]', 
		GetCookie('affiliateurl') 
	      ) ;
	if (GetCookie('affiliatelogo')) {
	    s = ReplaceString(
		s,
		'[affiliatelogo]',
		GetCookie('affiliatelogo') 
	      ) 
	} else {
	    s = ReplaceString( 
		'<A href=[affiliateurl]>[affiliatesname]</A>', 
		'[affiliateurl]', 
		GetCookie('affiliateurl') 
	      )
	    s = ReplaceString(
		s,
		'[affiliatesname]',
		GetCookie('affiliatesname') 
	      )
	}
  } else {
    s = '' ;
  }
  return s 
}



function aAffiliateLogobig( str ) {
  var s
  if (GetAffiliate()) {
    s = ReplaceString( 
        '<A href=[affiliateurl]><img src="[affiliatelogobig]" border=0></A>', 
        '[affiliateurl]', 
        GetCookie('affiliateurl') 
      )
    s = ReplaceString(
        s,
        '[affiliatelogobig]',
        GetCookie('affiliatelogobig') 
      )
  } else {
    s = '' ;
  }
  return s 
}


function GetAffiliate() {
  var s
  s = GetCookie('affiliate')
  if (s == '') s = GetCookie('area_affiliate')
//  not sure why we would do this. doesn't work with code above
//if (s == '') s = 'noaffiliate'
  return s
}  