function toggleReferral() {
    var val = document.getElementById('referral_type').value;
    var detail = document.getElementById('ref_detail_field');
    var contact = document.getElementById('ref_contact_field');

    if (!val) {
        detail.style.display = 'none';
        contact.style.display = 'none';
    } else if (val == 1 || val == 2) {
        detail.style.display = 'none';
        contact.style.display = '';

        document.getElementById('referral_detail').value = '';
    } else {
        detail.style.display = '';
        contact.style.display = 'none';

        document.getElementById('referral_contact').value = '';
    }
}

function setBillingInfo() {
    var billing_info = document.getElementById('billing_info').checked;

    var billing_table = document.getElementById('billing_table');

    var contact_first_name = document.getElementById('first_name');
    var contact_last_name = document.getElementById('last_name');
    var contact_company_name = document.getElementById('company_name');
    var contact_address1 = document.getElementById('street');
    var contact_address2 = document.getElementById('address2');
    var contact_city = document.getElementById('city');
    var contact_state = document.getElementById('state');
    var contact_postal_code = document.getElementById('zip');
    var contact_country = document.getElementById('country');

    var billing_first_name = document.getElementById('billing_first_name');
    var billing_last_name = document.getElementById('billing_last_name');
    var billing_company_name = document.getElementById('billing_company_name');
    var billing_address1 = document.getElementById('billing_address1');
    var billing_address2 = document.getElementById('billing_address2');
    var billing_city = document.getElementById('billing_city');
    var billing_state = document.getElementById('billing_state');
    var billing_postal_code = document.getElementById('billing_zip');
    var billing_country = document.getElementById('billing_country');

    if (billing_info) {
        billing_table.style.display = 'none';

        billing_first_name.value = contact_first_name.value;
        billing_last_name.value = contact_last_name.value;
        billing_company_name.value = contact_company_name.value;
        billing_address1.value = contact_address1.value;
        billing_address2.value = contact_address2.value;
        billing_city.value = contact_city.value;
        billing_state.value = contact_state.value;
        billing_postal_code.value = contact_postal_code.value;
        billing_country.value = contact_country.value;
    } else {
        billing_table.style.display = 'table';
    }
}

function validateFields() {
    clearErrors();
    toggleReferral();
    setBillingInfo();

    var website_name = document.getElementById('website_name').value;
    var website_url = document.getElementById('website_url').value;
    var website_description = document.getElementById('website_description').value;

    var first_name = document.getElementById('first_name').value;
    var last_name = document.getElementById('last_name').value;
    var company_name = document.getElementById('company_name').value;
    var email = document.getElementById('email').value;
    var street = document.getElementById('street').value;
    var address2 = document.getElementById('address2').value;
    var city = document.getElementById('city').value;
    var state = document.getElementById('state').value;
    var zip = document.getElementById('zip').value;
    var country = document.getElementById('country').value;
    var phone = document.getElementById('phone').value;
    var fax = document.getElementById('fax').value;

    var billing_info = document.getElementById('billing_info').value;
    var billing_first_name = document.getElementById('billing_first_name').value;
    var billing_last_name = document.getElementById('billing_last_name').value;
    var billing_company_name = document.getElementById('billing_company_name').value;
    var billing_address1 = document.getElementById('billing_address1').value;
    var billing_address2 = document.getElementById('billing_address2').value;
    var billing_city = document.getElementById('billing_city').value;
    var billing_state = document.getElementById('billing_state').value;
    var billing_zip = document.getElementById('billing_zip').value;
    var billing_country = document.getElementById('billing_country').value;
    var payment_type_id = document.getElementById('payment_type_id').value;

    var referral_type = document.getElementById('referral_type').value;
    var referral_detail = document.getElementById('referral_detail').value;
    var referral_contact = document.getElementById('referral_contact').value;
    var estimated_traffic_volume_id = document.getElementById('estimated_traffic_volume_id').value;
//    var captcha_entry = document.getElementById('captcha_entry').value;

    var agreed_to_terms = document.getElementById('agreed_to_terms');
    var initials = document.getElementById('initials').value;

    // WEBSITE VALIDATION
    if (isEmptyField(website_name)) {
        showError('website_name',Locale.LANG_ERROR_COMPANY_WEBSITE_NAME_REQUIRED);
        return false;
    }

    if (isEmptyField(website_url)) {
        showError('website_url',Locale.LANG_ERROR_WEBSITE_URL_REQUIRED);
        return false;
    }

    // website url should be a valid format
    if (!isValidUrl(website_url)) {
        showError('website_url',Locale.LANG_ERROR_WEBSITE_FORMAT);
        return false;
    }

    if (isEmptyField(website_description)) {
        showError('website_description',Locale.LANG_ERROR_WEBSITE_DESCRIPTION_REQUIRED);
        return false;
    }
    
    
    // CONTACT INFO VALIDATION
    if (isEmptyField(first_name)) {
        showError('first_name',Locale.LANG_ERROR_FISRTNAME_REQUIRED);
        return false;
    }

    if (isEmptyField(last_name)) {
        showError('last_name',Locale.LANG_ERROR_LASTNAME_REQUIRED);
        return false;
    }

    if (isEmptyField(company_name)) {
        showError('company_name',Locale.LANG_ERROR_COMPANY_NAME_REQUIRED);
        return false;
    }

    if (isEmptyField(email)) {
        showError('email',Locale.LANG_ERROR_EMAIL_REQUIRED);
        return false;
    }

    // email is required and should be properly formed
    if (!isValidEmail(email)) {
        showError('email',Locale.LANG_INVALID_EMAIL_ADDRESS_FORMAT);
        return false;
    }

    if (isEmptyField(street)) {
        showError('street',Locale.LANG_ERROR_ADDRESS_REQUIRED);
        return false;
    }

    if (isEmptyField(city)) {
        showError('city',Locale.LANG_ERROR_CITY_REQUIRED);
        return false;
    }

    if (isEmptyField(state)) {
        showError('state',Locale.LANG_ERROR_STATE_REQUIRED);
        return false;
    }

    if (isEmptyField(zip)) {
        showError('zip',Locale.LANG_ERROR_ZIP_REQUIRED);
        return false;
    }

    if (isEmptyField(country)) {
        showError('country',Locale.LANG_ERROR_COUNTRY_REQUIRED);
        return false;
    }

    if (isEmptyField(phone)) {
        showError('phone',Locale.LANG_ERROR_PHONE_REQUIRED);
        return false;
    }

    // phone should be a valid format
    var phonePattern = /^[2-9]{1}[0-9]{2}-[0-9]{3}-[0-9]{4}$/;
    if (!phonePattern.test(phone)) {
        showError('phone',Locale.LANG_ERROR_INVALID_PHONE_FORMAT);
        return false;
    }

    // fax number should be a valid format
    if (!isEmptyField(fax) && !phonePattern.test(fax)) {
        showError('fax',Locale.LANG_ERROR_INVALID_FAX_FORMAT);
        return false;
    }


    // OTHER INFORMATION VALIDATION
    if (isEmptyField(referral_type)) {
        showError('referral_type',Locale.LANG_ERROR_REFERRAL_TYPE_REQUIRED);
        return false;
    }

    // require additional info once referral type is set
    if ((referral_type == 1 || referral_type == 2) && isEmptyField(referral_contact)) {
        showError('referral_contact',Locale.LANG_ERROR_REFERRAL_CONTACT_REQUIRED);
        return false;
    }

    // estimated traffic volume is required
    if (isEmptyField(estimated_traffic_volume_id)) {
        showError('estimated_traffic_volume_id',Locale.LANG_ERROR_REGISTER_TRAFFIC_VOLUME_REQUIRED);
        return false;
    }

//    if (isEmptyField(captcha_entry)) {
//        showError('captcha_entry',Locale.LANG_ERROR_CAPTCHA_ENTRY);
//        return false;
//    }

    // verify the user agreed to the terms of service
    if (getCheckbox(agreed_to_terms) == 0) {
        showError('agreed_to_terms',Locale.LANG_ERROR_TERMS_OF_SERVICE);
        return false;
    }

    if (isEmptyField(initials)) {
        showError('initials',Locale.LANG_INITIALS);
        return false;
    }

    return true;
}

function showError(element,error) {
    var field = document.getElementById(element);
    
    var notice = document.createElement('span');
    var noticeText = document.createTextNode(error);
    notice.setAttribute('id', 'error_' + element);
    notice.setAttribute('class', 'redText');
    notice.setAttribute('style', 'vertical-align:top')
    notice.appendChild(noticeText);

    field.parentNode.appendChild(notice);
    field.focus();
}

function clearErrors() {
    var spanList = document.getElementsByTagName('span');

    for(var s = 0; s < spanList.length; s++) {
        if (spanList[s].id.indexOf('error_') == 0) {
            spanList[s].parentNode.removeChild(spanList[s]);
        }
    }
}

function init() {
    setBillingInfo();
    toggleReferral();
    if (errorElement) showError(errorElement,errorMessage);
}
