$(function(){

    $('#minty_menu li').click(function(){
    
        if (!$(this).hasClass('locked')) {
            if ($(this).hasClass('user_selected')) {
                $(this).addClass("user_not_selected");
                $(this).removeClass("user_selected");
            }
            else 
                if ($(this).hasClass('user_not_selected')) {
                    $(this).removeClass("user_not_selected");
                    $(this).addClass("user_selected");
                }
        }
        
    });
    $('#minty_menu li').corner('5px');
    
    $('#submit_link').click(function(){
        $('#email_dialog').dialog('open');
        return false;
    });
    
    dialogButtons = {
        "Get Fresh": function(){
            $("#minty_menu li.user_selected").each(function(){
                id = $(this).attr('id');
                $('#the_form').append('<input type="hidden" name="feature[]" value="' + id + '" />');
            });
            
            
            $("#the_form").submit();
        }
    };
	
    dialogOptions = {
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        title: 'Your Details',
        buttons: dialogButtons
    };
    
    $('#email_dialog').dialog(dialogOptions);
    
    formOptions = {
		target: '#main_left',
		url: 'submit.php',
        beforeSubmit: validate, 
		success: function(){
			$('#email_dialog').dialog('close');
		}
	};   
    
    $('#the_form').ajaxForm(formOptions);
    
    function validate(formData, jqForm, options) { 
        // formData is an array of objects representing the name and value of each field 
        // that will be sent to the server;  it takes the following form: 
        // 
        // [ 
        //     { name:  username, value: valueOfUsernameInput }, 
        //     { name:  password, value: valueOfPasswordInput } 
        // ] 
        // 
        // To validate, we can examine the contents of this array to see if the 
        // username and password fields have values.  If either value evaluates 
        // to false then we return false from this method. 
     
        for (var i=0; i < formData.length; i++) { 
            if (!formData[i].value) {
                $('#error').text(""); 
                if (i==0) {
                    $('#error').append("We can't help you if we don't know your name. <b>Please enter your name in the box provided.</b>");  
                }
                if (i==1) {
                    $('#error').append("There isn't much point in contacting us if we can't contact you back. <b>Please put a valid email in the box provided.</b>");   
                }
                $('#error').show();
                return false;  
            } 
        } 
        return true; 
    }
});

