/*
categorias.js
2007
Daniel Ardila

Combos recursivos para cargar categorías y subcategorías
--------------------------------------------------------- 
Basado en http://www.captain.at/howto-ajax-form-post-get.php
dardila@undermedia.com.ec
http://www.undermedia.com.ec/
--------------------------------------------------------- */

   var http_request = false;
   function makeRequest_Cate(url) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('No se pudo crear la instancia XMLHTTP');
         return false;
      }
      http_request.onreadystatechange = alertContentsCat;
      http_request.open('GET', url, true);
      http_request.send(null);
   }

   function alertContentsCat() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
			
            result = http_request.responseText;
			
			var update = new Array();			
			if(result.indexOf('|') != -1) {
				update = result.split('|');
				changeTextCat(update[0], update[1]);
			}       
         } else {
            alert('Hubo un problema en ejecutar la petición.');
         }
      }
   }

	function sndReqCat(categoria) {
		
		var thediv = document.getElementById('cate'); // the div
		
		if(categoria == 0){
			thediv.innerHTML = '';
			return false;
		}
		
		// switch div with a loading div
		thediv.innerHTML = '<span class="loading"><strong>Cargando...</strong><img src="working.gif" /></span>';
		
		makeRequest_Cate ('ajax.php?c='+categoria+'&n=1');
	}
	
	function changeTextCat( div2show, text) {
		// Detect Browser
	
		id = parseInt(div2show);
		var IE = (document.all) ? 1 : 0;
		var DOM = 0; 
		if (parseInt(navigator.appVersion) >=5) {DOM=1};
	
		// Grab the content from the requested "div" and show it in the "container"
		if (DOM) {
			var viewer = document.getElementById('cate');
			//alert(viewer.innerHTML );
			viewer.innerHTML = text;		
		}  else if(IE) {
			document.all['cate'].innerHTML = text;
		}
	}


	function sndReqCat_rec(categoria, nivel) {
		
		var thediv = document.getElementById('cate_'+nivel); // the div
		if(categoria == 0){
			thediv.innerHTML = '';
			return false;
		}
		
		nivel++;
		
		// switch UL with a loading div
		thediv.innerHTML = '<span class="loading"><strong>Cargando...</strong><img src="working.gif" /></span>';
		
		makeRequest_rec('ajax.php?c='+categoria+'&n='+nivel);
	}

   function makeRequest_rec(url) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('No se pudo crear la instancia XMLHTTP');
         return false;
      }
      http_request.onreadystatechange = alertContents_rec;
      http_request.open('GET', url, true);
      http_request.send(null);
   }


   function alertContents_rec() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
			
            result = http_request.responseText;
			
			var update = new Array();			
			if(result.indexOf('|') != -1) {
				update = result.split('|');
				changeTextCat_rec(update[0], update[1],update[2]);
			}       
         } else {
            alert('Hubo un problema en ejecutar la petición.');
         }
      }
   }
	
	function changeTextCat_rec(div2show, text,nivel) {
		n=nivel-1;
	
		// Detect Browser
		id = parseInt(div2show);
		var IE = (document.all) ? 1 : 0;
		var DOM = 0; 
		if (parseInt(navigator.appVersion) >=5) {DOM=1};
	
		// Grab the content from the requested "div" and show it in the "container"
		if (DOM) {
			var viewer = document.getElementById('cate_'+n);
			viewer.innerHTML = text;		
		}  else if(IE) {
			document.all['cate_'+n].innerHTML = text;
		}
	}