// JavaScript Document
function createAjax() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

var http = createAjax();
var posicX = 0;
var posicY = 0;


function goCountry(){
	var pais = document.getElementById('cboPais').value;
	
	location.href = "?idpais="+pais;
}

///----acciones visualizacion de imagenes en anuncie----///

function iniciaimagenes(e){
	var imagen1 = document.getElementById('imagen1');
	var imagen2 = document.getElementById('imagen2');
	var imagen3 = document.getElementById('imagen3');
	var imagen4 = document.getElementById('imagen4');
	var imagen5 = document.getElementById('imagen5');
	var imagen6 = document.getElementById('imagen6');
	var imagen7 = document.getElementById('imagen7');
	var imagen8 = document.getElementById('imagen8');
	
	//imagen1.addEventListener("mouseover", verimagen, true);
}
function posicion_mouse(e){
	var navegador = navigator.appName;
	//document.title = navegador;
	//var IE =  document.all?true:false;
	//if (!IE) //document.captureEvents(Event.MOUSEMOVE) 
	if (navegador == "Microsoft Internet Explorer") { 
		/*posicX = event.clientX + document.body.scrollLeft; 
		posicY = event.clientY + document.body.scrollTop;*/
		posicX = event.clientX; 
		posicY = event.clientY;
	}else{ 
		posicX = e.pageX;
		posicY = e.pageY;
	}
	if (posicX < 0){
		posicX = 0;
	}
	if (posicY < 0){
		posicY = 0;
	} 
	x = posicX;
	y = posicY;
	return true;
}

function verimagen(capa,event){
	var capa = document.getElementById('anuncie'+capa);
	//document.onmousemove = posicion_mouse;
	//////////////////////////
	x = posicX + 50;
	y = posicY - 121;
	//alert(posicY);
	
	y += "px";
	x += "px";
	//alert(x+y);
	
	capa.style.top = y;
	capa.style.left = x;
	capa.style.visibility = "visible";
	
	/*http.open("post", 'ajax.php?action=verflash');
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.send('imagen='+imagen);
	http.onreadystatechange = function(){
		if(http.readyState == 4){
			var response = http.responseText;
			capa.innerHTML = response;
		}
	}*/
}

function ocultaimagen(capa){
	document.getElementById('anuncie'+capa).style.top = "-242px";
}
/*************************************************/
function sndReq(action,campos) {
    http.open("post", 'ajax.php?action='+action);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.send(campos);
		if(action == "contact_us"){
	    http.onreadystatechange = handleResponseContactUs;
		}else{
	    http.onreadystatechange = handleResponseSendmail;
		}
}

function handleResponseContactUs() {
    if(http.readyState == 4){
        var response = http.responseText;
        //alert(response);
				var update = new Array();
				
        if(response.indexOf('|' != -1)) {
            update = response.split('|');
						open_msg(update[1],update[2],update[0]);
        }
    }
}

function handleResponseSendmail() {
    if(http.readyState == 4){
        var response = http.responseText;
        //alert(response);
				var update = new Array();
				
        if(response.indexOf('|' != -1)) {
            update = response.split('|');
						open_msg_sendmail(update[1],update[2],update[0]);
						document.getElementById('apDiv2').style.visibility = "hidden"
       }
    }
}

function verifica(){
	//var formulario = document.getElementById('formulario');
	var nombre = document.getElementById('nombre').value;
	var email = document.getElementById('email').value;
	var telefono = document.getElementById('telefono').value;
	var comentarios = document.getElementById('comentarios').value;
	
	var errores = "";
	
	if (nombre == ""){
			errores += "* Faltan sus nombres y apellidos.<br>";
	}
	if (email == ""){
			errores += "* Falta su correo electr&oacute;nico.<br>";
	}
	if (telefono == ""){
			errores += "* Falta su Tel&eacute;fono.<br>";
	}
	if (comentarios == ""){
			errores += "* D&eacute;jenos sus comentarios.<br>";
	}
	if(errores != ""){
		var titulo;
		errores = '<b>Completar el formulario:</b> <br>'+errores;
		titulo = "Enviar Comentarios";
		open_msg(titulo,errores,'Error');
	}else{
		sndReq('contact_us','nombre='+nombre+'&email='+email+'&telefono='+telefono+'&comentarios='+comentarios);
	}
}

function open_msg(titulo,contenido,imagen){
	if((imagen == "Error") || (imagen == "Success")){
		if (imagen == "Success"){
			
			document.getElementById('nombre').value = "";
			document.getElementById('email').value = "";
			document.getElementById('telefono').value = "";
			document.getElementById('comentarios').value = "";
			
		}
		document.getElementById('div_imagen').innerHTML = '<img border=0 src="images/custom/' + imagen + '.png" width="64" height="64">';
	}
	document.getElementById('div_titulo').innerHTML = titulo;
	document.getElementById('div_contenido').innerHTML = contenido;
	document.getElementById('apDiv1').style.visibility = "visible";
}

function open_msg_sendmail(titulo,contenido,imagen){
	if((imagen == "Error") || (imagen == "Success")){
		if (imagen == "Success"){
			
			document.getElementById('email1').value = "";
			document.getElementById('email2').value = "";
			
		}
		document.getElementById('div_imagen').innerHTML = '<img border=0 src="images/custom/' + imagen + '.png" width="64" height="64">';
	}
	document.getElementById('div_titulo').innerHTML = titulo;
	document.getElementById('div_contenido').innerHTML = contenido;
	document.getElementById('apDiv1').style.visibility = "visible";
}

function cierra_msg(pcapa){
	var div_msg = document.getElementById(pcapa);
	div_msg.style.visibility = "hidden";
}
function show_paginador(idSumario,id){
	var titulo_paginador = document.getElementById('idSum_'+idSumario);
	var div_paginador = document.getElementById('div_paginacion'+id);
	div_paginador.style.visibility = "visible";
	div_paginador.style.padding = "2px 5px 2px 5px";
	div_paginador.innerHTML = titulo_paginador.value;
}

function oculta_paginador(id){
	var div_paginador = document.getElementById('div_paginacion'+id);
	div_paginador.innerHTML = "";
	div_paginador.style.visibility = "hidden";
}

function do_print(){
	window.print(); 
}
function open_sendmail(){
	/*if(show_div_sendmail == true){
		document.getElementById('div_sendmail').className = "sendmail_ok";
		show_div_sendmail = false;
	}else{
		document.getElementById('div_sendmail').className = "sendmail_oculto";
		show_div_sendmail = true;
	}*/
	var div_msg = document.getElementById("apDiv2");
	document.getElementById('apDiv2_titulo').innerHTML = "Enviar Noticia";

	div_msg.style.visibility = "visible";

}


function do_sendmail(){
	var email1 = document.getElementById('email_de').value;
	var email2 = document.getElementById('email_para').value;
	var idSumario = document.getElementById('idSumario').value;
	var error = "";
	if(email1 == ""){
		error += "<li>Ingresa tu email.</li>"
		open_msg("Enviar Artículo",error,"Error");
	}
	if(email2 == ""){
		error += "<li>Ingresa el email del destinatario.</li>"
		
	}
	if(error != ""){
		error = "<ul>" + error + "</ul>";
		open_msg("Enviar Art&iacute;culo",error,"Error");
	}else{
		sndReq('send_sumario','email1='+email1+'&email2='+email2+'&idSumario='+idSumario);
	}
}

function ajax_rotador(idRevista,pos){
	var imagen = document.getElementById('ajax_image');
	var titulo = document.getElementById('ajax_titulo');
	var sobretitulo = document.getElementById('ajax_sobretitulo');
	var campos = 'idRevista='+idRevista+'&pos='+pos;
	http.open("post", 'ajax.php?action=rotador');
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.send(campos);
	http.onreadystatechange = function(){
		if(http.readyState == 4){
			var response = http.responseText;
			var update = new Array();
			if(response.indexOf('|' != -1)) {
					update = response.split('|');
					imagen.src = update[0];
					titulo.innerHTML =  update[1];
					sobretitulo.innerHTML =  update[2];
					var next = update[3];
					setTimeout("ajax_rotador("+idRevista+","+next+")",5000);
			}
		}
	}
}

function load_issuu(idRevista){
	var campos = "idRevista="+idRevista;
	http.open("post", 'ajax.php?action=issuu');
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.send(campos);
	http.onreadystatechange = function(){
		if(http.readyState == 4){
			var response = http.responseText;
			document.getElementById('div_issuu').innerHTML = response;
			setTimeout("show_issuu()",3000);
		}
	}
}

function show_issuu(){
	document.getElementById('div_issuu').style.visibility = "visible";
}

function load_flashes(){
	http.open("get", 'ajax.php?action=flashes');
	http.send(null);
	http.onreadystatechange = function(){
		if(http.readyState == 4){
			var response = http.responseText;
			var update = new Array();
			if(response.indexOf('|' != -1)) {
					update = response.split('|');
				document.getElementById('div_anuncie').innerHTML = update[0];
				document.getElementById('div_futbol').innerHTML = update[1];
				document.getElementById('div_ads').innerHTML = update[2];
			}
		}
	}
}


