// var append_script_name = '.dev';
var append_script_name = '';

function pdsearchError(d,errorstr) {
  if ( errorstr && errorstr.length > 0 ) {
    var referer = 'Empty';
    var allcookies = 'None';
    
    if ( d.cookie ) 
      allcookies = escape(d.cookie);
    
    if ( d.location.referer && d.location.referer.length > 0 )
      referer = escape(d.location.referer);
    
    var errorimage = '//ps.digitalriver.com/cgi-bin/st'+append_script_name+'/error?url='+
         escape(d.location.href)+'&referer='+referer+'&error='+escape(errorstr)+'&domain='+
         escape(d.location.hostname)+'&cookies='+allcookies;
    var errorimage_tag = '<IMG SRC="'+errorimage+'" border="0" HEIGHT="1" WIDTH="1">';
    document.write(errorimage_tag);
  }
  
  return 1;
}

function pdsearchWarning(d,msg) {
  if ( msg && msg.length > 0 ) {
    var referer = 'Empty';
    var allcookies = 'None';
    
    if ( d.cookie ) 
      allcookies = escape(d.cookie);
    
    if ( d.location.referer && d.location.referer.length > 0 )
      referer = escape(d.location.referer);
    
    var errorimage = '//ps.digitalriver.com/cgi-bin/st'+append_script_name+'/error?url='+
         escape(d.location.href)+'&referer='+referer+'&warning='+escape(msg)+'&domain='+
         escape(d.location.hostname)+'&cookies='+allcookies;
    var errorimage_tag = '<IMG SRC="'+errorimage+'" border="0" HEIGHT="1" WIDTH="1">';
    document.write(errorimage_tag);
  }
  
  return 1;
}


// d = Always document.
// cName = name of Cookie.
function getCookieValue(d,cName) {
  var value;
  
  if ( d && d.cookie) {
    var allcookies = unescape(d.cookie);
    
    if ( allcookies ) {
      var pos = allcookies.indexOf(cName);
      if ( pos != -1 ) {
        var start = pos + cName.length + 1;
        var end = allcookies.indexOf(";",start);
        
        if ( end == -1 ) end = allcookies.length;
        
        value = unescape(allcookies.substring(start,end));
      }
    }
  }
  
  return value;
}



function pdsearchRedirect(d,pdsearchParam) {
  if ( d ) {
    var this_url = d.location.href;
    if ( this_url ) {
      var questionMarkPos = this_url.indexOf('?');
      if ( questionMarkPos != -1 ) {
        var pdSearchPos = this_url.indexOf(pdsearchParam+'=',questionMarkPos);
        if ( pdSearchPos != -1 ) {
          pdSearchPos += pdsearchParam.length+1;
          var end = this_url.indexOf('&',pdSearchPos);
          if ( end == -1)
            end = this_url.length;
            
          var params = this_url.substring(pdSearchPos,end);

          if ( params && params.length > 0 ) {
            var data = params.split('/');
            
            var clientCookieValue = getCookieValue(d,data[0]+'_stid');
            
            if ( !clientCookieValue ) clientCookieValue = '';
            
            var siteTesterValue = getCookieValue(d,data[0]+'_'+data[1]+'_testerid');
            if ( !siteTesterValue ) siteTesterValue = '';
               
            var referer = 'Empty';
            if ( d.location.referer && d.location.referer.length > 0 ) 
              referer = escape(d.location.referer);
            
            var script_url = '//ps.digitalriver.com/cgi-bin/st'+append_script_name+
                  '/js/'+params;
            var script_query = '';
            if ( referer.length > 0 ) {
              if (script_query != '' )
                script_query += '&';
              else
                script_query = '?';
              
              script_query += 'referer='+referer;
            }

            if ( clientCookieValue.length > 0 ) {
              if (script_query != '' )
                script_query += '&';
              else
                script_query = '?';
              
              script_query += 'uid='+clientCookieValue;
            }

            if ( this_url.length > 0 ) {
              if (script_query != '' )
                script_query += '&';
              else
                script_query = '?';
              
              script_query += 'url='+this_url;
            }
            
            var allcookies = d.cookie;
            if ( allcookies.length > 0 ) {
              if (script_query != '' )
                script_query += '&';
              else
                script_query = '?';
              
              script_query += 'cookies='+escape(allcookies);
            }

            if ( siteTesterValue.length > 0 ) {
              if (script_query != '' )
                script_query += '&';
              else
                script_query = '?';
              
              script_query += 'tester_id='+escape(siteTesterValue);
            }

            if ( d.location.hostname.length > 0 ) {
              if (script_query != '' )
                script_query += '&';
              else
                script_query = '?';
              
              script_query += 'domain='+d.location.hostname;
            }
            else {
              pdsearchError( d,'Javascript: Couldn\'t determine hostname.' );
            }

            var appendedScript = '<SC'+'RIPT LANGUAGE="JAVASCRIPT" src="'+script_url+script_query+'"></'+'SCRIPT>';
            
            document.write(appendedScript);
          }
          else {
            pdsearchError( d,'Javascript: No path found for search params.' );
          }
        }
        else {
          pdsearchError( d,'Javascript: Couldn\'t find \''+ pdsearchParam +'\' in query string.' );
        }
      }
      else {
        pdsearchError( d,'Javascript: Couldn\'t find \'?\' in url for start of query string.' );
      }
    }
    else {
      pdsearchError( d,'Javascript: No url found for current document.' );
    }
  }
  else {
    pdsearchError( d,'Javascript: Undefined document object specified in pdsearchRedirect function.' );
  }
}

function searchSetCookie(d) {
  if ( jscookies ) {
    for (var i=0;i<jscookies.length;i++) {
      d.cookie = jscookies[i];
    }
  }
  
  return 1;
}

// Old version changed 20080110 -k.aus.
// function searchRedirector(d) {
//   if ( url.match(/^https?:/) ) 
//     d.location.href = url;
//   else if ( url.length > 0 )
//     d.location.pathname = url
//   else {
//     pdsearchError( d,'Javascript: Unknown url passed from st ('+url+')' );
//   }
//   
//   return 1;
// }

function searchRedirector(d) {
  if ( url.length > 0 )
    d.location.replace(url);
  else {
    pdsearchError( d,'Javascript: Unknown url passed from st ('+url+')' );
  }
  
  return 1;
}

function searchRun(d) {
  if ( jscookies )
    searchSetCookie(d);
  if ( url )
    searchRedirector(d);
}



// d  = document
// cp = commerce platform
// c  = client name that matches st engine
// o  = order id
// p  = price

function searchConversion(d,cp,c,o,p) {
  if ( d ) {
    var cookie = d.cookie;
    if ( cookie ) {
      var clientid = c+"_stid";
      var id = getCookieValue(d,clientid);
      
      if ( id ) {
        p = p.replace( /^\$/,"");
        var image_url = "//ps.digitalriver.com/cgi-bin/sti"+append_script_name+"/" + cp + "/" + c + "/" + id + "/" + o + "/" + p + ".gif";

        var image_tag = '<img src="'+image_url+'" border="0" height="1" width="1">';
        document.write(image_tag);
      }
      else if ( cookie.match( /_sti\=/ ) )
        pdsearchWarning(d,"Found a possible pd search _stid match.");
    }
  }
}

