﻿/********************************************************************************
*	G2Tags.js
*
*   Responsible for adding tracking tags to page.
*
*	History:
*
*	8/27/2010	Steven Andekian	- First.
*   8/30/2010   Steven Andekian - Updated the page to handle both secure and non
*               secure page calls.
********************************************************************************/


//-------------------------------------------------------------------------------
//  Returns part of haystack string from the first occurrence of needle to the 
//  end of haystack. 
//
//  Return String on success and false on fail
//-------------------------------------------------------------------------------
function g2_strstr (haystack, needle, bool) {
    var pos = 0;
    
    haystack += '';
    pos = haystack.indexOf( needle );
    if (pos == -1) {
        return false;
    } else{
        if (bool){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}


//-------------------------------------------------------------------------------
//  g2_str_replace - does a search and replace on a string
//
//  Return: String
//-------------------------------------------------------------------------------
function g2_str_replace (search, replace, subject, count) 
{
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}


//-------------------------------------------------------------------------------
//  g2_strCompare - Compares two strings in there lowercase form.
//
//  Return: N/A
//-------------------------------------------------------------------------------
function g2_strCompare (strOne, strTwo) 
{
    if(strOne.toString().toLowerCase() == strTwo.toString().toLowerCase())
    {
        return true;
    }
    
    return false;
}


//-------------------------------------------------------------------------------
//  g2_stripProtocol - Removes the protocol from the front of the url.
//
//  Return: String
//-------------------------------------------------------------------------------
function g2_stripProtocol(url)
{
    if(g2_strstr(url, "https://"))
    {
        return g2_str_replace ("https://", "", url);
    }
    else if(g2_strstr(url, "http://"))
    {
        return g2_str_replace ("http://", "", url);
    }
    
    return url;
}


//-------------------------------------------------------------------------------
//  g2_getProtocol - Returns http or https based on pages current state.
//
//  Return: String 
//-------------------------------------------------------------------------------
function g2_getProtocol()
{
    return (("https:" == document.location.protocol) ? "https:" : "http:");
}


//-------------------------------------------------------------------------------
//	g2_tagAdd - Adds a G2 tracking tag to the page.
//
//  @param tracker_url  string = the url to send the tracking req. too.
//
//  Ex: Tracking code to be provided by G2
//
//  Return: N/A
//-------------------------------------------------------------------------------
function g2_tagAdd(traker_code)
{
    if(traker_code != 'undefined')
    {
        var dd = new Date();
        
        var ord = Math.round(Math.abs(Math.sin(dd.getTime()))*1000000000)%10000000;
        
        var fd_pct_src = new String("<scr"+"ipt src=\""+g2_getProtocol()+"//adsfac.us/pct_mx.asp?L="+traker_code+"&source=js&ord="+ord+"\" type=\"text/javascript\"></scr"+"ipt>");
        
        document.write(fd_pct_src);
    }
}


//-------------------------------------------------------------------------------
//  g2_addTrackingCodeForPage - Adds Tracking Code for the given page if 
//  applicable.
//
//  @param url          string = The url we want to add tracking code for.
//  @param traker_code  string = The G2 code used to track the request.
//
//  Return: N/A 
//-------------------------------------------------------------------------------
function g2_addTrackingCodeForPage(url, traker_code)
{
    if(url != 'undefined' && traker_code != 'undefined')
    {
        if(g2_strCompare(g2_stripProtocol(window.location), url))
        {
            g2_tagAdd(traker_code);
        }
    }
}
