var http    = createRequestObject();
var target  = '';
var action  = '';
var id      = '';
var cad     = '';
var from    = '';
var append  = '';
var type    = '';
var content = 'content';
var delay   = 1000;

// Creamos conexion
function createRequestObject() {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch(f) {
            xmlhttp = null;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

// Envia TEXTO por POST
function sendRequestTextPost(type, id) {
    var rnd = Math.random();
    try {

        type = escape(type);
        id   = escape(id);

		var actions = 'type='+type;
		if (id) {
			actions += '&id='+id;
		}
		if (action) {
			actions += '&action='+action;
		}
		if (cad) {
			actions += '&name='+escape(cad);
		}
		if (from) {
			actions += '&from='+escape(from);
		}
		if (append) {
			actions += append;
		}
		actions += '&rnd='+rnd;

        http.open('POST', '/register.php', true);
        //http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-1');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = handleResponseText;
        http.send(actions);
    } catch(e) {
//        alert("error send: "+e);
    } finally {}
}

// Recoge respuesta del servidor como TEXTO
function handleResponseText() {
	try {
		if (http.readyState == 4 && http.status == 200 && target) {
			var response = ""+http.responseText+"";
			var obj = document.getElementById(target);
			if (obj && response) {
				obj.innerHTML = response;
			}
			if (content) {
				var to = document.getElementById(content);
				if (to) {
					if (to.style.display != "block") {
						to.style.display = "block";
					}
				}
			}
			if (!response.match("ERROR:") && (action == 'ADD')) {
				setTimeout('location.reload();', delay);
			}
		}
	} catch(e) {
	   // alert("error rcv: "+e);
	} finally {}
}

function getResults(type, target, cad, from, append) {

	if (cad.length = 0) {
		return false;
	}
	if (target) {
		this.target = target;
	}
	if (cad) {
		this.cad = cad;
	}
	if (from) {
		this.from = from;
	}
	if (append) {
		this.append = append;
	}
	if (type) {
		this.type = type;
	}
	this.id 	= '';
	this.action = '';

	sendRequestTextPost(type, '');
}

function setResult(type, tid, tname, id, cad, disabled, from, idtipo) {
    if (idtipo) {
        var objid = opener.document.getElementById('idtipo');
        if (!objid) {
            alert('No se puede actualizar el idtipo');
            return false;
        }
        objid.value = idtipo;
    }

	// Casos especiales
	if (!from) {
    	if (type == 'img') {
    	    opener.location = '/imagenes/relacion.php?id='+id;
    	} else if (type == 'jue') {
    	    opener.location = '/juegos/relacion.php?id='+id;
    	} else if (type == 'vid') {
    	    opener.location = '/videos/relacion.php?id='+id;
    	} else if (type == 'ppt') {
    	    opener.location = '/ppts/relacion.php?id='+id;
    	}
	}

	// Casos generales
	var objid = opener.document.getElementById(tid);
	if (!objid) {
		alert('No se puede actualizar el ID');
		return false;
	}
	var objname = opener.document.getElementById(tname);
	if (!objname) {
		alert('No se puede actualizar el nombre');
		return false;
	}

	objid.value   = id;
	objname.value = cad;
    objname.focus();

	window.close();
}

function chooseThumb(id, thumb) {

    this.type   = 'thumb';
    this.from   = 'video';
    this.append = '&thb='+thumb;
    this.target = 'thumbs';

    this.id     = '';
	this.action = '';

	sendRequestTextPost(this.type, id);
}

function genThumbs(id) {

    this.type   = 'genthumbs';
    this.from   = 'video';
    this.target = 'thumbs';

    this.id     = '';
	this.action = '';

	if (this.target) {
	    var obj = document.getElementById(this.target);
	    if (obj) {
	        obj.innerHTML = '<img src="/images/loading.gif">';
	    }
	}

	sendRequestTextPost(this.type, id);
}
