//**************** FUNCIONES PARA EL MANEJO DEL AJAX **********************************
//*************************************************************************************

//--
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 http = getHTTPObject(); 

// ********************************************************************************
//*********************************************************************************


//********************* INDEX *****************************************************
//*********************************************************************************
function handleHttpResponseNavegacion() {
	if (http.readyState == 4) {    	
		//Cargamos el contenido en la capa
		document.getElementById("contenido").innerHTML = http.responseText;	
	}    
}

function navegacion(seccion){
	//Segun la seccion seleccionamos una url para cargar su contenido
	switch(seccion){
		
		case "home": 
			url='home.htm';
			sectionPictureTitle = 'cabecera-home.jpg';
			break;
			
		case "daedongenspain": 
			url='daedong-en-spain.htm';
			sectionPictureTitle = 'cabecera-home-catron.jpg';
			break;
			
		case "daedongenportugal": 
			url='daedong-en-portugal.asp';
			sectionPictureTitle = 'cabecera-home-catron.jpg';
			break;
		
		case "daedongfabrica": 
			url='daedong-fabrica.htm';
			sectionPictureTitle = 'cabecera-home-catron.jpg';
			break;
			
		case "identidad-corporativa": 
			url='daedong-identidad-corporativa.htm';
			sectionPictureTitle = 'cabecera-home-catron.jpg';
			break;
			
		case "investigacion-y-desarrollo": 
			url='daedong-investigacion-y-desarrollo.htm';
			sectionPictureTitle = 'cabecera-home-catron.jpg';
			break;		
			
		case "progreso": 
			url='daedong-progreso.htm';
			sectionPictureTitle = 'cabecera-home-catron.jpg';
			break;	
			
		case "tecnologia": 
			url='daedong-tecnologia.htm';
			sectionPictureTitle = 'cabecera-home-catron.jpg';
			break;	
			
		case "distribuidores": 
			url='distribuidores-catron-kioti.asp';
			sectionPictureTitle = 'cabecera-distribuidores-catron.jpg';
			break;
		
		case "distribuidoresPortugal": 
			url='distribuidores-catron-kioti-portugal.asp';
			sectionPictureTitle = 'cabecera-distribuidores-catron.jpg';
			break;
			
		case "productos": 
			url='productos-catron.asp';
			sectionPictureTitle = 'cabecera-productos-catron.jpg';
			break;
			
		case "noticias": 
			url='noticias-catron.asp';
			sectionPictureTitle = 'cabecera-noticias-catron.jpg';
			break;
			
		case "contacto": 
			url='contacto-catron.htm';
			sectionPictureTitle = 'cabecera-contacto-catron.jpg';
			break;
			
		default:
			url = 'contactenos-innsbruck.htm';
			//sectionPictureTitle = 'title-catalogo.jpg';
	}
	
	//Cambia la imagen de titulo de seccion
	with(document.getElementById("cabecera")){
		background = 'images/' + sectionPictureTitle;
		alt = seccion + " Innsbruck.";
	}
	
	
	//Ahora realizamos la carga del contenido
	http.open("GET", url , true);
	http.onreadystatechange = handleHttpResponseNavegacion;
	http.send(null);	
}

//*********************************************************************************
//********************* FIN DE INDEX **********************************************

//*********************** FUNCION PARA EL MANEJO DE LOS MENUS DESPLEGABLES ********
//*********************************************************************************
function despliegaTema(tema,modulo){
//funcion para desplegar u ocultar el menu que muestras las familias y subfamilias de productos
//Se utiliza tambien para la seccion del foro
//Cuando seleccionemos un tema ademas cargamos su portada asociada
	http.open("GET", "includes/getPortada.asp?tema=" + tema + "&modulo=" + modulo, true);
	http.onreadystatechange = handleHttpResponseContenido;
	http.send(null);
//Despliego el tema		
	if(document.getElementById("familia" + tema).style.display=='block'){
		document.getElementById("familia" + tema).style.display='none';
	}else{
		document.getElementById("familia" + tema).style.display='block';
	}
}

//*********************************************************************************
//*********************************************************************************

//**************** SECCION PRODUCTOS ***********************************************
//*********************************************************************************
function handleHttpResponseContenido() {
	if (http.readyState == 4) {    	
		//Cargamos el contenido en la capa
		document.getElementById("contenido-actualizer").innerHTML = http.responseText;	
	}    
}

function masInformacion(producto){
	//nos lleva a la seccion de catalogo pero cargando la galeria del producto que esta en portada
	http.open("GET", "catalogo-electromed.asp?producto=" + producto , true);
	http.onreadystatechange = handleHttpResponseNavegacion;
	http.send(null);
}

function ampliaProducto(producto){
//funcion para mostrar la ampliacion de un producto
	http.open("GET", "includes/getProductos.asp?numero="+ producto, true);    
	http.onreadystatechange = handleHttpResponseContenido;
	http.send(null);
}

//**********************************************************************************
//******************* FIN DE PRODUCTOS**********************************************


//************************ SECCION NOTICIAS **************************************
//*********************************************************************************

function ampliaNoticia(numero){
	http.open("GET","includes/getNoticias.asp?numero=" + numero , true);
	http.onreadystatechange = handleHttpResponseContenido;
	http.send(null);
}


//*********************************************************************************
//*********************************************************************************

//********************** SECCION FORO *********************************************
//*********************************************************************************

function muestraMensaje(numeroMensaje,padreMensaje){
//Funcion para ver el contenido de un mensaje
	http.open("GET","includes/getForo.asp?numeroMensaje=" + numeroMensaje + "&padreMensaje=" + padreMensaje , true);
	http.onreadystatechange = handleHttpResponseForo;
	http.send(null);
}

function handleHttpResponseForo() {
	if (http.readyState == 4) {    	
		//Cargamos el contenido en la capa
		document.getElementById("contenido-foro").innerHTML = http.responseText;	
	}    
}

function validarMensaje(){
//Funcion para validar el formulario de insertar un mensaje
	var texto=""
	var cadenaEnvio=""
	
	if ( document.getElementById("enviarMensaje").usuario.value=="" ){
		texto+=" - Debe poner su nombre.\n";
	}else{
		cadenaEnvio="usuario=" + document.getElementById("enviarMensaje").usuario.value;
	}
	
	if ( document.getElementById("enviarMensaje").resumen.value=="" ){
		texto+=" - Debe poner un titulo.\n";
	}else{
		cadenaEnvio+="&resumen=" + document.getElementById("enviarMensaje").resumen.value;
	}
	
	if ( document.getElementById("enviarMensaje").mensaje.value=="" ){
		texto+=" - Debe poner el mensaje.\n";
	}else{
		cadenaEnvio="&mensaje=" + document.getElementById("enviarMensaje").mensaje.value;
	}
	
	if ( texto!="" ){
		alert(texto);
	}else{
		//http.open("GET","includes/getForo.asp?numeroMensaje=" + numeroMensaje + "&padreMensaje=" + padreMensaje , true);
		//http.onreadystatechange = handleHttpResponseForo;
		//http.send(null);
	}
}

//*********************************************************************************
//*********************************************************************************

//****************** SECCION CONTACTO *********************************************
//*********************************************************************************

function validarEmail(valor){
//funcion para validar el formato del email
	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor));
	
}

function borrar(){
	document.getElementById("formularioContacto").reset();
}

function validar(){
//Funcion para la validar los datos enviados desde el formulario de contacto
	var texto;
	texto = "";
	
	var cadenaEnvio;
	cadenaEnvio = "";
	
	//Aqui validamos que los campos obligatorios tengan valor y se aņaden a la cadena de envio
	
	if ( document.getElementById("formularioContacto").nombre.value == "" ) {
		texto+=" - Debe indicar su nombre.\n";
	}else{
		cadenaEnvio="nombre=" + document.getElementById("formularioContacto").nombre.value;
	}
	
	if ( document.getElementById("formularioContacto").apellidos.value == "" ) {
		texto+=" - Debe indicar sus apellidos.\n";
	}else{
		cadenaEnvio+="&apellidos=" + document.getElementById("formularioContacto").apellidos.value;
	}
	
	if ( document.getElementById("formularioContacto").email.value == "" ) {
		texto+=" - Debe indicar su email.\n";
	}
	
	if(document.getElementById("formularioContacto").email.value!=""){
		if(!validarEmail(document.getElementById("formularioContacto").email.value)){
				texto+=" - Formato de E-mail incorrecto.\n";
		}else{
			cadenaEnvio+="&email=" + document.getElementById("formularioContacto").email.value;
		}
	}
	
	if ( document.getElementById("formularioContacto").comentarios.value == "" ){
		texto+=" - Debe incluir el motivo de su consulta.\n";
	}else{
		cadenaEnvio+="&comentarios=" + document.getElementById("formularioContacto").comentarios.value;
	}	
	
	if ( document.getElementById("formularioContacto").asunto.value == "" ){
		texto+=" - Debe incluir el asunto de su consulta.\n";
	}else{
		cadenaEnvio+="&asunto=" + document.getElementById("formularioContacto").asunto.value;
	}
	
	//Aqui vamos aņadiendo los demas campos sino estan vacion
	
	if ( document.getElementById("formularioContacto").direccion.value != "" ){
		cadenaEnvio+="&direccion=" + document.getElementById("formularioContacto").direccion.value;
	}
	
	if ( document.getElementById("formularioContacto").poblacion.value != "" ){
		cadenaEnvio+="&poblacion=" + document.getElementById("formularioContacto").poblacion.value;
	}
	
	if ( document.getElementById("formularioContacto").provincia.value != "" ){
		cadenaEnvio+="&provincia=" + document.getElementById("formularioContacto").provincia.value;
	}
	
	if ( document.getElementById("formularioContacto").telefono.value != "" ){
		cadenaEnvio+="&telefono=" + document.getElementById("formularioContacto").telefono.value;
	}
	
	if ( document.getElementById("formularioContacto").movil.value != "" ){
		cadenaEnvio+="&movil=" + document.getElementById("formularioContacto").movil.value;
	}
	
	if ( document.getElementById("formularioContacto").fax.value != "" ){
		cadenaEnvio+="&fax=" + document.getElementById("formularioContacto").fax.value;
	}
	
	if ( document.getElementById("formularioContacto").cp.value != "" ){
		cadenaEnvio+="&cp=" + document.getElementById("formularioContacto").cp.value;
	}
	
	
	//Si falta algun campo obligatorio o el email no tiene un formato correcto
	//mostramos un mensaje de avios
	
	if ( texto != "" ){
		alert(texto);
	}else{
		//alert(cadenaEnvio);
		http.open("GET", "actualizer/formcorreosMultiple/envio.asp?" + cadenaEnvio, true);
		http.onreadystatechange = handleHttpResponseContacto;
		http.send(null);
	}
	
}

function handleHttpResponseContacto() {
	if (http.readyState == 4) {    	
		//Aqui va el nombre de la capa donde queramos cargar el contenido
		document.getElementById("mensaje-respuesta").innerHTML = http.responseText;	
		document.getElementById("formularioContacto").reset();
	}    
}
//*********************************************************************************
//*********************************************************************************

//******************SECCION BOLSA DE TRABAJO***************************************
//*********************************************************************************
function validarTrabajo(){
	//Funcion para la validar los datos enviados desde el formulario de bolsa de trabajo
	var texto;
	texto = "";
	
	//Aqui validamos que los campos obligatorios tengan valor y se aņaden a la cadena de envio
	
	if ( document.getElementById("formularioContacto").nombre.value == "" ) {
		texto+=" - Debe indicar su nombre.\n";
	}
	
	if ( document.getElementById("formularioContacto").apellidos.value == "" ) {
		texto+=" - Debe indicar sus apellidos.\n";
	}
	
	if ( document.getElementById("formularioContacto").email.value == "" ) {
		texto+=" - Debe indicar su email.\n";
	}
	
	if(document.getElementById("formularioContacto").email.value!=""){
		if(!validarEmail(document.getElementById("formularioContacto").email.value)){
				texto+=" - Formato de E-mail incorrecto.\n";
		}
	}	
	
	if ( document.getElementById("formularioContacto").comentarios.value == "" ){
		texto+=" - Debe incluir el motivo de su consulta.\n";
	}
	
	if ( document.getElementById("formularioContacto").curriculum.value == "" ){
		texto+=" - Debe incluir su curriculum.\n";
	} 
	
	//Si falta algun campo obligatorio o el email no tiene un formato correcto
	//mostramos un mensaje de avios
	
	if ( texto != "" ){
		alert(texto);
	}else{
		document.getElementById("formularioContacto").submit();
	}

}

function volverBolsaTrabajo(mensaje){
	sectionPictureTitle = 'title-bolsadeTrabajo.jpg';
	
	//Cambia la imagen de titulo de seccion
	with(document.getElementById("seccionTitle")){
		src = 'images/' + sectionPictureTitle;
		alt = "bolsa ElectroMED.";
	}	
	
	//Ocultamos la imagen de novedades de producto
	with(document.getElementById("novedades")){
		src='images/background-title-red-part2-electromed.jpg';
		alt ='';
	}
	
	http.open("GET", "bolsa-de-trabajo-electromed.asp?mensaje=" + mensaje , true);
	http.onreadystatechange = handleHttpResponseNavegacion;
	http.send(null);	
}


//*********************************************************************************
//*********************************************************************************

//******************SECCION DISTRIBUIDORES ***************************************
//*********************************************************************************
function handleHttpResponseDistribuidor() {
	if (http.readyState == 4) {    	
		//Aqui va el nombre de la capa donde queramos cargar el contenido
		document.getElementById("contenido-actualizer").innerHTML = http.responseText;	
	}    
}


function getProvincia(provincia){
	http.open("GET", "includes/getDistribuidores.asp?provincia=" + provincia , true);
	http.onreadystatechange = handleHttpResponseDistribuidor;
	http.send(null);
}

//************************************************************************************
//************************ FUNCIONES PARA CAMBIAR LA CLASE DE UNA CAPA *****************

function cambiaClase(nombreCapa,numero){
	//Recibe el id de la capa o elemento que queremos cambiar la clase
	document.getElementById(nombreCapa + numero).className='breveOver';
}
function cambiaClaseOut(nombreCapa,numero){
	//Recibe el id de la capa o elemento que queremos cambiar la clase
	document.getElementById(nombreCapa + numero).className='breve';
}
