﻿
function TrafficLoggerController(axdlocation){
    this.axdLocation = axdlocation;
}

TrafficLoggerController.prototype.getHttpObject = function (){
    if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest) return new XMLHttpRequest();
    else {
        alert("Your browser does not support AJAX.");
        return null;
    }
}

TrafficLoggerController.prototype.makeAjaxCallNoReturn = function (pageCall, redirectURL, newWindow) {
    httpObject = this.getHttpObject()

    if (httpObject != null) {
        httpObject.open("GET", pageCall, true);
        httpObject.onreadystatechange = function () {
            if (httpObject.readyState == 4 && httpObject.status == 200) {
                if (redirectURL != null) {
                    if (!newWindow) {
                        location = URLLocation;
                    }
                }
            }
        }

        httpObject.send(null);

        if (redirectURL != null) {
            if (newWindow) {
                window.open(redirectURL, "_blank");
            }
        }
    }
}


TrafficLoggerController.prototype.addToQueryString = function (currentString, addValue) {
    
    if (addValue != null) {
        if (currentString == null) {
            currentString = "?" + addValue;
        }
        else {
            currentString = currentString + "&" + addValue;
        }
    }

    return currentString;
}

TrafficLoggerController.prototype.generateURL = function (trafficType, valuesToLog) {
    var trafficURL = this.axdLocation;
    var resultQueryString = null;


    if (trafficType != null) {
        resultQueryString = this.addToQueryString(resultQueryString, "TrafficTypeID=" + trafficType);
    }

    resultQueryString = this.addToQueryString(resultQueryString, valuesToLog);

    if (resultQueryString != null) {
        trafficURL = trafficURL + resultQueryString;
    }

    return trafficURL;
}

TrafficLoggerController.prototype.logClick = function (trafficType, valuesToLog, locationURL, newWindow) {
    this.makeAjaxCallNoReturn(this.generateURL(trafficType, valuesToLog), locationURL, newWindow);
}
