﻿// JScript File

window.onload = function()
{
    
    $("txtMailComment").onkeypress = function(){ $("btnSendProfile").disabled = false;
            $("btnSendProfile").className = "input_botons";};
            
    checkCurrSession();
    
    setSendButton();
}


//Envia articulo a tus amigos
var waitingProfile;

function sendArticle()
{
    var tester_rem = true;
    var tester_rem2 = true;
    //Primero chequeo los datos del remitente
    if($("divRemitenteName").style.display != "none")
    {
        if($F("txtRemitenteName") == "" || $F("txtRemitenteName") == " " || $F("txtRemitenteName").length < 3)
        {
            $("remitenteNameError").innerHTML = "*";
            tester_rem2 &= false;
        }
        else
        {
            $("remitenteNameError").innerHTML = "";
            tester_rem2 &= true;
        }
        
        if(!tester_rem2)
        {
            return false;
        }
        
        if($F("txtRemitenteMail") == "" || $F("txtRemitenteMail") == " ")
        {
            $("remitenteMailError").innerHTML = "*";
            tester_rem &= false;
        }    
        else if(!checkEmail($F("txtRemitenteMail")))
        {
            $("remitenteMailError").innerHTML = "*";
            tester_rem &= false;
        }
        else
        {
            $("remitenteMailError").innerHTML = "";
            tester_rem &= true;       
        }
        
        if(!tester_rem)
        {
            return false;
        }
    }
        
    var myMessage;
    var myMailsList="";
    var mailsArr = new Array(); 
    var tester = true;
    //------------------------>>    
    for(i=1;i<=5;i++)
    {
        if($("txtFriendMail_0" + i).value != "" && $("txtFriendMail_0" + i).value != " ")
        {
            mailsArr.push("txtFriendMail_0" + i);                   
        }
    }
    
    if(mailsArr.length < 1)
    {
        goResponseSendProfile("Please enter recipient’s email");
        return false;
    }
    
    //------------------------>>    
    for(i=0;i<mailsArr.length;i++)
    {
        if(!checkEmail($(mailsArr[i]).value))
        {        
            $(mailsArr[i] + '_err').innerHTML = '*';
            tester &= false;
        }
        else
        {
            myMailsList += $(mailsArr[i]).value + "#";
            $(mailsArr[i] + '_err').innerHTML = '';            
            tester &= true;
        }
    }    
    myMailsList += '#';
    myMailsList = myMailsList.replace('##','');
    
    if(!tester)
    {
        return false;
    }
    //------------------------>>   
    
    if(CheckPalabras($F("txtMailComment")))
    {
        myMessage = escape(deleteInvalidTags($F("txtMailComment")));
    }
    else
    {
        return false;
    }
    
    $("btnSendProfile").disabled = true;
    $("btnSendProfile").className = "input_botons_off";
    
    clearMailsErrors();
            
    $Ajax(basepath + "controllers/Articles/Index.aspx?method=sendArticle", {
        cache: false, 
        avisoCargando:"divCargando",
        metodo: $metodo.POST,            
        parametros: {"comment":myMessage,"myMailsList":myMailsList,"from_name":escape($F("txtRemitenteName")),"from_mail":escape($F("txtRemitenteMail"))},
        onfinish:function(re){
            if(re == "1"){
            goResponseSendProfile("Your message has been sent");
            hide('divCargando');            
            clearMailsComment(mailsArr);
            }else{
            goResponseSendProfile("Please try again later");
            hide('divCargando');
            $("btnSendProfile").disabled = false;
            $("btnSendProfile").className = "input_botons";
            }        
        },
        tipoRespuesta:$tipo.TEXT});
    
}

function goResponseSendProfile(texto)
{
    $("responseSendArticle").innerHTML = texto;
    Effect.Appear('responseSendArticle', { duration: 0.8 });
    waitingProfile = setTimeout(goResponseClose, 3000);
}

function goResponseClose()
{    
    Effect.Fade('responseSendArticle', { duration: 0.8 });
    if(waitingProfile != null)clearTimeout(waitingProfile);
}

function clearMailsComment(arr)
{
    var currArr = new Array();
    currArr = arr;
    for(i=0;i<currArr.length;i++)
    {
        $(currArr[i]).value = "";
    }
    
    $("txtRemitenteName").value = "";
    $("txtRemitenteMail").value = "";
    $("txtMailComment").value = "";
    $("charsLeft_txtMailComment").value = "";
    
}

function checkCurrSession()
{
     //----->
    $Ajax(basepath+"Controllers/Login/login.aspx?method=checkSession", {
        cache: false,
        onfinish:function(session){
            if(session=="True"){         
                $("divRemitenteName").style.display = "none";
                $("divRemitenteMail").style.display = "none";
                return false;
            }        
            else{
                $("divRemitenteName").style.display = "block";
                $("divRemitenteMail").style.display = "block";
                return false;
            }    
        },
        tipoRespuesta:$tipo.TEXT});
    
    return true;        
}

function clearMailsErrors()
{
    for(i=1;i<=5;i++)
    {
       $("txtFriendMail_0" + i + "_err").innerHTML = "";                   
    }
}

function setSendButton()
{
    var EsperandoAlWindow;
    if($("txtCommentMessage") != null)
    {
        $("txtCommentMessage").onkeypress = function(){$("btnSendComment").className = "input_botons"; $("btnSendComment").disabled = false;};
        if(EsperandoAlWindow != null)
        {
            clearTimeout(EsperandoAlWindow);
        }
    }
    else
    {
        EsperandoAlWindow = setTimeout(setSendButton, 500);
    }
}

function loginTo()
{  
    Effect.Fade('divControlsContainer', { duration: 0.5 });
    showAddNewComment();    
}

function showAddNewComment()
{
    //----->
    if($('divControlsContainer').style.display == "none")
    {
        //-----> 
        __doPostBack('linkLogin','');
        Effect.Appear('divControlsContainer', { duration: 2.0 });        
    }
    else
    {
        commentControlsWait = setTimeout(showAddNewComment, 1000);
    }
}

function goToSendComment()
{
    //Llama a la funcion que está en SendMessage.js
    SendArticleComment();
} 

//Creo los Tips
function showTipAbuse(id)
{
    Tip('Let us know if you believe <br />this content is not appropiate', STICKY, false,FADEIN, 500,CLICKCLOSE, false, SHADOW, true, TITLE, 'Report Abuse', PADDING, 1);
}

function goToProfile(user, this_href){
    if((user_id > 0) && (user != 'anonymouse')){ window.location = this_href; }
    else{return false;}
}

var CommentResponseWait;
//----->
function CommentResponse()
{
    Effect.BlindUp('divCommentResponse', { duration: 0.5 });
    clearTimeout(CommentResponseWait);
    $("btnSendComment").className = "input_botons_off";
    $("btnSendComment").disabled = true; 
}

function updateStats(article_id,okHref){
    $Ajax(basepath + "controllers/Articles/index.aspx?method=updateStats", 
            {metodo: $metodo.POST,            
            parametros: {"article_id":article_id},
            onfinish: function(result){
                window.location = okHref;
            },
            cache: false});

}