//  --------------------
//  The Affiliate Detector
//  Copyright 2009 Paulson Management Group
//  This is licenced code.  To purchase a licence for this product and obtain
//  implementation assistance please visit www.paulsonmanagementgroup.com!
//  Licensee is granted unlimited rights to use, but not resell, this script.
//  ---------------------
var PMG = { 
    // -----------
    // Functions for affiliates
    affiliates: {
        // Detect the affiliate and update the page according to the config.
        detect: function( config ) { 
            // First step is to find the affiliate id, if any.
            // -------------
            var key;
            var value;
            // for each param specified in the config
            for( idx in config.parameters ) { 
                key = config.parameters[ idx ];
                value = PMG.util.gup( key );
                if( value ) { break; }
            }   
            // if not found in parames, check the referrer
            if( !value ) { 
                for( idx in config.referrer ) { 
                    key = config.referrer[ idx ];
                    value = PMG.util.grp( key ); // NOTICE!  grp vs gup
                    if( value ) { break; }
                }   
            }   
            // if we found a value
            if( value ) { 
                // display it under the phone number
                for( idx in config.code_containers ) {
                    var code = config.code_containers[idx];
                    var code_element = document.getElementById( code );
                    code_element.innerHTML = config.prefix + " #" + value;
                }

            }   
            // Step two is to set the affiliate source
            var source = PMG.affiliates.getSource();

            if( source ) {
                // and save a cookie recording the affiliate that sent the traffic
                // PMG.util.setCookie( "SOURCE", PMG.affiliates.list[key.toLowerCase() ], 90 );
                PMG.util.setCookie( "SOURCE", source, 90 );
            }


        },
        getSource: function() {
            // if SOURCE param exists, and value is CJ, source is CJ
            if( PMG.util.gup( "SOURCE" ) && PMG.util.gup( "SOURCE" ).toLowerCase() == "cj" ) {
                return "CJ";

            // if only 'm' exists in document referrer, then it must be shareasale
            // TODO use SOURCE= or something equivalent
            } else if ( PMG.util.grp( "m" ) ) {
                return "SAS";
            } else {
                return null;
            }
            
        }
    },  
    util: {

        // GET URL PARAMETER
        gup: function( name )
        {   
          name = name.toLowerCase();
            var url = ""+window.location.href;
            url = url.toLowerCase();
          name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
          var regexS = "[\\?&]"+name+"=([^&#]*)";
          var regex = new RegExp( regexS );
          var results = regex.exec( url );
          if( results == null )
            return ""; 
          else
            return results[1];
        },  

        // GET REFERRER PARAMETER
        grp: function( name )
        {   
            if( !document.referrer ) { 
                return null;
            }   
            var url = ""+document.referrer;
            if( "" == url ) { 
                return null;
            }   

            url = url.toLowerCase();
          name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
          var regexS = "[\\?&]"+name+"=([^&#]*)";
          var regex = new RegExp( regexS );
          var results = regex.exec( url );
          if( results == null )
            return ""; 
          else
            return results[1];
        },

        // SET A COOKIE
        setCookie: function(c_name,value,expiredays) {
            var exdate=new Date();
            exdate.setDate(exdate.getDate()+expiredays);
            document.cookie=c_name+ "=" +escape(value)+
            ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
        },  

        // GET A COOKIE
        getCookie: function(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 ""; 
        }   


    }   

}


// Detect the affiliate.
PMG.affiliates.detect({
    parameters: [ "pid" ], // Paramaters that hold the affiliate ID.  Will be displayed under phone number.
    referrer: [ "m" ], // none of the URL parameters matched?  Check the referrer for these.
    code_container: "code_container", // stick the phone number inside this element.
    prefix: "Promotion Code"
});

