function remove_favorites_charities(url, charity_id){
    var a = $('charity_row_'+charity_id).select('a')[0];
    a.innerHTML = 'Removing...';
    a.stopObserving('click');
    //Event.stopOserving(a, 'click');
    url = url + '&charity_id=' + charity_id;
    new Ajax.Request(
        url,{
            method: 'get',
            onComplete:function(){
            //Fin
            },
            onLoading:function(){
            //Inicion
            },
            onLoaded:function(){
            //Fin
            },
            onFailure: function(response){
                //Fin
                alert(response.responseText.toJSON);
            },
            onSuccess: function(response){
                //Fin
                var objJSon = response.responseText.evalJSON();
                if(objJSon.result){
                    if(!objJSon.not_logged || objJSon.not_logged != 1){
                        $('charity_row_'+objJSon.html.charity_header_selector.charity_id).remove();
                    }
                }else{
                    alert(objJSon.message);
                }
                if(objJSon.close_after_selection == 1){
                    Windows.closeAll();
                    window.location.reload(true);
                }
            }
        });
}
function favorites_charities(url, element){
    new Ajax.Request(
        url,{
            method: 'get',
            onComplete:function(){
            //Fin
            },
            onLoading:function(){
            //Inicion
            },
            onLoaded:function(){
            //Fin
            },
            onFailure: function(response){
                //Fin
                alert(response.responseText.toJSON);
            },
            onSuccess: function(response){
                //Fin
                var objJSon = response.responseText.evalJSON();
                if(objJSon.result){
                    update_charity_selected(objJSon.html.charity_header_selector.charity_name, objJSon.update_list, (objJSon.update_charity_name_in_order != null ? objJSon.update_charity_name_in_order : null ));
					/*
                    $(element).innerHTML= objJSon.html.label;
                    $(element).onclick = function(e){
                        favorites_charities(objJSon.html.url+'&isAjax=1', element);
                    }
					*/
                    if(objJSon.not_logged || objJSon.not_logged == 1){
                        if(objJSon.html.charity_header_selector.charity_id && $('charity_id_button_'+objJSon.html.charity_header_selector.charity_id)){
                            $('charity_id_button_'+objJSon.html.charity_header_selector.charity_id).style.visibility='hidden';
                        }
                        if(objJSon.html.charity_old.charity_id && $('charity_id_button_'+objJSon.html.charity_old.charity_id)){
                            $('charity_id_button_'+objJSon.html.charity_old.charity_id).style.visibility='visible';
                        }
                    }
                }else{
                    alert(objJSon.message);
                }
                if(objJSon.close_after_selection == 1){
                    Windows.closeAll();
                    window.location.reload(true);
                }
            }
        });
}
function default_charity(url, element){
    url = url + '&charity_id=' + element.value;
    new Ajax.Request(
        url,{
            method: 'get',
            onComplete:function(){
            //Fin
            },
            onLoading:function(){
            //Inicion
            },
            onLoaded:function(){
            //Fin
            },
            onFailure: function(response){
                //Fin
                alert(response.responseText.toJSON);
            },
            onSuccess: function(response){
                //Fin
                var objJSon = response.responseText.evalJSON();
                if(objJSon.result){
                    update_charity_selected(objJSon.html.charity_header_selector.charity_name, objJSon.update_list, (objJSon.update_charity_name_in_order != null ? objJSon.update_charity_name_in_order : null ));
                }else{
            // TODO: Add code to show an error message
            }
            }
        });
}
function update_charity_selected(charity_name, charity_update_list, update_checkout_charity_info){
    if(charity_name){
        $$('a[rel=#charity_name]').each(function(item){
            item.update(charity_name);
        });
		$$('div[rel=#charity_name]').each(function(item){
            item.update(charity_name);
        });
    }
    if(update_checkout_charity_info && $$('.selected-charity-info') && $$('.selected-charity-info')[0]){
        $$('.selected-charity-info')[0].update(update_checkout_charity_info);
    }
    if($('charities_selection') && charity_update_list && charity_update_list != null){
        reload_list_charities(charity_update_list);
    }
}
function reload_list_charities(charity_update_list){
    if(charity_update_list != null){
        $('charities_selection').innerHTML = charity_update_list;
    }else{
        var updater = new Ajax.Updater('charities_selection', '/customercharity/index/charitiesSelection', {
            method: 'get',
            onFailure: function(response){
                alert(response);
            }
        });
    }

}

// closed shipping method popup
jQuery(document).ready(function() {
  jQuery("input[name=estimate_method]").live("click", function() {
    if(jQuery('input[name=estimate_method]').is(':checked')){
      document.estimate_shipping_win.hide();
    }
  });
});
