addEvent(window,'load',inicializarEventos,false);

function inicializarEventos()
{
  var ref=document.getElementById('formulario');
  addEvent(ref,'submit',enviarDatos,false);
}

function enviarDatos(e)
{
  if (window.event)
    window.event.returnValue=false;
  else
    if (e)
      e.preventDefault();
  enviarFormulario();
}


function retornarDatos()
{
  var cad='';
  var com=document.getElementById('comentarios').value;
	var nic=document.getElementById('nick').value;
	var cor=document.getElementById('correo').value;
  var pai=document.getElementById('pais').value;
	var id=document.getElementById('idnota').value;
//	var fot=document.getElementById('archivo').value;
	var tabla=document.getElementById('p_tabla').value;	
  cad='idnota='+encodeURIComponent(id)+'&correo='+encodeURIComponent(cor)+'&comentarios='+encodeURIComponent(com)+'&nick='+encodeURIComponent(nic)+'&pais='+encodeURIComponent(pai)+'&p_tabla='+encodeURIComponent(tabla);
	var comenta = document.getElementById("rescom");
	comenta.innerHTML = '<b>'+nic+'</b>, tu comentario:<br> '+com+' <b>fue enviado exitosamente</b>';
 document.getElementById('comentarios').value="";
 document.getElementById('nick').value="";
 document.getElementById('pais').value="";
 document.getElementById('correo').value="";
// document.getElementById('archivo').value="";
  return cad;
}

var conexion1;
function enviarFormulario() 
{
  if(document.getElementById('comentarios').value == "" | document.getElementById('nick').value == "" | document.getElementById('correo').value == "" | document.getElementById('pais').value == "")
	{
	 alert("Debes llenar los campos requeridos");
	}
	else
	{
   var corr=document.getElementById('correo').value;
	 if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(corr)){
    alert("La dirección de correo electrónico no tiene la sintaxis correcta, favor de corregirla")
   }
	 else
	 {
    conexion1=crearXMLHttpRequest();
    conexion1.onreadystatechange = procesarEventos;
    conexion1.open('POST','http://de10.com.mx/comunidad/envia_comentariosnotas.php', true);
    conexion1.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    conexion1.send(retornarDatos());
	 }	
	}  
}

function procesarEventos()
{
  var resultados = document.getElementById("resultados");
  if(conexion1.readyState == 4)
  {
	  //conexion1.send(retornarDatos());
    resultados.innerHTML = '<b>Gracias por participar</b>';
  } 
  else 
  {
    resultados.innerHTML = '<img src="http://de10.com.mx/directorio/de10loader2.gif" alt="Procesando...">';
  }
}



//***************************************
//Funciones comunes a todos los problemas
//***************************************
function addEvent(elemento,nomevento,funcion,captura)
{
  if (elemento.attachEvent)
  {
    elemento.attachEvent('on'+nomevento,funcion);
    return true;
  }
  else  
    if (elemento.addEventListener)
    {
      elemento.addEventListener(nomevento,funcion,captura);
      return true;
    }
    else
      return false;
}

function crearXMLHttpRequest() 
{
  var xmlHttp=null;
  if (window.ActiveXObject) 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}


 function toggle(targetId){
    target = document.all[ targetId ];
    if (target.style.display == "none"){
     target.style.display = "";
    } else {
     target.style.display = "none";
    }
   }
