﻿var strhttpurl;
var strhttpsurl;

strhttpurl  =  "http://softadvertise.com/soft.aspx";
strhttpsurl = "https://softadvertise.com/soft.aspx";

// Sends a request to SoftAdvertise to mark the advertisement ID as a hit
function SoftSet(strclientid, stradvid, stradl)
{
    var strurl;
    if (document.location.href.indexOf('https') > -1)
    {
        strurl = strhttpsurl;
    }
    else
    {
        strurl = strhttpurl;
    }
    try
    {
        if (strclientid == null)
            strclientid = "";
        if (stradl == null)
            stradl = "";
        if (stradvid == null)
            stradvid = "";
        
        if (GetQueryVariable('AdvID') != '')
        {
            //QueryString advertisement ID takes precedence over the passed-in advertisement id
            stradvid = GetQueryVariable('AdvID');
        }
        
        if ((ReadCookie("SoftAdvertise") == null || ReadCookie("SoftAdvertise").indexOf(stradvid) < 0) && stradvid != null && stradvid != '')
        {
            //Cookie doesn't exist, create cookie and redirect
            //Create cookie
            CreateCookie("SoftAdvertise", strclientid + "=" + stradvid, 1);
            //Redirect to set SoftAdvertise cookie
            document.location.href = strurl + "?act=s&c=" + strclientid + "&a=" + stradvid + "&adl=" + stradl + "&u=" + escape(document.location.href);
        }
    }
    catch (ex)
    {
        // alert("Error: " + ex);
    }
}
// Sends a request to SoftAdvertise to mark the advertisement ID as a conversion
function SoftConvert(strclientid, strref, strsaleamount, strorderdate, stradl)
{
    var strurl;
    if (document.location.href.indexOf('https') > -1)
    {
        strurl = strhttpsurl;
    }
    else
    {
        strurl = strhttpurl;
    }
    try
    {
        if (strclientid == null)
            strclientid = "";
        if (strref == null)
            strref = "";
        if (strsaleamount == null)
            strsaleamount = "0";
        if (strorderdate == null)
            strorderdate = "";
        if (stradl == null)
            stradl = "";
        
        //Write Web Bug
        document.writeln("<p style='font-size: 9px; font-family: Arial, Helvetica, sans-serif;  color: #AAAAAA;'><img src='" + strurl + "?act=c&c=" + strclientid + "&r=" + strref + "&s=" + strsaleamount + "&d=" + strorderdate + "&adl=" + stradl + "' width='6' height='6' border='0' />&nbsp;&nbsp;Information tracked by SoftAdvertise</p>");
    }
    catch (ex)
    {
        // alert("Error: " + ex);
    }
}

// Sets the cookie
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=/";
}

// Returns the value of the specified cookie
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;
}

// Removes the specified cookie by resetting its expiration to a date in the past
function EraseCookie(name)
{
    CreateCookie(name, "", -1);
}

// Returns the value of the requested querystring variable
// Returns empty string if the key is not found
function GetQueryVariable(key)
{
    try
    {
        // Get the querystring
        var query = window.location.search.substring(1);
        // Get the array of key and value pairs
        var vars = query.split("&");
        
        // For each key-value pair
        for (var i = 0; i < vars.length; i++)
        {
            // Get the current key-value pair
            var pair = vars[i].split("=");
            
            //Using "toUpperCase()" to make it case-insensitive
            if (pair[0].toUpperCase() == key.toUpperCase())
            {
                // This is the requested key, return the value
                if (pair.length > 1)
                    return pair[1];
                else
                    return "";
            }
        }
        
        // Key not found, return empty string
        return "";
    }
    catch (ex)
    {
        // alert("Error: " + ex);
        return "";
    }
}