<?php 
/*****************************************************************************************
**  REDCap is only available through a license agreement with Vanderbilt University
******************************************************************************************/

// Need to call survey functions file to utilize a function
require_once APP_PATH_DOCROOT . "Surveys/survey_functions.php";

// Begin HTML
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<title><?php echo strip_tags(remBr(br2nl($app_title))) ?> | REDCap</title>
	<meta name="googlebot" content="noindex, noarchive, nofollow, nosnippet">
	<meta name="robots" content="noindex, noarchive, nofollow">
	<meta name="slurp" content="noindex, noarchive, nofollow, noodp, noydir">
	<meta name="msnbot" content="noindex, noarchive, nofollow, noodp">
	<meta http-equiv="Cache-Control" content="no-cache">
	<meta http-equiv="Pragma" content="no-cache">
	<meta http-equiv="expires" content="0">
	<?php print ($isIE ? '<meta http-equiv="X-UA-Compatible" content="IE=edge">' : '') ?>
	<link rel="shortcut icon" href="<?php echo APP_PATH_IMAGES ?>favicon.ico" type="image/x-icon">
	<link rel="apple-touch-icon-precomposed" href="<?php echo APP_PATH_IMAGES ?>apple-touch-icon.png">
	<link rel="stylesheet" type="text/css" href="<?php echo APP_PATH_CSS ?>smoothness/jquery-ui-<?php echo JQUERYUI_VERSION ?>.custom.css" media="screen,print">
	<link rel="stylesheet" type="text/css" href="<?php echo APP_PATH_CSS ?>style.css" media="screen,print">
	<script type="text/javascript" src="<?php echo APP_PATH_JS ?>base.js"></script>
	<script type="text/javascript" src="<?php echo APP_PATH_JS ?>underscore-min.js"></script>
	<script type="text/javascript" src="<?php echo APP_PATH_JS ?>backbone-min.js"></script>
	<script type="text/javascript" src="<?php echo APP_PATH_JS ?>RedCapUtil.js"></script>
            }
            exit;
            break;
    }
}
//DATE Field
if ($_GET['view'] == "date") {
    print "<div id='change_date' style='display:block;'>\n\t\t\t\t<b>" . DateTimeRC::format_ts_from_ymd($event_date) . " (" . DateTimeRC::getDay($event_date) . ")</b>&nbsp; ";
    // Dont' allow user to change date here if tied to an Event (need to change on Scheduling page where it might affect other scheduled dates)
    if ($row['event_id'] == "") {
        print "<a href='javascript:;' style='text-decoration:underline;font-size:11px;' onclick=\"\$('#change_date').css({'display':'none'});\$('#save_date').css({'display':'block'});\">{$lang['calendar_popup_ajax_03']}</a>";
    }
    print "{$msg}\n\t\t\t</div>\n\t\t\t<div id='save_date' style='display:none;position:relative;'>\n\t\t\t\t<input type='text' id='newdate' name='newdate' onblur=\"redcap_validate(this,'','','hard','date_'+user_date_format_validation,1,1,user_date_format_delimiter);\" value='" . DateTimeRC::format_ts_from_ymd($event_date) . "' class='x-form-text x-form-field' style='width:70px;' maxlength='10'><span class='df'>(" . DateTimeRC::get_user_format_label() . ")</span>\n\t\t\t\t&nbsp;&nbsp;\n\t\t\t\t<input type='button' id='savebtndatecalpopup' style='font-size:11px;' value='" . cleanHtml($lang['calendar_popup_ajax_04']) . "' onclick='saveDateCalPopup({$_GET['cal_id']})'> &nbsp;\n\t\t\t\t<input type='button' style='font-size:11px;' value='" . cleanHtml($lang['global_53']) . "' onclick=\"\$('#change_date').css({'display':'block'});\$('#save_date').css({'display':'none'});\">\n\t\t\t</div>";
    //TIME Field
} elseif ($_GET['view'] == "time") {
    $time_field = "<input type='text' class='x-form-text x-form-field time' id='event_time' name='event_time' value='" . remBr(cleanHtml($_GET['event_time'])) . "' maxlength='5' style='width:50px;' onblur=\"redcap_validate(this,'','','soft_typed','time')\"> \n\t\t\t\t\t<span style='font-size:10px;color:#777;font-family:tahoma;'>HH:MM</span> &nbsp; \n\t\t\t\t\t<input type='button' id='savebtntimecalpopup' style='font-size:11px;' value='{$lang['calendar_popup_ajax_06']}' onclick='saveTimeCalPopup({$_GET['cal_id']})'>";
    //Visit Time
    if ($_GET['event_time'] == "") {
        $visible = $time_field;
        $hidden = "";
    } else {
        $visible = "<b>" . DateTimeRC::format_ts_from_ymd($_GET['event_time']) . "</b>&nbsp; \n\t\t\t\t\t<a href='javascript:;' style='text-decoration:underline;font-size:11px;' onclick=\"\$('#change_time').css({'display':'none'});\$('#save_time').css({'display':'block'});\">{$lang['calendar_popup_ajax_07']}</a>";
        $hidden = $time_field . " &nbsp;\n\t\t\t\t\t<input type='button' style='font-size:11px;' value='{$lang['global_53']}' onclick=\"\$('#change_time').css({'display':'block'});\$('#save_time').css({'display':'none'});\">";
    }
    print "<div id='change_time' style='display:block;'>\n\t\t\t\t{$visible}\n\t\t\t\t{$msg}\n\t\t\t</div>\n\t\t\t<div id='save_time' style='display:none;'>\n\t\t\t\t{$hidden}\n\t\t\t</div>";
    //STATUS Field
} elseif ($_GET['view'] == "status") {
    //Set display text for visit status
    switch ($_GET['event_status']) {
        case 0:
            $status = "<img src='" . APP_PATH_IMAGES . "star_empty.png' style='position:relative;top:1px;'> <b style='color:#777;'>{$lang['calendar_popup_ajax_08']}</b>";
 public static function getCustomRecordLabelsSecondaryFieldAllRecords($records = array(), $removeHtml = false, $arm = null, $boldSecondaryPkValue = false, $cssClass = 'crl')
 {
     global $is_child, $secondary_pk, $custom_record_label, $Proj;
     // Determine which arm to pull these values for
     if ($arm == 'all' && $Proj->longitudinal && $Proj->multiple_arms) {
         // If project has more than one arm, then get first event_id of each arm
         $event_ids = array();
         foreach (array_keys($Proj->events) as $this_arm) {
             $event_ids[] = $Proj->getFirstEventIdArm($this_arm);
         }
     } else {
         // Get arm
         if ($arm === null) {
             $arm = getArm();
         }
         // Get event_id of first event of the given arm
         $event_ids = array($Proj->getFirstEventIdArm(is_numeric($arm) ? $arm : getArm()));
     }
     // Place all records/labels in array
     $extra_record_labels = array();
     // If $records is a string, then convert to array
     $singleRecordName = null;
     if (!is_array($records)) {
         $singleRecordName = $records;
         $records = array($records);
     }
     // Set flag to limit records
     $limitRecords = !empty($records);
     // Customize the Record ID pulldown menus using the SECONDARY_PK appended on end, if set.
     if ($secondary_pk != '' && !$is_child) {
         // Get validation type of secondary unique field
         $val_type = $Proj->metadata[$secondary_pk]['element_validation_type'];
         $convert_date_format = substr($val_type, 0, 5) == 'date_' && (substr($val_type, -4) == '_mdy' || substr($val_type, -4) == '_mdy');
         // Set secondary PK field label
         $secondary_pk_label = $Proj->metadata[$secondary_pk]['element_label'];
         // PIPING: Obtain saved data for all piping receivers used in secondary PK label
         if (strpos($secondary_pk_label, '[') !== false && strpos($secondary_pk_label, ']') !== false) {
             // Get fields in the label
             $secondary_pk_label_fields = array_keys(getBracketedFields($secondary_pk_label, true, true, true));
             // If has at least one field piped in the label, then get all the data for these fields and insert one at a time below
             if (!empty($secondary_pk_label_fields)) {
                 $piping_record_data = Records::getData('array', $records, $secondary_pk_label_fields, $event_ids);
             }
         }
         // Get back-end data for the secondary PK field
         $sql = "select record, event_id, value from redcap_data \n\t\t\t\t\twhere project_id = " . PROJECT_ID . " and field_name = '{$secondary_pk}' \n\t\t\t\t\tand event_id in (" . prep_implode($event_ids) . ")";
         if ($limitRecords) {
             $sql .= " and record in (" . prep_implode($records) . ")";
         }
         $q = db_query($sql);
         while ($row = db_fetch_assoc($q)) {
             // Set the label for this loop (label may be different if using piping in it)
             if (isset($piping_record_data)) {
                 // Piping: pipe record data into label for each record
                 $this_secondary_pk_label = Piping::replaceVariablesInLabel($secondary_pk_label, $row['record'], $event_ids, $piping_record_data);
             } else {
                 // Static label for all records
                 $this_secondary_pk_label = $secondary_pk_label;
             }
             // If the secondary unique field is a date/time field in MDY or DMY format, then convert to that format
             if ($convert_date_format) {
                 $row['value'] = DateTimeRC::datetimeConvert($row['value'], 'ymd', substr($val_type, -3));
             }
             // Set text value
             $this_string = "(" . remBr($this_secondary_pk_label . " " . ($boldSecondaryPkValue ? "<b>" : "") . filter_tags(label_decode($row['value']))) . ($boldSecondaryPkValue ? "</b>" : "") . ")";
             // Add HTML around string (unless specified otherwise)
             $extra_record_labels[$Proj->eventInfo[$row['event_id']]['arm_num']][$row['record']] = $removeHtml ? $this_string : RCView::span(array('class' => $cssClass), $this_string);
         }
         db_free_result($q);
     }
     // [Retrieval of ALL records] If Custom Record Label is specified (such as "[last_name], [first_name]"), then parse and display
     // ONLY get data from FIRST EVENT
     if (!empty($custom_record_label)) {
         // Loop through each event (will only be one UNLESS we are attempting to get label for multiple arms)
         $customRecordLabelsArm = array();
         foreach ($event_ids as $this_event_id) {
             $customRecordLabels = getCustomRecordLabels($custom_record_label, $this_event_id, $singleRecordName ? $records[0] : null);
             if (!is_array($customRecordLabels)) {
                 $customRecordLabels = array($records[0] => $customRecordLabels);
             }
             $customRecordLabelsArm[$Proj->eventInfo[$this_event_id]['arm_num']] = $customRecordLabels;
         }
         foreach ($customRecordLabelsArm as $this_arm => &$customRecordLabels) {
             foreach ($customRecordLabels as $this_record => $this_custom_record_label) {
                 // If limiting by records, ignore if not in $records array
                 if ($limitRecords && !in_array($this_record, $records)) {
                     continue;
                 }
                 // Set text value
                 $this_string = remBr(filter_tags(label_decode($this_custom_record_label)));
                 // Add initial space OR add placeholder
                 if (isset($extra_record_labels[$this_arm][$this_record])) {
                     $extra_record_labels[$this_arm][$this_record] .= ' ';
                 } else {
                     $extra_record_labels[$this_arm][$this_record] = '';
                 }
                 // Add HTML around string (unless specified otherwise)
                 $extra_record_labels[$this_arm][$this_record] .= $removeHtml ? $this_string : RCView::span(array('class' => $cssClass), $this_string);
             }
         }
     }
     // If we're not collecting multiple arms here, then remove arm key
     if ($arm != 'all') {
         $extra_record_labels = array_shift($extra_record_labels);
     }
     // Return string (single record only)
     if ($singleRecordName != null) {
         return isset($extra_record_labels[$singleRecordName]) ? $extra_record_labels[$singleRecordName] : '';
     } else {
         // Return array
         return $extra_record_labels;
     }
 }
		$('#inputString').keypress(function(e) {
			if (e.which == 13) {
				 $('#inputString').trigger('blur');
				return false;
			}
		});
		$('#inputString').blur(function() {
			var refocus = false;
			var idval = trim($('#inputString').val());
			if (idval.length < 1) {
				return;
			}
			if (idval.length > 100) {
				refocus = true;
				alert('<?php 
    echo remBr($lang['data_entry_186']);
    ?>
');
			}
			if (refocus) {
				setTimeout(function(){document.getElementById('inputString').focus();},10);
			} else {
				$('#inputString').val(idval);
				<?php 
    echo isset($text_val_string) ? $text_val_string : '';
    ?>
				setTimeout(function(){
					idval = $('#inputString').val();
					idval = idval.replace(/&quot;/g,''); // HTML char code of double quote
					// Don't allow pound signs in record names
					if (/#/g.test(idval)) {