function goPage(pg,id) {
  $("#ctmp").html('<span class="loading">Cargando...</span>');
  $("#ctmp").load('/script/comentario.php?idnoticia='+id+'&pagina='+pg);
}
function goPage2(pg,id) {
  $("#ctmp").html('<span class="loading">Cargando...</span>');
  $("#ctmp").load('/script/comentariosup.php?idarticulo='+id+'&pagina='+pg);
}
function goPage3(pg,id) {
  $("#ctmp").html('<span class="loading">Cargando...</span>');
  $("#ctmp").load('/script/comentarionota.php?idnota='+id+'&pagina='+pg);
}

function validmail(email) {
  var str=email;
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(str)) {
    return true;
  } else {
    return false;
  }
}

function validRequired(form) {
  valid = true;
  $('#'+form+' .required').each(function(i) {
    if(!jQuery.trim($(this).val())) {
      msg = '';
      if($(this).attr("title")) msg = $(this).attr("title");
      else msg = 'El campo '+$(this).attr("name")+' es requerido, por favor llenelo';
      alert(msg);
      $(this).focus();
      valid = false;
      return false;
    } else if($(this).hasClass("email")) {
      if(!validmail(jQuery.trim($(this).val()))) {
        alert('El email introducido no es una direccion valida');
        $(this).focus();
        valid = false;
        return false;
      }
    }
    else if($(this).hasClass("compare")){
     if($("#"+$(this).attr("origen")).val()!=$("#"+$(this).attr("destino")).val()){
      alert("Los campos "+$("#"+$(this).attr("origen")).attr("name")+" y "+$("#"+$(this).attr("destino")).attr("name")+" deben tener valores iguales");
      $(this).focus();
      valid=false;
      return false;
     }
    }
  });
  return valid;
}

function resetFields(form) {
  $('#'+form+' input:text').val('');
  $('#'+form+' textarea').val('');
}

$(document).ready(function() {
  $(".valid").submit(function() {
    id = $(this).attr("id");
    if(validRequired(id)) {
      archivo="/comentar.php";
      $.post(archivo,{action:"comment",idarticulo:$("#idarticulo").val(),idnota:$("#idnota").val(),idnoticia:$('#idnoticia').val(),comentario:$("#comentario").val(),nombre:$("#nombre").val(),email:$("#email").val(),web:$("#web").val(),captcha_code:$("#captcha_code").val()},function(data) { if(data==0) { document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); alert('Error Captcha no coincide, por favor ingreselo de nuevo'); $("#captcha_code").val('');$("#captcha_code").focus(); } else if (data==-2) { alert('Error EMAIL: Dirección de Correo Electronica no es válida'); } else { document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random();$("#msg_status").html(data);resetFields(id); }},"text");
    }
    return false;
  });
 
  //Para formulario normalitos - Melvin - 2009-10-02 15:17  
  $(".valid2").submit(function() {
    id = $(this).attr("id");
    if(!validRequired(id)) {
     return false;
    }
    return true;
  });
});

function ismaxlength(obj) {
  if(obj.getAttribute && obj.value.length>500) { 
    obj.value=obj.value.substring(0,500);
  } else {
    document.getElementById("msg").innerHTML="<b>"+obj.value.length+" de 500 caracteres</b>";
  }
}

function validarmarcados(){
   var checkeado=$('#mag').attr('checked');
   var checkeado2 = $('#sus').attr('checked');
   if(checkeado){
     $("#sus").removeAttr("checked");
   }
   if(checkeado2){
     $("#mag").removeAttr("checked");
   }
 
}

function validardatossuscripcion(){
    direccion = document.getElementById('dir').value;
    telefono = document.getElementById('tel').value;
    if(direccion == ""){
       alert("El campo direccion es obligatorio, por favor rellenelo");
       return false;
    }else{
       if(isNaN(telefono)){
          alert("El campo telefono solo puede contener valores numericos");
          return false;
       }else{
          return true;
       }
    }
}

