//  #################################################################
//  # This file is part of the SEO Affiliate package.               #
//  # Copyright Mind In Motion Technology / Pickled Brain Inc. 2005 #
//  #################################################################
//

// Get referrer info (and filter out if referrer is our own domain
// ###############################################################

// Check the referrer for incoming request and report it to 
// SEO affiliate module: aff.cgi.  
// Report the following info:
//  + referrer (full URL of incoming referrer)
// 
// Aff.cgi will also collect the userAgent and remoteIP address.
// After collecting the info, aff.cgi will redirect to a given
// URL (passed to the function below).  
// This URL points to an image on the merchant website.
//
// The JS function below returns the content of the SRC="..."
// of an <img ... src="..."> tag which is encoded with the information
// collected and will call the aff.cgi script to record it.
// 
// If the referrer is from the same domain as the JS function will
// simply return the URL of the local image so the aff.cgi is not
// called needlessly.
//
// Note: imgURL and affCgiUrl should be absolute.
//       If aff.cgi is not hosted on the same server as website, 
//       the two parameters should also contain the http://domainName.com
//
// EX:  var imgUrl    = '/img/logo.jpg';
//      var affCgiUrl = '/cgi/aff.cgi';
//      var src_str = SEOcollectRefInfo(imgUrl, affCgiUrl); 
//      document.write('<img height=20 width=30 border=0 src="' + src_str + '">');
//  or
//    WITH DEBUG:  You can enable the debug mode from a single page (As opposed to turning it on for everybody in aff.cgi)
//     var debug = 1;
//     var src_str = SEOcollectRefInfo(imgUrl, affCgiUrl,debug); 
//
function SEOcollectRefInfo(imgUrl, affCgiUrl, debug)
{
    if (document.referrer == '') {   // No referrer information
	return(imgUrl);
    }


    // Now check to see if the referrer is coming from this domain and 
    // if so, do not report this referrer (i.e. just return imgUrl)
    // ###########################################################
    var myReferrer    = document.referrer;
    var myHostName    = window.location.hostname;
    var tempstring    = myReferrer.split("//"); 
    tempstring        = tempstring[1].split("/");
    var myRefHostName = tempstring[0].split("?");
    if (myRefHostName == myHostName.toLowerCase()) {
	return(imgUrl);
    }


    // Encode the referrer URL before passing it to aff.cgi
    var myRefEncoded  = encodeURIComponent(myReferrer);  // Encoded URL to pass as param to aff.cgi
    var myImgEncoded  = encodeURIComponent(imgUrl);      // Encoded URL to pass as param to aff.cgi

    // Build the call to aff.cgi (to be used as SRC="..." in an IMG tag)
//    var src_str = '"' + affCgiUrl + '?ref=' + myRefEncoded + '&imgUrl=' + myImgEncoded + '"';

//    var src_str = '"' + affCgiUrl + '?ref=' + myRefEncoded + '"';  # was the working code
    var src_str = '"' + affCgiUrl + '?ref=' + myRefEncoded;


    // Add a debug param if we got a debug argument ourselves
    if (debug) {
	src_str = src_str + '&debug=1';
    }

    // complete the string
    src_str = src_str + '"';

    return(src_str);
}


//   Print a transparent single dog .gif image on the landing page
//   and record SEO affiliate information (if visitor is coming from a  registered URL
//   ###################################################################################
//
function SEOrecordAffiliate()
{
   var imgUrl    = '/cart/img/cart/aff/tracker_dot.gif';  // This is displayed IF there is no referrer
   // var affCgiUrl = 'http://www.mindinmotiontech.com/cgi/show_CGI_parameters.cgi';
   var affCgiUrl = '/cgi/aff.cgi';
   var src_str = SEOcollectRefInfo(imgUrl, affCgiUrl);

   // The first line is to display the image, the second/third are there to help debug
   document.write('<img src=' + src_str + '>');
   // document.write('<IFRAME SRC=' +src_str + 'WIDTH=650 HEIGHT="400">You should not see this</IFRAME>');
   // document.write('<h4> Writing URL to img: |' +src_str + '|</h4>');  // Debug
    
}
