﻿/////////////////////////////////////////////////////////////////////////////////////
//                                                                                 //
//  Grupo de Soluciones Informáticas                                               //
//  ********************************                                               //
//                                                                                 //
//  Nombre: comment.js                                                             //
//  Contiene funciones para el comportamiento de los comentarios en la web.        //
//                                                                                 //
//  Nombre del Autor: Abel Pérez Martínez                                          //
//  Fecha de creación: 05/06/2010                                                  //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////

Type.registerNamespace("Comment");

Comment.Form = function (subjectId, commentId, userId, emailId, url) {
    this._userId = userId;
    this._subjectId = subjectId;
    this._commentId = commentId;
    this._emailId = emailId;
    this._url = url;
}

Comment.Form.prototype = {

    getUserId: function () {
        return this._userId;
    },

    getSubjectId: function () {
        return this._subjectId;
    },

    getCommentId: function () {
        return this._commentId;
    },

    getEmailId: function () {
        return this._emailId;
    },

    getUrl: function () {
        return this._url;
    },

    CleanNotifications: function () {
        var ok = document.getElementById("textOk");
        if (ok != null)
            ok.style.display = 'none';
        var error = document.getElementById("textError");
        if (error != null)
            error.style.display = 'none';
        var sent = document.getElementById("textEnviar");
        if (sent != null)
            sent.style.display = 'none';
    },

    InsertComment: function (idcontrol, IDContent, TypeEntity, UserName, Estate) {

        var div = document.getElementById("textOk");
        div.style.display = 'none';
        var div2 = document.getElementById("textError");
        div2.style.display = 'none';
        var send = document.getElementById("textEnviar");
        send.style.display = 'none';

        //        if (Page_ClientValidate(idcontrol + "_commentgroup")) {

        var subjectValue = "";
        var commentValue = "";
        var userValue = ""
        var emailValue = ""

        var subjectID = document.getElementById(this.getSubjectId());
        var commentID = document.getElementById(this.getCommentId());
        var userID = document.getElementById(this.getUserId());
        var emailID = document.getElementById(this.getEmailId());
        var url = this.getUrl();

        if (userID != null) {
            userValue = userID.value;
        }
        else
            userValue = UserName;

        if (subjectID != null)
            subjectValue = subjectID.value;

        commentValue = commentID.value;

        if (emailID != null)
            emailValue = emailID.value;

        var send = document.getElementById("textEnviar");
        //alert(send);
        send.style.display = '';

        GSI.CRM.Comments.CommentServices.InsertComment(IDContent, commentValue, subjectValue, TypeEntity, userValue, Estate, emailValue, url, SucceededCallback, errorCallback,
                    [this.getSubjectId(), this.getCommentId(), this.getUserId(), this.getEmailId()]);

    },

    ClientValidate: function () {

        var isValid = true;
        var re = new RegExp("<((?:.|\n)*?)>|</((?:.|\n)*?)>|</((?:.|\n)*?)>");
        var username = document.getElementById(this.getUserId());

        if (username != null) {
            var result = re.exec(username.value);

            if (result == null) {
                isValid = true;
            }
            else {
                isValid = false;
            }
        }

        if (isValid == true) {
            var subject = document.getElementById(this.getSubjectId());

            if (subject != null) {
                var result = re.exec(subject.value);

                if (result == null) {
                    isValid = true;
                }
                else {
                    isValid = false;
                }
            }
        }

        if (isValid == true) {
            var comment = document.getElementById(this.getCommentId()).value;

            var result = re.exec(comment);

            if (result == null) {
                isValid = true;
            }
            else {
                isValid = false;
            }
        }
        return isValid;
    }
}

function SucceededCallback(result, context) {
    var div = document.getElementById("textOk");
    div.style.display = '';
    var send = document.getElementById("textEnviar");
    send.style.display = 'none';

    // Limpiar el Formulario
    for (var i = 0; i < context.length; i++) {
        var control = document.getElementById(context[i]);
        if (control != null)
            control.value = "";
    }

}

function errorCallback(error) {
    var div2 = document.getElementById("textError");
    div2.style.display = '';
    var send = document.getElementById("textEnviar");
    send.style.display = 'none';
}

Comment.Form.registerClass('Comment.Form', null, Sys.IDisposable);


function Check_NewComment(grid, button_object, args) {
    var context = $("#" + grid.filterID);
    var sel_cont = $("select[id$=listContent]", context).val();
    var sel_entity = $("select[id$=listEntity]", context).val();
    DisableGroup(button_object, (sel_cont !== "-1" || sel_entity === "0") ? "" : "disabled");
}

function Check_EditComment(grid, button_object, args) {
    var context = $("#" + grid.filterID);
    var sel_entity = $("select[id$=listEntity]", context).val();
    DisableGroup(button_object, (args.get_num_rows() == 1 && sel_entity !== "-1") ? "" : "disabled");
}

function CommentDblClick(theRow, grid, id_filter) {
    var context = $("#" + id_filter);
    var sel_cont = $("select[id$=listContent]", context).val();
    var sel_entity = $("select[id$=listEntity]", context).val();
    var sel_cli = grid.getSelectedClient();
    var result = new Array();
    result = grid.SplitParam(theRow);
    var id = result[1];
    var edit_url = String.format(grid.redirect_edit, id, sel_cli);
    var querystring = String.format(grid.edit_querystring, sel_cont, sel_entity);
    window.location = edit_url + querystring;
}

function NewCommentClick(grid, id_filter) {
    var context = $("#" + id_filter);
    var sel_cont = $("select[id$=listContent]", context).val();
    var sel_entity = $("select[id$=listEntity]", context).val();
    var sel_cli = grid.getSelectedClient();
    var new_url = String.format(grid.redirect_new, "", sel_cli);
    var querystring = String.format(grid.new_querystring, sel_cont, sel_entity);
    window.location = new_url + querystring;
}

function EditCommentClick(grid, id_filter) {
    var context = $("#" + id_filter);
    var sel_cont = $("select[id$=listContent]", context).val();
    var sel_entity = $("select[id$=listEntity]", context).val();
    var sel_cli = grid.getSelectedClient();
    var edit_url = String.format(grid.redirect_edit, grid.GetSelectedIds(), sel_cli);
    var querystring = String.format(grid.edit_querystring, sel_cont, sel_entity);
    window.location = edit_url + querystring;
}


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();