﻿// Archivo JScript
var NOMBRE ="ctl00_ContentPlaceHolder1_";
function ActualizarPaises(val, idBuscador, idEntidadCliente, idioma){
	Index.CargarPaises(idBuscador, idEntidadCliente, val, idioma, CallBackPaises);			
}
function ActualizarProvincias(val, idBuscador, idEntidadCliente, idioma){
    idProducto = document.getElementById("LbType").value;
	Index.CargarProvincias(val,idBuscador, idEntidadCliente, idProducto, idioma, CallBackProvincias);			
}
function ActualizarLocalidades(val, idBuscador, idEntidadCliente, idioma){
    idProducto = document.getElementById("LbType").value;
	Index.CargarLocalidades(val,idBuscador, idEntidadCliente, idProducto, idioma, CallBackLocalidades);			
}
function ActualizarZonas(cbCiudad, idBuscador, idEntidadCliente, idioma){ 
    NOMBRE ="";
	count = OptionsCounter(cbCiudad);
	cbZona = document.getElementById(NOMBRE +"LbZona");
	if (count > 1){
		cbZona.disabled = true; 
	}else{
		cbZona.disabled = false;
		idProducto = document.getElementById("LbType").value;
		idProvincia = document.getElementById("LbProvincia").value;
		idLocalidad = cbCiudad.value;
		Index.CargarZonas(idProvincia,idLocalidad,idBuscador, idEntidadCliente, idProducto, idioma, CallBackZonas);
	}			
}
function CallBackPaises(response){
    NOMBRE ="";
    cbPais = document.getElementById(NOMBRE +"LbPais");
	cbPro = document.getElementById(NOMBRE +"LbProvincia");
	cbCiu = document.getElementById(NOMBRE +"LbCiudad");
	cbZona = document.getElementById(NOMBRE +"LbZona");	
	LimpiarCombo(cbPais);
	LimpiarCombo(cbPro);
	LimpiarCombo(cbCiu);
	LimpiarCombo(cbZona);
	rows = response.value;
	if (rows != null){
		for (i = 0; i < rows.length; i++){
			cbPais.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["idPais"]); //TODO:revisar los nombres de campos. Dependen del SP que manden
		}
	}	
}

function CallBackProvincias(response){
    NOMBRE ="";
	cbPro = document.getElementById(NOMBRE +"LbProvincia");
	cbCiu = document.getElementById(NOMBRE +"LbCiudad");
	cbZona = document.getElementById(NOMBRE +"LbZona");	
	LimpiarCombo(cbPro);
	LimpiarCombo(cbCiu);
	LimpiarCombo(cbZona);
	rows = response.value;
	if (rows != null){
		for (i = 0; i < rows.length; i++){
			cbPro.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["idProvincia"]); //TODO:revisar los nombres de campos. Dependen del SP que manden
		}
		cbPro.options[0] = new Option(document.getElementById("SeleccioneOpcion").value, "-1"); 
	}	
}
function CallBackLocalidades(response){
    NOMBRE ="";
	cbCiu = document.getElementById(NOMBRE +"LbCiudad");
	cbZona = document.getElementById(NOMBRE +"LbZona");	
	LimpiarCombo(cbCiu);
	LimpiarCombo(cbZona);
	rows = response.value;
	if (rows != null){
		for (i = 0; i < rows.length; i++){
			cbCiu.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["idLocalidad"]); //TODO:revisar los nombres de campos. Dependen del SP que manden
		}
		cbCiu.options[0] = new Option(document.getElementById("SeleccioneOpcion").value, "-1"); 
	}	
    cbCiu.options[0].selected = true;
}
function CallBackZonas(response){

	cbZona = document.getElementById(NOMBRE +"LbZona");
	LimpiarCombo(cbZona);
	rows = response.value;

	if (rows != null){		
		for (i = 0; i < rows.length; i++){
			cbZona.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["idZona"]); //TODO:revisar los nombres de campos. Dependen del SP que manden
		}
		cbZona.options[0] = new Option(document.getElementById("SeleccioneOpcion").value, "-1"); 
	}	
	cbZona.options[0].selected = true;
}
function Selecciones(mensaje){
	/*ref = document.getElementById(NOMBRE +"TbRef");
	if (ref != null)
		{
			if (ref.value != "")
				return true;
		}

	AreasZonasSeleccionadas();
	if ((document.getElementById("ctl00_validar").value == "false")) 
	{
		alert(mensaje);
		return false;
	}
	else
		return true;*/
    return true		;
}
// Funciones genericas
function OptionsCounter(cbA){	
	count = 0;
	for (i = 0; i < cbA.length; i++){
			if (cbA[i].selected) {
				count++;
			}
		}
	return count;
}
function LimpiarCombo(combo){
	for(i=combo.options.length;i>1;i--)
		combo.remove(1);
}