/*
 * ajax/form/standard_forms.js - Standard validation and other AJAX functions. 
 */

/*
 * Validate form to subscribe to notification email list that is sent when
 * new animals are added.
 */
function validate_animal_notify_sub(formData, jqForm, options) {
    //console.log("called validate_animal_notify_sub");
    // 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 (e.g.): 
    // 
    // [ 
    //     { name:  username, value: valueOfUsernameInput }, 
    //     { name:  password, value: valueOfPasswordInput } 
    // ] 
 
     
    var form = jqForm[0]; 
    if (!form.fname.value) { 
        alert('Please enter a first name.'); 
        return false; 
    }
    if (!form.lname.value) { 
        alert('Please enter a last name.'); 
        return false; 
    }
    if (!form.email.value) {
        alert('Please enter an email address.');
        return false;
    }
    /*
    if (!form.location.value) {
        alert('Please select a store location.');
        return false;
    }
    */
    
    return true;
}

/*
 *
 */
function show_response_animal_notify_sub(data) {
    //console.log("called show_response_animal_notify_sub, is_success = " + data.is_success);
    if (data.is_success != 1) {
        if (data.reason) {
            $("#animal_notify_sub_user_message").text(data.reason);
        }
        return false;
    }
    else {
        var overlay_api = $("#animal_notify_sub_btn").data("overlay");
        overlay_api.close();
        alert("Thank you. Please click on the verification link in the confirmation message sent to your e-mail address.");
        return true;
    }
}

$(document).ready(function(){
    $("#animal_notify_sub_btn").overlay();
    var options = { 
        //target:        '#output1', // target element(s) to be updated with server response 
        beforeSubmit:  validate_animal_notify_sub,      // pre-submit callback 
        success:       show_response_animal_notify_sub, // post-submit callback 
        dataType:      'json'
        
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    };
    
    $('#animal_notify_sub_form').ajaxForm(options);

    //console.log("Finished setting up animal_notify_sub_btn and ajaxForm");    
});


