/**
  * Output the metabox
  */
 public static function output($post)
 {
     echo '<div class="propertyhive_meta_box">';
     echo '<div class="options_group">';
     // On Market
     propertyhive_wp_checkbox(array('id' => '_on_market', 'label' => __('On Market', 'propertyhive'), 'desc_tip' => true, 'description' => __('Setting the property to be on the market means the property will be displayed on the website, and portals too if a <a href="http://wp-property-hive.com/add-ons/" target="_blank">portal add-on</a> is present.', 'propertyhive')));
     // Availability
     $options = array();
     $args = array('hide_empty' => false, 'parent' => 0);
     $terms = get_terms('availability', $args);
     $selected_value = '';
     if (!empty($terms) && !is_wp_error($terms)) {
         foreach ($terms as $term) {
             $options[$term->term_id] = $term->name;
         }
         $term_list = wp_get_post_terms($post->ID, 'availability', array("fields" => "ids"));
         if (!is_wp_error($term_list) && is_array($term_list) && !empty($term_list)) {
             $selected_value = $term_list[0];
         }
     }
     $args = array('id' => '_availability', 'label' => __('Availability', 'propertyhive'), 'options' => $options, 'desc_tip' => false);
     if ($selected_value != '') {
         $args['value'] = $selected_value;
     }
     propertyhive_wp_select($args);
     // Featured
     propertyhive_wp_checkbox(array('id' => '_featured', 'label' => __('Featured', 'propertyhive')));
     $args = array('hide_empty' => false, 'parent' => 0);
     $terms = get_terms('marketing_flag', $args);
     if (!empty($terms) && !is_wp_error($terms)) {
         $options = array();
         $selected_values = array();
         foreach ($terms as $term) {
             $options[$term->term_id] = $term->name;
             $term_list = wp_get_post_terms($post->ID, 'marketing_flag', array("fields" => "ids"));
             if (!is_wp_error($term_list) && is_array($term_list) && !empty($term_list)) {
                 if (in_array($term->term_id, $term_list)) {
                     $selected_values[] = $term->term_id;
                 }
             }
         }
         propertyhive_wp_checkboxes(array('name' => '_marketing_flags', 'label' => __('Marketing Flags', 'propertyhive'), 'options' => $options, 'value' => $selected_values));
     }
     do_action('propertyhive_property_marketing_fields');
     echo '</div>';
     echo '</div>';
 }
    /**
     * Output the metabox
     */
    public static function output($post)
    {
        $parent_post = false;
        if (isset($_GET['post_parent']) && $_GET['post_parent'] != '') {
            $parent_post = $_GET['post_parent'];
        }
        echo '<div class="propertyhive_meta_box">';
        echo '<div class="options_group">';
        $available_as = array();
        if (get_post_meta($post->ID, '_for_sale', true) == 'yes') {
            $available_as[] = 'sale';
        }
        if (get_post_meta($post->ID, '_to_rent', true) == 'yes') {
            $available_as[] = 'rent';
        }
        if ($parent_post !== FALSE && get_post_meta($parent_post, '_for_sale', TRUE)) {
            $available_as[] = 'sale';
        }
        if ($parent_post !== FALSE && get_post_meta($parent_post, '_to_rent', TRUE)) {
            $available_as[] = 'rent';
        }
        propertyhive_wp_checkboxes(array('id' => '_available_as', 'label' => __('Available As', 'propertyhive'), 'desc_tip' => false, 'value' => $available_as, 'options' => array('sale' => 'For Sale', 'rent' => 'To Rent')));
        // Currency / Price
        $ph_countries = new PH_Countries();
        $default_country = get_option('propertyhive_default_country', 'GB');
        $countries = get_option('propertyhive_countries', array($default_country));
        $currencies = array();
        foreach ($countries as $country) {
            $country = $ph_countries->get_country($country);
            if (!isset($currencies[$country['currency_code']])) {
                $currencies[$country['currency_code']] = $country['currency_symbol'];
            }
        }
        $selected_sale_currency = get_post_meta($post->ID, '_commercial_price_currency', true);
        if ($selected_sale_currency == '') {
            $country = $ph_countries->get_country($default_country);
            $selected_sale_currency = $country['currency_code'];
        }
        $selected_rent_currency = get_post_meta($post->ID, '_commercial_rent_currency', true);
        if ($selected_rent_currency == '') {
            $country = $ph_countries->get_country($default_country);
            $selected_rent_currency = $country['currency_code'];
        }
        // Sale fields
        echo '<div class="commercial-sale-fields"' . (!in_array('sale', $available_as) ? ' style="display:none"' : '') . '>';
        echo '<p class="form-field price_field">
        
            <label for="_price_from">' . __('Price', 'propertyhive') . (empty($currencies) || count($currencies) <= 1 ? ' (<span class="currency-symbol">' . $currencies[$selected_sale_currency] . '</span>)' : '') . '</label>';
        if (count($currencies) > 1) {
            echo '<select id="_commercial_price_currency" name="_commercial_price_currency" class="select" style="width:auto; float:left;">';
            foreach ($currencies as $currency_code => $currency_sybmol) {
                echo '<option value="' . $currency_code . '"' . ($currency_code == $selected_sale_currency ? ' selected' : '') . '>' . $currency_sybmol . '</option>';
            }
            echo '</select>';
        } else {
            echo '<input type="hidden" name="_commercial_price_currency" value="' . $selected_sale_currency . '">';
        }
        $price_options = get_commercial_price_units();
        echo '
        <input type="text" class="" name="_price_from" id="_price_from" value="' . get_post_meta($post->ID, '_price_from', true) . '" placeholder="" style="width:15%; min-width:85px;">
        <span style="float:left"> - </span>
        <input type="text" class="" name="_price_to" id="_price_to" value="' . get_post_meta($post->ID, '_price_to', true) . '" placeholder="" style="width:15%; min-width:85px;">

        <select name="_price_units" id="_price_units">
            <option value=""></option>';
        foreach ($price_options as $key => $value) {
            echo '<option value="' . $key . '"';
            if ($key == get_post_meta($post->ID, '_price_units', true)) {
                echo ' selected';
            }
            echo '>' . $value . '</option>';
        }
        echo '</select>

        </p>';
        // POA
        propertyhive_wp_checkbox(array('id' => '_commercial_price_poa', 'label' => __('Price On Application', 'propertyhive'), 'desc_tip' => false, 'value' => get_post_meta($post->ID, '_price_poa', true)));
        // Price Qualifier
        $options = array('' => '');
        $args = array('hide_empty' => false, 'parent' => 0);
        $terms = get_terms('price_qualifier', $args);
        $selected_value = '';
        if (!empty($terms) && !is_wp_error($terms)) {
            foreach ($terms as $term) {
                $options[$term->term_id] = $term->name;
            }
            $term_list = wp_get_post_terms($post->ID, 'price_qualifier', array("fields" => "ids"));
            if (!is_wp_error($term_list) && is_array($term_list) && !empty($term_list)) {
                $selected_value = $term_list[0];
            }
        }
        $args = array('id' => 'commercial_price_qualifier_id', 'label' => __('Price Qualifier', 'propertyhive'), 'desc_tip' => false, 'options' => $options);
        if ($selected_value != '') {
            $args['value'] = $selected_value;
        }
        propertyhive_wp_select($args);
        // Sale By
        $options = array('' => '');
        $args = array('hide_empty' => false, 'parent' => 0);
        $terms = get_terms('sale_by', $args);
        $selected_value = '';
        if (!empty($terms) && !is_wp_error($terms)) {
            foreach ($terms as $term) {
                $options[$term->term_id] = $term->name;
            }
            $term_list = wp_get_post_terms($post->ID, 'sale_by', array("fields" => "ids"));
            if (!is_wp_error($term_list) && is_array($term_list) && !empty($term_list)) {
                $selected_value = $term_list[0];
            }
        }
        $args = array('id' => 'commercial_sale_by_id', 'label' => __('Sale By', 'propertyhive'), 'desc_tip' => false, 'options' => $options);
        if ($selected_value != '') {
            $args['value'] = $selected_value;
        }
        propertyhive_wp_select($args);
        // Tenure
        $options = array('' => '');
        $args = array('hide_empty' => false, 'parent' => 0);
        $terms = get_terms('commercial_tenure', $args);
        $selected_value = '';
        if (!empty($terms) && !is_wp_error($terms)) {
            foreach ($terms as $term) {
                $options[$term->term_id] = $term->name;
            }
            $term_list = wp_get_post_terms($post->ID, 'commercial_tenure', array("fields" => "ids"));
            if (!is_wp_error($term_list) && is_array($term_list) && !empty($term_list)) {
                $selected_value = $term_list[0];
            }
        }
        $args = array('id' => 'commercial_tenure_id', 'label' => __('Tenure', 'propertyhive'), 'desc_tip' => false, 'options' => $options);
        if ($selected_value != '') {
            $args['value'] = $selected_value;
        }
        propertyhive_wp_select($args);
        do_action('propertyhive_property_commercial_sale_details_fields');
        echo '</div>';
        // Rent Fields
        echo '<div class="commercial-rent-fields"' . (!in_array('rent', $available_as) ? ' style="display:none"' : '') . '>';
        echo '<p class="form-field price_field">
        
            <label for="_rent_from">' . __('Rent', 'propertyhive') . (empty($currencies) || count($currencies) <= 1 ? ' (<span class="currency-symbol">' . $currencies[$selected_rent_currency] . '</span>)' : '') . '</label>';
        if (count($currencies) > 1) {
            echo '<select id="_commercial_rent_currency" name="_commercial_rent_currency" class="select" style="width:auto; float:left;">';
            foreach ($currencies as $currency_code => $currency_sybmol) {
                echo '<option value="' . $currency_code . '"' . ($currency_code == $selected_rent_currency ? ' selected' : '') . '>' . $currency_sybmol . '</option>';
            }
            echo '</select>';
        } else {
            echo '<input type="hidden" name="_commercial_rent_currency" value="' . $selected_rent_currency . '">';
        }
        $rent_units = get_post_meta($post->ID, '_rent_units', true);
        echo '
        <input type="text" class="" name="_rent_from" id="_rent_from" value="' . get_post_meta($post->ID, '_rent_from', true) . '" placeholder="" style="width:15%; min-width:85px;">
        <span style="float:left; padding:0 5px"> - </span>
        <input type="text" class="" name="_rent_to" id="_rent_to" value="' . get_post_meta($post->ID, '_rent_to', true) . '" placeholder="" style="width:15%; min-width:85px;">
        
        <select name="_rent_units" id="_rent_units">
            <option value="pw"' . ($rent_units == 'pw' ? ' selected' : '') . '>' . __('Per Week', 'propertyhive') . '</option>
            <option value="pcm"' . ($rent_units == 'pcm' || $rent_units == '' ? ' selected' : '') . '>' . __('Per Calendar Month', 'propertyhive') . '</option>
            <option value="pq"' . ($rent_units == 'pq' ? ' selected' : '') . '>' . __('Per Quarter', 'propertyhive') . '</option>
            <option value="pa"' . ($rent_units == 'pa' ? ' selected' : '') . '>' . __('Per Annum', 'propertyhive') . '</option>';
        foreach ($price_options as $key => $value) {
            echo '<option value="' . $key . '"';
            if ($key == $rent_units) {
                echo ' selected';
            }
            echo '>' . $value . '</option>';
        }
        echo '</select>

        </p>';
        // POA
        propertyhive_wp_checkbox(array('id' => '_commercial_rent_poa', 'label' => __('Rent On Application', 'propertyhive'), 'desc_tip' => false, 'value' => get_post_meta($post->ID, '_rent_poa', true)));
        do_action('propertyhive_property_commercial_rent_details_fields');
        echo '</div>';
        // end commercial-rent-fields
        ?>

        <p class="form-field"><label for="property_type_ids"><?php 
        _e('Property Types', 'propertyhive');
        ?>
</label>
        <select id="property_type_ids" name="property_type_ids[]" multiple="multiple" data-placeholder="<?php 
        _e('Select property types', 'propertyhive');
        ?>
" class="multiselect attribute_values">
            <?php 
        $options = array('' => '');
        $args = array('hide_empty' => false, 'parent' => 0);
        $terms = get_terms('commercial_property_type', $args);
        if (!empty($terms) && !is_wp_error($terms)) {
            foreach ($terms as $term) {
                $options[$term->term_id] = $term->name;
                $args = array('hide_empty' => false, 'parent' => $term->term_id);
                $subterms = get_terms('commercial_property_type', $args);
                if (!empty($subterms) && !is_wp_error($subterms)) {
                    foreach ($subterms as $subterm) {
                        $options[$subterm->term_id] = '- ' . $subterm->name;
                        $args = array('hide_empty' => false, 'parent' => $subterm->term_id);
                        $subsubterms = get_terms('commercial_property_type', $args);
                        if (!empty($subsubterms) && !is_wp_error($subsubterms)) {
                            foreach ($subsubterms as $subsubterm) {
                                $options[$subsubterm->term_id] = '- - ' . $subsubterm->name;
                            }
                        }
                    }
                }
            }
        }
        $selected_values = array();
        $term_list = wp_get_post_terms($post->ID, 'commercial_property_type', array("fields" => "ids"));
        if (!is_wp_error($term_list) && is_array($term_list) && !empty($term_list)) {
            foreach ($term_list as $term_id) {
                $selected_values[] = $term_id;
            }
        }
        if (!empty($options) && !is_wp_error($options)) {
            foreach ($options as $key => $value) {
                echo '<option value="' . esc_attr($key) . '"';
                if (in_array($key, $selected_values)) {
                    echo ' selected';
                }
                echo '>' . esc_html($value) . '</option>';
            }
        }
        ?>
        </select>

        <?php 
        $size_options = get_area_units();
        $floor_area_units = get_post_meta($post->ID, '_floor_area_units', true);
        echo '<p class="form-field">

        <label for="_floor_area_from">' . __('Floor Area', 'propertyhive') . '</label>
        
        <input type="text" class="" name="_floor_area_from" id="_floor_area_from" value="' . get_post_meta($post->ID, '_floor_area_from', true) . '" placeholder="" style="width:15%; min-width:85px;">
        <span style="float:left; padding:0 5px"> - </span>
        <input type="text" class="" name="_floor_area_to" id="_floor_area_to" value="' . get_post_meta($post->ID, '_floor_area_to', true) . '" placeholder="" style="width:15%; min-width:85px;">

        <select name="_floor_area_units" id="_floor_area_units">';
        foreach ($size_options as $key => $value) {
            echo '<option value="' . $key . '"';
            if ($key == $floor_area_units) {
                echo ' selected';
            }
            echo '>' . $value . '</option>';
        }
        echo '</select>

        </p>';
        $site_area_units = get_post_meta($post->ID, '_site_area_units', true);
        echo '<p class="form-field">

        <label for="_site_area_from">' . __('Site Area', 'propertyhive') . '</label>
        
        <input type="text" class="" name="_site_area_from" id="_site_area_from" value="' . get_post_meta($post->ID, '_site_area_from', true) . '" placeholder="" style="width:15%; min-width:85px;">
        <span style="float:left; padding:0 5px"> - </span>
        <input type="text" class="" name="_site_area_to" id="_site_area_to" value="' . get_post_meta($post->ID, '_site_area_to', true) . '" placeholder="" style="width:15%; min-width:85px;">

        <select name="_site_area_units" id="_site_area_units">';
        foreach ($size_options as $key => $value) {
            echo '<option value="' . $key . '"';
            if ($key == $site_area_units) {
                echo ' selected';
            }
            echo '>' . $value . '</option>';
        }
        echo '</select>

        </p>';
        do_action('propertyhive_property_commercial_details_fields');
        echo '</div>';
        echo '</div>';
        echo '<script>

            jQuery(document).ready(function()
            {
                jQuery(\'#_available_as_sale\').change(function()
                {
                    console.log(jQuery(this).is(\':checked\'));
                    if (jQuery(this).is(\':checked\'))
                    {
                        jQuery(\'.commercial-sale-fields\').slideDown(\'fast\');
                    }
                    else
                    {
                        jQuery(\'.commercial-sale-fields\').slideUp(\'fast\');
                    }
                });

                jQuery(\'#_available_as_rent\').change(function()
                {
                    console.log(jQuery(this).is(\':checked\'));
                    if (jQuery(this).is(\':checked\'))
                    {
                        jQuery(\'.commercial-rent-fields\').slideDown(\'fast\');
                    }
                    else
                    {
                        jQuery(\'.commercial-rent-fields\').slideUp(\'fast\');
                    }
                });
                    
            });

        </script>';
    }
    /**
     * Output the metabox
     */
    public static function output($post)
    {
        global $post, $wpdb, $thepostid;
        $total_profiles = 0;
        $owner_profiles = array();
        // get properties where this is the owner
        $args = array('post_type' => 'property', 'meta_query' => array(array('key' => '_owner_contact_id', 'value' => $post->ID, 'compare' => '=')));
        $property_query = new WP_Query($args);
        if ($property_query->have_posts()) {
            while ($property_query->have_posts()) {
                $property_query->the_post();
                $owner_profiles[] = $post;
                ++$total_profiles;
            }
        }
        wp_reset_postdata();
        $applicant_profiles = array();
        $num_applicant_profiles = get_post_meta($thepostid, '_applicant_profiles', TRUE);
        if ($num_applicant_profiles == '') {
            $num_applicant_profiles = 0;
        }
        if ($num_applicant_profiles > 0) {
            $total_profiles += $num_applicant_profiles;
            for ($i = 0; $i < $num_applicant_profiles; ++$i) {
                $applicant_profiles[] = get_post_meta($thepostid, '_applicant_profile_' . $i, TRUE);
            }
        }
        $third_party_profiles = array();
        $third_party_categories = get_post_meta($thepostid, '_third_party_categories', TRUE);
        if (is_array($third_party_categories) && !empty($third_party_categories)) {
            $ph_third_party_contacts = new PH_Third_Party_Contacts();
            foreach ($third_party_categories as $third_party_category) {
                $third_party_profiles[] = $third_party_category;
                ++$total_profiles;
            }
        }
        echo '<div class="propertyhive_meta_box">';
        echo '<div class="panel-wrap">
            
            <div class="ph-tabs-back"></div>

                <ul class="contact_data_tabs ph-tabs">';
        $tab = 0;
        foreach ($owner_profiles as $property_post) {
            $owner_type = __('Property Owner', 'propertyhive');
            $department = get_post_meta($property_post->ID, '_department', TRUE);
            if ($department == 'lettings') {
                $owner_type = __('Property Landlord', 'propertyhive');
            }
            echo '<li class="property_tab' . ($tab == 0 ? ' active' : '') . '">
                        <a href="#tab_property_data_' . $property_post->ID . '">' . $owner_type . '</a>
                    </li>';
            ++$tab;
        }
        foreach ($applicant_profiles as $key => $applicant_profile) {
            $label = __('New Applicant', 'propertyhive');
            if (isset($applicant_profile['department']) && $applicant_profile['department'] == 'residential-sales') {
                $label = __('Sales Applicant', 'propertyhive');
            } elseif (isset($applicant_profile['department']) && $applicant_profile['department'] == 'residential-lettings') {
                $label = __('Lettings Applicant', 'propertyhive');
            } elseif (isset($applicant_profile['department']) && $applicant_profile['department'] == 'commercial') {
                $label = __('Commercial Applicant', 'propertyhive');
            }
            echo '<li class="property_tab' . ($tab == 0 ? ' active' : '') . '">
                        <a href="#tab_applicant_data_' . $key . '">' . $label . '</a>
                    </li>';
            ++$tab;
        }
        foreach ($third_party_profiles as $key => $third_party_profile) {
            $label = __('New Third Party', 'propertyhive');
            if ($third_party_profile != '' && $third_party_profile != 0) {
                $category_name = $ph_third_party_contacts->get_category($third_party_profile);
                if ($category_name !== false) {
                    $label = $category_name;
                }
            }
            echo '<li class="property_tab' . ($tab == 0 ? ' active' : '') . '">
                        <a href="#tab_third_party_data_' . $key . '">' . $label . '</a>
                    </li>';
            ++$tab;
        }
        echo '<li class="property_tab' . ($tab == 0 ? ' active' : '') . '">
                        <a href="#tab_add_relationship">' . __('Add Relationship', 'propertyhive') . '</a>
                    </li>';
        echo '</ul>';
        $contact_id = $thepostid;
        $tab = 0;
        foreach ($owner_profiles as $property_post) {
            $the_property = new PH_Property($property_post->ID);
            echo '<div id="tab_property_data_' . $property_post->ID . '" class="panel propertyhive_options_panel" style="' . ($tab == 0 ? 'display:block;' : 'display:none;') . '">
                        <div class="options_group" style="float:left; width:100%;">';
            echo '<p class="form-field">';
            echo '<label>' . __('Address', 'propertyhive') . '</label>';
            echo $the_property->get_formatted_summary_address('<br>');
            echo '</p>';
            echo '<p class="form-field">';
            echo '<label>' . __('Price', 'propertyhive') . '</label>';
            echo $the_property->get_formatted_price();
            echo '</p>';
            echo '<p class="form-field">';
            echo '<label>' . __('Bedrooms', 'propertyhive') . '</label>';
            echo $the_property->_bedrooms;
            echo '</p>';
            echo '<p class="form-field">';
            echo '<label>' . __('Status', 'propertyhive') . '</label>';
            echo $the_property->_on_market == 'yes' ? __('On Market', 'propertyhive') : __('Not On Market', 'propertyhive');
            echo '</p>';
            echo '<p class="form-field">';
            echo '<label></label>';
            echo '<a href="' . get_edit_post_link($property_post->ID) . '" class="button">' . __('View Property Record', 'propertyhive') . '</a>';
            echo '</p>';
            echo '
                        </div>
                    </div>';
            ++$tab;
        }
        foreach ($applicant_profiles as $key => $applicant_profile) {
            echo '<div id="tab_applicant_data_' . $key . '" class="panel propertyhive_options_panel" style="' . ($tab == 0 ? 'display:block;' : 'display:none;') . '">
                        
                        <div class="options_group applicant-fields-' . $key . '" style="float:left; width:100%;">';
            $departments = array();
            if (get_option('propertyhive_active_departments_sales') == 'yes') {
                $departments['residential-sales'] = __('Residential Sales', 'propertyhive');
            }
            if (get_option('propertyhive_active_departments_lettings') == 'yes') {
                $departments['residential-lettings'] = __('Residential Lettings', 'propertyhive');
            }
            if (get_option('propertyhive_active_departments_commercial') == 'yes') {
                $departments['commercial'] = __('Commercial', 'propertyhive');
            }
            $value = isset($applicant_profile['department']) && $applicant_profile['department'] != '' ? $applicant_profile['department'] : get_option('propertyhive_primary_department');
            $args = array('id' => '_applicant_department_' . $key, 'label' => 'Looking For', 'value' => $value, 'options' => $departments);
            if (count($departments) == 1) {
                foreach ($departments as $department_key => $value) {
                    $args['value'] = $department_key;
                }
            }
            propertyhive_wp_radio($args);
            echo '<div class="propertyhive-applicant-residential-sales-details-' . $key . '">';
            // Price
            propertyhive_wp_text_input(array('id' => '_applicant_maximum_price_' . $key, 'label' => __('Maximum Price', 'propertyhive') . ' (&pound;)', 'desc_tip' => false, 'type' => 'text', 'class' => '', 'custom_attributes' => array('style' => 'width:100%; max-width:150px;'), 'value' => isset($applicant_profile['max_price']) ? $applicant_profile['max_price'] : ''));
            echo '</div>';
            echo '<div class="propertyhive-applicant-residential-lettings-details-' . $key . '">';
            // Rent / Rent Frequency
            $rent_frequency = isset($applicant_profile['rent_frequency']) ? $applicant_profile['rent_frequency'] : '';
            echo '<p class="form-field rent_field ">
                        
                            <label for="_applicant_maximum_rent_' . $key . '">' . __('Maximum Rent', 'propertyhive') . ' (&pound;)</label>
                            
                            <input type="text" class="" name="_applicant_maximum_rent_' . $key . '" id="_applicant_maximum_rent_' . $key . '" value="' . (isset($applicant_profile['max_rent']) ? $applicant_profile['max_rent'] : '') . '" placeholder="" style="width:20%; max-width:150px;">
                            
                            <select id="_applicant_rent_frequency_' . $key . '" name="_applicant_rent_frequency_' . $key . '" class="select short">
                                <option value="pw"' . ($rent_frequency == 'pw' ? ' selected' : '') . '>' . __('Per Week', 'propertyhive') . '</option>
                                <option value="pcm"' . ($rent_frequency == 'pcm' || $rent_frequency == '' ? ' selected' : '') . '>' . __('Per Calendar Month', 'propertyhive') . '</option>
                                <option value="pq"' . ($rent_frequency == 'pq' ? ' selected' : '') . '>' . __('Per Quarter', 'propertyhive') . '</option>
                                <option value="pa"' . ($rent_frequency == 'pa' ? ' selected' : '') . '>' . __('Per Annum', 'propertyhive') . '</option>
                            </select>
                            
                        </p>';
            echo '</div>';
            echo '<div class="propertyhive-applicant-residential-details-' . $key . '">';
            // Bedrooms
            propertyhive_wp_text_input(array('id' => '_applicant_minimum_bedrooms_' . $key, 'label' => __('Minimum Bedrooms', 'propertyhive'), 'desc_tip' => false, 'type' => 'number', 'class' => '', 'custom_attributes' => array('style' => 'width:100%; max-width:75px;'), 'value' => isset($applicant_profile['min_beds']) ? $applicant_profile['min_beds'] : ''));
            // Residential Types
            ?>
                        <p class="form-field"><label for="_applicant_property_types_<?php 
            echo $key;
            ?>
"><?php 
            _e('Property Types', 'propertyhive');
            ?>
</label>
                        <select id="_applicant_property_types_<?php 
            echo $key;
            ?>
" name="_applicant_property_types_<?php 
            echo $key;
            ?>
[]" multiple="multiple" data-placeholder="Start typing to add property types..." class="multiselect attribute_values">
                            <?php 
            $options = array('' => '');
            $args = array('hide_empty' => false, 'parent' => 0);
            $terms = get_terms('property_type', $args);
            $selected_values = array();
            $term_list = isset($applicant_profile['property_types']) ? $applicant_profile['property_types'] : array();
            if (is_array($term_list) && !empty($term_list)) {
                foreach ($term_list as $term_id) {
                    $selected_values[] = $term_id;
                }
            }
            if (!empty($terms) && !is_wp_error($terms)) {
                foreach ($terms as $term) {
                    echo '<option value="' . esc_attr($term->term_id) . '"';
                    if (in_array($term->term_id, $selected_values)) {
                        echo ' selected';
                    }
                    echo '>' . esc_html($term->name) . '</option>';
                    $args = array('hide_empty' => false, 'parent' => $term->term_id);
                    $subterms = get_terms('property_type', $args);
                    if (!empty($subterms) && !is_wp_error($subterms)) {
                        foreach ($subterms as $term) {
                            echo '<option value="' . esc_attr($term->term_id) . '"';
                            if (in_array($term->term_id, $selected_values)) {
                                echo ' selected';
                            }
                            echo '>- ' . esc_html($term->name) . '</option>';
                        }
                    }
                }
            }
            ?>
                        </select></p>
                    <?php 
            echo '</div>';
            // end 'propertyhive-applicant-residential-details-' . $key
            echo '<div class="propertyhive-applicant-commercial-details-' . $key . '">';
            $args = array('id' => '_applicant_available_as_' . $key, 'label' => 'Available As', 'value' => array('sale', 'rent'), 'options' => array('sale' => 'For Sale', 'rent' => 'To Rent'));
            if (isset($applicant_profile['available_as']) && is_array($applicant_profile['available_as'])) {
                $args['value'] = array();
                foreach ($applicant_profile['available_as'] as $value) {
                    $args['value'][] = $value;
                }
            }
            propertyhive_wp_checkboxes($args);
            // Commercial Types
            ?>
                        <p class="form-field"><label for="_applicant_commercial_property_types_<?php 
            echo $key;
            ?>
"><?php 
            _e('Property Types', 'propertyhive');
            ?>
</label>
                        <select id="_applicant_commercial_property_types_<?php 
            echo $key;
            ?>
" name="_applicant_commercial_property_types_<?php 
            echo $key;
            ?>
[]" multiple="multiple" data-placeholder="Start typing to add property types..." class="multiselect attribute_values">
                            <?php 
            $options = array('' => '');
            $args = array('hide_empty' => false, 'parent' => 0);
            $terms = get_terms('commercial_property_type', $args);
            $selected_values = array();
            $term_list = isset($applicant_profile['commercial_property_types']) ? $applicant_profile['commercial_property_types'] : array();
            if (is_array($term_list) && !empty($term_list)) {
                foreach ($term_list as $term_id) {
                    $selected_values[] = $term_id;
                }
            }
            if (!empty($terms) && !is_wp_error($terms)) {
                foreach ($terms as $term) {
                    echo '<option value="' . esc_attr($term->term_id) . '"';
                    if (in_array($term->term_id, $selected_values)) {
                        echo ' selected';
                    }
                    echo '>' . esc_html($term->name) . '</option>';
                    $args = array('hide_empty' => false, 'parent' => $term->term_id);
                    $subterms = get_terms('property_type', $args);
                    if (!empty($subterms) && !is_wp_error($subterms)) {
                        foreach ($subterms as $term) {
                            echo '<option value="' . esc_attr($term->term_id) . '"';
                            if (in_array($term->term_id, $selected_values)) {
                                echo ' selected';
                            }
                            echo '>- ' . esc_html($term->name) . '</option>';
                        }
                    }
                }
            }
            ?>
                        </select></p>
                    <?php 
            echo '</div>';
            // end 'propertyhive-applicant-commercial-details-' . $key
            // Locations
            ?>
                        <p class="form-field"><label for="_applicant_locations_<?php 
            echo $key;
            ?>
"><?php 
            _e('Locations', 'propertyhive');
            ?>
</label>
                        <select id="_applicant_locations_<?php 
            echo $key;
            ?>
" name="_applicant_locations_<?php 
            echo $key;
            ?>
[]" multiple="multiple" data-placeholder="Start typing to add location..." class="multiselect attribute_values">
                            <?php 
            $options = array('' => '');
            $args = array('hide_empty' => false, 'parent' => 0);
            $terms = get_terms('location', $args);
            $selected_values = array();
            $term_list = isset($applicant_profile['locations']) ? $applicant_profile['locations'] : array();
            if (is_array($term_list) && !empty($term_list)) {
                foreach ($term_list as $term_id) {
                    $selected_values[] = $term_id;
                }
            }
            if (!empty($terms) && !is_wp_error($terms)) {
                foreach ($terms as $term) {
                    echo '<option value="' . esc_attr($term->term_id) . '"';
                    if (in_array($term->term_id, $selected_values)) {
                        echo ' selected';
                    }
                    echo '>' . esc_html($term->name) . '</option>';
                    $args = array('hide_empty' => false, 'parent' => $term->term_id);
                    $subterms = get_terms('location', $args);
                    if (!empty($subterms) && !is_wp_error($subterms)) {
                        foreach ($subterms as $term) {
                            echo '<option value="' . esc_attr($term->term_id) . '"';
                            if (in_array($term->term_id, $selected_values)) {
                                echo ' selected';
                            }
                            echo '>- ' . esc_html($term->name) . '</option>';
                            $args = array('hide_empty' => false, 'parent' => $term->term_id);
                            $subsubterms = get_terms('location', $args);
                            if (!empty($subsubterms) && !is_wp_error($subsubterms)) {
                                foreach ($subsubterms as $term) {
                                    echo '<option value="' . esc_attr($term->term_id) . '"';
                                    if (in_array($term->term_id, $selected_values)) {
                                        echo ' selected';
                                    }
                                    echo '>- - ' . esc_html($term->name) . '</option>';
                                }
                            }
                        }
                    }
                }
            }
            ?>
                        </select></p>
                    <?php 
            // Additional Requirement Notes
            propertyhive_wp_textarea_input(array('id' => '_applicant_requirement_notes_' . $key, 'label' => __('Additional Requirements', 'propertyhive'), 'desc_tip' => false, 'class' => '', 'value' => isset($applicant_profile['notes']) ? $applicant_profile['notes'] : ''));
            propertyhive_wp_checkbox(array('id' => '_send_matching_properties_' . $key, 'label' => __('Send Matching Properties', 'propertyhive'), 'desc_tip' => false, 'value' => isset($applicant_profile['send_matching_properties']) && $applicant_profile['send_matching_properties'] == 'yes' || !isset($applicant_profile['send_matching_properties']) ? 'yes' : ''));
            $auto_property_match = get_option('propertyhive_auto_property_match', '');
            if ($auto_property_match == 'yes') {
                // Auto match emails are enabled. Add ability to disable on per-applicant basis
                propertyhive_wp_checkbox(array('id' => '_auto_match_disabled_' . $key, 'label' => __('Disable Auto-Match', 'propertyhive'), 'desc_tip' => false, 'value' => isset($applicant_profile['auto_match_disabled']) && $applicant_profile['auto_match_disabled'] == 'yes' ? 'yes' : ''));
            }
            echo '

                        </div>

                        <div class="actions">

                            <a 
                                href="' . admin_url('admin.php?page=ph-matching-properties&contact_id=' . $contact_id . '&applicant_profile=' . $key) . '" 
                                class="button view-matching-properties-' . $key . '" 
                                ' . (isset($applicant_profile['send_matching_properties']) && $applicant_profile['send_matching_properties'] == '' ? ' disabled title="Send Matching Properties not selected"' : '') . '
                            >' . __('View Matching Properties', 'propertyhive') . '</a>

                            <a 
                                href="' . wp_nonce_url(admin_url('post.php?post=' . $contact_id . '&action=edit#propertyhive-contact-relationships'), $key, 'delete_applicant_relationship') . '" 
                                class="button"
                                onclick="var confirmBox = confirm(\'' . __('Are you sure you wish to delete this applicant relationship?', 'propertyhive') . '\'); return confirmBox;"
                            >' . __('Delete Relationship', 'propertyhive') . '</a>

                            <div id="view_matching_properties_' . $key . '" style="display:none;">
                                
                                <div class="loading-properties" style="text-align:center;"><br><br>Loading matching properties...</div>

                                <div class="need-to-save-changes" style="text-align:center;"><br><br>Please save your changes before viewing matching properties</div>

                                <div class="matching-properties" id="matching_properties_' . $key . '" style="display:none;">

                                </div>

                            </div>

                        </div>

                    </div>

                    <script>

                        var applicant_details_changed_' . $key . ' = false;
                        jQuery(document).ready(function()
                        {
                            showHideApplicantDepartmentMetaBox_' . $key . '();

                            jQuery(\'input[type=\\\'radio\\\'][name=\\\'_applicant_department_' . $key . '\\\']\').change(function()
                            {
                                 showHideApplicantDepartmentMetaBox_' . $key . '();
                            });

                            jQuery(\'.applicant-fields-' . $key . ' input, .applicant-fields-' . $key . ' select, .applicant-fields-' . $key . ' textarea\').change(function()
                            {
                                applicant_details_changed_' . $key . ' = true;
                            });

                            jQuery(\'a.view-matching-properties-' . $key . '\').click(function(e)
                            {
                                if (applicant_details_changed_' . $key . ')
                                {
                                    alert(\'You\\\'ve made changes to the requirements. Please save the changes before viewing matching properties\');
                                    return false;
                                }

                                return true;
                            });
                        });
                        
                        function showHideApplicantDepartmentMetaBox_' . $key . '()
                        {
                            jQuery(\'.propertyhive-applicant-residential-details-' . $key . '\').hide();
                            jQuery(\'.propertyhive-applicant-residential-sales-details-' . $key . '\').hide();
                            jQuery(\'.propertyhive-applicant-residential-lettings-details-' . $key . '\').hide();
                            jQuery(\'.propertyhive-applicant-commercial-details-' . $key . '\').hide();
                            
                            switch (jQuery(\'input[type=\\\'radio\\\'][name=\\\'_applicant_department_' . $key . '\\\']:checked\').val())
                            {
                                case "residential-sales":
                                {
                                    jQuery(\'.propertyhive-applicant-residential-details-' . $key . '\').show();
                                    jQuery(\'.propertyhive-applicant-residential-sales-details-' . $key . '\').show();
                                    break;
                                }
                                case "residential-lettings":
                                {
                                    jQuery(\'.propertyhive-applicant-residential-details-' . $key . '\').show();
                                    jQuery(\'.propertyhive-applicant-residential-lettings-details-' . $key . '\').show();
                                    break;
                                }
                                case "commercial":
                                {
                                    jQuery(\'.propertyhive-applicant-commercial-details-' . $key . '\').show();
                                    break;
                                }
                            }
                            
                        }
                        
                    </script>';
            ++$tab;
        }
        foreach ($third_party_profiles as $key => $category) {
            echo '<div id="tab_third_party_data_' . $key . '" class="panel propertyhive_options_panel" style="' . ($tab == 0 ? 'display:block;' : 'display:none;') . '">
                        <div class="options_group" style="float:left; width:100%;">';
            echo '<p class="form-field rent_field ">
                        
                            <label for="_third_party_category_' . $key . '">' . __('Contact Category', 'propertyhive') . '</label>
                            
                            <select id="_third_party_category_' . $key . '" name="_third_party_category[]" class="select short">';
            if ($category == '' || $category == 0) {
                echo '<option value="0"></option>';
            }
            $categories = $ph_third_party_contacts->get_categories();
            foreach ($categories as $id => $category_name) {
                echo '<option value="' . $id . '"';
                if ($id == $category) {
                    echo ' selected';
                }
                echo '>' . $category_name . '</option>';
            }
            echo '</select>
                            
                        </p>';
            echo '
                        </div>
                    </div>';
            ++$tab;
        }
        echo '<div id="tab_add_relationship" class="panel propertyhive_options_panel" style="' . ($tab == 0 ? 'display:block;' : 'display:none;') . '">
                    <div class="options_group">';
        echo '<p class="form-field">';
        echo '<label>' . __('New Relationship Type', 'propertyhive') . '</label>';
        echo '<a href="' . wp_nonce_url(admin_url('post.php?post=' . $thepostid . '&action=edit#propertyhive-contact-relationships'), '1', 'add_applicant_relationship') . '" class="button">' . __('New Applicant Profile', 'propertyhive') . '</a><br><br>';
        echo '<a href="' . admin_url('post-new.php?post_type=property&owner_contact_id=' . $thepostid) . '" class="button">' . __('New Property Owner / Landlord', 'propertyhive') . '</a><br><br>';
        echo '<a href="' . wp_nonce_url(admin_url('post.php?post=' . $thepostid . '&action=edit#propertyhive-contact-relationships'), '1', 'add_third_party_relationship') . '" class="button">' . __('New Third Party Contact', 'propertyhive') . '</a>';
        echo '</p>';
        echo '
                        </div>
                    </div>';
        echo '<div class="clear"></div>
            
        </div>';
        echo '</div>';
        //do_action('propertyhive_contact_relationships_fields');
    }
 /**
  * Output the metabox
  */
 public static function output($post)
 {
     echo '<div class="propertyhive_meta_box">';
     echo '<div class="options_group">';
     // Currency / Rent / Rent Frequency
     $ph_countries = new PH_Countries();
     $default_country = get_option('propertyhive_default_country', 'GB');
     $countries = get_option('propertyhive_countries', array($default_country));
     $currencies = array();
     foreach ($countries as $country) {
         $country = $ph_countries->get_country($country);
         if (!isset($currencies[$country['currency_code']])) {
             $currencies[$country['currency_code']] = $country['currency_symbol'];
         }
     }
     // Cater for when no currency selected or currencies have been updated in settings so existing currency doesn't exist
     $selected_currency = get_post_meta($post->ID, '_currency', true);
     if ($selected_currency == '' || !isset($currencies[$selected_currency])) {
         $country = $ph_countries->get_country($default_country);
         $selected_currency = $country['currency_code'];
     }
     $rent_frequency = get_post_meta($post->ID, '_rent_frequency', true);
     echo '<p class="form-field rent_field ">
     
         <label for="_rent">' . __('Rent', 'propertyhive') . (empty($currencies) || count($currencies) <= 1 ? ' (<span class="currency-symbol">' . $currencies[$selected_currency] . '</span>)' : '') . '</label>';
     if (count($currencies) > 1) {
         echo '<select id="_rent_currency" name="_rent_currency" class="select" style="width:auto; float:left;">';
         foreach ($currencies as $currency_code => $currency_sybmol) {
             echo '<option value="' . $currency_code . '"' . ($currency_code == $selected_currency ? ' selected' : '') . '>' . $currency_sybmol . '</option>';
         }
         echo '</select>';
     } else {
         echo '<input type="hidden" name="_rent_currency" value="' . $selected_currency . '">';
     }
     echo '<input type="text" class="" name="_rent" id="_rent" value="' . get_post_meta($post->ID, '_rent', true) . '" placeholder="" style="width:20%;">
         
         <select id="_rent_frequency" name="_rent_frequency" class="select" style="width:auto">
             <option value="pppw"' . ($rent_frequency == 'pppw' ? ' selected' : '') . '>' . __('Per Person Per Week', 'propertyhive') . '</option>
             <option value="pw"' . ($rent_frequency == 'pw' ? ' selected' : '') . '>' . __('Per Week', 'propertyhive') . '</option>
             <option value="pcm"' . ($rent_frequency == 'pcm' || $rent_frequency == '' ? ' selected' : '') . '>' . __('Per Calendar Month', 'propertyhive') . '</option>
             <option value="pq"' . ($rent_frequency == 'pq' ? ' selected' : '') . '>' . __('Per Quarter', 'propertyhive') . '</option>
             <option value="pa"' . ($rent_frequency == 'pa' ? ' selected' : '') . '>' . __('Per Annum', 'propertyhive') . '</option>
         </select>
         
     </p>';
     // POA
     propertyhive_wp_checkbox(array('id' => '_rent_poa', 'label' => __('Rent On Application', 'propertyhive'), 'desc_tip' => false, 'value' => get_post_meta($post->ID, '_poa', true)));
     // Deposit
     propertyhive_wp_text_input(array('id' => '_deposit', 'label' => __('Deposit', 'propertyhive'), 'desc_tip' => false, 'class' => '', 'custom_attributes' => array('style' => 'width:20%')));
     // Furnished
     $options = array('' => '');
     $args = array('hide_empty' => false, 'parent' => 0);
     $terms = get_terms('furnished', $args);
     $selected_value = '';
     if (!empty($terms) && !is_wp_error($terms)) {
         foreach ($terms as $term) {
             $options[$term->term_id] = $term->name;
         }
         $term_list = wp_get_post_terms($post->ID, 'furnished', array("fields" => "ids"));
         if (!is_wp_error($term_list) && is_array($term_list) && !empty($term_list)) {
             $selected_value = $term_list[0];
         }
     }
     $args = array('id' => 'furnished_id', 'label' => __('Furnishing', 'propertyhive'), 'desc_tip' => false, 'options' => $options);
     if ($selected_value != '') {
         $args['value'] = $selected_value;
     }
     propertyhive_wp_select($args);
     // Available Date
     propertyhive_wp_text_input(array('id' => '_available_date', 'label' => __('Available Date', 'propertyhive'), 'desc_tip' => false, 'class' => 'short date-picker', 'placeholder' => 'YYYY-MM-DD', 'custom_attributes' => array('maxlength' => 10, 'pattern' => "[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])")));
     do_action('propertyhive_property_residential_lettings_details_fields');
     echo '</div>';
     echo '</div>';
 }
 /**
  * Output the metabox
  */
 public static function output($post)
 {
     echo '<div class="propertyhive_meta_box">';
     echo '<div class="options_group">';
     // Currency / Price
     $ph_countries = new PH_Countries();
     $default_country = get_option('propertyhive_default_country', 'GB');
     $countries = get_option('propertyhive_countries', array($default_country));
     $currencies = array();
     foreach ($countries as $country) {
         $country = $ph_countries->get_country($country);
         if (!isset($currencies[$country['currency_code']])) {
             $currencies[$country['currency_code']] = $country['currency_symbol'];
         }
     }
     // Cater for when no currency selected or currencies have been updated in settings so existing currency doesn't exist
     $selected_currency = get_post_meta($post->ID, '_currency', true);
     if ($selected_currency == '' || !isset($currencies[$selected_currency])) {
         $country = $ph_countries->get_country($default_country);
         $selected_currency = $country['currency_code'];
     }
     echo '<p class="form-field price_field ">
     
         <label for="_price">' . __('Price', 'propertyhive') . (empty($currencies) || count($currencies) <= 1 ? ' (<span class="currency-symbol">' . $currencies[$selected_currency] . '</span>)' : '') . '</label>';
     if (count($currencies) > 1) {
         echo '<select id="_price_currency" name="_price_currency" class="select" style="width:auto; float:left;">';
         foreach ($currencies as $currency_code => $currency_sybmol) {
             echo '<option value="' . $currency_code . '"' . ($currency_code == $selected_currency ? ' selected' : '') . '>' . $currency_sybmol . '</option>';
         }
         echo '</select>';
     } else {
         echo '<input type="hidden" name="_price_currency" value="' . $selected_currency . '">';
     }
     echo '<input type="text" class="" name="_price" id="_price" value="' . get_post_meta($post->ID, '_price', true) . '" placeholder="" style="width:15%;">
         
     </p>';
     // POA
     propertyhive_wp_checkbox(array('id' => '_sale_poa', 'label' => __('Price On Application', 'propertyhive'), 'desc_tip' => false, 'value' => get_post_meta($post->ID, '_poa', true)));
     // Price Qualifier
     $options = array('' => '');
     $args = array('hide_empty' => false, 'parent' => 0);
     $terms = get_terms('price_qualifier', $args);
     $selected_value = '';
     if (!empty($terms) && !is_wp_error($terms)) {
         foreach ($terms as $term) {
             $options[$term->term_id] = $term->name;
         }
         $term_list = wp_get_post_terms($post->ID, 'price_qualifier', array("fields" => "ids"));
         if (!is_wp_error($term_list) && is_array($term_list) && !empty($term_list)) {
             $selected_value = $term_list[0];
         }
     }
     $args = array('id' => 'price_qualifier_id', 'label' => __('Price Qualifier', 'propertyhive'), 'desc_tip' => false, 'options' => $options);
     if ($selected_value != '') {
         $args['value'] = $selected_value;
     }
     propertyhive_wp_select($args);
     // Sale By
     $options = array('' => '');
     $args = array('hide_empty' => false, 'parent' => 0);
     $terms = get_terms('sale_by', $args);
     $selected_value = '';
     if (!empty($terms) && !is_wp_error($terms)) {
         foreach ($terms as $term) {
             $options[$term->term_id] = $term->name;
         }
         $term_list = wp_get_post_terms($post->ID, 'sale_by', array("fields" => "ids"));
         if (!is_wp_error($term_list) && is_array($term_list) && !empty($term_list)) {
             $selected_value = $term_list[0];
         }
     }
     $args = array('id' => 'sale_by_id', 'label' => __('Sale By', 'propertyhive'), 'desc_tip' => false, 'options' => $options);
     if ($selected_value != '') {
         $args['value'] = $selected_value;
     }
     propertyhive_wp_select($args);
     // Tenure
     $options = array('' => '');
     $args = array('hide_empty' => false, 'parent' => 0);
     $terms = get_terms('tenure', $args);
     $selected_value = '';
     if (!empty($terms) && !is_wp_error($terms)) {
         foreach ($terms as $term) {
             $options[$term->term_id] = $term->name;
         }
         $term_list = wp_get_post_terms($post->ID, 'tenure', array("fields" => "ids"));
         if (!is_wp_error($term_list) && is_array($term_list) && !empty($term_list)) {
             $selected_value = $term_list[0];
         }
     }
     $args = array('id' => 'tenure_id', 'label' => __('Tenure', 'propertyhive'), 'desc_tip' => false, 'options' => $options);
     if ($selected_value != '') {
         $args['value'] = $selected_value;
     }
     propertyhive_wp_select($args);
     do_action('propertyhive_property_residential_sales_details_fields');
     echo '</div>';
     echo '</div>';
 }