//*******************************************************************
//
// contact.js - v1.0 - 21/Aug/2007 18:54
// Part of the Decadence Site Management System
// (c) Copyright 2007 Michael Stenta (http://www.mstenta.net)
//
// Description:
//      Functions for displaying and sending contact information from
//      a form.
//
//###################################################################


// sendContactInfo()
// makes an AJAX call to contact.php to email the contact info
function sendContactInfo() {
    var contactName = $F('contact_name');
    var contactReturn = $F('contact_return');
    var contactSubject = $F('contact_subject');
    var contactBody = $F('contact_body');
    var visitorIp = $F('visitor_ip');
    
    // make sure all the fields were filled out. if not, alert the user.
    if(contactBody == '') {
        $('contact_form_msg').update("You didn't say anything.");
        return;
    }
    if(contactReturn == '') {
        $('contact_form_msg').update("You forgot to enter your email address or phone number.");
        return;
    }
    if(contactName == '') {
        $('contact_form_msg').update("You forgot to enter your name.");
        return;
    }
    if((contactName != '')&&(contactReturn != '')&&(contactBody != '')) {
        $('contact_form_msg').update("");
    }

    // when an Ajax request is in progress, display a notification
    Ajax.Responders.register({
        onCreate: function() {
            $('contact_form_msg').update("Sending message...");
        }, 
        onComplete: function() {
            $('contact_form_msg').update("");
        }
    });

    // send it to the php script
    new Ajax.Request('common/modules/contact/contact.php', {
        parameters: {
            contact_name: contactName,
            contact_return: contactReturn,
            contact_subject: contactSubject,
            contact_body: contactBody,
            visitor_ip: visitorIp
        },
        onSuccess: function(transport) { // transport should return 1 for success, 0 for failure of the mail sending attempt in contact.php
            if(transport.responseText == "1") {
                $('contact_form').update("Your message has been sent. Thanks!");
            }
            if(transport.responseText == "0") {
                $('contact_form').update('The message was unable to be sent (technical difficulties). Try emailing me directly at <a href="mailto:mike@mstenta.net">mike@mstenta.net</a>.');
            }
        },
        onFailure: function(transport) {
            $('contact_form_msg').update('The message was unable to be sent (technical difficulties). Try emailing me directly at <a href="mailto:mike@mstenta.net">mike@mstenta.net</a>.');
        }
    });
}