var valid = false;

function updateDest() {
  if (xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
    
    if (response == "0") {
    	document.newmsg.to.className = 'forminput_no';
    	$('dest').innerHTML = '<font color=red>Destinatario inválido</font>';
    	valid = false;
    } 
    
    if (response == "1") {
		document.newmsg.to.className = 'forminput_ok';
    	$('dest').innerHTML = '<font color=green>Destinatario válido</font>';
    	valid = true;
    }
    
  }
}

function checkfull() {
	checkto();
	if (!valid) {
		alert("Destinatario inválido");	
		return false;
	} else {
		return true;
	}
}

function checkto() {
  var gtag = document.newmsg.to.value;
  var chars = gtag.length;
  
  if (chars <= 3) {
  	document.newmsg.to.className = 'forminput';
  	$('dest').innerHTML = 'Escribe el nombre del destinatario';
  	valid = false;
  	return;
  }
  
  if (document.newmsg.to.value.toLowerCase() == document.newmsg.from.value.toLowerCase()) {
		  	document.newmsg.to.className = 'forminput_no';
		    $('dest').innerHTML = '<font color=red>Destinatario inválido</font>';
		    valid = false;
		    return;
		  } 

  var url = "call/username/?gtag=" + escape(gtag);
if (xmlHttp) {
	xmlHttp.onreadystatechange = function () {}
	xmlHttp.abort();
}
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = updateDest;
  xmlHttp.send(null);
}

function preview(id,marcarmensaje) {
	if (document.getElementById('preview-'+id).style.display == 'none') {
		document.getElementById('trigger-'+id).src = 'images/icon_msg_dropup.gif';
		new Effect.BlindDown('preview-'+id, { duration: 0.2 });
		if (marcarmensaje == true) {
			marcar(id)
		}
	} else {
		document.getElementById('trigger-'+id).src = 'images/icon_msg_dropdown.gif';
		new Effect.BlindUp('preview-'+id, { duration: 0.2 });
	}
	
}

function marcar(id) {
  var id = id;
  if (xmlHttp)
{
	xmlHttp.onreadystatechange = function () {}
	xmlHttp.abort();
}
  var url = "call/markread/?msgid=" + escape(id);
if (xmlHttp) {
	xmlHttp.onreadystatechange = function () {}
	xmlHttp.abort();
}
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
  document.getElementById('state-'+id).src = 'images/msg_entrada_leido.gif';
}