// JavaScript Document
	// ==========================================================================
    // AJAX ENABLED!
    // ==========================================================================
    
    var xhid = 1 // Clave para múltiples operaciones al tiempo
	var sendToTop = 500

    function GetXmlHttpObject(handler){ 
        xhid ++
        var xh=null
        if (window.XMLHttpRequest){
            xh=new XMLHttpRequest()
        }else if (window.ActiveXObject){
            xh=new ActiveXObject("Microsoft.XMLHTTP")
        }
        if (xh==null){
            alert ("Su browser no soporta HTTP Request. Lo sentimos.")
            return
        } 
        return xh
    }
	
	// ==========================================================================
	// respuestaAjx - PROCESA TODA RESPUESTA QUE VIENE DE PHP - Errores también - Ver hw.php
	// ==========================================================================
	function respuestaAjx(miXmlHttp){
		debugSalida = true
		debugSalidaPlenoCodigo = true
		tmp = miXmlHttp.responseText.split('[[')
		var respuestas = new Array()
		var arrTmp = new Array()
		for(i=0; i<tmp.length; i++) {
			arrTmp = new Array()
			if (tmp[i].indexOf(']]')!=-1) {
				arrTmp = tmp[i].split(']]')
				respuestas[arrTmp[0]]=arrTmp[1]
			}
		}
		respuestas['todo']=miXmlHttp.responseText
	// CONTROL DE ERRORES DEVUELTOS POR PHP - Ver hw.php
		// Nótese que ok puede ser 1 pero igual venir un error de php... Muestra dvErr y lo deja seguir...
		// Si ok= 0 se detiene aquí
		if (respuestas['ok']!=1 || respuestas['err']) {
			d=document.getElementById('dvErrMsg')
			d.removeChild(d.childNodes[0]) 
			var a = document.createElement('div');
			d.appendChild(a)
			if (respuestas['err']) a.innerHTML += respuestas['err']		
			if (!debugSalidaPlenoCodigo) {
				a.innerHTML += debugSalida ? respuestas['todo'] : ''	
				a.style.clear='both'
			} else if (debugSalida){
				var textA = document.createElement('TEXTAREA'); 
				textA.style.width = '100%'
				textA.style.height = 200
				textA.value = respuestas['todo'];
				a.appendChild(textA)
			}
			papa =document.getElementById('dvErr')
			showDivAbsolute=true
			showDiv(papa)
			if(respuestas['ok']!=1) return false // Si ok= 0 deténgase aquí
		}
		// Cargar scripts pedidos desde PHP por el contenido... (evita hacerlo 2 veces)
		if (respuestas['js']) {
			tmpStr = respuestas['js']
			tmpArr=new Array
			tmpArr = tmpStr.split(',')
			for(ii=0; ii<tmpArr.length; ii++) {
				if (jsCargados.indexOf(','+tmpArr[ii]+',')==-1) {
					jsCargados+=tmpArr[ii]+','
					var script = document.createElement('script');
					script.type = 'text/javascript';
					script.src = tmpArr[ii];
					document.getElementsByTagName('head')[0].appendChild(script);
				}
			}
		}
		return respuestas
	}
	function showDiv(d) {
		if (!d) return false;
		if(!showDivAbsolute) d.style.position = 'relative';
		showDivAbsolute=false
		d.style.visibility = 'visible';
		d.style.overflow = 'visible';	
		d.style.height = 'auto';	
		d.style.zIndex = sendToTop++;
		if(d.getAttribute('esContent'))	document.getElementById('content').scrollTop= d.getAttribute('lastScrollTop') 
	}

	function prepararUrl(url){
		// Enviar 'aprovecho%20%' devuelve error forbidden 403 porque asume que es echo... Increible!!!!
		return url.replace(/echo%20/g,"echo%26nbsp%3B")
	}


	// -------------------------------------------------------------
	// FUNCIONALIDAD AJAX POR FUERA DE LA MESA DE TRABAJO - ESTO ES LO QUE VARIA DE UN SITIO A OTRO...
	// -------------------------------------------------------------
	
	function peticionAjax(){
		// Requeridos
		var nodeFieldNombre = document.getElementById('fieldTokenAddContactoNombre')
		var resNombre = nodeFieldNombre.value
		resNombre = encodeURIComponent(resNombre)
		var resLink = revisarLink(document.getElementById('fieldTokenAddContactoLink').value)
		resLink = resLink
		if(!resNombre){
			alert('Es preciso llenar los campos marcados (*)')
			return false
		}

		// Ocultar DIV de agregar contacto
		var nodeAgregar = document.getElementById('divTokenAddContacto')
		var nodeCargandoOld = document.getElementById('hiwebxCargando')
		var nodeCargando = nodeCargandoOld.cloneNode(true)
		nodeCargandoOld.parentNode.removeChild(nodeCargandoOld)
		nodeAgregar.parentNode.appendChild(nodeCargando)
		ocultarTokenAddContacto()
		nodeCargando.style.visibility='visible'
		nodeCargando.style.display='block'
		// Preparar la URL que se va a pedir
		var url="?accion=act_fotofolio&subaccion=sub_add_contacto&nombre="+resNombre+"&link="+resLink	
		var yaExistia=1
		var idt=20

		miXhName = 'xhVar'+xhid // Empieza AJAX
		xh=self[miXhName] = GetXmlHttpObject()
		xh.onreadystatechange= new Function ("peticionAjaxRes('"+miXhName+"',"+yaExistia+","+idt+")")
		prepararUrl(url)
		xh.open("GET",url,true)
		xh.send(null) // TERMINA AJAX	
		return false
		
	}
	function peticionAjaxRes(strXh,yaExistia,idt) {
		xh=(self[strXh])
		if (xh.readyState==4 || xh.readyState=="complete"){ 
			var nodeCargando = document.getElementById('hiwebxCargando')
			nodeCargando.style.visibility='hidden'
			nodeCargando.style.display='none'
			self['mTcargando'+idt]=null
			respuestas = respuestaAjx(xh)
			if (respuestas['ok']!=1) {
				alert('SUCEDIO ERROR 25FH')
			}else{
				var id_cont = respuestas['id_cont']
				// Insertar nuevo contacto en arrContactos y reordenarlo
				arrContactos['id_'+id_cont] = new unContacto(respuestas['nombre'],id_cont,99);
				ordenarMiArrayContactos('')
				var nodeBuscar = document.getElementById(fieldDestinoToken+'_tokenFieldBuscar')
				crearToken(respuestas['nombre'], respuestas['id_cont'])
				nodeBuscar.focus()
				return
			}
		}
	}	




	
	function peticionAjax_b(){
		// Requeridos
		var nodeFieldNombre = document.getElementById('fieldTokenAddContactoNombre_b')
		var resNombre = encodeURIComponent(nodeFieldNombre.value)
		var resLink = revisarLink(document.getElementById('fieldTokenAddContactoLink_b').value)
		resLink = (resLink)	
		if(!resNombre){
			alert('Es preciso llenar los campos marcados (*)')
			return false
		}

		// Ocultar DIV de agregar contacto
		var nodeAgregar = document.getElementById('divTokenAddContacto_b')
		var nodeCargandoOld = document.getElementById('hiwebxCargando')
		var nodeCargando = nodeCargandoOld.cloneNode(true)
		nodeCargandoOld.parentNode.removeChild(nodeCargandoOld)
		nodeAgregar.parentNode.appendChild(nodeCargando)
		ocultarTokenAddContacto()
		nodeCargando.style.visibility='visible'
		nodeCargando.style.display='block'
		// Preparar la URL que se va a pedir
		var url="?accion=act_fotofolio&subaccion=sub_add_cliente&nombre="+resNombre+"&link="+resLink	
		var yaExistia=1
		var idt=20

		miXhName = 'xhVar'+xhid // Empieza AJAX
		xh=self[miXhName] = GetXmlHttpObject()
		xh.onreadystatechange= new Function ("peticionAjaxRes_b('"+miXhName+"',"+yaExistia+","+idt+")")
		prepararUrl(url)
		xh.open("GET",url,true)
		xh.send(null) // TERMINA AJAX	
		return false
		
	}
	function peticionAjaxRes_b(strXh,yaExistia,idt) {
		xh=(self[strXh])
		if (xh.readyState==4 || xh.readyState=="complete"){ 
			var nodeCargando = document.getElementById('hiwebxCargando')
			nodeCargando.style.visibility='hidden'
			nodeCargando.style.display='none'
			self['mTcargando'+idt]=null
			respuestas = respuestaAjx(xh)
			if (respuestas['ok']!=1) {
				alert('SUCEDIO ERROR 2FH_b')
			}else{
				var id_cont = respuestas['id_cont']
				// Insertar nuevo contacto en arrContactos y reordenarlo
				arrContactos_b['id_'+id_cont] = new unContacto_b(respuestas['nombre'],id_cont,99);
				ordenarMiArrayContactos('b')
				var nodeBuscar = document.getElementById(fieldDestinoToken+'_tokenFieldBuscar')

				crearToken(respuestas['nombre'], respuestas['id_cont'])
				nodeBuscar.focus()
				return
			}
		}
	}	
	
	function peticionAjax_c(){
		// Requeridos
		var nodeFieldNombre = document.getElementById('fieldTokenAddContactoNombre_c')
		var resNombre = encodeURIComponent(nodeFieldNombre.value)
		var resLink = revisarLink(document.getElementById('fieldTokenAddContactoLink_c').value)
		resLink = (resLink)	
		if(!resNombre){
			alert('Es preciso llenar los campos marcados (*)')
			return false
		}

		// Ocultar DIV de agregar contacto
		var nodeAgregar = document.getElementById('divTokenAddContacto_c')
		var nodeCargandoOld = document.getElementById('hiwebxCargando')
		var nodeCargando = nodeCargandoOld.cloneNode(true)
		nodeCargandoOld.parentNode.removeChild(nodeCargandoOld)
		nodeAgregar.parentNode.appendChild(nodeCargando)
		ocultarTokenAddContacto()
		nodeCargando.style.visibility='visible'
		nodeCargando.style.display='block'
		
		// Preparar la URL que se va a pedir
		var url="?accion=act_fotofolio&subaccion=sub_add_proyecto&nombre="+resNombre+"&link="+resLink	
		var yaExistia=1
		var idt=20

		miXhName = 'xhVar'+xhid // Empieza AJAX
		xh=self[miXhName] = GetXmlHttpObject()
		xh.onreadystatechange= new Function ("peticionAjaxRes_c('"+miXhName+"',"+yaExistia+","+idt+")")
		prepararUrl(url)
		xh.open("GET",url,true)
		xh.send(null) // TERMINA AJAX	
		return false
		
	}
	function peticionAjaxRes_c(strXh,yaExistia,idt) {
		xh=(self[strXh])
		if (xh.readyState==4 || xh.readyState=="complete"){ 
			var nodeCargando = document.getElementById('hiwebxCargando')
			nodeCargando.style.visibility='hidden'
			nodeCargando.style.display='none'
			self['mTcargando'+idt]=null
			respuestas = respuestaAjx(xh)
			if (respuestas['ok']!=1) {
				alert('SUCEDIO ERROR 2FH_c')
			}else{
				var id_cont = respuestas['id_cont']
				// Insertar nuevo contacto en arrContactos y reordenarlo
				arrContactos_c['id_'+id_cont] = new unContacto_c(respuestas['nombre'],id_cont,99);
				ordenarMiArrayContactos('c')
				var nodeBuscar = document.getElementById(fieldDestinoToken+'_tokenFieldBuscar')
				crearToken(respuestas['nombre'], respuestas['id_cont'])
				nodeBuscar.focus()
				return
			}
		}
	}	



	function peticionAjax_verItemCatalogo(id_cont){		
		// Preparar la URL que se va a pedir
		var url="?accion=act_fotofolio&id_cont="+id_cont+"&esAjax="+1
		var yaExistia=1
		var idt=20

		miXhName = 'xhVar'+xhid // Empieza AJAX
		xh=self[miXhName] = GetXmlHttpObject()
		xh.onreadystatechange= new Function ("peticionAjaxRes_verItemCatalogo('"+miXhName+"',"+yaExistia+","+idt+")")
		prepararUrl(url)
		xh.open("GET",url,true)
		xh.send(null) // TERMINA AJAX	
		return false
		
	}
	function peticionAjaxRes_verItemCatalogo(strXh,yaExistia,idt) {
		xh=(self[strXh])
		if (xh.readyState==4 || xh.readyState=="complete"){ 
			var nodeCargando = document.getElementById('hiwebxCargando')
			respuestas = respuestaAjx(xh)
			if (respuestas['ok']!=1) {
				alert('SUCEDIO ERROR 2FH_c')
			}else{
				document.getElementById('htmlDelItem').innerHTML = respuestas['res_htmlDelItem']
				document.getElementById('divStrTitulo').innerHTML = respuestas['res_strTitulo']
				document.getElementById('strVideo').innerHTML = respuestas['strVideo']

				// REACTIVAR LA GALERIA ROTATIVA 
				self['strArrImagenes'] = respuestas['strArrImagenes']
				self['strArrImagenesId'] = respuestas['strArrImagenesId']
				self['strArrPiedefoto'] = respuestas['strArrPiedefoto']
				galeriaRotativaInstancia++
				blendimageB(1,'prueba', galeriaRotativaInstancia)
				resetGaleriaRotativa()
				return
			}
		}
	}	
