// <summary>
//     Manages the download initiation process. This method would be coded
//     on the 'onclick' event of the download link. If the 'downloadPage'
//     is not supplied then then the link will execute in page.
// </summary>
// <param name="eventId">The event that is tracked when the user clicks on download</param>
// <param name="promoId">The promotion id that the player downloaded from - tracked in the lobby</param>
// <param name="downloadPage">Ignores the eventId and the promoId and simply launches a download page. eventId and promoId can be set to null values</param>
function mvtDownload(eventId, promoId, downloadPage) {
    var profileId = "";   // This will be replaced by the javascript controller

    if (profileId != "") {
        try {
            var gwoTracker = _gat._getTracker("");
            gwoTracker._trackPageview("/[[TEST_ID]]/goal");
        } catch (err) { }
    }
    
    if (argumentIsBlank(downloadPage)) {
        if (argumentIsBlank(promoId)) {
            window.location.href = '/viper-download/' + eventId;
        }
        else {
            window.location.href = '/viper-download/' + eventId + '/' + promoId;
        }
    }
    else
        window.location.href = downloadPage;
}

// <summary>
//    Captures the variables contained in the mvt areas on the test page
//    and posts it to the url responsible for capturing data to the database.
// </summary>
function mvtDataCapture() {
    var xmlHttp = initAjax();
    var url = '/mvt-test-capture/?';

    var hiddenInputTags = document.getElementsByTagName('input');

    for (var i = 0; i < hiddenInputTags.length; i++) {
        if (hiddenInputTags[i].type == "hidden") {
            if (hiddenInputTags[i].name.substr(0, 3) == 'mv_') {
                if (i == 0) {
                    url += hiddenInputTags[i].name + '=' + hiddenInputTags[i].value;
                }
                else {
                    url += '&' + hiddenInputTags[i].name + '=' + hiddenInputTags[i].value
                }
            }
        }

    }

    callAjax(url);
}

function initAjax() {
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { } //IE
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } //IE
    try { return new XMLHttpRequest(); } catch (e) { } //Native Javascript
    
    return null; 
}

function callAjax(url) {

    var req = initAjax();

    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if (req.status == 200) {

            }
        }
    };

    req.open("GET", url, true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
    req.send(null); //send value
}

function argumentIsBlank(argument) {
    return (argument == 'undefined' || argument == null || argument == '');
}
