/// <reference path="jquery.js" />

var $j = jQuery.noConflict();

var collecter = new function () {
    this.updateUserData = function () {
        var url = getRequestURL();

        $j.ajax({
            url: url,
            type: "GET",
            cache: false,
            success: updateCookies,
            async: false
        });
    };

    var updateCookies = function (data) {
        var response = data.split("|");

        for (var i = 0; i < response.length; i++) {
            if (response[i].indexOf("visitid") != -1) {
                setVisitSession(response[i]);
            }
            else {
                createCookie(response[i]);
            }
        }
    };

    var getRequestURL = this.getRequestURL = function () {
        var url = location.protocol + "//" + location.host + "/WebTracker.aspx"
        //find the referer
        url += "?referer=" + getReferer();
        //find the screen resolution
        url += "&screenRes=" + screenResolution.getScreenResolutionId();
        //find the current URL
        url += "&currentURL=" + stringHelper.encode(window.location);
        //find the browser
        url += "&browser=" + browserType.getBrowserId();
        //find the OS
        url += "&os=" + operatingSystem.getOperatingSystemId();
        // pass the url
        return url + "&visitorid=" + VisitID;
    };

    var createCookie = function (cookieString) {
        var date = new Date();
        date.setTime(date.getTime() + (7200 * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
        var domain = (location.host.indexOf('localhost') > -1) ? "" : "; domain=rushcard.com";
        document.cookie = cookieString + expires + domain + "; path=/";
    };

    var setVisitSession = function (visitString) {
        var visitid = visitString.substring(visitString.indexOf("=") + 1);
        $j('.visitid')[0].value = visitid;
        // delete the visitid cookie
        if (location.host.indexOf('localhost') == -1) {
            document.cookie = "visitid=0;path=/;domain=rushcard.com;expires=Thu, 01-Jan-1970 00:00:01 GMT";
        }
        document.cookie = "visitid=0;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
    };

    // Find the referrer check first to make sure the referrer was not sent in the URL (some promotions will do this)
    // if the refferer was sent in the URL, extract it and use it
    var getReferer = function () {
        var referer = 'Direct';
        var params = window.location.search;
        var paramStart = params.indexOf('referer=');

        if (paramStart != -1) {
            startIdx = params.indexOf('=', paramStart);
            endIdx = params.indexOf('&', startIdx);

            if (endIdx == -1) {
                endIdx = params.length + 1;
                referer = params.substring(startIdx + 1, endIdx);
            }
            else {
                referer = params.substring(startIdx + 1, endIdx + 1);
            }
        }
        else {
            var UrlReferer = escape(document.referrer) + "";

            if ((UrlReferer != "undefined") && (UrlReferer != "")) {
                referer = UrlReferer;
            }
        }

        return referer;
    };
};

var stringHelper = new function () {
    // Removes leading whitespaces
    this.LTrim = function (value) {
        var re = /\s*((\S+\s*)*)/;
        return value.replace(re, "$1");
    };

    // Removes ending whitespaces
    this.RTrim = function (value) {
        var re = /((\s*\S+)*)\s*/;
        return value.replace(re, "$1");
    };

    // Removes leading and ending whitespaces
    this.trim = function (value) {
        return LTrim(RTrim(value));
    };

    // URL encodes a string
    this.encode = function (value) {
        var entry = value;
        var out = "&"; // replace this
        var add = "%26"; // with this
        var temp = "" + entry; // temporary holder

        while (temp.indexOf(out) > -1) {
            pos = temp.indexOf(out);
            temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
        }

        return temp;
    };
};

var screenResolution = new function () {
    // lf adding another screen resolution make sure the screen resolution id is added to the UNISCREEN.TblWebTrackingScreenResTypes table
    this.getScreenResolutionId = function () {
        var resolutionId = 3;

        if (screen.width == 1024)
            resolutionId = 1;
        else if (screen.width == 800)
            resolutionId = 2;

        return resolutionId;
    };
};

var operatingSystem = new function () {
    // lf adding another OS make sure the OS id is added to the UNISCREEN.TblWebTrackingOSTypes table
    this.getOperatingSystemId = function () {
        var operatingSystemId = 10;
        var agt = navigator.userAgent.toLowerCase();

        if ((agt.indexOf('windows nt') != -1) && (agt.indexOf('5.1') != -1)) {
            operatingSystemId = 1;
        } //Windows XP
        if ((agt.indexOf('win') != -1) && (agt.indexOf('98') != -1)) {
            operatingSystemId = 2;
        } //Windows 98
        else if ((agt.indexOf('win') != -1) && (agt.indexOf('95') != -1)) {
            operatingSystemId = 3;
        } // Windows 95
        else if ((agt.indexOf('windows nt 5.0') != -1)) {
            operatingSystemId = 4;
        } // Windows 2000
        else if ((agt.indexOf('windows nt 5.2') != -1)) {
            operatingSystemId = 5;
        } // Windows Server 2003
        else if ((agt.indexOf('win') != -1) && (agt.indexOf('4.9') != -1)) {
            operatingSystemId = 6;
        } // Windows ME
        else if ((agt.indexOf('windows nt 6.') != -1)) {
            operatingSystemId = 7;
        } // Windows Vista
        else if (agt.indexOf('mac') != -1) {
            operatingSystemId = 8;
        } // Mac
        else if (agt.indexOf('linux') != -1) {
            operatingSystemId = 9;
        } // Linux

        return operatingSystemId;
    };
};

var browserType = new function () {
    // lf adding another browser make sure the browser id is added to the UNISCREEN.TblWebTrackingBrowserTypes table
    // convert all characters to lowercase to simplify testing - TODO-NRC: Add new browser and OS types
    this.getBrowserId = function () {
        var agt = navigator.userAgent.toLowerCase();
        var version = navigator.appVersion;
        var is_major = parseInt(navigator.appVersion);
        var is_minor = parseFloat(navigator.appVersion);

        var browserTypeId = 21;

        if (agt.indexOf('aol') != -1) {
            browserTypeId = 1;
        }
        else if (agt.indexOf('netscape') != -1) {
            browserTypeId = 4;

            if (agt.indexOf('8.') != -1) {
                browserTypeId = 2;
            }
            else if (agt.indexOf('7.') != -1) {
                browserTypeId = 3;
            }
        }
        else if (agt.indexOf('firefox') != -1) {
            browserTypeId = 5;

            if (version >= 2 && version < 3) {
                browserTypeId = 12;
            }
            else if (version >= 3 && version < 4) {
                browserTypeId = 13;
            }
        }
        else if (agt.indexOf('opera') != -1) {
            browserTypeId = 6;
        }
        else if (agt.indexOf('ie') != -1) {
            browserTypeId = 10;

            if (agt.indexOf("9.") != -1) {
                browserTypeId = 22;
            }
            else if (agt.indexOf("8.") != -1) {
                browserTypeId = 11;
            }
            else if (agt.indexOf("7.") != -1) {
                browserTypeId = 7;
            }
            else if (agt.indexOf("6.") != -1) {
                browserTypeId = 8;
            }
            else if (agt.indexOf("5.") != -1) {
                browserTypeId = 9;
            }
        }
        else if (agt.indexOf("chrome") != -1) {
            browserTypeId = 14;
        }
        else if (agt.indexOf("omniweb") != -1) {
            browserTypeId = 15;
        }
        else if (agt.indexOf("apple") != -1) {
            browserTypeId = 16;
        }
        else if (agt.indexOf("icab") != -1) {
            browserTypeId = 17;
        }
        else if (agt.indexOf("kde") != -1) {
            browserTypeId = 18;
        }
        else if (agt.indexOf("camino") != -1) {
            browserTypeId = 19;
        }
        else if (agt.indexOf("gecko") != -1) {
            browserTypeId = 20;
        }

        return browserTypeId;
    };
};

$j(document).ready(function () { collecter.updateUserData(); });

