// JavaScript Document



/*
Documento: ajax.js;
Autor: Ignacio Jacob Gonzalez Perez;
Proyecto: Smart Sites;
Ultima Modificacion: 24 nov 2008; 

Descripcion: Funcion ajax para cargar contenidos en bloques html definidos.


*/

function AjaxRef(){

	HttpRef = false;

	if (window.XMLHttpRequest){
		HttpRef=new XMLHttpRequest;

		return HttpRef;
	}else if(window.ActiveXObject){
		try {
			HttpRef=new ActiveXObject("Microsoft.XMLHTTP");
			if (HttpRef){
				return HttpRef;
			}
		}catch (e){}
	}
}

function llamar(ruta, div, variables){
	
	peticion=AjaxRef();
	if (peticion){
		URL=ruta;
		peticion.open("GET", URL, true);
		peticion.onreadystatechange=function(){
			asignardatos(div,null);
		}
		peticion.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		peticion.send(variables);
	}
}

function llamarWO(ruta, div, variables , conf){
	var Config=eval(conf);
	peticion=AjaxRef();
	if (peticion){
		URL=ruta;
		var method="GET";
		
		if(Config.method!=undefined) method=Config.method;
		if(Config.method=="GET"){
			URL+="?"+variables;
		}
		
		
		peticion.open(method, URL, true);
		
		peticion.onreadystatechange=function(){
			asignardatos(div,conf);
		}
		
		peticion.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		peticion.send(variables);
	}
}


function asignardatos(div, opc){
	var Config=eval(opc);
	
	if(peticion.readyState==4){		
		var respuesta=peticion.responseText;
		var midiv=document.getElementById(div);
		midiv.innerHTML=respuesta; // insersion de los datos recobrados
		
		
		// --------------- Configuracion de las OPCIONES de la funcion llamarWO a nivel de retorno de datos ---------------//
		
		//opcion de 'ret'
		//hace que se llame a otra pagina pasado medio segundo
		if(Config.ret!=undefined){ 
			setTimeout("llamar('"+Config.ret+"','"+div+"','')",500); 
		}
		//opcion de 'openLightBox'
		//hace que se abra un lightbox con el id del elemento especificado como parametro
		if(Config.openLightBox!=undefined){ 
			showLightBox(Config.openLightBox); 
		}
		//opcion de 'closeLightBox' y 'delayCloseLightBox'
		//hace que se cierre la ventana lightbox
		if(Config.closeLightBox!=undefined){ 
			closeLightBox(); 
		}
		if(Config.delayCloseLightBox!=undefined){ 
			//closeLightBox(); 
			setTimeout("closeLightBox()",500); 
		}
		//opcion de 'funtemp'
		//funcion del script de plantilla para configuracion individual por plantilla
		if(Config.funtemp){ onSeccionChange(); } // para que se ejecute sin errores debe existir la funcion onSeccionChange() en un script js activo.
		
		
		//funcion para corregir la transparencia de los png en explorer 6
		if(checkNavigator()=="explorer 6")	correctPNG();
				
		
	}
}




function sendAjaxForm(form,direccion,div,opc){
	var obj=document.getElementById(form);
	var campos=obj.elements.length;
	var opciones=eval(opc);
	var mensaje="";
	
	if(opciones.valEmail!=null){
		if(!valEmail(opciones.valEmail)){
			mensaje+="el campo "+opciones.valEmail+" es erroneo\r\n";
		}
	}
	if(opciones.valFields!=null){

		var arreglo=opciones.valFields.split(',');
		for(var i=0;i<arreglo.length;i++){
			if(!valFields(arreglo[i])){
				mensaje+="el campo "+arreglo[i]+" es obligatorio\r\n";
			}
		}
	}
	var cadena="";
	//formado de la cadena de variables
	for(var i=0;i<campos;i++){
		if(obj.elements[i].name!=""){
			if(i>0){
				cadena+="&";		
			}
			cadena+=obj.elements[i].name+"="+obj.elements[i].value;
			

		}
	}
	if(mensaje==""){
		llamarWO(direccion,div,cadena,opc);
	}else{
		alert(mensaje);
	}
}

function valEmail(cadena){
	var elemento=document.getElementById(cadena);
	var expresion=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	var respuesta=false;
	if(expresion.test(elemento.value)){
		respuesta=true;
	}
	return(respuesta);
}

function valFields(id){
	var campo = document.getElementById(id);
//	alert(campo.value);
	var expresion=/\w+/
	var respuesta=false;
	if(expresion.test(campo.value)){
		respuesta=true;
	}
	return(respuesta);
}

function muestra(id){
	var objeto = document.getElementById(id);
	objeto.style.display='block';
}

function oculta(id){
	var objeto = document.getElementById(id);
	objeto.style.display='none';
}

function cambiaValor(id,valor){
	var objeto = document.getElementById(id);
	objeto.value=valor;
}

function cambiaContenido(id,valor){
	var objeto = document.getElementById(id);
	objeto.innerHTML=valor;
}