﻿function __SelectUnselectGetStats(fieldId, idNames)
{
    var currentObj = jQuery("#" + fieldId);
    jQuery("input[id*='" + idNames + "']").attr("checked", currentObj.attr("checked"));
    
    jQuery("input[id*='" + idNames + "']").each(function() {
        var enableId = this.id;
        
        __GetStatsWS(enableId);
    });
}

function __RemoveItemFromList(itemToRemove)
{
    var storage = dnn.getVar('SHDWSStatsId');
    
    var list = storage.split('|');
    var newList = new Array();
    
    for (i = 0; i < list.length; i++)
    {
        if (list[i] != itemToRemove)
            newList[newList.length] = list[i];
    }
    
    storage = ltrim(newList.join('|'), '|');
    //save to back to server
    dnn.setVar('SHDWSStatsId', storage);
    return storage
}

function __AddItemToList(itemToAdd)
{
    var storage = dnn.getVar('SHDWSStatsId');
    
    var list = storage.split('|');
    var exists = false;
    for (i = 0; i < list.length; i++)
    {
        if (list[i] == itemToAdd)
        {
            exists = true;
            break;
        }
    }
    if (! exists)
    {
        list[list.length] = itemToAdd;
    }
    
    storage = ltrim(list.join('|'), '|');
    //save to back to server
    dnn.setVar('SHDWSStatsId', storage);
    return storage
}

function __GetStatsWS(fieldId)
{
    var obj = jQuery("#" + fieldId);
    if (obj == null)
        return;  //exit because no valid field found
        
    var listingId = obj.parent().attr("int_id");
    var selected = obj.attr("checked")
    var listingIds = '';
    
    if (selected)
        listingIds = __AddItemToList(listingId);
    else
        listingIds = __RemoveItemFromList(listingId);
     
    var statsPanel = jQuery("div[id$='pnlFooter']");
    
    //do not need to calculate stats if does not exists
    if (statsPanel.html() == null)
        return;
    
    //disable object first 
    obj.attr('disabled', true);
    var fullWSPath = dnn.getVar('SHDWSPath') + "/SHDWS.asmx/GetListingStats";
    var postData = "{'key':'" + dnn.getVar('SHDWSAuthId') + "'"; 
    postData += ",'listingIds':'" + listingIds + "'";
    postData += ",'ctrlid':'" + fieldId + "'";
    postData += "}";   

    //alert(postData);
    jQuery.ajax({
    type: "POST",
    url: fullWSPath,
    data: postData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        __ManageStatsResults(msg);
    }
    });
    
}

function __ManageStatsResults(msg)
{
    if (msg != '')
    {
        var message = '';
        if (typeof msg == 'object')
            message = msg['d'];
        else
            message = msg;
            
        var parts = message.split('|');
        
        for (i = 0; i < parts.length; i++)
        {
            var keyvalue = parts[i].split('^');
            if (keyvalue.length > 1)
            {
                var obj = jQuery('span[id$=BBPI_' + keyvalue[0] + ']');
                //var obj = jQuery('#' + keyvalue[0]);
                if (obj != null)
                    obj.html(keyvalue[1]);
            }
            else if (keyvalue.length > 0)
            {
                var obj = jQuery('#' + keyvalue[0]);
                if (obj != null)
                    obj.attr('disabled', false); //enable
            }
        }
    }
}

function __InitPrint(id)
{
    var obj = jQuery('#' + id);
    if (obj == null)
        returnl;
        
    var url = obj.attr("printurl");
    displayModalBox('Print Listing', url, 800, 500);
    
    return false;
}

function __InitPrint2(id)
{
    var obj = jQuery('#' + id);
    if (obj == null)
        returnl;
        
    var url = obj.attr("printurl");
    //displayModalBox('Print Listing', url, 800, 500);
    window.open(url, "BBP", "directories=0,status=0,toolbar=0,menubar=0,location=0,width=800,height=500"); 
    
    return false;
}


function __InitPrintStats(id)
{
    var obj = jQuery('#' + id);
    if (obj == null)
        returnl;
        
    var listIds = dnn.getVar('SHDWSStatsId'); 
    
    if (listIds == null || listIds == "") {
        alert('No Listings selected.');
        return false;
    }
       
    var url = obj.attr("printurl") + '&listids=' + listIds;
    displayModalBox('Print Selected Stats Listings', url, 800, 500);
    
    return false;
}


function __InitBulkAction(id)
{
    var obj = jQuery('#' + id);
    if (obj == null)
        returnl;
        
    var listIds = dnn.getVar('SHDWSStatsId'); 
    
    if (listIds == null || listIds == "") {
        alert('No Listings selected.');
        return false;
    }
       
    var url = obj.attr("actionurl") + '&listids=' + listIds;
    displayModalBox('Perform Bulk Action on Selected Listings', url, 400, 170);
    
    return false;
}

function __InitBulkAction2(id)
{
    var obj = jQuery('#' + id);
    if (obj == null)
        returnl;
        
    var listIds = dnn.getVar('SHDWSStatsId'); 
    
    if (listIds == null || listIds == "") {
        alert('No Listings selected.');
        return false;
    }
       
    var url = obj.attr("actionurl") + '&listids=' + listIds;
    window.open(url, "BBP", "directories=0,status=0,toolbar=0,menubar=0,location=0,width=400,height=200"); 
    
    return false;
}


function __InitSendEmailSelectedContacts(id)
{
    var obj = jQuery('#' + id);
    if (obj == null)
        returnl;
        
    var contactIds = dnn.getVar('SHDWSStatsId'); 
    
    if (contactIds == null || contactIds == "") {
        alert('No Contacts selected.');
        return false;
    }
       
    var url = obj.attr("sendemailurl") + '&contactids=' + contactIds;
    displayModalBox('Email Selected Contacts', url, 500, 300);
    
    return false;
}

function __InitSendEmailSelectedContacts2(id)
{
    var obj = jQuery('#' + id);
    if (obj == null)
        returnl;
        
    var contactIds = dnn.getVar('SHDWSStatsId'); 
    
    if (contactIds == null || contactIds == "") {
        alert('No Contacts selected.');
        return false;
    }
       
    var url = obj.attr("sendemailurl") + '&contactids=' + contactIds;
    window.open(url, "BBP", "directories=0,status=0,toolbar=0,menubar=0,location=0,width=500,height=350"); 
    
    return false;
}
