﻿// Class
function PesquisarCatalogos(serverclass) {

	var listOpcionaisPesqCatalog = new Array();

	var Constructor = function() {
		// Implementação
	};

	var alertErros = function(res) {
		if (res.error != null) {
			alert(res.error);
			return true;
		} else { return false; }
	};

	this.HideOrShowPresqPreco = function(field) {
		if ($('bQuote').style.display == 'block') {
			$html(field, 'Pesquisa por preço');
			$('bQuote').style.display = 'none';
		} else {
			$html(field, 'Fechar pesquisa por preço');
			$('bQuote').style.display = 'block';
		}
	};

	this.checkOpcional = function(codOpcional, add) {
		if (add) listOpcionaisPesqCatalog.push(codOpcional);
		else listOpcionaisPesqCatalog.remove(listOpcionaisPesqCatalog.indexOf(codOpcional));
	};

	this.GetModelos = function(callCombo, loadCombo) {
	    callCombo.disabled = 'disabled';
	    var CodMarca = parseInt(callCombo.value);
		if (CodMarca > 0)
			serverclass.getModelos(CodMarca, function(res) { renderCombo(res, loadCombo); });
		else
			clearCombo(loadCombo);
		callCombo.disabled = '';
	};

	this.GetVersoes = function(callCombo, loadCombo) {
	    callCombo.disabled = 'disabled';
	    var CodModelo = parseInt(callCombo.value);
		if (CodModelo > 0)
			serverclass.getVersoes(CodModelo, function(res) { renderCombo(res, loadCombo); });
		else
			clearCombo(loadCombo);
		callCombo.disabled = '';
	};

	var clearCombo = function(combo) {
		while ($(combo).childNodes.length > 0) $(combo).removeChild($(combo).firstChild);
	};

	// Renders
	var renderCombo = function(res, combo) {
		if (alertErros(res)) return;
		//Combo
		clearCombo(combo);
		$(combo).options[$(combo).options.length] = new Option('- Selecione -', '0');
		for (var i = 0; i < res.value.rows.length; i++)
			$(combo).options[$(combo).options.length] = new Option(res.value.rows[i].Nome, res.value.rows[i].CodItem);
	};

	this.comparar = function() {
		var query = '';
		if (($('sltVersaoLine1').value != null) && (parseInt($('sltVersaoLine1').value) > 0)) {
			query = $('sltVersaoLine1').value + ',';
		}

		if (($('sltVersaoLine2').value != null) && (parseInt($('sltVersaoLine2').value) > 0)) {
			query += $('sltVersaoLine2').value + ',';
		}
		
		if (($('sltVersaoLine3').value != null) && (parseInt($('sltVersaoLine3').value) > 0)) {
			query += $('sltVersaoLine3').value + ',';
		}

		if (($('sltVersaoLine4').value != null) && (parseInt($('sltVersaoLine4').value) > 0))
			query += $('sltVersaoLine4').value + ',';

		if (query.length > 0) {
		    query = query.substring(0, query.length - 1)
			if(query.split(',').length > 1)
			    window.location = 'CompararVeiculos.aspx?v=' + query;
			else
			    alert('Selecione no mínimo duas versões para serem comparadas');
		} else
			alert('Preencha corretamente os campos para efetuar a comparação');
	};

	this.buscarCatalogos = function() {
		if (($('sltPrecoMaior').selectedIndex == 0) && ($('sltPrecoMenor').selectedIndex == 0))
			return alert('Selecione as faixas de valores');
		if ($('sltPrecoMaior').selectedIndex > $('sltPrecoMenor').selectedIndex) {
			var query = 'ResultadosBuscaCatalogo.aspx?PrecoDe=' + $('sltPrecoMenor').options[$('sltPrecoMenor').selectedIndex].text.substring(3).split('.')[0].replace(',', '.');
			query += '&PrecoAte=' + $('sltPrecoMaior').options[$('sltPrecoMaior').selectedIndex].text.substring(3).split('.')[0].replace(',', '.');
			var opcionais = '';
			if (listOpcionaisPesqCatalog.length > 0) {
				for (var x = 0; x < listOpcionaisPesqCatalog.length; x++) {
					if (opcionais != '')
						opcionais += ',';
					opcionais += listOpcionaisPesqCatalog[x];
				}
			}
			if (opcionais != '')
				query += '&op=' + opcionais;
			window.location = query;
		} else {
			alert('Selecione as faixas de valores corretamente');
		}
	};
	
	Constructor();
};

// Load
var oPesquisarCatalogos;
$load(function() {
	oPesquisarCatalogos = new PesquisarCatalogos(UI_PesquisarCatalagos);
});