var _awe = "";
var _action = "";
    
// Function to track a page view
function TrackPage()
{
    var unique;
    var isnew;
    var oref = document.referrer;
    
    if(!oref || oref == "")
    {
        oref = "X";
    }
    
    //attempt cookie read
    if(readCookie("CASTATS"))
    {
        unique = readCookie("CASTATS");
        isnew = "F";
    }
    else
    {
        unique = CreateUniqueID();
        
        createCookie("CASTATS",unique,1);
        isnew = "T";
    }
    
    // Construct QueryString
    var URL = "www.conversiondashboard.com/WebTrack/track.gif";
    
    //Here we need to append all the params we'll need, such as...
    URL = addURLParam(URL,"awe",_awe);
    URL = addURLParam(URL,"uqid",unique);
    URL = addURLParam(URL,"isnew",isnew);
    URL = addURLParam(URL,"oref",oref);
    URL = addURLParam(URL,"docloc",document.location);
    URL = addURLParam(URL,"href","x");
    URL = addURLParam(URL,"action",_action);
    
    SendData(URL);

}

function CreateUniqueID()
{
    var unique = GetRandomBetween(100,9999999);
    
    var char1 = GetRandomBetween(65,90);
    var char2 = GetRandomBetween(97,122);
    
    unique =  _awe +  String.fromCharCode(char1) + String.fromCharCode(char2) + unique;
    
    char2 = GetRandomBetween(65,90);
    char1 = GetRandomBetween(97,122);
        
    unique +=String.fromCharCode(char1) + String.fromCharCode(char2);
    
    return unique;
}

function addURLParam(URL,ParamName,ParamVal)
{
    URL += ( URL.indexOf("?") == -1 ? "?" : "&" );
    URL += encodeURIComponent(ParamName) + "=" + encodeURIComponent(ParamVal);
    return URL;
}

function GetRandomBetween(first,last)
{
    var Range = last - first;
    return Math.floor(Math.random() * Range + first);
}
                
function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}



function TrackDownload(link )
{
    // test extension agains list -- Trackable Files
    //if ext is trackable
    // construct query string with file dl data
    
    var submitMe = true
    
    
    if(submitMe == true)
    {
        var URL = "www.conversiondashboard.com/WebTrack/track.gif";
    
        var unique;
        var isnew;
        var oref = document.referrer;
    
        if(!oref || oref == "")
        {
            oref = "X";
        }
    
        //attempt cookie read
        if(readCookie("CASTATS"))
        {
            unique = readCookie("CASTATS");
            isnew = "F";
        }
        else
        {
            unique = CreateUniqueID();
        
            createCookie("CASTATS",unique,1);
            isnew = "T";
        }
    
        URL = addURLParam(URL,"awe",_awe);
        URL = addURLParam(URL,"uqid",unique);
        URL = addURLParam(URL,"isnew",isnew);
        URL = addURLParam(URL,"oref",oref);
        URL = addURLParam(URL,"docloc",document.location);
        URL = addURLParam(URL,"href",link.href);
        URL = addURLParam(URL,"action","Download");
        //make Track Request
        SendData(URL);
    }
    return true;
}


function SendData(URL)
{
    var image = new Image();
    
    image.src = location.protocol + "//" + URL;
    image.onload=function() { retnada(); }
}

function retnada() { return; }

// function to send form contents to CADash
function InterceptForm( theForm )
{
    // loop through all input elements in the form and create a POST request
    // submit via XMLHttpRequest
    
}