﻿window.onload = function(){
    $("Search1").disabled = true;

    loadTherapies();

}

function onEnterSearchName(ev){
    if(ev == 13){
        return SearchByName();
    }
}

function loadTherapies(){
    $("cmbTherapies").value = '';
    $("cmbTherapies").disabled = true;
    $("cmbCountries").value = '';
    $("cmbCountries").disabled = true;
    $("cmbCities").value = '';
    $("cmbCities").disabled = true;
       
    $Ajax(basepath + "Controllers/Index.aspx?method=getAssignedTherapies", {
            cache: false,
            onfinish:function(result){ 
                loadDropDownComboInGroups("cmbTherapies",result,"Select / Seleccione");
                $("cmbTherapies").onchange = loadCountries;
                $("cmbTherapies").disabled = false; 
            }, 
            tipoRespuesta:$tipo.JSON});
}

function loadCountries(){
    $("cmbCountries").value = '';
    $("cmbCountries").disabled = true;
    $("cmbCities").value = '';
    $("cmbCities").disabled = true;  
       
    $Ajax(basepath + "Controllers/Locations/locations.aspx?method=getCountriesByTherapy", {
            cache: false,
            metodo: $metodo.POST,         
            parametros: {"TherapyId":$F('cmbTherapies')},
            onfinish:function(paises){ 
                loadDropDownCombo("cmbCountries",paises,"Select Country / Seleccione País");
                $("cmbCountries").onchange = loadCities;
                $("cmbCountries").disabled = false; 
            }, 
            tipoRespuesta:$tipo.JSON});
}

function loadCities(){
    $("cmbCities").value = '';
    $("cmbCities").disabled = true;    
       
    $Ajax(basepath + "Controllers/Locations/locations.aspx?method=getCitiesByTherapy", {
            cache: false,
            metodo: $metodo.POST,         
            parametros: {"TherapyId":$F('cmbTherapies'),"CountryId":$F("cmbCountries")},
            onfinish:function(ciudades){ 
                loadDropDownComboInGroups("cmbCities",ciudades,"Select City / Seleccione Ciudad");
                $("cmbCities").onchange = function(){$('Search1').disabled = ($F("cmbCities")=='');}
                $("cmbCities").disabled = false; 
            }, 
            tipoRespuesta:$tipo.JSON});
}

function SearchByCity(){
    $Ajax(basepath + "Controllers/Index.aspx?method=SearchByCity", {
            cache: false,
            metodo: $metodo.POST,         
            parametros: {"TherapyId":$F('cmbTherapies'),"CityId":$F("cmbCities")},
            onfinish:function(result){ window.location = result; }, 
            tipoRespuesta:$tipo.TEXTO});

}

function SearchByName(){
    if($F('txtSearchName')!="")
        $Ajax(basepath + "Controllers/Index.aspx?method=SearchByName", {
            cache: false,
            metodo: $metodo.POST,         
            parametros: {"Name":$F('txtSearchName').replace(/\'/g,'´')},
            onfinish:function(result){ window.location = result; }, 
            tipoRespuesta:$tipo.TEXTO});
}