function clearForm (element)
{
    element.value = "";
    element.onfocus = null;
};

function toggle_comment_form(id){
    var cform = $('#comment_form_' + id);
    if(cform.hasClass('hidden')) {
        cform.prev().text('Stop replying to this comment');
        cform.slideDown();
    }
    else {
        cform.prev().text('Reply to this comment');
        cform.slideUp();
    }
    cform.toggleClass('hidden');
}

function pushOption (el, t, v, dsel, sel) {
    el.options[el.options.length] = new Option(t, v, dsel, sel);
};

function clearOptions (el, leavefirst) {
    var i = leavefirst ? 1 : 0;
    while (el.options.length > i) {
        el.options[el.options.length - 1] = null;
    }
};
		
// adding leftnav "on" class			
function flashnav_hilite(){
	var title = document.title;
	var titleshort = title.replace('AMIN Worldwide : ', '');
	$('#leftnav li a').each(function() {
   	if ($(this).attr('name') == titleshort){
			$(this).addClass('leftnav_on');
		}
  });
}
			
// http://erik.eae.net/archives/2005/06/05/17.53.19/
	// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach
		if (!Array.prototype.forEach) {
				Array.prototype.forEach = function (f, obj) {
					var l = this.length;	// must be fixed during loop... see docs
					for (var i = 0; i < l; i++) {
						f.call(obj, this[i], i, this);
					}
				};
			}
			
	
function updateCountries (val) {
	var country = $('#id_countrysearch').val(); 
	var sel = document.getElementById("id_countrysearch");

	clearOptions(sel, true);
	
	if (val != ''){
			$('#countrysearch').show('fast');			  			
			
			locations.forEach(function (el) {
           if (el.fields.globalarea == val) {
               pushOption (sel, el.fields.name, el.pk);
           }
       });

			$('#id_countrysearch').val(country);
			   
      if (val == "1" && country == "229"){
				$('#statesearch').show('fast');
			}
			else{
					$('#id_statesearch').attr('selectedIndex', '-1');
					$('#statesearch').hide('fast');					
			}
	} else {
      $('#countrysearch').hide('fast');			
     	$('#id_statesearch').attr('selectedIndex', '-1');
      $('#statesearch').hide('fast');			
   }	
	
};
						
				
$(document).ready(function(){ 

    $('a#all_members').click(function(e){
        $('tr.all').toggleClass('hide');
        $(this).hide('fast');
        $('a#less_members').show('fast');
        return false;
    });
		
    $('a#less_members').click(function(e){
        $('tr.all').toggleClass('hide');
        $(this).hide('fast');
        $('a#all_members').show('fast');
        return false;
    });

    $('a.all').click(function(e){
        $(this).siblings('select').children('option').attr('selected', 'selected');
        return false;
    });
		
    $('a.none').click(function(e){
        $(this).siblings('select').children('option').removeAttr('selected');
        return false;
    });


		
		
		$('#id_countrysearch').change(function() {
			var countryArray = $('#id_countrysearch').val();     
      if (countryArray == "229" ){
				$('#statesearch').show('fast');
			}
			else{
					var select_index = $('#id_statesearch').attr('selectedIndex', '-1');
					$('#statesearch').hide('fast');					
				}
		});  
		
		     
});
