
//FUNCTION FORM
function getObject(id){
	  var obj = document.getElementById(id);
    if(obj){
        return obj;
    }else{
        return false;}
}

function readFromCookie(){
    // get user name from cookie
    var ckArr = document.cookie.split("; ");
    var userInfo = null;
    var userType = 0;
    var ipUser = false;
    for (var nBucle=0;nBucle<ckArr.length;nBucle++){    
        var aCookie = ckArr[nBucle].split("=");
        if ( (aCookie[0].substring(0,5)) == 'cUser'){
            userInfo = unescape(aCookie[1]);
        }
        if( (aCookie[0].substring(0,4)) == 'iUser'){
            ipUser = true;
        }
        if( (aCookie[0].substring(0,9)) == 'elPaisXXX'){
            var userInfo2;
            userInfo2 = unescape(aCookie[1]).split("#");
            userType = userInfo2[0];
        }
    }
    if (userInfo == null) {
        var date = new Date();
        date.setTime((date.getTime() + (8760*60*60*1000)));
        document.cookie = "cUser=nouser; PATH=/; DOMAIN=.elpais.com; expires=" + date.toGMTString();
        userInfo = '';
    }
    var obj=getObject("comment_nick");
    if(userInfo=="nouser") userInfo="";
    var cookieValue	= " "+userInfo;
    obj.innerHTML	= cookieValue;
}
$(document).ready(readFromCookie);

function focus(obj){
    try{
        if ((obj["class"]=='') || (obj["class"]==''))
            obj.className = obj["class"];
        else
            obj.className = "ctexto novalida";
    }catch(e){
        obj.className = "ctexto novalida";
    }
    try{
        $err = getObject("mostrar_error");
        $err.style.display="block";
    }catch(e){}
    obj.focus();
}

function checkMail(obj){
    var arr	= obj.value.lastIndexOf('@');
    var pun = obj.value.lastIndexOf('.');
    var tam = obj.value.length;	
    if( arr != 0 && pun != 0  && pun < (tam-2) && arr < (pun-2)){
        return true;
    }else{	
        return false;}
}

function checkEnvioComentario(){
    var aceptar_comentario =getObject("acepto_comentario");
    var capa = getObject("error_acepar_comentario");   
    if( aceptar_comentario.checked == false){
        if(capa.style.display=='none')
            capa.style.display="block";
    }else{
        if( aceptar_comentario.checked == true)
            capa.style.display="none";
    }  

    var email = getObject("email");
    email["class"] = "ctexto novalida";

    var comentario = getObject("comentario");
    comentario["class"] = "novalida";

    var nombre = getObject("nombre");
    nombre["class"] = "ctexto novalida";

    var collection = Array(comentario, nombre, email);
    var errT = false;
    for (i=0; i< collection.length ;i++ ){
        collection[i].className = "ctexto";
        if(collection[i].value == null || collection[i].value == ""){
            try{
                collection[i].className = collection[i]["class"];
            }catch(e){
                collection[i].className = "ctexto novalida";
            }
            if (!errT) {
                collection[i].focus();
            }
            errT = true;
        }
        if(collection[i].name=='email'){
            if(collection[i].value.length > 0){
                if(!checkMail(collection[i])){
                    focus(collection[i]);
                    errT = true;
                }
            }
        }
    }
    if( aceptar_comentario.checked == false){
        errT = true;
    }
    if (errT) {
        try{
            err = getObject("mostrar_error");
            err.style.display="block";
        }catch(e){}
        return false;
    }
}

function checkCorregir(){
    var email_corregir = getObject("email_corregir");
    email_corregir["class"] = "ctexto novalida";

    var comentario_corregir = getObject("comentario_corregir");
    comentario_corregir["class"] = "novalida";

    var collection = Array(email_corregir, comentario_corregir);
    var err = false;
    for (i=0; i< collection.length ;i++ ){
        collection[i].className = "ctexto";
        if(collection[i].value == null || collection[i].value == ""){
            try{
                collection[i].className = collection[i]["class"];
            }catch(e){
                collection[i].className = "ctexto novalida";
            }
            if (!err) collection[i].focus();
            err = true;
        }
    }
    if (err) {
        try{
            err = getObject("mostrar_error_corregir");
            err.style.display="block";
        }catch(e){}
          return false;
    }
}

function checkEnvioNoticia(){
    var envio_emails = getObject("envio_emails");
    envio_emails["class"] = "ctexto novalida";

    var envio_nombres = getObject("envio_nombres");
    envio_nombres["class"] = "ctexto novalida";

    var remitente_email = getObject("remitente_email");
    remitente_email["class"] = "ctexto novalida";

    var remitente_nombre = getObject("remitente_nombre");
    remitente_nombre["class"] = "ctexto novalida";

    var collection = Array(envio_emails, envio_nombres, remitente_email, remitente_nombre);
    var err = false;
    for (i=0; i< collection.length ;i++ ){
        collection[i].className = "ctexto";
        if(collection[i].value == null || collection[i].value == ""){
            try{
                collection[i].className = collection[i]["class"];
            }catch(e){
                collection[i].className = "ctexto novalida";
            }
            if (!err) collection[i].focus();
            err = true;
        }
    }
    if (err) {
        try{
            err = getObject("mostrar_error_enviar");
            err.style.display="block";
        }catch(e){}
        return false;
    }
}

function checkEnvioPregunta(id_form){

    var formulario = getObject(id_form);
    preguntaObj =  formulario.elements["pregunta"];
    nombreObj =  formulario.elements["nombre"];

    var pregunta = preguntaObj;
    pregunta["class"] = "novalida";

    var nombre = nombreObj;
    nombre["class"] = "ctexto novalida";

    preguntaTxt = pregunta.value;
    nombreTxt = nombre.value; 
    texto=preguntaTxt.split(" ");
    if (texto.length>101 || preguntaTxt.length>500 || nombreTxt.length>50) {
        alert("La pregunta no puede sobrepasar las 100 palabras. Por favor, inténtelo de nuevo");
        pregunta.value=preguntaTxt.substr(0,500);
        return false;
    }

    var collection = Array(pregunta, nombre);
    var errT = false;
    for (i=0; i< collection.length ;i++ ){
        collection[i].className = "ctexto";
        if(collection[i].value == null || collection[i].value == ""){
            try{
                collection[i].className = collection[i]["class"];
            }catch(e){
                collection[i].className = "ctexto novalida";
            }
            if (!errT) {
                collection[i].focus();                
            }   
            errT = true;            
        }
    }
    if (errT) {  
        try{            
            err = getObject("error");
            err.style.display="block";        
        }catch(e){}              
        return false;        
    }   
}

TEXTO_CARGANDO_PESTANA = "<br><br><center><b>CARGANDO</b><br><img src='/im/loading.gif'></center>";

function envioForm(c,rules){
    frm = new Form(c.id);
    y = new Ajax();
    var all= frm.getQueryString();
    var params = escape(all);
    var modulo = all.split("modulo=")[1];
    var	modulo  = modulo.split("&")[0];
    var ctn  = all.split("ctn=")[1];
    var	ctn  = ctn.split("&")[0];
    var url=modulo+"&params="+ escape(params);
    fin = function (){
        $("#"+ctn).each(rules);
    }
    y.replace("/modulo/index.html",url,TEXTO_CARGANDO_PESTANA, ctn,"GET",fin);
}

//envio noticia
rules_envio_noticia = {
    evento_cli : function(){
                     $('input.boton_form_envio_noticia').click(rules_envio_noticia.validar);
                 },
    validar : function(){
                  return checkEnvioNoticia();
              },
    evento_sub : function(){
                     $('#envio_noticia_amigo').submit(rules_envio_noticia.ejecutar);
                 },
    ejecutar : function(){
                   envioForm(this,rules_envio_noticia.evento_sub);
                   //return  false;
               }
}
$(document).ready(rules_envio_noticia.evento_cli);
$(document).ready(rules_envio_noticia.evento_sub);

//envio corrección
rules_envio_correccion = {
    evento_cli : function(){
                     $('input.boton_form_corregir').click(rules_envio_correccion.validar);
                 },
    validar : function(){
                  return checkCorregir();
              },
    evento_sub : function(){
                     $('#envio_noticia_correction').submit(rules_envio_correccion.ejecutar);
                 },
    ejecutar : function(){
                   envioForm(this,rules_envio_correccion.evento_sub);
                   return  false;
               }
}
$(document).ready(rules_envio_correccion.evento_cli);
$(document).ready(rules_envio_correccion.evento_sub);

//envio comentario
rules_envio_comentario = {
    evento_cli : function(){
                     $('input.boton_form').click(rules_envio_comentario.validar);
                 },
    validar : function(){
			              return  checkEnvioComentario();
              },
    evento_sub : function(){
                     $('#envio_comentario').submit(rules_envio_comentario.ejecutar);
                 },
    evento_sub2 : function(){
                     $('a.aPc').click(rules_paginacion_comentario.ejecutar);
                     $('input.boton_form').click(rules_envio_comentario.validar);
                     $('#envio_comentario').submit(rules_envio_comentario.ejecutar);
                 },
    ejecutar : function(){
                   envioForm(this,rules_envio_comentario.evento_sub2);
                   return  false;
               }
}
$(document).ready(rules_envio_comentario.evento_cli);
$(document).ready(rules_envio_comentario.evento_sub);

//PAGINACIÓN Comentarios
rules_paginacion_comentario = {
    evento : function() {
             $("a.aPc").click(rules_paginacion_comentario.ejecutar);
    },
    evento2 : function() {
             $("a.aPc").click(rules_paginacion_comentario.ejecutar);
             $('input.boton_form').click(rules_envio_comentario.validar);
             $('#envio_comentario').submit(rules_envio_comentario.ejecutar);
    },
    ejecutar : function() {
              ajax(this.href, rules_paginacion_comentario.evento2);
              return false;
    }
}
$(document).ready(rules_paginacion_comentario.evento);


//envío preguntas en entrevistas
rules_envio_preg_entrevista = {
    evento_cli : function(){
                     $('input.boton_entrevista').click(rules_envio_preg_entrevista.validar);
                 },
    validar : function(){
                  return  checkEnvioPregunta('envio_formulario');
              },
    evento_cli_2 : function(){
                       $('input.boton_entrevista2').submit(rules_envio_preg_entrevista.validar2);
                   },
    validar_2 : function(){
                    return  checkEnvioPregunta('envio_formulario2');
                },
    evento_sub : function(){
                     $('#envio_formulario').submit(rules_envio_preg_entrevista.ejecutar);
                     $('#envio_formulario2').submit(rules_envio_preg_entrevista.ejecutar);
                 },
    ejecutar : function(){
                   envioForm(this,rules_envio_preg_entrevista.evento_sub);
                   return  false;
               }
}
$(document).ready(rules_envio_preg_entrevista.evento_cli);
$(document).ready(rules_envio_preg_entrevista.evento_cli_2);
$(document).ready(rules_envio_preg_entrevista.evento_sub);


