var http = createRequestObject();


function createRequestObject() {
   var ro = false;
    if (window.XMLHttpRequest)
    {
        // Si es Mozilla, Safari etc
        ro = new XMLHttpRequest ();
    } else if (window.ActiveXObject)  {
        // pero si es IE
        try 
        {
            ro = new ActiveXObject ("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            // en caso que sea una versión antigua
            try
            {
            
                ro = new ActiveXObject ("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert('No soporta ajax');
                return false;
            }
        }
    }  else{
        return false;
    }

    return ro;
}

function trim(cadena) {
	for(i=0; i<cadena.length; ) {
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}
  for(i=cadena.length-1; i>=0; i=cadena.length-1) {
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}

	return cadena;
}

function sndReq(url) {
	url = url + "&ms=" + new Date().getTime();
    
    http.open('GET', url);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();
		var e=null;
		var imgajax;

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            if ( update[0] == 'ejecutar' ) {
            	imgajax = update[2];
            	eval(update[1]);
            } else if ( update[0] == 'ordenable' ) {
            	e=document.getElementById(update[1]);
	            imgajax = update[1];
	            e.innerHTML = update[3];
	            e.style.display="block";
            	dragsort.makeListSortable(document.getElementById(update[2]),	saveOrder)
            } else if ( update[0] == 'nada' ) {
	            imgajax = update[1];
            } else {
	            e=document.getElementById(update[0]);
	            imgajax = update[0];
	            e.innerHTML = update[1];
	            e.style.display="block";
            }
        }
        document.getElementById('AW' + imgajax ).style.display="none";
    }
}

function apuntarPromo(promo, user) {
	if ( confirm("Confirma?") ) {
		url='ajax/ajax-promos.php?action=join&idpromo=' + promo + '&usr=' + user;
		document.getElementById('AWjoinPromo').style.display="block";
		http.open('GET', url);
	    http.onreadystatechange = function () {
			    if(http.readyState == 4){
			        var response = http.responseText;
			        var update = new Array();
					if(response.indexOf('|') != -1) {
						 update = response.split('|');
						 document.getElementById('promo' + promo).innerHTML = update[1];
						 document.getElementById('AWjoinPromo').style.display="none";
					}
			    }
			};
	    http.send(null);
	}
}

function cargarVideo(idvid) {
	var archivo = document.getElementById('arch' + idvid).value;
	var label = document.getElementById('lbl' + idvid).value;
	
	document.getElementById('dreamsTVLabel').innerHTML=label;
	
	if (AC_FL_RunContentTV == 0) {
				alert("Esta página requiere el archivo AC_RunActiveContent.js.");
	} else {
		AC_FL_RunContentTV(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', '313',
			'height', '237',
			'src', 'reproductor',
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'showall',
			'wmode', 'window',
			'devicefont', 'false',
			'id', 'reproductor',
			'flashvars', 'flv=videos/' + archivo,
			'bgcolor', '#666666',
			'name', 'reproductor',
			'menu', 'true',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'movie', 'reproductor',
			'salign', ''
			);
	}
}