﻿/// <reference path="../jquery-1.4.1.js" />
/// <reference path="../jquery-ui-1.8.1.custom.js" />


$(document).ready(function() {

    getClassifieds('selling');

    //==============================================
    //set dialogs
    //==============================================
    $('#divPost').dialog({
        autoOpen: false,
        width: 640,
        title: 'Add New Classified',
        modal: true,
        resizable: false

    });

    $('#dvTerms').dialog({
        autoOpen: false,
        width: 640,
        height: 500,
        title: 'Terms and Conditions',
        modal: true,
        resizable: false

    });


    //==============================================
    //clicks events
    //==============================================

    $('DIV[ID*=dvBtn]').click(function() {
        var t = $(this).attr('rel');
        if (t == 'b')
            getClassifieds('buying');
        if (t == 's')
            getClassifieds('selling');
        if (t == 'a')
            getClassifieds('announcements');

    });

    $('#dvMyCl').click(function() {
        getMyClassifieds();
    });


    //==============================================
    //functions
    //==============================================
    function getClassifieds(clType) {

        var hdnSec = $('INPUT[ID*=hdnSec]').val();
        if (hdnSec == '-1')
            $('#dvMyCl').hide();


        var ldr = '<div style=\"margin-top:20px;text-align:center;width:200px;margin-left:auto;margin-right:auto;\">';
        ldr += '<img src=\"images\/ajax-loaderbert.gif\" \/><br\/>';
        ldr += '<span style=\"font-size:18px;\">Loading Classified Information<\/span>';
        ldr += '<\/div>';
        $('#dvPage').html(ldr);

        var bld = '';
        $.ajax({
            type: "POST",
            url: "services/classifieds.asmx/showList",
            data: "{'clType':'" + clType + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var tt = (typeof msg.d) == 'string' ? eval('(' + msg.d + ')') : msg.d;

                if (tt != '') {
                    bld = '<div style=\"border-right:1px solid #e5e5e5;border-left:1px solid #e5e5e5;border-bottom:1px solid #e5e5e5;\">';
                    for (var i in tt) {
                        bld += '<div style=\"border-top:1px solid #e5e5e5;\">';
                        bld += '<div style=\"padding:10px;float:left; width:75px; text-align:center; border-right:1px solid #e5e5e5; color:#000000;\">' + tt[i].dateEntered + '<\/div>';
                        bld += '<div class=\"dvCl\" rel=\"' + tt[i].classifiedID + '\"  style=\"cursor:pointer;padding:10px;float:left;text-decoration:underline;\">' + tt[i].title.toUpperCase() + '<\/div>';
                        bld += '<div style=\"padding:10px; text-align:center;float:right; border-left:1px solid #e5e5e5; width:150px; background-color:#666666; color:#ffffff; \">' + tt[i].username.toUpperCase() + '<\/div>';
                        bld += '<div style=\"clear:both;\"><\/div>';
                        bld += '<\/div>';

                    }
                    bld += '<\/div>';
                } else {
                    bld += '<div style=\"margin-top:15px; font-size:18px;\">Nothing Found<\/div>';
                }


                $('#dvPage').html(bld);

                $('.dvCl').click(function() {
                    var clid = $(this).attr('rel');
                    getClassified(clid);
                });

            },
            error: function(xhr) {
                var err = eval("(" + xhr.responseText + ")");
                bld = err.Message;

                if (bld == 'LOGERR')
                    document.location = '../login.aspx';

                $('#dvPage').html(bld);
            }
        });
    }

    function getClassified(clid) {

        var ldr = '<div style=\"margin-top:20px;text-align:center;width:200px;margin-left:auto;margin-right:auto;\">';
        ldr += '<img src=\"images\/ajax-loaderbert.gif\" \/><br\/>';
        ldr += '<span style=\"font-size:18px;\">Loading Classified Information<\/span>';
        ldr += '<\/div>';
        $('#dvPage').html(ldr);

        var bld = '';
        $.ajax({
            type: "POST",
            url: "services/classifieds.asmx/showClassified",
            data: "{'clid':'" + clid + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var tt = (typeof msg.d) == 'string' ? eval('(' + msg.d + ')') : msg.d;
                bld = '<div>';
                if (tt != '') {
                    for (var i in tt) {
                        bld += '<div>';

                        bld += '<div style=\"font-size:20px;color:#000000;\">' + tt[i].title.toUpperCase() + '<\/div>';
                        bld += '<div>';
                        bld += '<div style=\"float:left; padding-right:10px;font-size:14px;color:#CC0000;\">' + tt[i].dateEntered + '<\/div>';
                        bld += '<div style=\"float:left;font-size:14px;\">' + tt[i].username.toUpperCase() + '<\/div>';
                        bld += '<div style=\"clear:both\"><\/div>';
                        bld += '<\/div>';
                        bld += '<div style=\"margin-top:20px;color:#000000;\">' + tt[i].message + '<\/div>';

                        bld += '<\/div>';
                    }
                } else {
                    bld += '<div style=\"margin-top:15px;\">Nothing Found<\/div>';
                }
                bld += '<\/div>';

                $('#dvPage').html(bld);
                $('html, body').animate({ scrollTop: 420 }, 0);

            },
            error: function(xhr) {
                var err = eval("(" + xhr.responseText + ")");
                bld = err.Message;

                if (bld == 'LOGERR')
                    document.location = '../login.aspx';

                $('#dvPage').html(bld);
            }
        });
    }

    function getMyClassifieds() {

        var ldr = '<div style=\"margin-top:20px;text-align:center;width:200px;margin-left:auto;margin-right:auto;\">';
        ldr += '<img src=\"images\/ajax-loaderbert.gif\" \/><br\/>';
        ldr += '<span style=\"font-size:18px;\">Loading Classified Information<\/span>';
        ldr += '<\/div>';
        $('#dvPage').html(ldr);

        var bld = '';
        $.ajax({
            type: "POST",
            url: "services/classifieds.asmx/showMyList",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var tt = (typeof msg.d) == 'string' ? eval('(' + msg.d + ')') : msg.d;

                bld += '<div style=\"border:1px solid #666666; background-color:#e5e5e5;padding:5px;margin-bottom:10px;\">';
                bld += '<div style=\"float:left;padding: 5px;\">Please make sure you&#39;ve read the ';
                bld += '<span id=\"spTerms\" style=\"cursor:pointer; text-decoration:underline;\">CPG Classifieds Terms and Conditions</span>.'
                bld += '</div>';
                bld += '<div style=\"cursor: pointer; text-align: center;float:right; background-color:#ffffff;';
                bld += 'width: 100px; padding: 5px; border: solid 1px #666666; margin-right: 10px;\" id=\"addNew\">Add New</div>';

                bld += '<div style=\"clear:both;\"><\/div>';
                bld += '<\/div>';
                bld += '<div>';
                if (tt != '') {
                    bld += '<div style=\"border-right:1px solid #e5e5e5;border-left:1px solid #e5e5e5;border-bottom:1px solid #e5e5e5;\">';
                    var lastPartner = '';
                    var lastAircraft = '';
                    for (var i in tt) {
                        bld += '<div style=\"border-top:1px solid #e5e5e5;\">';
                        bld += '<div style=\"padding:10px;float:left; width:75px; text-align:center; border-right:1px solid #e5e5e5; color:#000000;\">' + tt[i].dateEntered + '<\/div>';
                        bld += '<div class=\"dvCl\" rel=\"' + tt[i].classifiedID + '\"  style=\"cursor:pointer;padding:10px;float:left; text-decoration:underline;\">' + tt[i].title.toUpperCase() + '<\/div>';
                        bld += '<div class=\"dvStp\" rel=\"' + tt[i].classifiedID + '\" style=\"padding:10px; cursor:pointer; text-align:center;float:right; border-left:1px solid #e5e5e5; background-color:#666666; color:#ffffff; width:150px;\">STOP ' + tt[i].classifiedType.toUpperCase() + '<\/div>';
                        bld += '<div style=\"clear:both;\"><\/div>';
                        bld += '<\/div>';
                    }
                    bld += '<\/div>';
                } else {
                    bld += '<div style=\"margin-top:15px; font-size:18px;\">Nothing Found<\/div>';
                }


                //add html to the page
                $('#dvPage').html(bld);


                //show this particular classified
                $('.dvCl').click(function() {
                    var clid = $(this).attr('rel');
                    getClassified(clid);
                });

                //show this particular classified
                $('.dvStp').click(function() {
                    var clid = $(this).attr('rel');
                    var cat = $(this).html();
                    $('#divPost').dialog('open');
                    stopClassified(clid, cat);
                });

                //open dialog to add new classified
                $('#addNew').click(function() {
                    $('#dvAdding').hide().html('');
                    $('#dvAdInfo').show();
                    $('#clTitle').val('');
                    $('#clMsg').val('');
                    $('#divPost').dialog('open');
                    $('#divPost').dialog('option', 'buttons', {
                        'Post New Ad': function() {
                            addNewClassified();
                            //$(this).dialog('close');
                        },
                        Cancel: function() {
                            $(this).dialog('close');
                        }
                    });
                });

                //disable add button
                var nCnt = $('SPAN[ID*=lblMyClassifieds]').html();
                var hdnSec = $('INPUT[ID*=hdnSec]').val();
                var nAmt = nCnt.replace('(', '').replace(')', '').replace(' ', '');


                if ((parseInt(hdnSec) < 2) && (parseInt(nAmt) > 0)) {
                    $('#addNew').unbind();
                    $('#addNew').click(function() {
                        ldr = '<div style=\"margin-top:20px;text-align:center;width:200px;margin-left:auto;margin-right:auto;\">';
                        ldr += '<span style=\"font-size:18px;\">You are at the maximum allowed for free users<\/span>';
                        ldr += '<\/div>';
                        $('#dvAdding').show().html(ldr);
                        $('#dvAdInfo').hide();
                        $('#divPost').dialog('open');
                        $('#divPost').dialog('option', 'buttons', {
                            'Close': function() {
                                $(this).dialog('close');
                            }
                        });
                    });

                }

                //open dialog for terms and conditions
                $('#spTerms').click(function() {
                    $('#dvTerms').dialog('open');
                });

            },
            error: function(xhr) {
                var err = eval("(" + xhr.responseText + ")");
                bld = err.Message;

                if (bld == 'LOGERR')
                    document.location = '../login.aspx';

                $('#dvPage').html(bld);
            }
        });
    }

    function addNewClassified() {

        var clTitle = $('#clTitle').val();
        var clMsg = $('#clMsg').val();
        var clCat = $("#clCat option:selected").val();

        if ((clTitle.length <= 0) || (clMsg.length <= 0))
            return;

        var ldr = '<div style=\"margin-top:20px;text-align:center;width:200px;margin-left:auto;margin-right:auto;\">';
        ldr += '<img src=\"images\/ajax-loaderbert.gif\" \/><br\/>';
        ldr += '<span style=\"font-size:18px;\">Adding New Classified<\/span>';
        ldr += '<\/div>';

        $('#divPost').dialog('option', 'buttons', {});

        $('#dvAdding').show().html(ldr);
        $('#dvAdInfo').hide();

        $.ajax({
            type: "POST",
            url: "services/classifieds.asmx/addClassified",
            data: "{'cat':'" + clCat + "', 'title':'" + clTitle + "', 'msg':'" + escape(clMsg) + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var tt = (typeof msg.d) == 'string' ? eval('(' + msg.d + ')') : msg.d;

                if (tt == '-1') {
                    ldr = '<div style=\"margin-top:20px;text-align:center;width:300px;margin-left:auto;margin-right:auto;\">';
                    ldr += '<span style=\"font-size:18px;\">Sorry, That Ad Exists<\/span>';
                    ldr += '<\/div>';
                    $('#dvAdding').html(ldr);
                }
                else {
                    ldr = '<div style=\"margin-top:20px;text-align:center;width:300px;margin-left:auto;margin-right:auto;\">';
                    ldr += '<span style=\"font-size:18px;\">Classifieds Updated<\/span>';
                    ldr += '<\/div>';

                    var nCnt = '';
                    if (clCat.toLowerCase() == 'selling')
                        nCnt = $('SPAN[ID*=lblAmtSelling]');
                    if (clCat.toLowerCase() == 'buying')
                        nCnt = $('SPAN[ID*=lblAmountBuying]');
                    if (clCat.toLowerCase() == 'announcements')
                        nCnt = $('SPAN[ID*=lblAnnounce]');


                    var nAmt = nCnt.html().replace('(', '').replace(')', '').replace(' ', '');
                    nAmt = parseInt(nAmt) + 1;
                    nCnt.html(' (' + nAmt + ')');

                    $('SPAN[ID*=lblMyClassifieds]').html(' (' + tt + ')');

                    $('#dvAdding').html(ldr);
                    $('#divPost').dialog('option', 'buttons', {
                        'Close': function() {
                            $(this).dialog('close');
                        }
                    });

                    getMyClassifieds();
                }

            },
            error: function(xhr) {
                var err = eval("(" + xhr.responseText + ")");
                var bld = err.Message;

                if (bld == 'LOGERR')
                    document.location = '../login.aspx';

                $('#dvAdding').html(bld);
            }
        });
    }

    function stopClassified(clid, cat) {
        var catType = cat.replace('STOP ', '');
        var ldr = '<div style=\"margin-top:20px;text-align:center;width:200px;margin-left:auto;margin-right:auto;\">';
        ldr += '<img src=\"images\/ajax-loaderbert.gif\" \/><br\/>';
        ldr += '<span style=\"font-size:18px;\">Stopping Classified<\/span>';
        ldr += '<\/div>';

        $('#divPost').dialog('option', 'buttons', {});

        $('#dvAdding').show().html(ldr);
        $('#dvAdInfo').hide();

        $.ajax({
            type: "POST",
            url: "services/classifieds.asmx/stopClassified",
            data: "{'catid':'" + clid + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var tt = (typeof msg.d) == 'string' ? eval('(' + msg.d + ')') : msg.d;

                if (tt == '-1') {
                    ldr = '<div style=\"margin-top:20px;text-align:center;width:300px;margin-left:auto;margin-right:auto;\">';
                    ldr += '<span style=\"font-size:18px;\">There was an error.<\/span>';
                    ldr += '<\/div>';
                    $('#dvAdding').html(ldr);
                }
                else {
                    ldr = '<div style=\"margin-top:20px;text-align:center;width:300px;margin-left:auto;margin-right:auto;\">';
                    ldr += '<span style=\"font-size:18px;\">Classified Stopped<\/span>';
                    ldr += '<\/div>';

                    var nCnt = '';
                    if (catType.toLowerCase() == 'selling')
                        nCnt = $('SPAN[ID*=lblAmtSelling]');
                    if (catType.toLowerCase() == 'buying')
                        nCnt = $('SPAN[ID*=lblAmountBuying]');
                    if (catType.toLowerCase() == 'announcements')
                        nCnt = $('SPAN[ID*=lblAnnounce]');


                    var nAmt = nCnt.html().replace('(', '').replace(')', '').replace(' ', '');
                    nAmt = parseInt(nAmt) - 1;
                    nCnt.html(' (' + nAmt + ')');

                    $('SPAN[ID*=lblMyClassifieds]').html(' (' + tt + ')');

                    $('#dvAdding').html(ldr);
                    $('#divPost').dialog('option', 'buttons', {
                        'Close': function() {
                            $(this).dialog('close');
                        }
                    });

                    getMyClassifieds();
                }


            },
            error: function(xhr) {
                var err = eval("(" + xhr.responseText + ")");
                var bld = err.Message;

                if (bld == 'LOGERR')
                    document.location = '../login.aspx';

                $('#dvAdding').html(bld);
            }
        });
    }

});