//*******************************************************************
//
// 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 form_text = $('#contact_form :textarea').fieldValue();
    
    var contactName = form_text[0];
    var contactEmail = form_text[1];
    var contactPhone = form_text[2];
    var contactSubject = form_text[3];
    var contactMessage = form_text[4];

    // make sure all the fields were filled out. if not, alert the user.
    if((contactName == '')||(contactEmail == '')||(contactPhone == '')||(contactSubject == '')||(contactMessage == '')) {
        $('#contact_form_msg').text("All fields are required.");
    } else {
        
        // assemble the data object
        var formdata = {
            'contact_name' : contactName,
            'contact_email' : contactEmail,
            'contact_phone' : contactPhone,
            'contact_subject' : contactSubject,
            'contact_message' : contactMessage
        }
        
        // generic error message
        var error_msg = 'The message was unable to be sent via this form due to technical difficulties. Try emailing us directly at <a href="mailto:koss@ix.netcom.com">koss@ix.netcom.com</a>.';
        
        // 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);
            }
        });
        
    }
}
