﻿//Below js functions are used in the Live feed page 
//to connect with database for diffierent purposes
////get comments
function ReloadComments(selectedTab) { 
    //give page no
    if (selectedTab == '3_My Messages') selectedTab = '3_Messages';
    var records = $('#hdmore5').val();

    getvalues(1, selectedTab, records);

    var rec = parseInt(records) + 1;
    $('#hdmore5').val(rec);

}

//retrieve comments
function getvalues(j, selectedTab, pageNo) {
    try {
        $.ajax({
            type: "POST",
            url: "LiveFeed.aspx/LoadData",
            data: "{'id':'" + j + "','selectedTab':'" + selectedTab + "','pageNo':'" + pageNo + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg.d.length > 0) {
                    var dvComment = document.getElementById("Comments");
                    dvComment.innerHTML += msg.d;
                } else {
                    $('#more_').hide();
                }
            },
            async: true,
            error: function (xhr, status, error) {
               // alert(xhr.statusText);
            }
        });
    }
    catch (e) {
        alert(e);
    }
}


//Update Global Feed table
function updateFeeds() {
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/UpdateFeeds",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                /// ReloadComments('0');
            }
        },
        async: true,
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
}
////////Save User Comments
function SaveUserComments(userID, activityLogID, actID, refID, username, memberID) {
    
    var txt = document.getElementById("txtComment_" + activityLogID)
    var pass = false;

    if (txt.value == '') {
        parent.showMessage('Please enter some comments to post!');
        return;
    }

    if (txt.value.substring(0, 1) == ' ') {
        parent.showMessage('Please enter some comments to post!');
        return;
    }

    if (txt.value.indexOf('<') > 0 || txt.value.indexOf('>') > 0) {
        parent.showMessage('Invalid comments entered!');
        return;
    } 

    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/AddComments",
        data: "{'comments':'" + txt.value + "','userID':'" + userID + "','activityLogID':'" + activityLogID + "','actID':'" + actID + "','refID':'" + refID + "','username':'" + username + "','memberID':'" + memberID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                document.getElementById('CommentDiv_' + activityLogID).style.display = "none";
                txt.value = '';
                if (msg.d == 'pass')
                    setTimeout("ViewLessComments(" + activityLogID + ", " + actID + ", " + refID + ", " + userID + ")", 1000);
            }
        },
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
}

//link Users
function Link(id, ActivityLogID) {
    //-----------------------------webmethod
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/CreateLink",
        data: "{'id':'" + id + "','ActivityLogID':'" + ActivityLogID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                if (msg.d == 'Member in your Contact cannot be linked.' || msg.d == 'Member is already linked to you.')
                    parent.showMessage(msg.d);
                else {
                    //var dv = $('#Feed_' + ActivityLogID);
                    //dv.html(msg.d); 
                    UpdateLinkIcon('unlink', id, ActivityLogID);
                }
                //update user control
                UpdateLink2Me();
            }
        },
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    }); 
   
}

//Unlink Users
function Unlink(id, ActivityLogID) {
    //-----------------------------webmethod
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/DeleteLink",
        data: "{'id':'" + id + "','ActivityLogID':'" + ActivityLogID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                if (msg.d == 'Member cannot be unlinked.')
                    parent.showMessage(msg.d);
                else {
                    //var dv = $('#Feed_' + ActivityLogID);
                    //dv.html(msg.d); 
                    UpdateLinkIcon('link', id, ActivityLogID);
                }
                //update user control
                UpdateMyLinking();
            }
        },
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });

}

function UpdateLinkIcon(status, userID, activityLogID) {
    var divList;
    var iconDv;

    if (status == 'link') {
        divList = $("div[id^='Unlink_" + userID + "_']");

        for (var i = 0; i < divList.length; i++) {
            iconDv = divList[i].id;
            $('#' + iconDv).hide();
            var newDiv = iconDv.substring(2);
            $('#' + newDiv).show();
        }
        parent.showMessage('Member unlinked successfully');
        //updateUserControl();
    }
    else {
        divList = $("div[id^='link_" + userID + "_']");

        for (var i = 0; i < divList.length; i++) {
            iconDv = document.getElementById(divList[i].id);
            iconDv = divList[i].id;
            $('#' + iconDv).hide();
            var newDiv = "Un" + iconDv;
            $('#' + newDiv).show();
        }
        parent.showMessage('Member added to your linking successfully');
        //updateUserControl();
    }
}


function TestLink(id) {
    UpdateLinkIcon('unlink', id, "0");
}

function TestUnlink(id) {
    UpdateLinkIcon('link', id, "0");
}

//end link
function GetActivyByID(id, div) {
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/RefreshActivity",
        data: "{'ActivityLogID':'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                var dv = document.getElementById(div);
                $(dv).html('');
                $(dv).html(msg.d);
            }
        },
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
}

//Delete Comments
function DeleteComment(id) {
    if (!confirm('Are you sure you want to delete?')) {
        return false;
    }
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/DeleteComments",
        data: "{'id':'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                var dv = document.getElementById('Feed_' + id);
                $(dv).fadeOut(500);
            }
        },
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
    //reload data
    //ReloadComments('0');
}

//Delete a SubComments
function DeleteSubComment(id, actId, refId, UserId, logId) {
    if (!confirm('Are you sure you want to delete?')) {
        return false;
    }
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/DeleteSubComments",
        data: "{'id':'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                //reload data
                var dv = document.getElementById('DV_Del_' + id);
                $(dv).fadeOut(500);
                if (actId == '40') {
                    setTimeout("getBillBoard('less')", 1500);
                } else
                    setTimeout("ViewLessComments(" + logId + ", " + actId + ", " + refId + ", " + UserId + ")", 1500);
            }
        },
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
    //reload data
    // ReloadComments('0'); 
}
//delete a reply to Message
function DeleteReply(messageID) {
    if (!confirm('Are you sure you want to delete?')) {
        return false;
    }
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/DeleteReply",
        data: "{'ID':'" + messageID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                //remove the deleted message div
                var dv = document.getElementById('DV_Del_' + messageID);
                $(dv).fadeOut(500);
            }
        },
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
}


//View All Comments
function ViewAllComments(logID, actID, refID, memID) {
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/loadFeeds",
        data: "{'logID':'" + logID + "', 'actID':'" + actID + "', 'refID':'" + refID + "', 'membID':'" + memID + "', 'count':0,'xmlDtl':'','isBill':'false'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                var div = document.getElementById('TD_' + logID);
                div.innerHTML = "";
                div.innerHTML = msg.d;
                //document.getElementById('more_' + logID).style.display = "none";
                //
            }
        },
        error: function (xhr, status, error) {
            // alert(xhr.statusText);
        }
    });
}
//View Less Comments
function ViewLessComments(logID, actID, refID, memID) {
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/loadFeeds",
        data: "{'logID':'" + logID + "', 'actID':'" + actID + "', 'refID':'" + refID + "', 'membID':'" + memID + "', 'count':3,'xmlDtl':'','isBill':'false'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                var div = document.getElementById('TD_' + logID);
                div.innerHTML = "";
                div.innerHTML = msg.d;
                //document.getElementById('less_' + logID).style.display = "none";
                //
            }
        },
        error: function (xhr, status, error) {
            // alert(xhr.statusText);
        }
    });
}

///////load counts in tabs
function LoadCount() {
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/LoadCounters",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {

            }
        },
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
}

//billboard***************************************

//get biilboard and its comments
function getBillBoard(mode) {
    var dvComment = document.getElementById("billboard");
    //dvComment.innerHTML = '';
    //$(dvComment).slideUp('slow');
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/LoadBillboard",
        data: "{'mode':'" + mode + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                if (msg == 'No Billboard') {
                    $('#').val() = '0';
                } else {
                    dvComment.innerHTML = msg.d;
                    //$(dvComment).slideDown('slow');
                }
            }
        },
        async: false,
        error: function (xhr, status, error) {
            // alert(xhr.statusText);
        }
    });
}
////show biilboard Box to replace the existing billboard and write a new one
function NewBillboard(obj) {
    if (obj == 'new1') {
        $('#billInput').show('slow');
        $('#biilText').hide('slow');
        setTimeout("focusBillboard('txtbillboardUpdate')", 1000);
    }
    if (obj == 'can') {
        $('#billInput').hide('slow');
        $('#biilText').show('slow');
    }
    if (obj == 'new11') {
        $('#billInput').show('slow');
        $('#NoBillboard').hide('slow');
        $('#txtbillboard').val('');
        setTimeout("focusBillboard('txtbillboard')", 1000);
    }
    if (obj == 'can1') {
        $('#billInput').hide('slow');
        $('#NoBillboard').show('slow');
    }
}

///////method used to show the comment box for billboard
function openBillboardCommentBox(obj) {
    $('#DvBillboard').toggle('slow');
    setTimeout("focusBillboard('txtbillboardComment')", 1000);
}
function focusBillboard(txt) {
    $('#' + txt).focus();
    $('#' + txt).select();

}
//New biilboard
function SaveNewBillboard() {
    var txt = document.getElementById('txtbillboard');

    if (txt.value == '') {
        parent.showMessage('Please enter some comments to post!');
        return;
    }
    if (txt.value.indexOf('<') > 0 || txt.value.indexOf('>') > 0) {
        parent.showMessage('Invalid comments entered!');
        return;
    }
    var billText = txt.value.replace(/'/g, "^");

    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/NewBillboard",
        data: "{'newBillboard':'" + billText + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                if (msg.d == 'pass') {
                    $('#NoBillboard').hide('slow');
                    getBillBoard('less');
                }
            }
        },
        async: false,
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });

    getBillBoard('less');
    NewBillboard('can');
}


//Update biilboard
function UpdateBillboard(actID) {
    var txt = document.getElementById('txtbillboardUpdate');

    if (txt.value == '') {
        parent.showMessage('Please enter your billboard!!');
        return;
    }
    if (txt.value.indexOf('<') > 0 || txt.value.indexOf('>') > 0) {
        parent.showMessage('Invalid billboard text!');
        return;
    }
    var billText = txt.value.replace(/'/g, "^");

    //-------------------------ajax
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/UpdateBillboardText",
        data: "{'actID':'" + actID + "','newBillboard':'" + billText + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d == 'success') {
                //alert('ok');
            }
        },
        async: false,
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
    getBillBoard('less');
    NewBillboard('can');
}

//save biilboard comments
function SaveBillboardComment(actID) {
    var txt = document.getElementById('txtbillboardComment');

    if (txt.value == '') {
        parent.showMessage('Please enter some comments to post!!');
        return;
    }
    if (txt.value.indexOf('<') > 0 || txt.value.indexOf('>') > 0) {
        parent.showMessage('Invalid comments entered!');
        return;
    }
    var comment = txt.value.replace(/'/g, "^");
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/SaveBillboardComments",
        data: "{'comment':'" + comment + "','actID':'" + actID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                var str = msg.d;
                if (str == 'already')
                    parent.showMessage('Comment already posted!');
            }
        },
        async: false,
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });

    getBillBoard('less');
    NewBillboard('can');
}

//View All Bollboard Comments
function ViewAllBollboardComments() {
    getBillBoard('more');
}
//View Less Bollboard Comments
function ViewLessBollboardComments() {
    getBillBoard('less');
}
/////update comment posting time
function UpdateTime(id) {
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/UpdateTime",
        data: "{'id':'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                obj = document.getElementById('feed_TimeStamp' + id);
                obj.innerHTML = msg.d;
                //$(obj).slideDown("slow");
            }
        },
        async: true,
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
}
/////////////////////////////////////view more
function ViewMore() {

    try {

        var tabInd = $('#hdTabInd').val();
        if (tabInd == '') tabInd = '0_Posts';
        //show busiy image
        $('#imgBusy').html("<img src='../../Images/circular.gif' width='20' height='23' />");
        var rowNum = $('#hdmore5').val(); //$('#ContentBody_hdRowCount').val();
        var pageNo = rowNum;


        if ($('#hdFilter').val() == '0') {

            //get tab index from hidden field\
            var tabIndex;
            var ind = $('#hdTabInd').val();
            if (ind != '')
                tabIndex = ind;
            //if 3rd level tabs are selected
            if ($('#hd3rdLevel').val() == 1) {
                var tabID = $('#hd3rdLevelTab').val();
                if (tabIndex.indexOf('2_') >= 0) {
                    for (var i = 1; i < 21; i += 5) {
                        LoadData3rdLevel(tabIndex + "@Network_" + tabID, pageNo);
                        pageNo++;
                    }
                }
                else if (tabIndex.indexOf('6_') >= 0) {
                    for (var i = 1; i < 21; i += 5) {
                        LoadData3rdLevel(tabIndex + "@Linkings_" + tabID, pageNo);
                        pageNo++;
                    }
                }
            } else {
                for (var i = 1; i < 21; i += 5) {
                    getvalues(i, tabInd, pageNo);
                    pageNo++;
                }
            }
        }
        else {
            for (var i = 1; i < 21; i += 5) {
                FilterPostsData($('#hdCatID').val(), $('#hdTypeID').val(), $('#hdCountryID').val(), $('#Keyword').val(), pageNo, $('#hdMiles').val(), $('#hdZip').val(), $('#hdTabInd').val());
                pageNo++;
            }
        }
        $('#vLess').show();
        //$('#vMore').hide();
        rowNum = parseInt(rowNum) + 5;
        $('#hdmore5').val(pageNo);
        $('#imgBusy').html('');
    }
    catch (e) {
        alert(e);
    }
}


//////view less button click
function ViewLess(obj) {
    //reset hidden fields 

    $('#' + obj.id).val(6);
    $('#hdmore5').val(1);

    if ($('#hdFilter').val() == '0') {
        //clean previously loaded data
        $('#Comments').html('');
        $('#vLess').hide();
        $('#noMore').hide();
        $('#more_').show();
        /////get current tab index
        var tabInd = $('#hdTabInd').val();
        if (tabInd == '') tabInd = '0_Posts';
         //if 3rd level tabs are selected
        if ($('#hd3rdLevel').val() == 1) {
            var tabID = $('#hd3rdLevelTab').val();
            if (tabIndex.indexOf('2_') >= 0) {
                LoadData3rdLevel(tabIndex + "@Network_" + tabID, 1);
            }
            else if (tabIndex.indexOf('6_') >= 0) {
                LoadData3rdLevel(tabIndex + "@Linkings_" + tabID, 1);
            }
        } else {
            ReloadComments(tabInd);
        }
    } else {
        //clean previously loaded data
        $('#Comments').html('');
        $('#vLess').hide();
        $('#noMore').hide();
        $('#more_').show();
        FilterPostsData($('#hdCatID').val(), $('#hdTypeID').val(), $('#hdCountryID').val(), $('#Keyword').val(), 1, $('#hdMiles').val(), $('#hdZip').val(), $('#hdTabInd').val());
    }

}

////scroll to bottom
////now we will show 5 more records on each scroll to 
///Bottom untill 20 records are there then we will 
//show another 20 per 'View More' click
$(window).scroll(function () {
    if (isScrollBottom()) { 

        var records = $('#hdmore5').val();
        if ($('#hdFilter').val() == '0') {
            //get tab index from hidden field\
            var tabIndex;
            var ind = $('#hdTabInd').val();
            if (ind != '')
                tabIndex = ind;
            else
                tabIndex = '0_Posts';
            //show 5 more records on fist scroll to bottom 

            //if 3rd level tabs are selected
            if ($('#hd3rdLevel').val() == 1) {
                if (records <= 4) {
                    var tabID = $('#hd3rdLevelTab').val();
                    if (tabIndex.indexOf('2_') >= 0) {
                        LoadData3rdLevel(tabIndex + "@Network_" + tabID, records);
                    }
                    else if (tabIndex.indexOf('6_') >= 0) {
                        LoadData3rdLevel(tabIndex + "@Linkings_" + tabID, records);
                    }
                }
            }
            else {
                if (records <= 4) { 
                    $('#more_').show();
                    ReloadComments(tabIndex);
                }
                $('#noMore').hide();
            }
        }
        else {
            var cat = $('#hdCatID').val();
            var post = $('#hdTypeID').val();
            var country = $('#hdCountryID').val();
            var keyword = $('#Keyword').val();
            var mile = $('#hdMiles').val();
            var zip = $('#hdZip').val();

            if (records <= 4) {
                IsScroll = true;
                FilterPostsDataOnScroll(cat, post, country, keyword, mile, zip, records);

                var rec = parseInt(records) + 1;
                $('#hdmore5').val(rec);
            }
        }
    }
});

function isScrollBottom() {
 
    //if listing
    if ($('#hdListing').val() == '1') {
        return false;
    }
    var pos = '.8';
    //-----------------------------
    var documentHeight = $(document).height();
    var scrollPosition = $(window).height() + $(window).scrollTop();
    //alert(documentHeight + ' ' + scrollPosition);

    return (scrollPosition >= (documentHeight * pos));
}
///toggle post Div
function togglepostDiv(shortID, fullID) {
    $('#' + shortID).toggle();
    $('#' + fullID).toggle();
}
/////////////////trim special characters
function trim(str) {

}

///////update user controls
function updateUserControl() {
    var ControlName = "/UserControls/Login.ascx";
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/LoadControls",
        data: "{ controlName:'" + ControlName + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            $('#DVlinkings').html(response.d);
        },
        async: false,
        error: function (xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
}

//////////////////////////////////////////////////////////////////////////////////
//-----------------------Filter Posts---------------------------------------------
//retrieve comments
function FilterPostsData1(CatID, PostID, CountryID, Keywords, PageNo, Miles, Zip, Tab) {

    if ($('#hd3rdLevel').val() == "1") {
        Tab = Tab + "@" + $('#hd3rdLevelTab').val();
    }
    FilterPostsData(CatID, PostID, CountryID, Keywords, PageNo, Miles, Zip, Tab);
    /*
    //if Filter is for Listings then go to Market Place Page
    if (PostID.indexOf("1_") >= 0) {
    window.location = '/PublicSite/MarketPlace/Default.aspx?listingID=' + PostID;
    } else {
    FilterPostsData(CatID, PostID, CountryID, Keywords, PageNo, Miles, Zip, Tab);
    }
    */
}
function FilterPostsData(CatID, PostID, CountryID, Keywords, PageNo, Miles, Zip, Tab) {
    
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/FilterPosts",
        data: "{'CatID':'" + CatID + "','PostID':'" + PostID + "','CountryID':'" + CountryID + "','Postkeywords':'" + Keywords + "','PageNo':'" + PageNo + "','Miles':'" + Miles + "','Zip':'" + Zip + "','Tab':'" + Tab + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) { 
                NotFound = false;
                var dvComment = document.getElementById("Comments");
                dvComment.innerHTML += msg.d;
                $('#more_').show();
                //////
                var rec = parseInt(PageNo) + 1;
                $('#hdmore5').val(rec);
            }
            else {
                //NotFound = true;
                $('#more_').hide();
                var dvComment = document.getElementById("Comments");
                
                if (dvComment.innerHTML == '') {
                    dvComment.innerHTML = "<br /><br /><center><strong>Sorry! Your search did not match any posts.</strong></center>";
                    $('#more_').hide();
                }
            }
        },
        async: true,
        error: function (xhr, status, error) {
             //alert(xhr.statusText);
        }
    });

}

////////////////////////////////////
var IsScroll = false;
var NotFound = false;
function FilterPostsDataOnScroll(catID, postID, countryID, keywords, miles, zip, records) {

    FilterPostsData(catID, postID, countryID, keywords, records, miles, zip, $('#hdTabInd').val());

    if (NotFound == true && IsScroll == false) {

        var dvComment = document.getElementById("Comments");
        dvComment.innerHTML = "<br /><br /><center><strong>Sorry! Your search did not match any posts.</strong></center>";
        $('#more_').hide();

    }
    IsScroll = false;
}

////////////////////////////Hide Links

function HideLinks() {
    $('#imgPost2World').hide();
    $('#Post2World2').hide();
    $('#imgPost2Vendor').hide();
    $('#Post2Vendor').hide();
    $('#imgPost2Client').hide();
    $('#Post2Client').hide();
    $('#imgPost2Linking').hide();
    $('#Post2Linking').hide();
    $('#imgMessage2Vendor').hide();
    $('#Message2Vendor').hide();
    $('#imgMessage2Client').hide();
    $('#Message2Client').hide();
    $('#imgMessage2Linking').hide();
    $('#Message2Linking').hide();
}

function ShowPostLinks() {
    $('#imgPost2World').show();
    $('#Post2World2').show();
    $('#imgPost2Vendor').show();
    $('#Post2Vendor').show();
    $('#imgPost2Client').show();
    $('#Post2Client').show();
    $('#imgPost2Linking').show();
    $('#Post2Linking').show();
}

function ShowNetworkLinks() {
    $('#imgPost2Vendor').show();
    $('#Post2Vendor').show();
    $('#imgPost2Client').show();
    $('#Post2Client').show();
}

function ShowMessageLinks() {
    $('#imgMessage2Vendor').show();
    $('#Message2Vendor').show();
    $('#imgMessage2Client').show();
    $('#Message2Client').show();
    $('#imgMessage2Linking').show();
    $('#Message2Linking').show();
}

function ShowLinkingsLinks() {
    $('#imgMessage2Linking').show();
    $('#Message2Linking').show();
    $('#imgPost2Linking').show();
    $('#Post2Linking').show();
}


//var listinghtml;

function getlistings(StatusCode, ListingTypeID, PageNumber) {
    var pnListing = document.getElementById("listings");
    pnListing.innerHTML = " ";
    //var pagenumber = $('#hdPageListings').val();
    if (PageNumber == 1) {
        $('#dv_less').hide();
    }
    else
        $('#dv_less').show();
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/GenerateListingsHtml",
        data: "{'StatusCode':'" + StatusCode + "','ListingTypeID':'" + ListingTypeID + "','PageNumber':'" + PageNumber + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                var pnListing = document.getElementById("listings");
                pnListing.innerHTML = "";
                pnListing.innerHTML += msg.d;
                //listinghtml = msg.d;

            } else {
                $('#dv_more').hide();

            }
        },
        async: false,
        error: function (xhr, status, error) {
            alert(xhr.statusText);
        }
    });
}

//to get all the listing types on subtabs of listings
function getAllListingTypes() {

    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/GetListingTypes",

        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                var dvSub = document.getElementById("ListingTypesdv");
                dvSub.innerHTML = "";
                dvSub.innerHTML += msg.d;
            } else {

            }
        },
        async: false,
        error: function (xhr, status, error) {
            alert(xhr.statusText);
        }
    });
}



function setHdlistings(obj, ListingTypeID) {
    var LTypeID = ListingTypeID;
    $('#hdlistingTypes').val(LTypeID);
    var ItemID;
    var links = $("a[id^='lnk_']");
    for (var i = 0; i < links.length; i++) {
        ItemID = links[i].id;
        $('#' + ItemID).attr('color', 'red');

    }
    return true;


}

function setListingTypesID(statusCode, title) {
    var listingTypeID = $('#hdlistingTypes').val();
    $('#hdListingStatus').val(statusCode);
    $('#hdPageListings').val(1);
    getlistings(statusCode, listingTypeID, 1);

    // change classes for selected or not selected tabs
    $('#subtabActive').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    $('#subtabPayment').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    $('#subtabShipment').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    $('#subtabWatching').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    $('#subtabCompleted').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    $('#subtabArchived').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');

    $('#subtab' + title).removeClass('rtsLink rad_tab').addClass('rtsLink rad_tab rtsSelected newTabSelected');
}


function DeleteoneListing(id) {

    try {
        if (!confirm('Are you sure you want to delete?')) {
            return false;
        }
        $.ajax({
            type: "POST",
            url: "LiveFeed.aspx/DeleteListing",
            data: "{'id':'" + id + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                if (msg.d.length > 0) {

                    var dv = document.getElementById('tbrow' + id);
                    $(dv).fadeOut(500);
                    var status = $('#hdListingStatus').val();
                    var listingTypeID = $('#hdlistingTypes').val();
                    var pagenumber = $('#hdPageListings').val();
                    getlistings(status, listingTypeID, pagenumber);


                }
            },
            async: false,
            error: function (xhr, status, error) {
                alert(xhr.statusText);
            }
        });
    }
    catch (e) { alert(e); }

}

function DeleteListings() {

    try {
        var chkList = $("input[id^='chkListing_']");
        var ItemID;
        var chkID;
        var arrListingID = "";
        for (var i = 0; i < chkList.length; i++) {
            ItemID = chkList[i].id;
            chkID = ItemID.substring(ItemID.lastIndexOf("_") + 1);
            if ($('#' + ItemID).is(':checked')) {
                arrListingID += chkID + ",";
            }

        }
        if (!confirm('Are you sure you want to delete?')) {
            return false;
        }
        $.ajax({
            type: "POST",
            url: "LiveFeed.aspx/DeleteListings",
            data: "{'id':'" + arrListingID + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                if (msg.d.length > 0) {
                    alert(msg);
                    var status = $('#hdListingStatus').val();
                    var listingTypeID = $('#hdlistingTypes').val();
                    $('#listings').html("");
                    var pagenumber = $('#hdPageListings').val();
                    getlistings(status, listingTypeID, pagenumber);
                }
            },
            async: false,
            error: function (xhr, status, error) {
                alert(xhr.statusText);
            }
        });
    }
    catch (e) { alert(e); }

}

function ViewNextListings() {
    var pagenumber = $('#hdPageListings').val();
    pagenumber++;
    $('#hdPageListings').val(pagenumber);
    var listingTypeID = $('#hdlistingTypes').val();
    var statusCode = $('#hdListingStatus').val();


    getlistings(statusCode, listingTypeID, pagenumber);
    //var listhtml=listinghtml;
    // getlistings(statusCode, listingTypeID, pagenumber + 1);
    // var pnListing = document.getElementById("listings");
    // pnListing.innerHTML = listhtml;


}

function ViewPreviousListings() {
    var pagenumber = $('#hdPageListings').val();
    pagenumber--;
    $('#hdPageListings').val(pagenumber);
    var listingTypeID = $('#hdlistingTypes').val();
    var statusCode = $('#hdListingStatus').val();
    getlistings(statusCode, listingTypeID, pagenumber);
    $('#dv_more').show();
    if (pagenumber == 1)
        $('#dv_less').hide();


}

function leftPanelListings() {

    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/GenerateLeftmenu",

        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                var dvSub = document.getElementById("Listing_panelLeft");
                dvSub.innerHTML = msg.d;
            } else {

            }
        },
        async: false,
        error: function (xhr, status, error) {
            alert(xhr.statusText);
        }
    });
}

// to print the selected listing
function PrintSelected(date, price, title) {
    var win = window.open();
    self.focus();
    win.document.open();
    win.document.write('<' + 'html' + '><' + 'body' + '>');
    win.document.write("<table style='width:70%;'>");
    win.document.write("<tr style='height:25px; font-size:10pt; font-weight:bold; padding-top:0px;'>");
    win.document.write("<td width='30%'>&nbsp Date</td>");
    win.document.write("<td width='50%'>Title</td>");
    win.document.write("<td width='20%'>Price</td></tr>");
    win.document.write("<tr style='height:25px; font-size:10pt; font-weight:bold; padding-top:0px;'>");
    win.document.write("<td width='30%'>" + date + "</td>");
    win.document.write("<td width='50%'>" + title + "</td>");
    win.document.write("<td width='20%'>" + "$" + price + "</td></tr>");
    win.document.write("</table>");
    win.document.write('<' + '/body' + '><' + '/html' + '>');
    win.document.close();
    win.print();
    win.close();

}

// set the listing types on left panel
function getListings_LeftPan(listingTypeID, statusCode) {

    $('#hdListingStatus').val(statusCode);
    $('#hdPageListings').val(1);
    getlistings(statusCode, listingTypeID, 1);

}

function ChangeStatus(status) {

    try {
        var chkList = $("input[id^='chkListing_']");
        var ItemID;
        var chkID;
        var arrListingID = "";
        for (var i = 0; i < chkList.length; i++) {
            ItemID = chkList[i].id;
            chkID = ItemID.substring(ItemID.lastIndexOf("_") + 1);
            if ($('#' + ItemID).is(':checked')) {
                arrListingID += chkID + ",";
            }

        }


        $.ajax({
            type: "POST",
            url: "LiveFeed.aspx/ChangeStatus",
            data: "{'id':'" + arrListingID + "','status':'" + status + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                if (msg.d.length > 0) {
                    alert(msg);
                    var status = $('#hdListingStatus').val();
                    var listingTypeID = $('#hdlistingTypes').val();
                    $('#listings').html("");
                    var pagenumber = $('#hdPageListings').val();
                    getlistings(status, listingTypeID, pagenumber);

                }
            },
            async: false,
            error: function (xhr, status, error) {
                alert(xhr.statusText);
            }
        });
    }
    catch (e) { alert(e); }

}

function ChangeoneStatus(id, status) {

    try {

        $.ajax({
            type: "POST",
            url: "LiveFeed.aspx/ChangeStatus",
            data: "{'id':'" + id + "','status':'" + status + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                if (msg.d.length > 0) {
                    var status = $('#hdListingStatus').val();
                    var listingTypeID = $('#hdlistingTypes').val();
                    $('#listings').html("");
                    var pagenumber = $('#hdPageListings').val();
                    getlistings(status, listingTypeID, pagenumber);

                }
            },
            async: false,
            error: function (xhr, status, error) {
                alert(xhr.statusText);
            }
        });
    }
    catch (e) { alert(e); }

}

function PrintChecked() {

    try {

        var win = window.open();
        self.focus();
        win.document.open();
        win.document.write('<' + 'html' + '><' + 'body' + '>');
        win.document.write("<table style='width:70%;'>");
        win.document.write("<tr style='height:25px; font-size:10pt; font-weight:bold; padding-top:0px;'>");
        win.document.write("<td width='30%'>&nbsp Date</td>");
        win.document.write("<td width='50%'>Title</td>");
        win.document.write("<td width='20%'>Price</td></tr>");

        var chkList = $("input[id^='chkListing_']");
        var ItemID;
        var chkID;
        var arrListingID = "";
        for (var i = 0; i < chkList.length; i++) {
            ItemID = chkList[i].id;
            chkID = ItemID.substring(ItemID.lastIndexOf("_") + 1);
            if ($('#' + ItemID).is(':checked')) {
                var hiddenfield = $("input[id^='hd_" + chkID + "']");
                for (var j = 0; j < hiddenfield.length; j++) {
                    win.document.write("<tr style='height:25px; font-size:10pt; font-weight:bold; padding-top:0px;'>");
                    win.document.write("<td width='30%'>" + hiddenfield.attr('name').substring(hiddenfield.attr('name').lastIndexOf("_") + 1) + "</td>");
                    win.document.write("<td width='50%'>" + hiddenfield.val() + "</td>");
                    win.document.write("<td width='20%'>" + "$" + hiddenfield.attr('name').substring(0, hiddenfield.attr('name').lastIndexOf("_")) + "</td></tr>");
                }
            }

        }

        win.document.write("</table>");
        win.document.write('<' + '/body' + '><' + '/html' + '>');
        win.document.close();
        win.print();
        win.close();

    } catch (e) {

    }

}


///Change css for 3rd level tab in LiveFeed
function ChangeCSS(id, tab) {
    //clear Live Feeds
    $('#Comments').html("");
    //remeber which 3rd tab is clicked
    $('#hd3rdLevelTab').val(tab);

    // change classes for selected or not selected tabs
    $('#subtabAll').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    $('#subtabIst').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    $('#subtab2nd').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    //---------------------------------------------------------------------------------------------------
    $('#' + id).removeClass('rtsLink rad_tab').addClass('rtsLink rad_tab rtsSelected newTabSelected'); 

}
function CallDBMethod(id, tab) {
    try {
        //reset page no.
        $('#hdmore5').val(1);
        ChangeCSS(id, tab);
        //add 1 to hidden field 3rd level
        $('#hd3rdLevel').val(1);

        var currentTab = $('#hdTabInd').val();
        var records = $('#hdmore5').val();


        //----------------------------------
        ChangeHeading(currentTab, tab, records);
        

    } catch (e) {
        //alert(e);
    }
}

function LoadData3rdLevel(selectedTab, pageNo) {
    
    ////get page no
    var records = $('#hdmore5').val(); 
    //-----------------------------------------------------
    $.ajax({
        type: "POST",
        url: "LiveFeed.aspx/LoadData3rdLevel",
        data: "{'selectedTab':'" + selectedTab + "','pageNo':'" + pageNo + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                var dvComment = document.getElementById("Comments");
                dvComment.innerHTML += msg.d;
                $('#more_').show();
            } else {
                $('#more_').hide();
            }
        },
        async: false,
        error: function (xhr, status, error) {
             //alert(xhr.statusText);
        }
    });

    //---------------------------------------------------
    //-----------------add     +1 to page no
    var rec = parseInt(records) + 1;
    $('#hdmore5').val(rec);
}


//select and highligh the 3rd and fourth level tab in Listing/DealingFloor.aspx

function SelectCurrent(id) {
    setTimeout("GetHighlight('" + id + "')", 1000);
}

function GetHighlight(id) {
    $('#dvSelling').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    $('#dvBuying').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    $('#dvQuestion').removeClass("rtsLink rad_tab rtsSelected newTabSelected").addClass('rtsLink rad_tab');
    //---------------------------------------------------------------------------------------------------
    $('#' + id).removeClass('rtsLink rad_tab').addClass('rtsLink rad_tab rtsSelected newTabSelected');
}

