//*******************************************************************
//
// contact.js - v1.1 - 8/Apr/2009 1:27
// Part of the Decadence Site Management System
// (c) Copyright 2009 Michael Stenta (http://www.mstenta.net)
//
// Description:
//      Functions for displaying and sending contact information from
//      a form.
//
// Version:
//      1.1 - JQuery port.
//      1.0 - First version, requires Prototype + Scriptaculous.
//
//###################################################################


// sendContactInfo()
// makes an AJAX call to contact.php to email the contact info
function sendContactInfo() {
    
    var contactFirst = $('#contact_first').val();
    var contactLast = $('#contact_last').val();
    var contactEmail = $('#contact_email').val();
    var contactMessage = $('#contact_message').val();

    // make sure all the fields were filled out. if not, alert the user.
    if((contactFirst == '')||(contactLast == '')||(contactEmail == '')||(contactMessage == '')) {
        $('#contact_form_msg').text("All fields are required.");
    } else {
        
        // assemble the data object
        var formdata = {
            'contact_first' : contactFirst,
            'contact_last' : contactLast,
            'contact_email' : contactEmail,
            'contact_message' : contactMessage
        }
        
        // generic error message
        var error_msg = 'The message was unable to be sent due to technical difficulties.';
        
        // send it to the php script
        $.ajax({
            type: 'POST',
            url: '/common/modules/contact/contact.php',
            data: formdata,
            ajaxStart: function() { // when an Ajax request is in progress, display a notification
                $('#contact_form_msg').text("Sending message...");
            },
            complete: function() {
                $('#contact_form_msg').text("");
            },
            success: function(statusText, responseText) { // transport should return 1 for success, 0 for failure of the mail sending attempt in contact.php
                if(statusText == "1") {
                    $('#contact_form').text('Your message has been sent. Thanks!');
                }
                if(statusText == "0") {
                    $('#contact_form').text(error_msg);
                }
            },
            error: function() {
                $('#contact_form').text(error_msg);
            }
        });
        
    }
}
