﻿$(function () {
    // track visit
    $.post('/mvc/capture/visit', { url: window.location.href, referrer: document.referrer });
});

function captureCustRecInfo(name, email, recname, recemail) {
//    $.ajax({
//        type: "POST",
//        url: '/DataService.asmx/CaptureRecByInfo',
//        data: "{CustName: '" + name + "', CustEmail: '" + email + "', RecName: '" + recname + "', RecEmail: '" + recemail + "' }",
//        contentType: "application/json; charset=utf-8",
//        dataType: "json"

//    });
}

function captureExit(fromPage, toPage) {
//    $.ajax({
//        type: "POST",
//        url: '/DataService.asmx/CaptureExit',
//        data: "{FromPage: '" + window.location + "', ToPage: '" + toPage + "', Referrer: '" + document.referrer + "' }",
//        contentType: "application/json; charset=utf-8",
//        dataType: "json"

//    });
}

function captureVideoStats(video, event, time) {
//    $.ajax({
//        type: "POST",
//        url: '/DataService.asmx/CaptureVideoStats',
//        data: "{CurrentPage: '" + window.location + "', Video: '" + video + "', Event: '" + event + "', Time: '" + time + "', Referrer: '" + document.referrer + "' }",
//        contentType: "application/json; charset=utf-8",
//        dataType: "json"

//    });
}

function SendEmailAlert(FileName, email, name) {
//    $.ajax({
//        type: "POST",
//        url: '/DataService.asmx/SendEmailAlert',
//        data: "{FileName: '" + FileName + "', CustEmail: '" + email + "', Name: '" + name + "' }",
//        contentType: "application/json; charset=utf-8",
//        dataType: "json"

//    });
}

function UrlHasQueryString() {
    return location.href.lastIndexOf('?') != -1;
}

//FOR COOKIES
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 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 eraseCookie(name) {
    createCookie(name, "", -1);
}


// $.QueryString['param']
(function ($) {
    $.QueryString = (function (a) {
        if (a == "") return {};
        var b = {};
        for (var i = 0; i < a.length; ++i) {
            var p = a[i].split('=');
            if (p.length != 2) continue;
            b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
        }
        return b;
    })(window.location.search.substr(1).split('&'))
})(jQuery);

// set vtid cookie so we can track origin of sales
if (typeof($.QueryString['vtid']) != 'undefined')
{
    createCookie('vtid', $.QueryString['vtid'], 30);
}

// strip vtid out of EntryPage cookie so we accurately use the last vtid
if (readCookie('EntryPage') != null) {
    var entryPage = readCookie('EntryPage').replace(/&vtid=([_a-z0-9-]+)/, '').replace(/\?vtid=([_a-z0-9-]+)&/, '?').replace(/\?vtid=([_a-z0-9-]+)/, '');
    createCookie('EntryPage', entryPage, 100);
}


// adds one to a stat counter
function addStatCount(id) {
    $.ajax({
        url: 'http://analytics.thedietsolutionprogram.com/Stats/Add',
        data: { id: id },
        dataType: 'jsonp'
    });
}
