var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
   xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	xmlhttp = new XMLHttpRequest();
}

var lastshowed = '';
var catsList = 'cats';
var areasList = 'areas';
var areasetted = 0;
var prodsList = 'prods';
var systemList = 'systems';

function showCats() {

	if (document.getElementById( areasList )) {
		//
		// Código para la página de edicion
		if (document.getElementById( 'idarea' ) && (areasetted == 0)) {
			areasetted = 1;
			for (ct=0;ct<document.getElementById( areasList ).options.length;ct++) {
				if (document.getElementById( areasList ).options[ct].value == document.getElementById( 'idarea' ).value) {
					document.getElementById( areasList ).selectedIndex = ct;
				}
			}
		}

		// Fin del código para la página de edicion
		//
		id = document.getElementById( areasList ).options[document.getElementById( areasList ).selectedIndex].value;
		xmlhttp.open("GET", "/code/getcats.php?id=" + id,true);
		xmlhttp.onreadystatechange = function() {
	
			if (xmlhttp.readyState == 4) {
				opts = xmlhttp.responseText.split('|');
				emptyList( catsList );
				document.getElementById( catsList ).options[0] = new Option( 'Seleccione una opción', '-99' );
				for (ct=0;ct<opts.length;ct++) {
					opt = opts[ct].split('~');
					document.getElementById( catsList ).options[ct+1] = new Option( opt[0], opt[1] );
					//
					// Código para la página de edicion
					if (document.getElementById( 'idcat' )) {
						if (document.getElementById( 'idcat' ).value == opt[1]) {
							document.getElementById( catsList ).selectedIndex = ct+1;
						}
					}
					// Fin del código para la página de edicion
					//
				}
				if (document.getElementById( prodsList )) {
					document.getElementById( prodsList ).innerHTML = '';
				}
			}
		}
		xmlhttp.send(null);
	}
}

function showProds() {
	id = document.getElementById( catsList ).options[document.getElementById( catsList ).selectedIndex].value;
	xmlhttp.open("GET", "/code/getprods.php?id=" + id,true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			document.getElementById( prodsList ).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

function showSystems() {
	id = document.getElementById( catsList ).options[document.getElementById( catsList ).selectedIndex].value;
	xmlhttp.open("GET", "/code/getsystems.php?id=" + id,true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			document.getElementById( systemList ).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

function emptyList( listName ){
	var lst = document.getElementById(listName);
	lst.options.length = 0;
// 	lst.onchange = null;
	lst.disabled = false;
}

window.onload = showCats;