﻿//jQuery.fn.sort = function() { return this.pushStack([].sort.apply(this, arguments), []); };

function sortByID(a, b)
{
    return a.id > b.id ? 1 : -1;
}


function sortByIDDescending(a, b) {
    return a.id > b.id ? -1 : 1;
}


function sortByInnerHTML(a, b) 
{
    //alert(a.innerHTML + ' - ' + b.innerHTML);
    return a.innerHTML > b.innerHTML ? 1 : -1;
}


function sortByInnerHTMLDescending(a, b) {
    //alert(a.innerHTML + ' - ' + b.innerHTML);
    return a.innerHTML > b.innerHTML ? -1 : 1;
}


function showCollectionIds(collection) 
{
    var output = '';
    for (var i = 0; i < collection.length; i++) 
    {
        output += i + ' = ' + collection[i].id + "\n";
    }

    alert(output);
}

function removeAllChildNodes(element) 
{
    if (element == null) return;
    //alert(element.id + ' hasChildNodes = ' + element.hasChildNodes());
    while (element.hasChildNodes()) { element.removeChild(element.firstChild); }
}


function GetResultsErrorMsg(results)
{
    if (results == null)
        return "Results are null.";
    else if (results.Contestants == null)
        return "Results.Contestants is null.";
    else if (results.Contestants.length == 0)
        return "Results.Contestants length is 0.";
    else
        return "Unknown error.";
}


function ScrubString(input)
{
    //Replace double quotes with nothing, replace newline characters with spaces
    //return input.replace(/\"/g, '').replace(/\n/g, ' ');
    //return input.replace(/\"/g, '').replace(/\n/g, ' ').replace(/\r/g, '').replace(/\&amp\;/g, " BLAH ");
    return input.replace(/\"/g, '').replace(/\n/g, ' ').replace(/\r/g, '').replace('&', "&amp;");
}


function LogUploadInfo(Event, Username, IPAddress, Purpose, BrowserInfo)
{
    //alert('hi');

    //alert("Event = " + Event + "\nUsername = " + Username + "\nIPAddress = " + IPAddress + "\nPurpose = " + Purpose + "\nBrowser Info = " + BrowserInfo);

    //$.ajax
    jQuery.ajax
    ({
        type: "POST",
        contentType: "application/json",
        url: "AJAXServices/InteractivityService.svc/LogUploadInfo",
        data: '{ "Event": "' + Event + '", "Username": "' + Username + '", "IPAddress": "' + IPAddress + '", "Purpose": "' + Purpose + '", "BrowserInfo": "' + BrowserInfo + '", "AddtlInfo": null, "ParentUploadInfoID": null }',
        processData: true,
        async: false
    });
    //alert('Bye');
}
