﻿// Javasript para la funcionalidad del SendMessage

//Defino variables globales
var index = 1;    
var win = null;
var autenticate = 0;
var width = 0;
var height = 0; 
var fncOk = '';

// Variables Globales Para Definir que Mensaje enviar
var currentPath;
var thisType;

//****  SendMessage recibe type que significa quién lo llama ****  //
//  1 - Allegations     2 - Topic          3 - Post                //
//*****************************************************************//
function SendMessage(type, wildcardType, wilcardID, thisPath){ 
    
    currentPath = thisPath;
    thisType = type;
     
    if(type != '1'){
    
        //Debo evaluar si el usuario está autenticado
        checkAutentication();
    }
    else{ viewScreenMessage(wildcardType, wilcardID); 
    }
}

//*******************************************************************************************************************************//
//
//  FUNCION PARA MANEJAR QUÉ TIPO DE PANTALLA DEBO MOSTRAR SEGÚN LA LLAMADA
//
//*******************************************************************************************************************************//


function viewScreenMessage(wildcardType, wilcardID)
{
    if($("divImgContainer")) hide("divImgContainer","fade");
        

    //Debo chequear quien me llamó
    var url = null;
    switch(thisType) 
    {   
        case '1':
            url = basepath + "Messages/SendAllegation.aspx";
            width = 600;
            height = 250;
            fncOk = function(){SendAllegation(wildcardType, wilcardID);};
            break;
            
//        case '2':
//            url = basepath + "Messages/SendTopic.aspx";
//            width = 600;
//            height = 350;
//            fncOk = SendNewTopic;
//            break;
//     
//        case '3':
//            url = basepath + "Messages/SendPost.aspx"; 
//            width = 600;
//            height = 350;
//            fncOk = SendForumPost;
//            break;        
            
        default:
            alert('Tipo desconocido de llamada');
    }

    
    Dialog.confirm({url:url}, 
               {windowParameters: {className: "alphacube", width:width, height:height, closable:false}, okLabel: "Send", ok:fncOk, onCancel:function(win) {Windows.closeAllModalWindows();if($("divImgContainer")) show("divImgContainer","fade");return true;}});
    
    var timer_t = setTimeout(waterEmail, 1000);
    index++;
    
    
    
}

//*******************************************************************************************************************************//
//
//  FUNCION PARA MANEJAR EL ENVIO DE ALLEGATIONS
//
//*******************************************************************************************************************************//

function SendAllegation(type, id)
{
    var txtAllegations = deleteInvalidTags($F("txtAllegation"));
    
    //Valido contenido del mensaje
    if(txtAllegations == '') {
        $("lblMsgError").innerHTML = "You Must Give a Reason!";  
        show("lblMsgError");
        return false;
    }
    else if(CheckPalabras(txtAllegations) == false)
    {
        return false;
    }
    else if(txtAllegations.length > 200)
    {
      $("lblMsgError").innerHTML = "Only 200 Characters Allowed!";  
      show("lblMsgError");
      return false;
    }
    
    //-->Escape de Caracteres
    txtAllegations = escape(txtAllegations);
    
    switch (type)
    {
        case 'comment':
            $Ajax(basepath + "controllers/Messages/Allegations/SendAllegation.aspx?method=reportComment", 
                {metodo: $metodo.POST,            
                parametros: {"message":txtAllegations,"comment_id":id,"ip":userIP},
                cache: false});
            break;
        case 'topic':
            $Ajax(basepath + "controllers/Messages/Allegations/SendAllegation.aspx?method=reportTopic", 
                {metodo: $metodo.POST,            
                parametros: {"message":txtAllegations,"topic_id":id,"ip":userIP},
                cache: false});
            break;  
        case 'post':
            $Ajax(basepath + "controllers/Messages/Allegations/SendAllegation.aspx?method=reportPost", 
                {metodo: $metodo.POST,            
                parametros: {"message":txtAllegations,"post_id":id,"ip":userIP},
                cache: false});
            break; 
        case 'photo':
            $Ajax(basepath + "controllers/Messages/Allegations/SendAllegation.aspx?method=reportPhoto", 
                {metodo: $metodo.POST,            
                parametros: {"message":txtAllegations,"photo_id":id,"ip":userIP},
                cache: false});
            break; 
        case 'video':
            $Ajax(basepath + "controllers/Messages/Allegations/SendAllegation.aspx?method=reportVideo", 
                {metodo: $metodo.POST,            
                parametros: {"message":txtAllegations,"video_id":id,"ip":userIP},
                cache: false});
            break; 
    }
    
    //Cierro todas las ventanas abiertas que hayan actualmente.
    Windows.closeAllModalWindows();
    if($("divImgContainer")) show("divImgContainer","fade");
    return true;

}

//*******************************************************************************************************************************//
//
//  FUNCION PARA MANEJAR EL ENVÍO DE TÓPICO GENERAL
//
//*******************************************************************************************************************************//

function SendNewTopic(){
    var title = deleteInvalidTags($F("txtTopicTitle"));
    var message = deleteInvalidTags($F("txtTopicPost")); 
    
    if(message == '' || message == ' ' || title == '' || title == ' ') {
        $("divNewTopicResponse").innerHTML = "You Must Enter Title and Message!";    
        Effect.BlindDown('divNewTopicResponse', { duration: 0.5 });  
        SendNewTopicResponseWait = setTimeout(TopicResponse, 1500);
        return false;
    }
    else if(message.length > 5000)
    {
       $("divNewTopicResponse").innerHTML = "Only 5000 Characters Allowed!";    
       Effect.BlindDown('divNewTopicResponse', { duration: 0.5 });  
       SendNewTopicResponseWait = setTimeout(TopicResponse, 1500);
       return false;
    }
    else if(CheckPalabras(message) == false)
    {
        return false;
    }
    
    $Ajax(basepath + "controllers/Forums/Topics.aspx?method=SendNewTopic", 
        {metodo: $metodo.POST,
        parametros: {"title":escape(title),"message":escape(message)},
        onfinish: function(InsertResponse){ 
            if(InsertResponse == '1')
            {
                $("divNewTopicResponse").innerHTML = "Your Topic was created"; 
                $("txtTopicTitle").value = "";
                $("txtTopicPost").value = "";
                $("charsLeft").value = "";
                Effect.BlindDown('divNewTopicResponse', { duration: 0.5 });    
                __doPostBack('AddedTopic','');    
                SendNewTopicResponseWait = setTimeout(TopicResponse, 2000);
            }
            else
            {
                $("divNewTopicResponse").innerHTML = "Please try again later";    
                Effect.BlindDown('divNewTopicResponse', { duration: 0.5 });  
                SendNewTopicResponseWait = setTimeout(TopicResponse, 2000);           
            }
            
        },
        cache: false});
    
    return true;
}

//*******************************************************************************************************************************//
//
//  FUNCION PARA MANEJAR EL ENVÍO DE POST DE UN TOPICO GENERAL
//
//*******************************************************************************************************************************//

function SendForumPost(){    
    var message = deleteInvalidTags($F("txtPostMessage"));
    var topic_id = parseFriendlyQS(window.location);
    //Valido contenido del mensaje
    if(message == '' || message == ' ') {
        $("divPostResponse").innerHTML = "You Must Enter Message!";    
        Effect.BlindDown('divPostResponse', { duration: 0.5 });  
        PostResponseWait = setTimeout(TopicResponse, 1500);
        return false;
    }
    else if(message.length > 5000)
    {
       $("divPostResponse").innerHTML = "Only 5000 Characters Allowed!";    
       Effect.BlindDown('divPostResponse', { duration: 0.5 });  
       PostResponseWait = setTimeout(TopicResponse, 1500);
       return false;
    }
    else if(CheckPalabras(message) == false)
    {
        return false;
    }
    
    $Ajax(basepath + "controllers/Forums/Posts.aspx?method=sendPost", 
        {metodo: $metodo.POST,
        parametros: {"message":escape(message),"topic_id":topic_id},
        onfinish: function(InsertResponse){ 
            if(InsertResponse == '1')
            {
                $("divPostResponse").innerHTML = "Your Message was posted"; 
                $("txtPostMessage").value = "";
                $("charsLeft").value = "";
                Effect.BlindDown('divPostResponse', { duration: 0.5 });    
                __doPostBack('AddedPost','');    
                PostResponseWait = setTimeout(TopicResponse, 2000);
            }
            else
            {
                $("divPostResponse").innerHTML = "Please try again later";    
                Effect.BlindDown('divPostResponse', { duration: 0.5 });  
                PostResponseWait = setTimeout(TopicResponse, 2000);           
            }
            
        },
        cache: false});
    
    return true;
}

//*******************************************************************************************************************************//
//
//  FUNCION PARA MANEJAR EL ENVÍO DE COMMENTS
//
//*******************************************************************************************************************************//

function SendHealerComment(){    
    var message = deleteInvalidTags($F("txtCommentMessage"));
    var healer_id = parseFriendlyQS(window.location);
    //Valido contenido del mensaje
    if(message == '' || message == ' ') {
        $("divCommentResponse").innerHTML = "You Must Enter Message!";    
        Effect.BlindDown('divCommentResponse', { duration: 0.5 });  
        CommentResponseWait = setTimeout(CommentResponse, 1500);
        return false;
    }
    else if(message.length > 5000)
    {
       $("divCommentResponse").innerHTML = "Only 5000 Characters Allowed!";    
       Effect.BlindDown('divCommentResponse', { duration: 0.5 });  
       CommentResponseWait = setTimeout(CommentResponse, 1500);
       return false;
    }
    else if(CheckPalabras(message) == false)
    {
        return false;
    }
    
    $Ajax(basepath + "controllers/Accounts/ViewHealerAccount.aspx?method=sendComment", 
        {metodo: $metodo.POST,
        parametros: {"message":escape(message),"healer_id":healer_id},
        onfinish: function(InsertResponse){ 
            if(InsertResponse == '1')
            {
                $("divCommentResponse").innerHTML = "Your Message was posted"; 
                $("txtCommentMessage").value = "";
                $("charsLeft").value = "";
                Effect.BlindDown('divCommentResponse', { duration: 0.5 });    
                __doPostBack('AddedComment','');    
                CommentResponseWait = setTimeout(CommentResponse, 2000);
            }
            else
            {
                $("divCommentResponse").innerHTML = "Please try again later";    
                Effect.BlindDown('divCommentResponse', { duration: 0.5 });  
                CommentResponseWait = setTimeout(CommentResponse, 2000);           
            }
            
        },
        cache: false});
    
    return true;
}

function SendCenterComment(){    
    var message = deleteInvalidTags($F("txtCommentMessage"));
    var center_id = parseFriendlyQS(window.location);
    //Valido contenido del mensaje
    if(message == '' || message == ' ') {
        $("divCommentResponse").innerHTML = "You Must Enter Message!";    
        Effect.BlindDown('divCommenttResponse', { duration: 0.5 });  
        CommentResponseWait = setTimeout(CommentResponse, 1500);
        return false;
    }
    else if(message.length > 5000)
    {
       $("divCommentResponse").innerHTML = "Only 5000 Characters Allowed!";    
       Effect.BlindDown('divCommentResponse', { duration: 0.5 });  
       CommentResponseWait = setTimeout(CommentResponse, 1500);
       return false;
    }
    else if(CheckPalabras(message) == false)
    {
        return false;
    }
    
    $Ajax(basepath + "controllers/Accounts/ViewCenterAccount.aspx?method=sendComment", 
        {metodo: $metodo.POST,
        parametros: {"message":escape(message),"center_id":center_id},
        onfinish: function(InsertResponse){ 
            if(InsertResponse == '1')
            {
                $("divCommentResponse").innerHTML = "Your Message was posted"; 
                $("txtCommentMessage").value = "";
                $("charsLeft").value = "";
                Effect.BlindDown('divCommentResponse', { duration: 0.5 });    
                __doPostBack('AddedComment','');    
                CommentResponseWait = setTimeout(CommentResponse, 2000);
            }
            else
            {
                $("divCommentResponse").innerHTML = "Please try again later";    
                Effect.BlindDown('divCommentResponse', { duration: 0.5 });  
                CommentResponseWait = setTimeout(CommentResponse, 2000);           
            }
            
        },
        cache: false});
    
    return true;
}


function SendVideoComment(){    
    var message = deleteInvalidTags($F("txtVideoComment")); 
    //Valido contenido del mensaje
    if(message == '' || message == ' ') {
        $("divVideoCommentResponse").innerHTML = "You Must Enter Message!";    
        Effect.BlindDown('divVideoCommentResponse', { duration: 0.5 });  
        VideoCommentResponseWait = setTimeout(VideoCommentResponse, 1500);
        return false;
    }
    else if(message.length > 5000)
    {
       $("divVideoCommentResponse").innerHTML = "Only 5000 Characters Allowed!";    
       Effect.BlindDown('divVideoCommentResponse', { duration: 0.5 });  
       VideoCommentResponseWait = setTimeout(VideoCommentResponse, 1500);
       return false;
    }
    else if(CheckPalabras(message) == false)
    {
        return false;
    }
    
    $Ajax(basepath + "controllers/Videos/Gallery.aspx?method=sendVideoComment", 
        {metodo: $metodo.POST,
        parametros: {"message":escape(message)},
        onfinish: function(InsertResponse){ 
            if(InsertResponse == '1')
            {
                $("divVideoCommentResponse").innerHTML = "Your Message was posted"; 
                $("txtVideoComment").value = "";
                $("charsLeft").value = "";
                Effect.BlindDown('divVideoCommentResponse', { duration: 0.5 });    
                __doPostBack('LinkCommentsRefresh','');    
                VideoCommentResponseWait = setTimeout(VideoCommentResponse, 2000);
            }
            else
            {
                $("divVideoCommentResponse").innerHTML = "Please select a video or add one if none are available";    
                Effect.BlindDown('divVideoCommentResponse', { duration: 0.5 });  
                VideoCommentResponseWait = setTimeout(VideoCommentResponse, 2000);           
            }
            
        },
        cache: false});
    
    return true;
}

//function SendPhotoComment(){    
//    var message = deleteInvalidTags($F("txtPhotoComment")); 
//    //Valido contenido del mensaje
//    if(message == '' || message == ' ') {
//        $("divPhotoCommentResponse").innerHTML = "You Must Enter Message!";    
//        Effect.BlindDown('divPhotoCommentResponse', { duration: 0.5 });  
//        PhotoCommentResponseWait = setTimeout(PhotoCommentResponse, 1500);
//        return false;
//    }
//    else if(message.length > 5000)
//    {
//       $("divPhotoCommentResponse").innerHTML = "Only 5000 Characters Allowed!";    
//       Effect.BlindDown('divPhotoCommentResponse', { duration: 0.5 });  
//       PhotoCommentResponseWait = setTimeout(PhotoCommentResponse, 1500);
//       return false;
//    }
//    else if(CheckPalabras(message) == false)
//    {
//        return false;
//    }
//    
//    $Ajax(basepath + "controllers/Messages/Photos/SendComment.aspx?method=sendPhotoComment", 
//        {metodo: $metodo.POST,
//        parametros: {"message":escape(message)},
//        onfinish: function(InsertResponse){ 
//            if(InsertResponse == 'true')
//            {
//                $("divPhotoCommentResponse").innerHTML = "Your Message was posted"; 
//                $("txtPhotoComment").value = "";
//                $("charsLeft").value = "";
//                Effect.BlindDown('divPhotoCommentResponse', { duration: 0.5 });    
//                __doPostBack('LinkCommentsRefresh','');    
//                PhotoCommentResponseWait = setTimeout(PhotoCommentResponse, 2000);
//            }
//            else
//            {
//                $("divPhotoCommentResponse").innerHTML = "Please select a picture or upload one if none are available";    
//                Effect.BlindDown('divPhotoCommentResponse', { duration: 0.5 });  
//                PhotoCommentResponseWait = setTimeout(PhotoCommentResponse, 2000);           
//            }
//            
//        },
//        cache: false});
//    
//    return true;
//}

function SendArticleComment(){    
    var message = deleteInvalidTags($F("txtCommentMessage"));
    var article_id = parseFriendlyQS(window.location);
    //Valido contenido del mensaje
    if(message == '' || message == ' ') {
        $("divCommentResponse").innerHTML = "You Must Enter Message!";    
        Effect.BlindDown('divCommentResponse', { duration: 0.5 });  
        CommentResponseWait = setTimeout(CommentResponse, 1500);
        return false;
    }
    else if(message.length > 5000)
    {
       $("divCommentResponse").innerHTML = "Only 5000 Characters Allowed!";    
       Effect.BlindDown('divCommentResponse', { duration: 0.5 });  
       CommentResponseWait = setTimeout(CommentResponse, 1500);
       return false;
    }
    else if(CheckPalabras(message) == false)
    {
        return false;
    }
    
    $Ajax(basepath + "controllers/Articles/index.aspx?method=sendComment", 
        {metodo: $metodo.POST,
        parametros: {"message":escape(message),"article_id":article_id},
        onfinish: function(InsertResponse){ 
            if(InsertResponse == '1')
            {
                $("divCommentResponse").innerHTML = "Your Message was posted"; 
                $("txtCommentMessage").value = "";
                $("charsLeft").value = "";
                Effect.BlindDown('divCommentResponse', { duration: 0.5 });    
                __doPostBack('AddedComment','');    
                CommentResponseWait = setTimeout(CommentResponse, 2000);
            }
            else
            {
                $("divCommentResponse").innerHTML = "Please try again later";    
                Effect.BlindDown('divCommentResponse', { duration: 0.5 });  
                CommentResponseWait = setTimeout(CommentResponse, 2000);           
            }
            
        },
        cache: false});
    
    return true;
}