// JavaScript Document
var tipoServicioGlobal;
var pagNoticias = 1;
var pagClientes = 1;
var counter = 0;
var uriCL = "";
//var uriCL = "/cl/html";
var urlCL = "http://clcomunicacion.com";
//var urlCL = "http://localhost/cl/html/";

// function to raise the counter and then store the change in the history
/*function raiseCounter(where) {
	counter++;
	// store the counter inside an object such as {counter:0} along with extra to test speed
	$.history( {'counter':counter, 'where':where} );
}

$.history._cache = uriCL+"/web/cache.html";
// function to handle the data coming back from the history upon forw/back hit
$.history.callback = function ( reinstate, cursor ) {
	// check to see if were back to the beginning without any stored data
	if (typeof(reinstate) == 'undefined') {
		counter = 0;
	} else {
		counter = parseInt(reinstate.counter)-1 || 0;
		eval(reinstate.where);
	}

};*/


function cargaContenido(tipo, id, subId) {
	//raiseCounter('cargaContenido('+tipo+', '+id+', '+subId+');');
	var url = uriCL+"/web/mostrarContenido.asp";
	var celdaCentral = docId("celdaCentral");
	var celdaServicios = docId("celdaServicios");
	var celdaBanners = docId("celdaBanners");
	if (tipo != null && id != null) {
		url += "?tipo="+tipo+"&id="+id;
		if (subId != null) url += "&subId="+subId;
	}
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		if (celdaServicios != null) celdaCargando(celdaServicios, 100);
		if (celdaBanners != null) celdaCargando(celdaBanners, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpCargaContenido;
		enProceso = true;
		http.send(null);
	} // if	
}
function handleHttpCargaContenido() {
	var capaMenuTop = docId("capaMenuTop");
	var capaMenuBot = docId("capaMenuBot");
	var celdaCentral = docId("celdaCentral");
	var celdaServicios = docId("celdaServicios");
	var celdaBanners = docId("celdaBanners");
	var celdaCreditos = docId("celdaCreditos");
	var celdaCredBreve = docId("celdaCredBreve");
	var imgCabecera = docId("imgCabecera");
	if (http.readyState == 4) {
		if (http.status == 200) {
			var results = http.responseText.split("#$#");
			if (results.length >= 7) {
				if (capaMenuTop != null) celdaContenido(capaMenuTop, results[0], "");
				if (capaMenuBot != null) celdaContenido(capaMenuBot, results[0], "");
				if (celdaServicios != null) celdaContenido(celdaServicios, results[1], "parteDerecha");
				if (celdaBanners != null) celdaContenido(celdaBanners, results[2], "parteDerecha");
				if (celdaCreditos != null) celdaContenido(celdaCreditos, results[3], "sobreCabecera");
				if (celdaCredBreve != null) celdaContenido(celdaCredBreve, results[4], "celdaCredBreve");
				if (imgCabecera != null) celdaContenido(imgCabecera, results[5], "");
				if (celdaCentral != null) celdaContenido(celdaCentral, results[6], "");
				var player = docId("player");
				if (player != null) {
					player.innerHTML = "<div id='media'></div>";
					var so = new SWFObject(uriCL+'/web/img/player.swf','mpl','350','250','9');
					var fld = results[7];
					var fil = results[8];
					so.addParam('allowscriptaccess','always');
					so.addParam('allowfullscreen','true');
					so.addParam('flashvars','file=http://www.clcomunicacion.com/web/img/'+fld+'/'+fil+'&backcolor=8A617B&frontcolor=FFFFFF&logo=web/img/logoVideo.png&controlbar=over&fullscreen=true&stretching=fill&autostart=false');
					so.write('media');
				}
			}
		}
	}
	enProceso = false;
}
function cargaContenido404() {
	//raiseCounter('cargaContenido404();');
	var url = uriCL+"/web/mostrarContenido404.asp";
	var celdaCentral = docId("celdaCentral");
	var celdaServicios = docId("celdaServicios");
	var celdaBanners = docId("celdaBanners");
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		if (celdaServicios != null) celdaCargando(celdaServicios, 100);
		if (celdaBanners != null) celdaCargando(celdaBanners, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpCargaContenido;
		enProceso = true;
		http.send(null);
	} // if	
}
function cargaContenido500() {
	//raiseCounter('cargaContenido500();');
	var url = uriCL+"/web/mostrarContenido500.asp";
	var celdaCentral = docId("celdaCentral");
	var celdaServicios = docId("celdaServicios");
	var celdaBanners = docId("celdaBanners");
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		if (celdaServicios != null) celdaCargando(celdaServicios, 100);
		if (celdaBanners != null) celdaCargando(celdaBanners, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpCargaContenido;
		enProceso = true;
		http.send(null);
	} // if	
}
function cargaContenidoBajaMail() {
	//raiseCounter('cargaContenidoBajaMail();');
	var url = uriCL+"/web/mostrarContenidoBajaMail.asp";
	var celdaCentral = docId("celdaCentral");
	var celdaServicios = docId("celdaServicios");
	var celdaBanners = docId("celdaBanners");
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		if (celdaServicios != null) celdaCargando(celdaServicios, 100);
		if (celdaBanners != null) celdaCargando(celdaBanners, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpCargaContenido;
		enProceso = true;
		http.send(null);
	} // if	
}

function cargaPortada() {
	//raiseCounter('cargaPortada();');
	var url = uriCL+"/web/mostrarPortada.asp";
	var celdaCentral = docId("celdaCentral");
	pagNoticias = 0;
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseCargaParteCentral;
		enProceso = true;
		http.send(null);
	} // if	
}

function cargaNoticias(noticia, pag) {
	//raiseCounter('cargaNoticias(\''+noticia+'\', '+pag+');');
	var url = uriCL+"/web/mostrarNoticias.asp";
	var celdaCentral = docId("celdaCentral");	
	if (noticia != null && noticia != '' && noticia != 'undefined') url += "?noticia="+noticia;
	if (pag != null) {
		url += "?pag="+pag;
		pagNoticias = pag;
	}
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseCargaParteCentral;
		enProceso = true;
		http.send(null);
	} // if	
}

function volver() {
	//raiseCounter('volver();');
	if (pagNoticias == 0) {
		cargaPortada();
	} else {
		cargaNoticias('', pagNoticias);
	}
}

function cargaClientes(cliente, pag) {
	//raiseCounter('cargaClientes(\''+cliente+'\', '+pag+');');
	var url = uriCL+"/web/mostrarClientes.asp";
	var celdaCentral = docId("celdaCentral");
	if (cliente != null && cliente != '' && cliente != 'undefined') url += "&cliente="+cliente;
	if (pag != null) {
		url += "?pag="+pag;
		pagClientes = pag;
	}
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseCargaParteCentral;
		enProceso = true;
		http.send(null);
	} // if	
}

function cargaQuienes() {
	//raiseCounter('cargaQuienes();');
	var url = uriCL+"/web/mostrarDatos.asp?tipo=quienes";
	var celdaCentral = docId("celdaCentral");
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseCargaParteCentral;
		enProceso = true;
		http.send(null);
	} // if	
}

function cargaAviso() {
	//raiseCounter('cargaAviso();');
	var url = uriCL+"/web/mostrarDatos.asp?tipo=aviso";
	var celdaCentral = docId("celdaCentral");
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseCargaParteCentral;
		enProceso = true;
		http.send(null);
	} // if	
}

function cargaContacto() {
	//raiseCounter('cargaContacto();');
	var url = uriCL+"/web/mostrarContacto.asp";
	var celdaCentral = docId("celdaCentral");
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseCargaParteCentral;
		enProceso = true;
		http.send(null);
	} // if	
}

function cargaTipoServicio(tipo) {
	//raiseCounter('cargaTipoServicio('+tipo+');');
	var url = uriCL+"/web/mostrarTipoServicio.asp?tipo="+tipo;
	var celdaCentral = docId("celdaCentral");
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseCargaParteCentral;
		enProceso = true;
		http.send(null);
	} // if	
}

function ocultarServicios(tipo) {
	var tipoServicio = docId("tipoServicio"+tipo);
	var aTipoServicio = docId("aTipoServicio"+tipo);
	var serviciosTipo = docId("serviciosTipo"+tipo);
	if (serviciosTipo) tipoServicio.removeChild(serviciosTipo);
	if (aTipoServicio) aTipoServicio.href="javascript:cargaServicios("+tipo+");";
}

function cargaServicios(tipo) {
	//raiseCounter('cargaServicios('+tipo+');');
	tipoServicioGlobal = tipo;
	var url = uriCL+"/web/mostrarServicios.asp?tipo="+tipo;
	if (!enProceso && http) {
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpCargaServicios;
		enProceso = true;
		http.send(null);
	} // if	
}
function handleHttpCargaServicios() {	
	var tipoServicio = docId("tipoServicio"+tipoServicioGlobal);
	var aTipoServicio = docId("aTipoServicio"+tipoServicioGlobal);
	var capaMenuTop = docId("capaMenuTop");
	var capaMenuBot = docId("capaMenuBot");
	var celdaCentral = docId("celdaCentral");
	if (http.readyState == 4) {
		if (http.status == 200) {
			var results = http.responseText.split("#$#");
			if (results.length > 2) {
				if (capaMenuTop != null) celdaContenido(capaMenuTop, results[0], "");
				if (capaMenuBot != null) celdaContenido(capaMenuBot, results[0], "");
				if (tipoServicio != null && aTipoServicio != null) {
					var contServicio = document.createElement("div");
					contServicio.id = "serviciosTipo"+tipoServicioGlobal;
					tipoServicio.appendChild(contServicio);
					addContenido(tipoServicio, contServicio);
					celdaContenido(contServicio, results[1], "");
					aTipoServicio.href="javascript:ocultarServicios("+tipoServicioGlobal+");";
				}
				if (celdaCentral != null) celdaContenido(celdaCentral, results[2], "");
				if (results.length == 5) {
					var player = docId("player");
					if (player != null) {
						player.innerHTML = "<div id='media'></div>";
						var so = new SWFObject(uriCL+'/web/img/player.swf','mpl','350','250','9');
						var fld = results[3];
						var fil = results[4];
						so.addParam('allowscriptaccess','always');
						so.addParam('allowfullscreen','true');
						so.addParam('flashvars','file=http://www.clcomunicacion.com/web/img/'+fld+'/'+fil+'&backcolor=8A617B&frontcolor=FFFFFF&logo=web/img/logoVideo.png&controlbar=over&fullscreen=true&stretching=fill&autostart=false');
						so.write('media');
					}
				}
			}
		}
	}
	enProceso = false;
}

function cargaTiposServicio() {
	//raiseCounter('cargaTiposServicio();');
	var url = uriCL+"/web/mostrarTiposServicio.asp";
	var celdaCentral = docId("celdaCentral");
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseCargaParteCentral;
		enProceso = true;
		http.send(null);
	} // if	
}

function cargaServicio(tipo) {
	//raiseCounter('cargaServicio('+tipo+');');
	var url = uriCL+"/web/mostrarServicio.asp?tipo="+tipo;
	var celdaCentral = docId("celdaCentral");
	if (!enProceso && http) {
		if (celdaCentral != null) celdaCargando(celdaCentral, 100);
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseCargaParteCentral;
		enProceso = true;
		http.send(null);
	} // if	
}

function handleHttpResponseCargaParteCentral() {
	var capaMenuTop = docId("capaMenuTop");
	var capaMenuBot = docId("capaMenuBot");
	var celdaCentral = docId("celdaCentral");
	if (http.readyState == 4) {
		if (http.status == 200) {
			var results = http.responseText.split("#$#");
			if (results.length > 1) {
				if (capaMenuTop != null) celdaContenido(capaMenuTop, results[0], "");
				if (capaMenuBot != null) celdaContenido(capaMenuBot, results[0], "");
				if (celdaCentral != null) celdaContenido(celdaCentral, results[1], "");
				if (results.length == 4) {
					var player = docId("player");
					if (player != null) {
						player.innerHTML = "<div id='media'></div>";
						var so = new SWFObject(uriCL+'/web/img/player.swf','mpl','350','250','9');
						var fld = results[2];
						var fil = results[3];
						so.addParam('allowscriptaccess','always');
						so.addParam('allowfullscreen','true');
						so.addParam('flashvars','file=http://www.clcomunicacion.com/web/img/'+fld+'/'+fil+'&backcolor=8A617B&frontcolor=FFFFFF&logo=web/img/logoVideo.png&controlbar=over&fullscreen=true&stretching=fill&autostart=false');
						so.write('media');
					}
				}
			} else {
				if (celdaCentral != null) celdaContenido(celdaCentral, results[0], "");
			}	
		}
	}
	enProceso = false;
}

function enviarConsultaContacto(d_nombre, d_email, d_comentario, d_captcha, b_copia) {
	var enviarConsulta = docId("enviarConsulta");
	if (!enProceso && http) {
		var	url = uriCL+"/web/consultaContacto.asp?d_nombre="+d_nombre+"&d_email="+d_email+"&d_captcha="+d_captcha+"&d_comentario="+d_comentario+"&b_copia="+b_copia;
		enviarConsulta.innerHTML = "<span class='enviar'>Enviando ........</span>"
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseConsultaContacto;
		enProceso = true;
		http.send(null);
	} // if	
}

function handleHttpResponseConsultaContacto() {
	var enviarConsulta = docId("enviarConsulta");
	var form = docId("formContacto");
	if (http.readyState == 4) {
		if (http.status == 200) {
			if (http.responseText.indexOf('invalid') == -1) {
				results = http.responseText.split(",");
				if (results[0] != "-1") {
					enviarConsulta.innerHTML = "<span class='enviar'>Muchas gracias</span>"
				} else if (results[1] == "-1") {
					campoError("El correo electrónico es incorrecto", form.d_email, "textError");
					enviarConsulta.innerHTML = "<a class='enviar' href='javascript:enviarConsulta();'>Enviar consulta</a>"
				} else if (results[1] == "1") {
					campoError("Todos los datos son obligatorios", form.d_email, "textError");
					enviarConsulta.innerHTML = "<a class='enviar' href='javascript:enviarConsulta();'>Enviar consulta</a>"
				};
			}
		}
	}
	enProceso = false
}

function altaMail(mail) {
	var url = uriCL+"/web/altaMail.asp?d_email="+mail;
	var capaMailing = docId("capaMailing");
	if (!enProceso && http) {
		if (capaMailing != null) capaMailing.innerHTML="<span class='enviar2'>Enviando...</span>";
		http.open('GET', url, true);
		http.onreadystatechange = handleHttpResponseAltaMailing;
		enProceso = true;
		http.send(null);
	} // if	
}

function handleHttpResponseAltaMailing() {
	var capaMailing = docId("capaMailing");
	var mail = docId("inputMail");
	if (http.readyState == 4) {
		if (http.status == 200) {
			if (http.responseText.indexOf('invalid') == -1) {
				if (http.responseText == "0") {
					capaMailing.innerHTML = "<span class='enviar2'>Alta realizada, gracias.</span>"
				} else if (http.responseText == "2") {
					campoError("La dirección de correo no es correcta", mail, "textError");
					capaMailing.innerHTML = "<a class='enviar2' href='javascript:altaMailing();'>Enviar</a>"
				} else if (http.responseText == "1") {
					campoError("Escriba su dirección de correo electrónico", mail, "textError");
					capaMailing.innerHTML = "<a class='enviar2' href='javascript:altaMailing();'>Enviar</a>"
				} else if (http.responseText == "-1") {
					campoError("El correo electrónico ya existe", mail, "textError");
					capaMailing.innerHTML = "<a class='enviar2' href='javascript:altaMailing();'>Enviar</a>"
				}
			}
		}
	}
	enProceso = false
}

function celdaCargando(celda, h) {
	var img = document.createElement("img");
	img.src = uriCL+"/web/img/cargando.gif";
	celda.innerHTML = "";
	celda.className = "alineaCentro alineaMiddle separadorH"+h;
	celda.appendChild(img);
}

function celdaContenido(celda, contenido, clase) {
	celda.removeAttribute("class");
	if (clase != "") celda.className = clase;
	celda.innerHTML = contenido;
}

function addContenido(celda, contenido) {
	celda.appendChild(contenido);
}

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) { xmlhttp = false; }
	}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) { 
			xmlhttp = false; 
		}
	}
	return xmlhttp;
}

var enProceso = false; // lo usamos para ver si hay un proceso activo
var http = getHTTPObject(); // Creamos el objeto XMLHttpRequest
