
function initForm() {
    var attendeeCategory = getRadioValue('attendee_category');
    var hearAboutUs = document.getElementById('hear_about_us').value;
    var sponsorship = document.getElementById('sponsorship').checked;

    updateAttendeeFields(attendeeCategory);
    updateHearAboutUs(hearAboutUs);
    updateSponsorshipFields(sponsorship);
}

function getRadioValue(radioName) {
    for(var i = 0; i < document.register_form[radioName].length; i++) {
        if (document.register_form[radioName][i].checked == true) {
            return document.register_form[radioName][i].value;
        }
    }
    return '';
}

function onSelectAttendeeCategory(categoryType) {
    updateAttendeeFields(categoryType);
}

function onSelectSponsorship(sponsorshipInput) {
    updateSponsorshipFields(sponsorshipInput.checked);
}

function onSelectHearAboutUs(value) {
    updateHearAboutUs(value);
}

function updateHearAboutUs(value) {
    var hearAboutUsOther = document.getElementById('hear_about_us_other_span');

    if (value == 'Other') {
        hearAboutUsOther.style.visibility = '';
    } else {
        hearAboutUsOther.style.visibility = 'hidden';
    }
}

function updateAttendeeFields(categoryType) {
    var organizationLabel = document.getElementById('organization_label');
    var titleOrMajorLabel = document.getElementById('title_or_major_label');
    var graduationYearRow = document.getElementById('graduation_year_row');

    if (categoryType == 'Student/Academic') {
        organizationLabel.innerHTML = 'University';
        titleOrMajorLabel.innerHTML = 'Major';
        graduationYearRow.style.display = '';
    } else {
        organizationLabel.innerHTML = 'Company';
        titleOrMajorLabel.innerHTML = 'Job Title';
        graduationYearRow.style.display = 'none';
    }
}

function updateSponsorshipFields(sponsorship) {
    var sponsorshipDirections = document.getElementById('sponsorship_directions');
    var sponsorNameRow = document.getElementById('sponsor_name_row');
    var sponsorNameSpacerRow = document.getElementById('sponsor_name_spacer_row');
    var sponsorPhoneRow = document.getElementById('sponsor_phone_row');
    var sponsorPhoneSpacerRow = document.getElementById('sponsor_phone_spacer_row');
    var sponsorEmailRow = document.getElementById('sponsor_email_row');

    if (sponsorship) {
        sponsorshipDirections.style.display = '';
        sponsorNameRow.style.display = '';
        sponsorNameSpacerRow.style.display = '';
        sponsorPhoneRow.style.display = '';  
        sponsorPhoneSpacerRow.style.display = '';
        sponsorEmailRow.style.display = '';
    } else {
        sponsorshipDirections.style.display = 'none';
        sponsorNameRow.style.display = 'none';
        sponsorNameSpacerRow.style.display = 'none';
        sponsorPhoneRow.style.display = 'none';  
        sponsorPhoneSpacerRow.style.display = 'none';
        sponsorEmailRow.style.display = 'none';
    }
}

function cancelSubmit() {
    return false;
}

var FormSubmitter = {

    submit: function(formID, url, callback) {
        var fields = this._marshalFields(formID);
        var result = this._validateFields(fields);

        if (result.isValid) {
            var request = '';
            for(var i = 0; i < fields.count(); i++) {
                var fieldName = fields.keyAt(i);
                var fieldValue = escape(fields.itemAt(i));

                if (i > 0) { request += '&'; }
                request += fieldName + '=' + fieldValue;
            }

            if (!this.ajaxClient) {
                this.ajaxClient = new Ajax.Client(url);
            }
            this.ajaxClient.getRequestText(request, callback, null);

            var conferenceFee = parseInt(fields.item('conference_fee'));

            switch(conferenceFee) {
                case 50:
                    $('register_for').innerHTML = '$50.00 - Luncheon (September 13, 2007(September 13, 2007 ­ 12:20pm to 1:40pm)';
                    $('buyButtonCart').value = 'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCjxjaGVja291dC1zaG9wcGluZy1jYXJ0IHhtbG5zPSJodHRwOi8vY2hlY2tvdXQuZ29vZ2xlLmNvbS9zY2hlbWEvMiI+DQogIDxzaG9wcGluZy1jYXJ0Pg0KICAgIDxpdGVtcz4NCiAgICAgIDxpdGVtPg0KICAgICAgICA8aXRlbS1uYW1lPkx1bmNoZW9uIChTZXB0ZW1iZXIgMTMsIDIwMDcoU2VwdGVtYmVyIDEzLCAyMDA3ID8gMTI6MjBwbSB0byAxOjQwcG0pPC9pdGVtLW5hbWU+DQogICAgICAgIDxxdWFudGl0eT4xPC9xdWFudGl0eT4NCiAgICAgICAgPHVuaXQtcHJpY2UgY3VycmVuY3k9IlVTRCI+NTAuMDwvdW5pdC1wcmljZT4NCiAgICAgICAgPGl0ZW0tZGVzY3JpcHRpb24+QmVpbmcgVWdseSwgQm9ueSwgYW5kIEJvc3N5ISBAIEpBTU4gQ2VudHJhbCBIYWxsLiBBbiBleHBsYW5hdGlvbiBvZiB0aGUgcGhlbm9tZW5vbiBvZiBVZ2x5ZG9sbHMgYW5kIGJleW9uZCwgYnkgRGF2aWQgSG9ydmF0aCwgY28tY3JlYXRvciBvZiBVZ2x5ZG9sbHMgYW5kIG1hbnkgb3RoZXIgcHJvcGVydGllcyBpbmNsdWRpbmcgTGl0dGxlIEJvbnkgYW5kIEJvc3N5IEJlYXIuPC9pdGVtLWRlc2NyaXB0aW9uPg0KICAgICAgPC9pdGVtPg0KICAgIDwvaXRlbXM+DQogIDwvc2hvcHBpbmctY2FydD4NCiAgPGNoZWNrb3V0LWZsb3ctc3VwcG9ydD4NCiAgICA8bWVyY2hhbnQtY2hlY2tvdXQtZmxvdy1zdXBwb3J0IC8+DQogIDwvY2hlY2tvdXQtZmxvdy1zdXBwb3J0Pg0KPC9jaGVja291dC1zaG9wcGluZy1jYXJ0Pg0KDQo=//separator//d5aZmv1Dn6ptwfw9qpBJPlqFg3Y=';
                    break;
                case 459:
                    $('register_for').innerHTML = '$459.00 - Full Conference (September 13, 2007)';
                    $('buyButtonCart').value = 'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCjxjaGVja291dC1zaG9wcGluZy1jYXJ0IHhtbG5zPSJodHRwOi8vY2hlY2tvdXQuZ29vZ2xlLmNvbS9zY2hlbWEvMiI+DQogIDxzaG9wcGluZy1jYXJ0Pg0KICAgIDxpdGVtcz4NCiAgICAgIDxpdGVtPg0KICAgICAgICA8aXRlbS1uYW1lPkZ1bGwgQ29uZmVyZW5jZSAoU2VwdGVtYmVyIDEzLCAyMDA3KTwvaXRlbS1uYW1lPg0KICAgICAgICA8cXVhbnRpdHk+MTwvcXVhbnRpdHk+DQogICAgICAgIDx1bml0LXByaWNlIGN1cnJlbmN5PSJVU0QiPjQ1OS4wPC91bml0LXByaWNlPg0KICAgICAgICA8aXRlbS1kZXNjcmlwdGlvbiAvPg0KICAgICAgPC9pdGVtPg0KICAgIDwvaXRlbXM+DQogIDwvc2hvcHBpbmctY2FydD4NCiAgPGNoZWNrb3V0LWZsb3ctc3VwcG9ydD4NCiAgICA8bWVyY2hhbnQtY2hlY2tvdXQtZmxvdy1zdXBwb3J0IC8+DQogIDwvY2hlY2tvdXQtZmxvdy1zdXBwb3J0Pg0KPC9jaGVja291dC1zaG9wcGluZy1jYXJ0Pg0KDQo=//separator//EZcRQtONQQlpj/BlZavg23912j0=';
                    break;
                case 509:
                    $('register_for').innerHTML = '509.00 - Full Conference (September 13, 2007) and Workshop (1:30 – 4:30 pm, September 12, 2007)';
                    $('buyButtonCart').value = 'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCjxjaGVja291dC1zaG9wcGluZy1jYXJ0IHhtbG5zPSJodHRwOi8vY2hlY2tvdXQuZ29vZ2xlLmNvbS9zY2hlbWEvMiI+DQogIDxzaG9wcGluZy1jYXJ0Pg0KICAgIDxpdGVtcz4NCiAgICAgIDxpdGVtPg0KICAgICAgICA8aXRlbS1uYW1lPkZ1bGwgQ29uZmVyZW5jZSAoU2VwdGVtYmVyIDEzLCAyMDA3KSBhbmQgV29ya3Nob3AgKDE6MzAgPyA0OjMwIHBtLCBTZXB0ZW1iZXIgMTIsIDIwMDcpPC9pdGVtLW5hbWU+DQogICAgICAgIDxxdWFudGl0eT4xPC9xdWFudGl0eT4NCiAgICAgICAgPHVuaXQtcHJpY2UgY3VycmVuY3k9IlVTRCI+NTA5LjA8L3VuaXQtcHJpY2U+DQogICAgICAgIDxpdGVtLWRlc2NyaXB0aW9uIC8+DQogICAgICA8L2l0ZW0+DQogICAgPC9pdGVtcz4NCiAgPC9zaG9wcGluZy1jYXJ0Pg0KICA8Y2hlY2tvdXQtZmxvdy1zdXBwb3J0Pg0KICAgIDxtZXJjaGFudC1jaGVja291dC1mbG93LXN1cHBvcnQgLz4NCiAgPC9jaGVja291dC1mbG93LXN1cHBvcnQ+DQo8L2NoZWNrb3V0LXNob3BwaW5nLWNhcnQ+DQoNCg==//separator//jGjvH6wBL2XBWR9YapT79R4uQHc=';
                    break;
            }

            showLayerNow('check_out');
        } else {
            $('error_message_row').style.display = 'block';
            $('error_message').innerHTML = result.errorMessage;
        }
    },

    _marshalFields: function(formID) {
        var form = $(formID);

        var fields = new Hash;
        for(var i = 0; i < form.elements.length; i++) {
            var elt = form.elements[i];

            if (elt.type == 'radio') {
                if (elt.checked) {
                    fields.add(elt.value, elt.name);
                }
            } else if (elt.type == 'checkbox') {
                fields.add(elt.value, elt.checked);
            } else {
                fields.add(elt.value, elt.name);
            }
        }

        return fields;
    },

    _validateFields: function(fields) {

        var result = {
            isValid: true,
            errorMessage: ''
        };
        
        var attendeeCategory = fields.item('attendee_category');

        var isStudent = (attendeeCategory == 'Student/Academic' ? true : false);
        var isClient = (attendeeCategory == 'interTrend Client' ? true : false);
        var isOther = (attendeeCategory == 'Other' ? true : false);

        if (!fields.item('last_name')) {
            result.errorMessage = 'Please enter a last name.';
            result.isValid = false;
        } else if (!fields.item('first_name')) {
            result.errorMessage = 'Please enter a first name.';
            result.isValid = false;
        } else if (!fields.item('organization')) {
            if (isStudent) {
                result.errorMessage = 'Please enter a university.';
            } else {
                result.errorMessage = 'Please enter a company.';
            }
            result.isValid = false;
        } else if (!fields.item('title_or_major')) {
            if (isStudent) {
                result.errorMessage = 'Please enter a major.';
            } else {
                result.errorMessage = 'Please enter a job title.';
            }
            result.isValid = false;
        } else if (isStudent && !fields.item('graduation_year')) {
            result.errorMessage = 'Please enter a graduation year.';
            result.isValid = false;
        } else if (!fields.item('address1')) {
            result.errorMessage = 'Please enter an address.';
            result.isValid = false;
        } else if (!fields.item('city')) {
            result.errorMessage = 'Please enter a city.';
            result.isValid = false;
        } else if (!fields.item('state')) {
            result.errorMessage = 'Please select a state.';
            result.isValid = false;
        } else if (!fields.item('zip_code')) {
            result.errorMessage = 'Please enter a zip code.';
            result.isValid = false;
        } else if (!fields.item('phone')) {
            result.errorMessage = 'Please enter a phone number.';
            result.isValid = false;
        } else if (this._isValidEmail(fields.item('email_address')) == false) {
            result.errorMessage = 'Please enter a valid email address.';
            result.isValid = false;
        } else if (!fields.item('conference_fee')) {
            result.errorMessage = 'Please select a conference fee.';
            result.isValid = false;
        } else if (!fields.item('hear_about_us')) {
            result.errorMessage = 'Please tell us how you heard about us.';
            result.isValid = false;
        }

        return result;
    },

    _isValidEmail: function (str) {
        if (!str) {
            return false;
        } else {
             return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
        }
    }
};


