var cant_ready = 0;
// funciones para uso de Ajax
var html_r;
function loadHTML(file, func) {
		var htmlhttp= newAjax();
		htmlhttp.open("GET", file, true);
		htmlhttp.onreadystatechange=function() {
			if (htmlhttp.readyState==4) {
				html_r = htmlhttp.responseText;			
				html_ok = true;
				func();
			}
		 }
		 htmlhttp.send(null);
}
function newAjax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

// muchas de las funciones deberian ser reemplazadas por esta
function cargar(url, div_id, variables){
	div = document.getElementById(div_id);
	ajax = newAjax();
	ajax.open("POST", url,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			div.innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(variables);
}
// -------------------------------------------------------

function cargando(sec_id) {
	if (sec_id > 1){
		document.getElementById("loading_"+sec_id).style.display = "";
	}
}

function cargado(size, sec_id) {
	if (sec_id > 1){
		cant_ready++;
		if(cant_ready == size)
			document.getElementById("loading_"+sec_id).style.display = "none";
	}
}

function sethistory(url, id, sec_id){
	var ajax = newAjax();
	var rta = document.getElementById('his');

	ajax.open("GET","setHistory.php?url=" + url + "&id=" + id + "&sec_id=" + sec_id, true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			rta.innerHTML = ajax.responseText;
			//cargado(ajax.length, sec_id);
		}
	}
	ajax.send('');
}

function cargarpagina(url, id, sec_id){
	sethistory(url, id, sec_id);
	cargarpagina_(url, id, sec_id);
}

function cargarpagina_(url, id, sec_id){
	var div = new Array(2);
	var ajax = new Array(newAjax(),newAjax(),newAjax(), newAjax());
//	var div_id = new Array("div_loginInfo","div_seccionImg", "div_contenido","div_micuenta","div_faq","div_accesos");
		var div_id = new Array("div_seccionImg", "div_contenido","div_accesos", "count_stat");
//	var urls = new Array("cajas/caja_loginInfo.php","cajas/caja_seccionImg.php",url,"cajas/caja_miCuenta.php","cajas/caja_faq.php","cajas/caja_accesos.php");

	var urls = new Array("cajas/caja_seccionImg.php",url,"cajas/caja_accesos.php", "contador.php");


	cant_ready = 0;
	cargando(sec_id);

	for (i=0;i<ajax.length;i++){
		div[i] = document.getElementById(div_id[i]);
		ajax[i].open("POST", urls[i],true);
	}

	ajax[0].onreadystatechange=function() {
		if (ajax[0].readyState==4) {
			div[0].innerHTML = ajax[0].responseText;
			cargado(ajax.length, sec_id);
		}
	}
	ajax[1].onreadystatechange=function() {
		if (ajax[1].readyState==4) {
			div[1].innerHTML = ajax[1].responseText;
			cargado(ajax.length, sec_id);
		}
	}
	ajax[2].onreadystatechange=function() {
		if (ajax[2].readyState==4) {
			div[2].innerHTML = ajax[2].responseText;
			cargado(ajax.length, sec_id);
		}
	}
	ajax[3].onreadystatechange=function() {
		if (ajax[3].readyState==4) {
			div[3].innerHTML = ajax[3].responseText;
			cargado(ajax.length, sec_id);
		}
	}
/*	ajax[3].onreadystatechange=function() {
		if (ajax[3].readyState==4) {
			div[3].innerHTML = ajax[3].responseText;
			cargado(ajax.length, sec_id);
		}
	}
	ajax[4].onreadystatechange=function() {
		if (ajax[4].readyState==4) {
			div[4].innerHTML = ajax[4].responseText;
			cargado(ajax.length, sec_id);
		}
	}
	ajax[5].onreadystatechange=function() {
		if (ajax[5].readyState==4) {
			div[5].innerHTML = ajax[5].responseText;
			cargado(ajax.length, sec_id);
		}
	}
*/
	for (i=0;i<ajax.length;i++){
		ajax[i].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax[i].send("id="+id);
	}
}

function abrirpagina(url, id, sec_id){
	document.location.replace(url);
	void(0);
}

function abrirpopup(url, id, sec_id) {
	window.open(url,'pop','location=no,toolbar=no,directories=no,status=no,scrollbar=yes,menubar=no,scrollbars=yes,top=50,left=50,resizable=yes');
	void(0);
}

function abrirpopuptv(url, id, sec_id) {
	window.open(url,'poptv','height=368,width=656,location=no,toolbar=no,directories=no,status=no,scrollbar=no,menubar=no,resizable=no');
	void(0);
}

// funciones para FAQ
function cargar_respuesta(id){
	divfaq = document.getElementById("respuesta"+id);
	if (divfaq.innerHTML == ""){ //si la respuesta no fue previamente cargada llamo al servidor
		ajaxfaq = newAjax();
		ajaxfaq.open("POST", "faq.php",true);
		ajaxfaq.onreadystatechange=function() {
			if (ajaxfaq.readyState==4) {
				divfaq.innerHTML = ajaxfaq.responseText;
			}
		}
		ajaxfaq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajaxfaq.send("idfaq="+id);
	} else {
		divfaq.style.display = ''; //muestro el div con su contenido previo
	}
}

function ir_respuesta(id){
	if (document.getElementById("respuesta"+id)){
		cargar_respuesta(id);
	} else {
			div = document.getElementById("div_contenido");
			ajax = newAjax();
			ajax.open("POST", "faq.php",true);
			ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				div.innerHTML = ajax.responseText;
				cargar_respuesta(id);
			}
		}
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.send("ver="+id);
	}
}

// funciones para Mi Cuenta
function mc_login(nick, pass){
	if ((nick != '') && (pass != '')){
		div = document.getElementById("div_micuenta");
		ajax = newAjax();
		ajax.open("POST", "cajas/caja_miCuenta.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				div.innerHTML = ajax.responseText;
				cargarpagina("micuenta.php",2,2);
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("mc_nick="+nick+"&mc_pass="+pass);
	} else {
		alert("Ingrese un usuario y una contraseña validos.");
	}
}

function mc_logout(){
	div = document.getElementById("div_loginInfo");
	ajax = newAjax();
	ajax.open("POST", "cajas/caja_loginInfo.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			div.innerHTML = ajax.responseText;
			cargarpagina("micuenta.php",2,2);
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("logout=true");
}

function mc_cambiardatos(){
	if (document.getElementById('cavUser').value != "") {
		div = document.getElementById("div_contenido");
		ajax = newAjax();
		ajax.open("POST", "mc_datospersonales.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				div.innerHTML = ajax.responseText;
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("save=y&cavUser="+document.getElementById('cavUser').value+"&homePhone="+document.getElementById('homePhone').value+"&contactPhone="+document.getElementById('contactPhone').value+"&email="+document.getElementById('email').value);
	} else {
		alert("Debe ingresar un usuario válido.");
	}
}

function mc_recuperarpass(){
	div = document.getElementById("div_contenido");
	ajax = newAjax();
	ajax.open("POST", "mc_recuperarpass.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			div.innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("save=y&rmc_nick="+document.getElementById('rmc_nick').value+"&rmc_mail="+document.getElementById('rmc_mail').value);
}

// funciones para WebMail
function wm_login(nick, pass){
	if ((nick != '') && (pass != '')){
		div = document.getElementById("wm_msj");
		div.innerHTML = "Validando...<img src=\"img/loading.gif\" /> ";
		ajax = newAjax();
		ajax.open("POST", "cajas/check_webmail.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				if (ajax.responseText == "") {
					div.style.color = "#666666";
					div.innerHTML = "Administr&aacute; tu cuenta de E-mail Yabir&uacute;:";
					document.frm_webmail.submit();
					document.getElementById('wm_nick').value = "";
					document.getElementById('wm_pass').value = "";
				} else {
					div.style.color = "#FF0000";
					div.innerHTML = "Usuario y/o contrase&ntilde;a incorrectos, por favor int&eacute;ntelo nuevamente.";
				}
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("wm_nick="+nick+"&wm_pass="+pass);
	} else {
		alert("Ingrese un usuario y una contraseña validos.");
	}
}

// funciones para Noticias
function ver_noticia(id,seccion,pagina){
	div = document.getElementById("div_n_contenido");
	ajax = newAjax();
	ajax.open("POST", "noticias.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			div.innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("noticia_id="+id+"&seccion="+seccion+"&pagina="+pagina);
}

function ver_lista_noticia(pagina,seccion){
	div = document.getElementById("div_n_contenido");
	ajax = newAjax();
	ajax.open("POST", "noticias.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			div.innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("pagina="+pagina+"&seccion="+seccion);
}

// otras
function crear_precliente(){
	if (document.getElementById('cavPassword').value == document.getElementById('cavPassword2').value){
		div = document.getElementById("div_contenido");
		ajax = newAjax();
		ajax.open("POST", "suscripcion.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				div.innerHTML = ajax.responseText;
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		strVarsPost = "save=si&";
		strVarsPost += "clientNumber="+document.getElementById('clientNumber').value+"&";
		strVarsPost += "clientType="+document.getElementById('clientType').value+"&";
		strVarsPost += "firstName="+document.getElementById('firstName').value+"&";
		strVarsPost += "lastName="+document.getElementById('lastName').value+"&";
		strVarsPost += "dni="+document.getElementById('dni').value+"&";
		strVarsPost += "birthday="+document.getElementById('birthday_day').value+"-";
		strVarsPost += document.getElementById('birthday_month').value+"-";
		strVarsPost += document.getElementById('birthday_year').value+"&";
		strVarsPost += "trademark="+document.getElementById('trademark').value+"&";
		strVarsPost += "iva="+document.getElementById('iva').value+"&";
		strVarsPost += "cuit="+document.getElementById('cuit').value+"&";
		strVarsPost += "homePhone="+document.getElementById('homePhone').value+"&";
		strVarsPost += "contactPhone="+document.getElementById('contactPhone').value+"&";
		strVarsPost += "email="+document.getElementById('email').value+"&";
		strVarsPost += "address="+document.getElementById('address').value+"&";
		strVarsPost += "city="+document.getElementById('city').value+"&";
		strVarsPost += "cp="+document.getElementById('cp').value+"&";
		strVarsPost += "comments="+document.getElementById('comments').value+"&";
		strVarsPost += "client="+document.getElementById('client').checked+"&";
		strVarsPost += "plan_id="+document.getElementById('plan_id').value+"&";
		strVarsPost += "cavUser="+document.getElementById('cavUser').value+"&";
		strVarsPost += "cavPassword="+document.getElementById('cavPassword').value+"&";
		strVarsPost += "acept="+document.getElementById('acept').checked;
		ajax.send(strVarsPost);
	} else {
		alert("Su contraseña no coincide con su confirmación.");
	}
}

function contratar_plan(plan_id){
	div = document.getElementById("div_contenido");
	ajax = newAjax();
	ajax.open("POST", "suscripcion.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			div.innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("plan_id="+plan_id);
}

// desplega el contenido de diferentes paginas con indices
function desplegar(pagina,indice){
	div = document.getElementById("div_contenido");
	ajax = newAjax();
	ajax.open("POST", pagina,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			div.innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("indice="+indice);
}


function crear_contacto(){
	div = document.getElementById("div_contenido");
	ajax = newAjax();
	ajax.open("POST", "contacto.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			div.innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	strVarsPost = "save=si&";
	strVarsPost += "tipo="+document.getElementById('tipo').value+"&";
	strVarsPost += "email="+document.getElementById('email').value+"&";
	strVarsPost += "titulo="+document.getElementById('titulo').value+"&";
	strVarsPost += "texto="+document.getElementById('texto').value+"&";
	ajax.send(strVarsPost);
}


function cargarmapa(){
	window.open('xtree/mapa.php','Mapa','width=400,scrollbars=yes');
}


//------------------fin de Ajax
