﻿// JScript File

var NOMBRE ="ctl00_ContentPlaceHolder1_";
var NOMBRE2 ="ctl00_";
function ActualizarGruposAreas(val, idBuscador, idEntidadCliente, idioma){
	Basic3.CargarGruposAreas(idBuscador, idEntidadCliente, val, idioma, CallBackGruposAreas);			
}
		
function ActualizarAreas(cbA, idBuscador, idioma){ 
	count = OptionsCounter(cbA);
	cbZ = document.getElementById(NOMBRE + "LbArea");
	if (count > 1){
		cbZ.disabled = true; 
	}else{
		cbZ.disabled = false;
		cbT = document.getElementById(NOMBRE + "LbType");		
		Basic3.CargarAreas(idBuscador, cbT.value, cbA.value, idioma, CallBackAreas);
	}			
}

function ActualizarProvincias(val, idBuscador, idEntidadCliente, idioma){
	Basic3.CargarProvincias(idBuscador, idEntidadCliente, val, idioma, CallBackProvincias);			
}
		
function ActualizarLocalidades(cbA, idBuscador, idEntidadCliente, idioma){ 
	count = OptionsCounter(cbA);
	cbZ = document.getElementById(NOMBRE + "LbArea");
	if (count > 1){
		cbZ.disabled = true; 
	}else{	
		cbZ.disabled = false;
		cbT = document.getElementById(NOMBRE +"LbType");	
		Basic3.CargarLocalidades(idBuscador, idEntidadCliente, cbT.value, cbA.value, idioma, CallBackLocalidades);
	}			
}

function ActualizarZonas(cbA, idBuscador, idEntidadCliente, idioma){ 
	count = OptionsCounter(cbA);
	cbZ = document.getElementById(NOMBRE +"LbZona");
	if (count > 1){
		cbZ.disabled = true; 
	}else{
		cbZ.disabled = false;
		cbT = document.getElementById(NOMBRE +"LbType");	
		Basic3.CargarZonas(idBuscador, idEntidadCliente, cbT.value, cbA.value, idioma, CallBackZonas);
	}			
}

function CallBackGruposAreas(response){
	cbA = document.getElementById(NOMBRE +"LbGrupoArea");
	cbZ = document.getElementById(NOMBRE +"LbArea");
	
	LimpiarCombo(cbZ);
	rows = response.value;
	if (rows != null){
		for (i = 0; i < rows.length; i++){
			cbA.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["rIdGrupoArea"]);
		}
	}	
}
		
function CallBackAreas(response){
	cbZ = document.getElementById(NOMBRE +"LbArea");
	LimpiarCombo(cbZ);
	rows = response.value;
	if (rows != null){		
		for (i = 0; i < rows.length; i++){
			cbZ.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["rIdArea"]);
		}
	}	
}

function CallBackProvincias(response){
	cbA = document.getElementById(NOMBRE +"LbGrupoArea");
	cbZ = document.getElementById(NOMBRE +"LbArea");
	cbB = document.getElementById(NOMBRE +"LbZona");	
	LimpiarCombo(cbZ);
	LimpiarCombo(cbA);
	LimpiarCombo(cbB);
	rows = response.value;
	if (rows != null){
		for (i = 0; i < rows.length; i++){
			cbA.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["idProvincia"]); //TODO:revisar los nombres de campos. Dependen del SP que manden
		}
	}	
}
		
function CallBackLocalidades(response){
	cbZ = document.getElementById(NOMBRE +"LbArea");
	cbB = document.getElementById(NOMBRE +"LbZona");	

	LimpiarCombo(cbZ);
	LimpiarCombo(cbB);
	rows = response.value;
	if (rows != null){		
		for (i = 0; i < rows.length; i++){
			cbZ.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["idLocalidad"]); //TODO:revisar los nombres de campos. Dependen del SP que manden
		}
	}	
}

function CallBackZonas(response){

	cbZ = document.getElementById(NOMBRE +"LbZona");
	LimpiarCombo(cbZ);
	rows = response.value;

	if (rows != null){		
		for (i = 0; i < rows.length; i++){
			cbZ.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["idZona"]); //TODO:revisar los nombres de campos. Dependen del SP que manden
		}
	}	
}

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);
}

function AreasZonasSeleccionadas(){
    document.getElementById("ctl00_validar").value = "false";
    cbA  = document.getElementById(NOMBRE +"LbGrupoArea");
    hdA1 = document.getElementById(NOMBRE2 +"HdidGrupoArea");
    hdA2 = document.getElementById(NOMBRE2 +"HdGrupoArea");
    hdA1.value = "";
    hdA2.value = "";


    for (i = 1; i < cbA.length; i++){
     if (cbA[i].selected) {
      document.getElementById("ctl00_validar").value = "true";
      hdA1.value += cbA[i].value + ",";
      hdA2.value += cbA[i].text + ",";
     }
    }
    cbZ = document.getElementById(NOMBRE +"LbArea");
    hdZ1 = document.getElementById(NOMBRE2 +"HdidAreas");
    hdZ2 = document.getElementById(NOMBRE2 +"HdAreas");
    hdZ1.value = "";
    hdZ2.value = "";
    for (i = 1; i < cbZ.length; i++){
     if (cbZ[i].selected) {
      document.getElementById("ctl00_validar").value = "true";
      hdZ1.value += cbZ[i].value + ",";
      hdZ2.value += cbZ[i].text + ",";
      
     }     
    }
    
    
    cbB = document.getElementById(NOMBRE +"LbZona");
    hdB1 = document.getElementById(NOMBRE2 +"HdidZonas");
    hdB2 = document.getElementById(NOMBRE2 +"HdZonas");
    hdB1.value = "";
    hdB2.value = "";
    for (i = 1; i < cbB.length; i++){
     if (cbB[i].selected) {
      document.getElementById("ctl00_validar").value = "true";
      hdB1.value += cbB[i].value + ",";
      hdB2.value += cbB[i].text + ",";
     }     
    }
    
}
		
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;
}
function Selecciones6(mensaje){
	ref = document.getElementById(NOMBRE +"TbRef");
	if (ref != null)
	{
		document.getElementById("ctl00_validar").value = "true";
		return true;
	}
	AreasZonasSeleccionadas6();
	if ((document.getElementById("ctl00_validar").value == "false")) 
	{
		alert(mensaje);
		return false;
	}
	else
		return true;
}

function AreasZonasSeleccionadas6(){
	document.getElementById("ctl00_validar").value = "false";
    
    hdA1 = document.getElementById(NOMBRE2 +"HdidGrupoArea");
    hdA2 = document.getElementById(NOMBRE2 +"HdGrupoArea");
    hdA1.value = "";
    hdA2.value = "";
        
    cbZ = document.getElementById(NOMBRE +"LbArea");
    hdZ1 = document.getElementById(NOMBRE2 +"HdidAreas");
    hdZ2 = document.getElementById(NOMBRE2 +"HdAreas");
    hdZ1.value = "";
    hdZ2.value = "";
    for (i = 1; i < cbZ.length; i++){
     if (cbZ[i].selected) {
      document.getElementById("ctl00_validar").value = "true";
      hdZ1.value += cbZ[i].value + ",";
      hdZ2.value += cbZ[i].text + ",";
     }     
    }
    
    
    cbB = document.getElementById(NOMBRE +"LbZona");
    hdB1 = document.getElementById(NOMBRE2 +"HdidZonas");
    hdB2 = document.getElementById(NOMBRE2 +"HdZonas");
    hdB1.value = "";
    hdB2.value = "";
    for (i = 1; i < cbB.length; i++){
     if (cbB[i].selected) {
      document.getElementById("ctl00_validar").value = "true";
      hdB1.value += cbB[i].value + ",";
      hdB2.value += cbB[i].text + ",";
     }     
    }
    
}
