/************************************************************************************************************
Ajax chained select
Copyright (C) 2006  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com


************************************************************************************************************/	
var ajax = new sack();


function getMarcaList(sel)
{
	if (document.getElementById('marca') != 'NL')
	{
		//var TVID = sel.options[sel.selectedIndex].value;
		var TVID = 'autos';
		document.getElementById('marca').options.length = 0;	// Empty Modelo select box
		document.getElementById('modelo').options.length = 1;	// Empty Modelo select box
		document.getElementById('modelo').disabled = true;
		document.getElementById('ano').options.length = 1;		// Empty Ao select box
		document.getElementById('ano').disabled = true;
		document.getElementById('valor').disabled = true;
		document.getElementById('valor').options.length = 1;
	
		if(TVID.length>0)
		{
			ajax.requestFile = 'obtieneDatos.asp?TVID='+TVID;	// Specifying which file to get
			ajax.onCompletion = createMarcas;	// Specify function that will be executed after file has been found
			ajax.runAJAX();		// Execute AJAX function
		}
	}else{
		window.location = "marca-nolistada.asp"
	}
}

function getModeloList(sel)
{	
	var MarcaID = sel.options[sel.selectedIndex].value;
	if(sel.options[sel.selectedIndex].value == 'NL'){  //Marca no listada
		doLayer(1,1);   //oculta combo y muestra caja
		doLayer(2,1);   //oculta combo y muestra caja
		document.getElementById('ano').disabled = false; //activado

		// llenamos combo de a&nacute;o, es como llamar a getAnoList()
		document.getElementById('ano').options.length = 0;	// Empty Ao select box
		document.getElementById('ano').disabled = false;
		document.getElementById('valor').options.length = 1;
		document.getElementById('valor').disabled = true;
		
		ajax.requestFile = 'obtieneDatos.asp?ModeloID=NL';	
		ajax.onCompletion = createAnos;	
		ajax.runAJAX();		
	}else{	
		document.getElementById('modelo').options.length = 0;	// Empty Modelo select box
		document.getElementById('modelo').disabled = false;
		document.getElementById('ano').options.length = 1;	// Empty Ao select box
		document.getElementById('ano').disabled = true;
		document.getElementById('valor').options.length = 1;
		document.getElementById('valor').disabled = true;

		if(MarcaID > 0){
			ajax.requestFile = 'obtieneDatos.asp?MarcaID='+MarcaID;	// Specifying which file to get
			ajax.onCompletion = createModelos;	// Specify function that will be executed after file has been found
			ajax.runAJAX();		// Execute AJAX function
		}
	}
}

function getAnoList(sel)
{
	var ModeloID = sel.options[sel.selectedIndex].value;
	if(sel.options[sel.selectedIndex].value == 'NL'){ //modelo no existente
		doLayer(2,1);   //oculta combo y muestra caja
		//desctivando combo valor
		document.getElementById('valor').options.length = 0;
		document.getElementById('valor').disabled = true;

		document.getElementById('ano').options.length = 0;
		document.getElementById('ano').disabled = false;
		document.getElementById('valor').options.length = 1;

		ajax.requestFile = 'obtieneDatos.asp?ModeloID=NL';	
		ajax.onCompletion = createAnos;	
		ajax.runAJAX();		
	
	}else{
		document.getElementById('ano').options.length = 0;	// Empty Ao select box
		document.getElementById('ano').disabled = false;
		if(ModeloID.length>0){
			ajax.requestFile = 'obtieneDatos.asp?ModeloID='+ModeloID;	// Specifying which file to get
			ajax.onCompletion = createAnos;	// Specify function that will be executed after file has been found
			ajax.runAJAX();		// Execute AJAX function
		}
	}
}

function getVersionList(sel)
{
	var AnoID = sel.options[sel.selectedIndex].value;
	//document.getElementById('ano').options.length = 0;	// Empty city select box
	//document.getElementById('version').disabled = false;
	if(AnoID.length>0){
		ajax.requestFile = 'obtieneDatos.asp?AnoID='+AnoID;	// Specifying which file to get
		ajax.onCompletion = createVersion	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}


function getValorList(sel)
{
	var ValorID = sel.options[sel.selectedIndex].value;

	if(ValorID.length>0){ //selecciono algun a&nacute;o	
		document.getElementById('valor').options.length = 0;
		document.getElementById('valor').disabled = false;

		ajax.requestFile = 'obtieneDatos.asp?ValorID=all';	// Specifying which file to get
		ajax.onCompletion = createValor;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}else{  // no hubo seleccion
		document.getElementById('valor').options.length = 0;
		document.getElementById('valor').disabled = true;	
	}
}

function getValortList(sel)
{
	if (sel == "1") {
	//var ValorID = sel.options[sel.selectedIndex].value;
	//document.getElementById('ano').options.length = 0;	// Empty city select box
	//document.getElementById('version').disabled = false;
	document.getElementById('valort').options.length = 0;
	document.getElementById('valort').disabled = false;
//	if(ValorID.length>0){
		ajax.requestFile = 'obtieneDatos.asp?ValorID=all';	// Specifying which file to get
		ajax.onCompletion = createValort;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
//	}
	}else{
		document.getElementById('valort').options.length = 0;
		document.getElementById('valort').disabled = true;
	}
}


function createMarcas()
{
	var obj = document.getElementById('marca');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function createModelos()
{
	var obj = document.getElementById('modelo');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function createAnos()
{
	var obj = document.getElementById('ano');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function createValor()
{
	var obj = document.getElementById('valor');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function createValort()
{
	var obj = document.getElementById('valort');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

//function createVersion()
//{
//	var obj = document.getElementById('version');
//	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
//}
		
function Desahilitacombos(){
	document.getElementById('modelo').disabled = true;
	document.getElementById('ano').disabled = true;
	document.getElementById('valor').disabled = true;
	//document.getElementById('tCobertura').disabled = true;	
	//document.getElementById('tCobertura').disabled = true;	
	
	hidelayer('layerMarca2');
	hidelayer('layerModelo2');
	getMarcaList('TVID=autos')
}
