﻿/// <reference name="MicrosoftAjax.js"/>

String.prototype.IsNullOrEmpty = function(value) {
  var isNullOrEmpty = true;
  if (value) {
   if (typeof (value) == 'string') {
    if (value.length > 0)
     isNullOrEmpty = false;
   }
  }
  return isNullOrEmpty;
}

function showElement(elem) {
    $(elem).value = "*";
    $(elem).css('color', 'Red');
    $(elem).show();
}

function hideElement(elem) {
    $(elem).hide();
}

function getCommentBox() {
    $("#dv1").hide();
    $("#commentBox").show();
}

function cancelarComentario() {
    $("#commentBox").hide();
    $("#dv1").show();
}

function createCommentsBox() {
    var html = new String();
    html += '<table width="260" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td colspan="3"><div class="smallTxt">Título</div>';
    html += '<input type="text" id="txtTitulo" name="titulo" class="form" /><label id="lblTitulo" style="display: none">*</label></td></tr>';
    html += '<tr><td colspan="3"><div class="smallTxt">Texto</div><textarea id="txtTexto" name="texto" rows="5" cols="20" class="form"></textarea><label id="lblTexto" style="display: none">*</label></td></tr>';
    html += '<tr><td colspan="3"><div class="smallTxt">Nome</div><input type="text" id="txtNome" name="nome" class="form" /><label id="lblNome" style="display: none">*</label></td></tr>';
    html += '<tr><td colspan="3"><div class="smallTxt">Localidade, País</div><input type="text" name="local" class="form" id="txtLocal" /><label id="lblLocal" style="display: none">*</label></td></tr>';
    html += '<tr><td colspan="3"><div class="smallTxt">Email</div><input type="text" id="txtEmail" name="email" class="form" /><label id="lblEmail" style="display: none">*</label></td></tr>';
    html += '<tr><td width="33%"><input type="checkbox" id="anonimo" name="anonimo" title="Anónimo" /><span class="txt">Anónimo</span></td><td width="33%"></td>';
    html += '<td width="66%" align="right" style="padding-right: 15px;"><input type="submit" onclick="javascript:validateComment();return false;" title="Enviar" value="Enviar" />';
    html += '<input type="button" onclick="javascript:cancelarComentario();" title="Cancelar" value="Cancelar" /></td></tr></table>';
    return html;
}

function validateComment() {
    var isValid = true;
    var str = new String();
    var titulo = $("#txtTitulo").val();
    var texto = $("#txtTexto").val();
    var nome = $("#txtNome").val();
    var local = $("#txtLocal").val();
    var email = $("#txtEmail").val();
    var anonimo = $("#anonimo").is(':checked');

    if (str.IsNullOrEmpty(titulo)) {
        isValid = false;
        showElement($("#lblTitulo"));
    }
    else 
        hideElement($("#lblTitulo"));

    if (str.IsNullOrEmpty(texto)) {
        isValid = false;
        showElement($("#lblTexto"));
    }
    else
        hideElement($("#lblTexto"));

    if (str.IsNullOrEmpty(nome)) {
        isValid = false;
        showElement($("#lblNome"));
    }
    else
        hideElement($("#lblNome"));

    if (str.IsNullOrEmpty(local)) {
        isValid = false;
        showElement($("#lblLocal"));
    }
    else
        hideElement($("#lblLocal"));

    if (str.IsNullOrEmpty(email)) {
        isValid = false;
        showElement($("#lblEmail"));
    } else {
        var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (reg.test(email) == false) {
            isValid = false;
            showElement($("#lblEmail"));
            return false;
        }
        else
            hideElement($("#lblEmail"));
    }

    if (isValid) {
        var x = document.forms[0];
        var _data = $(x).serialize();

        $.post('Default.aspx', _data,
            function() {
                alert("Comentário enviado com sucesso!");
                cancelarComentario();
            });
    }
    else {
        //alert("Falhou a validação");
    }
}






