$(document).ready(function(){
    // Hover nav bar 
    $('ul.nav>li').hover(function(){
        $(this).children('ul.submenu').removeClass("hidden");
        $(this).children('ul.submenu').addClass("hover");
    }, function(){
        $(this).children('ul.submenu').removeClass("hover");
        $(this).children('ul.submenu').addClass("hidden");
    });
    
    $("#tabs-home>ul").tabs();
    
    
});

function radiocheckbox(elt){
    // get all the checkboxes of the same name
    
    var checkboxes = $("input[name='" + elt.name + "']");
    
    checkboxes.each(function(e){
    
        if (elt.id != this.id && elt.checked) {
            this.checked = false;
        }
    })
    
}

function users_update(nb){
    var array_data = [];
    var str = "";
    var url = "";
	  if($("input[name^='users[" + nb + "]'][type='radio'][checked]").length == 0){alert("In order to save this user please select Active or Trial");return}
	  $("input[name^='users[" + nb + "]'][type='radio']").each(function(i){
		if(this.checked){
			array_data.unshift(this.name + "=" + this.value);
		}
	});
    $("input[name^='users[" + nb + "]'][type!='radio']").each(function(i){
        // We don't want unchecked checkbox
        if (this.type == 'checkbox' && !this.checked) {
            array_data.unshift(this.name + "=" + 0);
        }
        else {
            switch (this.type) {
                case 'checkbox':
                    if (this.checked) {
                        array_data.unshift(this.name + "=" + 1);
                    }
                    else {
                        array_data.unshift(this.name + "=" + 0);
                    }
                    break;
                    
                case 'radio':
                    break;
                    
                default:
                    {
                        array_data.unshift(this.name + "=" + this.value);
                        break;
                    }
            }
        }
    });
  
    
    str = array_data.join('&');
    url = $("#form_users").attr('action');
    $.ajax({
        type: "POST",
        url: url,
        data: str,
        success: function(msg){
            alert("User Saved" );
        }
    });
    
    return false;
    
}
