﻿// JScript File

var NOMBRE ="ctl00_";
var NOMBRE2 ="ctl00_";

function ActualizarProvincias(val, idBuscador, idEntidadCliente, idioma){
	Eralia.CargarProvincias(idBuscador, idEntidadCliente, val, idioma, CallBackProvincias);			
}
		
function ActualizarLocalidades(cbA, idBuscador, idEntidadCliente, idioma){ 
    if (cbA.value>0)
    {
	    count = OptionsCounter(cbA);
	    cbZ = document.getElementById(NOMBRE + "LbArea");
	    if (count > 1){
		    cbZ.disabled = true; 
	    }else{	
		    cbZ.disabled = false;
		    cbT = document.getElementById(NOMBRE +"LbType");	
		    
		    Eralia.CargarLocalidades(idBuscador, idEntidadCliente, cbT.value, cbA.value, idioma, CallBackLocalidades);
	    }
	}	
	else
	    LLamarResultados();
		
}


function CallBackProvincias(response){
	cbA = document.getElementById(NOMBRE +"LbGrupoArea");
	cbZ = document.getElementById(NOMBRE +"LbArea");
	LimpiarCombo(cbZ);
	LimpiarCombo(cbA);
	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 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 + ",";
      
     }     
    }
}
		
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 + ",";
     }     
    }
    
    
}
