﻿/**
 * Stream.Contact Namespace
 * Used for the new Stream site contact us page.
 * Developed by Soho Pros., Inc.
 *
 * All private variables and functions begin with _.  They should not be referenced or called from outside of Stream.Default.
 * --------------------------------------------------------------------------------------------------------------------------
 */

Stream.Contact = {
    
    init: function() {
    
    },
    
    submitContactMessage: function() {
        if ((tbContactName.value == '') || (tbContactEmail.value == '') || (tbContactPhone.value == '') || (tbContactMessage.value == '')) {
            var msg = '';
            var numErrors = 0;
            if (tbContactName.value == '') {
                msg += 'Your name';
                numErrors++;
            }
            if (tbContactEmail.value == '') {
                msg += (numErrors > 0) ? ', e-mail' : 'Your e-mail';
                numErrors++;
            }
            if (tbContactPhone.value == '') {
                msg += (numErrors > 0) ? ', phone number' : 'Your e-mail';
                numErrors++;
            }
            if (tbContactMessage.value == '') {
                msg += (numErrors > 0) ? ' and a message' : 'A message';
                numErrors++
            }
            if (numErrors > 1) msg += ' are required.';
            else msg += ' is required.';
            
            $('contactUsError').innerHTML = msg;
        }
        else {
            tbContactName.disabled = true;
            tbContactEmail.disabled = true;
            tbContactPhone.disabled = true;
            tbContactMessage.disabled = true;
            btnContactSubmit.disabled = true;
            $('contactUsError').innerHTML = 'Sending...';
            wsContact.submitContactMessage(tbContactName.value, tbContactEmail.value, tbContactPhone.value, tbContactMessage.value, SC._contactSentSuccess, SC._contactSentFailure);
        }
        return false;
    },
    
    _contactSentSuccess: function(result) {
        if (result) {
            $('tblContact').hide();
            $('contactUsError').innerHTML = '';
            $('contactSubmitStatus').innerHTML = 'Your message has been sent.';
        }
    },
    
    _contactSentFailure: function(result) {
        $('contactSubmitStatus').innerHTML = 'An error occured.  Please try again or email us at <a href=\"mailto:info@streamrealty.com\">info@streamrealty.com</a>';
        tbContactName.disabled = false;
        tbContactEmail.disabled = false;
        tbContactPhone.disabled = false;
        tbContactMessage.disabled = false;
        btnContactSubmit.disabled = false;
    }

};

