/* Functions to Manage Cookies */
var leadGenPrefix = "lg_";
var maxVisitTracers = 10;
var VisitTracerPrefix = "trc";
var debugMode = 1;

// URL Parameter Extraction Parameter
function getQueryParameter ( parameterName ) {
   var queryString = window.top.location.search.substring(1);
   var parameterName = parameterName + "=";
   if ( queryString.length > 0 ) {
     begin = queryString.indexOf ( parameterName );
     if ( begin != -1 ) {
       begin += parameterName.length;
       end = queryString.indexOf ( "&" , begin );
         if ( end == -1 ) {
         end = queryString.length
       }
       return unescape ( queryString.substring ( begin, end ) );
     }
   }
   return "null";
 }

function getUrlParameters() {
    // get the current URL 
    var url = window.location.toString();
    //get the parameters 
    url.match(/\?(.+)$/); var params = RegExp.$1;
    // split up the query string and store in an 
    // associative array 
    var params = params.split("&");
    var queryStringList = {};
    
    for (var i = 0; i < params.length; i++) {
        var tmp = params[i].split("=");
        queryStringList[tmp[0]] = unescape(tmp[1]);
    }
    return queryStringList;   
}

function PersistLeadGenVariablesToCookie() {
    // retrieve url parameters and find matching parameters
    var queryStringList = getUrlParameters();
    
    // Create cookies for all lead generation variables    
    for (var i in queryStringList) {
        if(i.indexOf(leadGenPrefix) >= 0) {            
            $.cookie(i, queryStringList[i], 30);
        }                        
    }
}

function SaveLeadGenVariablesToForm(form) {
    var cookieList = $.cookieList();    
    for (var i in cookieList) {
        if (i.indexOf(leadGenPrefix) >= 0) {            
            var existingInput = document.getElementById(i);
            if (existingInput == null) {
                var hiddenFormField = document.createElement("input");
                hiddenFormField.name = i;
                hiddenFormField.type = "hidden";
                hiddenFormField.id = i;
                hiddenFormField.value = cookieList[i];
                document.getElementById(form).appendChild(hiddenFormField);
            } else {
            existingInput.value = cookieList[i];
            }

        }
    }
}

function RegisterSaveLeadGenVariablesToForm(form) {
    setTimeout("SaveLeadGenVariablesToForm('" + form + "')", 100);
}

function TrackPageVisit() {
    try {
        // Find how many page visit tracers there are
        for (var i = 0; i < maxVisitTracers; i++) {
            var tracer = $.cookie(leadGenPrefix + VisitTracerPrefix + i);
            var pageTitle = document.title + "<BR>" + document.location.href;
            if (tracer == null) {
                // Create the next visit tracer
                $.cookie(leadGenPrefix + VisitTracerPrefix + i, pageTitle, 30);
                return;
            } else if (tracer == pageTitle) {
                // If this href is already stored then skip we just want the list of pages not specifically the order so we don't need duplicates
                return;
            }
        }
    }
    catch (err) {
        if (debugMode)
            throw err;        
    }
}
