dojo.require("dojo.io.*");
dojo.require("dojo.event.*");
dojo.require("dojo.fx.*");
dojo.require("dojo.io.IframeIO");

djConfig = { parseWidgets: false, searchIds: [] };

function EditProfile() {
  dojo.io.bind({
      url: "/perfil/php-include/exec_edit_profile.php",
      handler: ExecEditProfile,
      formNode: document.getElementById("edit_profile_form")
  });
}

function ExecEditProfile(type, data, evt) {
	if (type == 'error')
			document.getElementById("profile_form_error").innerHTML = error.message + "<br />";
	else {
	    tempData = data.split('***');
	    if ( 'true' == tempData[1] )
	         window.location= tempData[0];
	    else
	        document.getElementById("profile_form_error").innerHTML = tempData[1] + "<br />";
    }

}

function RedirectTo(web)
{
	cabecera = web.substring(0,7);
	if (cabecera == "http://")
	    window.location = web;
    else
    	window.location = "http://" + web;
}

function openAvatar(code)
{
        URL = "/perfil/php-include/change_avatar.php?code=" + code;
        day = new Date();
        id  = day.getTime();
        eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=130,left = 440,top = 365');");
}

function sendAvatar()
{
	dojo.io.bind ({
	url: 'process_avatar.php',
	handler: avatarCallback,
	mimetype: "text/plain",
	formNode: dojo.byId('upload_avatar')
	});
}

function avatarCallback(type, data, evt)
{
	//dojo.byId('result').innerHTML = data;
	alert(data);
}

function ConfirmContactDelete(LoggedUser,Contact)
{
    var answer = confirm("Estas seguro de borrar a "+Contact+" de tu lista?")
    if (answer)
        deleteContact(LoggedUser,Contact);
}

function ConfirmMessageDelete(Id)
{
    var answer = confirm("Estas seguro de borrar el mensaje?")
    if (answer)
        deleteMessage(Id);
}

function showAnswerMessage(Id, From, To)
{
	From = null == From ? "" : From;
	answer  = "<table width='730' border='0' cellspacing='0' cellpadding='0'>";
        answer += "<tr><td align='center'><table width='400' border='0' cellspacing='0' cellpadding='0'> <tr>";
        answer += "<td colspan='2'><form name='form1' method='post' action=''><textarea id='answer_message_text' class='formulario-comentario'></textarea></td>";
	answer += "</tr><tr>";
 	answer += "<td height='50' colspan='2'><br /><br /><input type='image' src='/imgs/enviar.gif' onclick='sendMessage(\""+From+"\",\""+To+"\",\"answer\",\""+Id+"\")' width='72' height='18' border='0'></td>";
        answer += "</tr></table></td></tr><table>";
	document.getElementById("answer_" + Id).innerHTML = answer;
}

/*************************** Esto deberia pasarse a DOJO *************************************/
function searchPeople() {
    try {
        // Moz supports XMLHttpRequest. IE uses ActiveX.
        xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
    }
    xmlhttp.onreadystatechange = triggeredPeople;
    name     = document.getElementById("busca_nombre").value;
    resident = document.getElementById("busca_residente").options[document.getElementById("busca_residente").selectedIndex].value;
    expert   = document.getElementById("busca_experto").options[document.getElementById("busca_experto").selectedIndex].value;
    opinion  = document.getElementById("busca_opinion").value;
    xmlhttp.open("GET", '/perfil/php-include/search_people.php?name='+name+'&resident='+resident+'&expert='+expert+'&opinion='+opinion);

    xmlhttp.send(null);
}

function triggeredPeople() {
    if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) ) {
        document.getElementById("people_output").innerHTML = xmlhttp.responseText;
    }
}

function deleteContact(usuario,contacto)
{
    try {
        xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
    }
    xmlhttp.onreadystatechange = triggeredDeleteContact;
    xmlhttp.open("GET", '/perfil/php-include/delete_contact.php?username='+usuario+'&contact='+contacto);

    xmlhttp.send(null);
}

function triggeredDeleteContact() {
    if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) ) {
        if ('true' == xmlhttp.responseText)
		{
			alert('El contacto fue removido!');
			window.location.reload();
		}
		else
    		alert(xmlhttp.responseText);
    }
}

function deleteMessage(Id)
{
    try {
        xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
    }
    xmlhttp.onreadystatechange = triggeredDeleteMessage;
    xmlhttp.open("GET", '/perfil/php-include/delete_message.php?id=' + Id);

    xmlhttp.send(null);
}

function triggeredDeleteMessage() {
    if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) ) {
        if ('true' == xmlhttp.responseText)
		{
			alert('El mensaje fue eliminado!');
			window.location.reload();
		}
		else
    		alert(xmlhttp.responseText);
    }
}

function addFavorite(usuario,contacto)
{
    try {
        xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
    }
    xmlhttp.onreadystatechange = triggeredFavorite;
    xmlhttp.open("GET", '/perfil/php-include/add_contact.php?usuario='+usuario+'&contacto='+contacto+'&tipo=Favorito');

    xmlhttp.send(null);
}

function triggeredFavorite() {
    if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) ) {
        if ('true' == xmlhttp.responseText)
		{
			alert('El usuario fue agregado como Favorito!');
		}
		else
    		alert(xmlhttp.responseText);
    }
}

function askFriend(usuario,contacto)
{
    try {
        xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
    }
    xmlhttp.onreadystatechange = triggeredAskFriend;
    xmlhttp.open("GET", '/perfil/php-include/send_invite.php?usuario='+usuario+'&contacto='+contacto);

    xmlhttp.send(null);
}

function triggeredAskFriend() {
    if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) ) {
        if ('true' == xmlhttp.responseText)
		{
			alert('Se envio la invitacion, ahora solo hay que esperar!');
		}
		else
    		alert(xmlhttp.responseText);
    }
}

function addFriend(usuario,contacto)
{
    try {
        xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
    }
    xmlhttp.onreadystatechange = triggeredFriend;
    xmlhttp.open("GET", '/perfil/php-include/add_contact.php?usuario='+usuario+'&contacto='+contacto+'&tipo=Amigo');

    xmlhttp.send(null);
}

function triggeredFriend() {
    if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) ) {
        if ('true' == xmlhttp.responseText)
		{
			alert('El usuario fue agregado como Amigo!');
		}
		else
    		alert(xmlhttp.responseText);
    }
}

function sendMessage(from, to, type, answer_to) {
	var type = type == null ? "normal" : type;
        try {
                // Moz supports XMLHttpRequest. IE uses ActiveX.
                xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {}
	xmlhttp.onreadystatechange = triggeredSendMessage;
	validator = type == 'answer' ? 'answer' : document.getElementById("validator").value;
	message   = type == 'answer' ? document.getElementById("answer_message_text").value : document.getElementById("message_text").value;
	xmlhttp.open("GET", '/perfil/php-include/send_message.php?validator='+validator+'&message='+message+'&from='+from+'&to='+to+'&type='+type+'&answer_to='+answer_to);
	xmlhttp.send(null);
}

function triggeredSendMessage() {
        if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) ) {
                if ('true' == xmlhttp.responseText){
                        alert('El mensaje ha sido enviado!');
			window.location.reload();
		}
                else
                        alert(xmlhttp.responseText);
    }
}
