﻿/* ------------------------
POST COMMENT METHODS
---------------------------*/
function PostComment()
{
    $get('divMsgPostComment').style.display = "none";
    
    if(ValidateCommentFields())
    {
        //Initialize messages/images
        $get('commentLoadingImg').style.visibility = "visible";
        SetValidationMessage('comprobar', "");
        
        //Create a new instance of comment
        var comment = new Terra.Portal.Data.Comentarios.Comentario();
        comment.NombreUsuario = $get('cmt_nomApelli').value;
        comment.EmailUsuario = $get('cmt_email').value;
        comment.WebUsuario = $get('cmt_web').value;
        comment.TextoComentarios = $get('cmt_comentario').value;
        comment.ComentarioVisible = true;
        
        //Post the new comment
        if (window.itemUrl)
            comment.UrlNoticiaComentario = itemUrl;
        
        Terra.Portal.AjaxServices.CommentsService.PostComment(itemUrl, comment, CaptchaComments.GetCaptchaHash(), $get("comprobar").value, OnPostComment, OnPostCommentError);
    }
    else
    {
        //Invalid form
    }
}

// Validar "Enter" en TextArea Captcha
function PressKeyComment(e)
{
    if(window.event) // IE
    {
        keynum = e.keyCode;    
    }
    else // Netscape/Firefox/Opera
    {    
        keynum = e.which;    
    } 
    if (keynum == 13){
        PostComment()     
    }else return false;
}

///When comment has been posted
function OnPostComment(captchaValid)
{
    $get('commentLoadingImg').style.visibility = "hidden";
    if(captchaValid)
    {
        SetValidationMessage('comprobar', "");
        CleanCommentFields();
        
        //Show confirmation message
        ShowConfirmationErrorMessage('El comentario se ha enviado correctamente. En unos minutos se actualizará.', 'cmmt_msg_ok');
    }
    else
    {
        //Show validation message in captcha input text
        SetValidationMessage('comprobar', '* Error');
        
        //Reload Captcha
        ReloadCaptcha(CaptchaComments);
    }  
}

///When error while post a comment
function OnPostCommentError(error)
{
    $get('commentLoadingImg').style.visibility = "hidden";
    //alert("Error: " + error.get_message() + " -- " + error.get_stackTrace());
    
    //Show error message
    ShowConfirmationErrorMessage('Ha habido un error al enviar el comentario. Revisa los campos que aparecen marcados en el formulario', 'cmmt_msg_error');
    
}

function ShowConfirmationErrorMessage(message, cssClass)
{
    $get('divMsgPostComment').style.display = "block";
    $get('msgPostComment').className = cssClass;
    $get('msgPostComment').innerHTML = message;
}

function CleanCommentFields()
{
    //Clean fields
    $get('cmt_nomApelli').value = "";
    $get('cmt_email').value = "";
    $get('cmt_web').value = "";
    $get('cmt_comentario').value = "";
    $get('comprobar').value = "";
    
    //Reset validators
    SetValidationMessage('cmt_nomApelli', "");
    SetValidationMessage('cmt_email', "");
    SetValidationMessage('cmt_comentario', "");
    SetValidationMessage('comprobar', "");
    SetValidationMessage('cmt_web', "");
    
     //Reload Captcha
     ReloadCaptcha(CaptchaComments);
}


/* -------------------------
VALIDATION METHODS
--------------------------- */

///Validate the text area max length and updates the text with the number of characters left
function ValidateMaxLength(textArea)
{
    var maxChars = 500;
    
    //Validate the max lenght
    if(textArea.value.length > maxChars)
    {
        textArea.value = textArea.value.substring(0, maxChars);
    }
    else
    {
        //Set text with number of characters left
        document.getElementById('commentCharsLeft').innerHTML = 'quedan ' + (maxChars - parseInt(textArea.value.length)) + ' caracteres';
    }  
}

///Validate all fields of the form
function ValidateCommentFields()
{
    var isValid = false;
    
    isValid = ValidateRequired('cmt_nomApelli');
    isValid = isValid & ValidateNoScripting('cmt_nomApelli');
    isValid = isValid & ValidateEmailRequired('cmt_email');
    isValid = isValid & ValidateRequired('cmt_comentario');
    isValid = isValid & ValidateNoScripting('cmt_comentario');
    isValid = isValid & ValidateNoScripting('cmt_web');
    
    return isValid;
}

///Validate an email
function ValidateEmailRequired(idInputToValidate)
{
    var isValid = false;
    var inputToValidate = document.getElementById(idInputToValidate);
    var email = inputToValidate.value;
    
    var filter=/^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/;     
    if (Trim(email) == "") 
        isValid = false;
    if (filter.test(email)) 
        isValid = true;
    else 
        isValid = false;
        
    
    //Set the validation error message
    if(isValid)
        SetValidationMessage(idInputToValidate, "");
    else
        SetValidationMessage(idInputToValidate, "* Error: Correo inv&aacute;lido");
        
        
    return isValid;
}

///Set a text in the validator control
function SetValidationMessage(idInputToValidate, message)
{
    var idSpanTextValidation = "val_" + idInputToValidate;
    var spanTextValidation = document.getElementById(idSpanTextValidation);
    
    spanTextValidation.innerHTML = message;
}

///Validate a required field
function ValidateRequired(idInputToValidate)
{
    var inputToValidate = document.getElementById(idInputToValidate);
    
    if(Trim(inputToValidate.value) == "")
    {
        SetValidationMessage(idInputToValidate, '* Error: Campo obligatorio');
        return false;
    }
    else
    {
        SetValidationMessage(idInputToValidate, "");
        return true;
    }
}

///Validate no Scripting (script or html tags)
function ValidateNoScripting(idInputToValidate)
{
    var inputToValidate = document.getElementById(idInputToValidate);
    
    var isValid = false;
    var inputToValidate = document.getElementById(idInputToValidate);
    
    var objRegExp = new RegExp("[<>\{\}]+", "g");   //forbid characters <>{}
    
    if (!objRegExp.test(inputToValidate.value))
    { 
        isValid = true;
        
    }
    else
    { 
        SetValidationMessage(idInputToValidate, '* Error: Contenido introducido no permitido');
        isValid = false;
    }
    
    return isValid;

}

///Trim a text string
function Trim(stringValue)
{
   return stringValue.replace(/^\s+|\s+$/g,'');
}

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();