<script language="javascript">
    var asInitVals = new Array();

    $(document).ready(function() {
        $("#tabs").tabs( {
            "show": function(event, ui) {
                var oTable = $('div.dataTables_scrollBody>table.display', ui.panel).dataTable();
                var tab1 = $("#tabs-1").is(":visible");
                var tab2 = $("#tabs-2").is(":visible");
                var tab3 = $("#tabs-3").is(":visible");

                if(tab1)
                {
                    $("#tab_id").val("1");
                }
                if(tab2)
                {
                    $("#tab_id").val("2");
                }
                if(tab3)
                {
                    $("#tab_id").val("3");
                }

                if ( oTable.length > 0 ) {
                    oTable.fnAdjustColumnSizing();
                }
            }
        } );
		
		$('#q1, #q2, #q3, #q4, #q7, #q8, #q13').change(function(){
			default_val();
		});
    } );
	
	function default_val()
	{ 
		
		//if(($('#q1').val() == 0) && ($('#q2').val() == 1) && ($('#q3').val() == 1) && ($('#q4').val() >= 18) && ($('#q7').val() == 1) && ($('#q8').val() == 1 ))
		// commented above by vidhya - 04012019 - to remove check on qn2
		if(($('#q1').val() == 0) && ($('#q3').val() == 1) && ($('#q4').val() >= 18) && ($('#q7').val() == 1) && ($('#q8').val() == 1 ))
			$("#q13").val(1);
		else
			$("#q13").val(0);
	}
    function part_id()
    {
        if(navigator.appName != "Microsoft Internet Explorer")
            frmname = document.recuirtment;

        lcn = frmname.location.value;
        lcn_typ = frmname.location_type.value;
        frmname.participant_id.value = lcn+lcn_typ;
    }
	
	jQuery(function(e1){
		jQuery("#q1").validate({
			expression: "if (VAL) return true; else return false;",
			message: "Please Answer for Q1"
		});
		jQuery("#q1a").validate({
			expression: "if ((jQuery('#q1').val() == 0) || VAL) return true; else return false;",
			message: "Please Answer for Q1a"
		});
		jQuery("#q1a").validate({
			expression: "if ((jQuery('#q1').val() == 0) || VAL.match(/^[A-Z]{2}[0-9]{4}$/)) return true; else return false;",
			message: "Please Provide Valid Value in Q1a"
		});
		jQuery("#q2").validate({
			expression: "if ((jQuery('#q1').val() == 1) || VAL) return true; else return false;",
			message: "Please Answer for Q2"
		});
		jQuery("#q3").validate({
			expression: "if ((jQuery('#q1').val() == 1) || jQuery('#q2').val() == 0 || VAL) return true; else return false;",
			message: "Please Answer for Q3"
		});
		jQuery("#q4").validate({
			expression: "if ((jQuery('#q1').val() == 1) || VAL) return true; else return false;",
			message: "Please Answer for Q4"
		});
		jQuery("#q5").validate({
			expression: "if ((jQuery('#q1').val() == 1) || (jQuery('#q3').val() == 0) || VAL) return true; else return false;",
			message: "Please Answer for Q5"
		});
		jQuery("#q5").validate({
			expression: "if ((jQuery('#q1').val() == 0 && eval(VAL) <= jQuery('#q4').val()) || jQuery('#q1').val() == 1 || jQuery('#q3').val() == 0) return true; else return false;",
			message: "Answer for Q5 Cannot be greater than age (Q4)"
		});
		jQuery("#q6").validate({
			expression: "if ((jQuery('#q1').val() == 1) || (jQuery('#q3').val() == 0) || VAL) return true; else return false;",
			message: "Please Answer for Q6"
		});
		jQuery("#q7").validate({
			expression: "if ((jQuery('#q1').val() == 1) || (jQuery('#q3').val() == 0) || VAL) return true; else return false;",
			message: "Please Answer for Q7"
		});
		jQuery("#q8").validate({
			expression: "if ((jQuery('#q1').val() == 1) || (jQuery('#q3').val() == 0) || (jQuery('#q7').val() == 0) || VAL) return true; else return false;",
			message: "Please Answer for Q8"
		});
		jQuery("#q9").validate({
			expression: "if ((jQuery('#q1').val() == 1) || VAL || jQuery('#q8').val() == 'F' || jQuery('#q8').val() == 'H') return true; else return false;",
			message: "Please Answer for Q9"
		});
		jQuery("#q10").validate({
			expression: "if ((jQuery('#q1').val() == 1) || (jQuery('#q3').val() == 0) || VAL) return true; else return false;",
			message: "Please Answer for Q10"
		});
		jQuery("#q11").validate({
			expression: "if ((jQuery('#q1').val() == 1) || (jQuery('#q3').val() == 0) || VAL) return true; else return false;",
			message: "Please Answer for Q11"
		});
		jQuery("#q12").validate({
			expression: "if ((jQuery('#q1').val() == 1) || (jQuery('#q3').val() == 0) || VAL) return true; else return false;",
			message: "Please Answer for Q12"
		});
	});
	
    function frm_validation()
    {
        if(navigator.appName != "Microsoft Internet Explorer")
            frmname = document.recuirtment;

        var tab1 = $("#tabs-1").is(":visible");
        var tab2 = $("#tabs-2").is(":visible");
        var tab3 = $("#tabs-3").is(":visible");

        if(tab1)
            $("#tab_id").val("1");
        if(tab2)
            $("#tab_id").val("2");
        if(tab3)
            $("#tab_id").val("3");
	}
	
	function frm_edit_val(frmval)
	{
		if(frmval != "")
		{
			var url = "";
			var loc = window.location;
			var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
			url = loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
			
			if(frmval != "")
			{
				$.ajax({
					type: "POST",
					data: "page="+getUrlVars()["page"]+"&mode=edit&id="+frmval,
					url: url+"index.php?page="+getUrlVars()["page"],
					error: function(msg){
						alert( msg.status );
					},
					success: function(msg){
						// alert(msg);
						close_frame();
						$('#tabs-1').html(msg);
						$("#delete_img").show();
					}
				});
			}
		}
	}
	
	function skip_q9(val)
	{
		if(val == "F" || val == "H")
		{
			$("#disp_q9").hide();
		}
		else
		{
			$("#disp_q9").show();
		}
	}
	
	function enb_q1(val)
	{
		if(val == 1)
		{
			$("#disp_q1a").show();
			$("#disp_q2").hide();
			$("#disp_q3").hide();
			$("#disp_q4").hide();
			$("#disp_q5").hide();
			$("#disp_q6").hide();
			$("#disp_q7").hide();
			$("#disp_q8").hide();
			$("#disp_q9").hide();
			$("#disp_q10").hide();
			$("#disp_q11").hide();
			$("#disp_q12").hide();
			
			$("#q2").val("");
			$("#q3").val("");
			$("#q4").val("");
			$("#q5").val("");
			$("#q6").val("");
			$("#q7").val("");
			$("#q8").val("");
			$("#q9").val("");
			$("#10").val("");
			$("#11").val("");
			$("#12").val("");
		}
		else
		{
			$("#disp_q1a").hide();
			$("#disp_q2").show();
			$("#disp_q3").show();
			$("#disp_q4").show();
			$("#disp_q5").show();
			$("#disp_q6").show();
			$("#disp_q7").show();
			$("#disp_q8").show();
			$("#disp_q9").show();
			$("#disp_q10").show();
			$("#disp_q11").show();
			$("#disp_q12").show();
		}
		var q3_val = $("#q3").val();
		enb_q3(q3_val);
	}
	
	function enb_q2(val)
	{
		if(val == 0)
		{
			$("#disp_q3").hide();
			$("#disp_q5").hide();
			$("#disp_q6").hide();
			$("#disp_q7").hide();
			$("#disp_q8").hide();
			$("#disp_q10").hide();
			$("#disp_q11").hide();
			$("#disp_q12").hide();
			
			$("#q3").val("");
			$("#q5").val("");
			$("#q6").val("");
			$("#q7").val("");
			$("#q8").val("");
			$("#10").val("");
			$("#11").val("");
			$("#12").val("");
		}
		else
		{
			$("#disp_q3").show();
			$("#disp_q5").show();
			$("#disp_q6").show();
			$("#disp_q7").show();
			$("#disp_q8").show();
			$("#disp_q10").show();
			$("#disp_q11").show();
			$("#disp_q12").show();
		}
		var q3_val = $("#q3").val();
		enb_q3(q3_val);
	}
	
	function enb_q3(val)
	{
		if(val == 0)
		{
			$("#disp_q5").hide();
			$("#disp_q6").hide();
			$("#disp_q7").hide();
			$("#disp_q8").hide();
			$("#disp_q10").hide();
			$("#disp_q11").hide();
			$("#disp_q12").hide();
		}
		else
		{
			$("#disp_q5").show();
			$("#disp_q6").show();
			$("#disp_q7").show();
			$("#disp_q8").show();
			$("#disp_q10").show();
			$("#disp_q11").show();
			$("#disp_q12").show();
		}
	}
	
	function enb_q7(val)
	{
		if(val == 0)
		{
			$("#disp_q8").hide();
			$("#q8").val("");
		}
		else
		{
			$("#disp_q8").show();
		}
	}
</script>
<style>
div.ValidationErrors {
    color: #DD0000;
    font-size: 12px;
    font-style: italic;
    padding-top: 10px;
    font-weight: bold;
}
.form {
	width: 100%;
}
.question {
	width: 60%;
}
.answer {
	width: 40%;
}
</style>
<?php
	$yes_no = array(0 => "No", 1 => "Yes");
	$yes_no_dk = array(0 => "No", 1 => "Yes", 997 => "Don't Know", 998 => "Refused");
	$q9 = array(1 => "Panthi", 2 => "Kothi", 3 => "Double-decker", 4 => "Gay", 5 => "Bisexual", 6 => "MSM", 7 => "Other");
	$q8_default = array(1 => "Within the last year", 2 => "More than 1 year to 2 years ago", 3 => "More than 2 years to 5 years ago", 4 => "More than 5 years to 10 years ago", 5 => "More than 10 years ago");
	$q9_default = array(1 => "Male", 2 => "Female", 3 => "Hijra/Transgender");
?>
<div style="width: 95%; margin: 0 auto; padding: 10px;" id="content">
    <div id="frm_display">
        <div id="tabs">
            <ul>
                <li class="active"><a href="#tabs-1">&nbsp;Screening Form&nbsp;</a></li>
            </ul>
            <form class="form" name="recuirtment" id="recuirtment" method="post" action="<?php echo $action; ?>" onsubmit="return frm_validation();">
                <div id="tabs-1">
					<div id="disp_q0" style="display: none;">
						<div class="questionrow">
							<div class="question">Participant ID</div>
							<div class="answer">
								<input type="hidden" name="loc_id" id="loc_id" value="<?php echo $loc_id; ?>" readonly />
								<input type="hidden" name="partcpnt_id" id="partcpnt_id" value="<?php echo $part_id; ?>" readonly />
								<!--<img src="includes/images/search_img.png" style="vertical-align: middle;" onclick="open_frame('autocomplete.php');">-->
							</div>
						</div>
						<div class="clear_form">
						</div>
					</div>
					<div id="disp_q1">
						<div class="questionrow">
							<div class="question">
								1. Is the participant a duplicate enrollment based on fingerprint?
								<div style="padding: 5px 0px; font-size: 90%;">
									<strong>Interviewer Instruction:</strong> This will be determined at the front desk before the participant goes for screening. There will be a note on the checklist that the participant is a duplicate. The note will also include this persons original unique Study ID.
								</div>
							</div>
							<div class="answer">
								<input type="hidden" name="id" id="id" value="<?php echo $id; ?>" />
								<input type="hidden" name="sur_id" id="sur_id" value="<?php echo $sur_id; ?>" />
								<input type="hidden" name="tab_id" id="tab_id" value="" />
								<?php echo $common->generateSelect('q1', $yes_no, $q1, 'onchange="return enb_q1(this.value);"'); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
					</div>
					<div id="disp_q1a" style="display: none;">
						<div class="questionrow">
							<div class="question">1a. Participant’s Duplicate Study ID (Copy from Fingerprint Application)</div>
							<div class="answer">
								<input type="text" name="q1a" id="q1a" value="<?php echo $q1a; ?>" style="text-transform: uppercase;" maxlength="6" size="7" />
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
                    <!--   commented q2 by vidhya - 04012019 -->
                    <!--  
					<div id="disp_q2">
						<div class="questionrow">
							<div class="question">
								2. Does the participant have a valid coupon?
								<div style="padding: 5px 0px; font-size: 90%;">
									<strong>Interviewer Instruction:</strong> This will be determined at the front desk before the participant goes for screening. There will be a note on the checklist if the coupon is not valid.
								</div>
							</div>
							<div class="answer">
								<?php echo $common->generateSelect('q2', $yes_no, $q2, 'onchange="return enb_q2(this.value);"'); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
                    -->
					<div id="disp_q3">
						<div class="questionrow">
							<div class="question">3. Did the participant provide informed consent?</div>
							<div class="answer">
								<?php echo $common->generateSelect('q3', $yes_no, $q3, 'onchange="return enb_q3(this.value);"'); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
					<div id="disp_q4">
						<div class="questionrow">
							<div class="question">4. What is your Date of Birth?</div>
							<div class="answer">
								<input type="hidden" name="dt_fmt" id="dt_fmt" value="<?php echo DT_FMT; ?>" />
								<input type="text" name="dob" id="dob" value="<?php echo $dob; ?>" readonly="readonly" size="10" />
								<script>
									$(function() {
										$('#dob').datepick({pickerClass: 'datepick-jumps', 
											renderer: $.extend({}, $.datepick.defaultRenderer, 
											{picker: $.datepick.defaultRenderer.picker. 
												replace(/\{link:prev\}/, '{link:prevJump}{link:prev}'). 
												replace(/\{link:next\}/, '{link:nextJump}{link:next}')
											}), 
											dateFormat: '<?php echo DT_FMT; ?>', 
											yearRange: '1900:<?php echo date('Y'); ?>',
											maxDate: new Date(),
											showTrigger: '#calImg',
											onSelect: showDateto,
											// altField: "#q4",
											// altFormat: "dd/mm/yyyy",
										});
									});
									function showDateto(date) {
										dob = $("#dob").val();
										q4 = $("#q4").val();
										if(dob == "")
										{
											var birthdate = q4.split("/");
											var born = new Date(birthdate[2], birthdate[1]-1, birthdate[0]);
											dob = dateFormat(born, '<?php echo DT_FMT; ?>');
											$("#dob").val(dob);
										}
										else
											q4 = dob.replace(/-/g, "/");
										
										// console.log(dob);
										// console.log(q4);
										
										var now = new Date();
										var birthdate = q4.split("/");
										var born = new Date(birthdate[2], birthdate[1]-1, birthdate[0]);
										age = get_age(born, now);
										$("#q4").val(age);
									}
								</script>
								<input type="text" name="q4" id="q4" value="<?php echo $q4; ?>" readonly="readonly" size="5" /> Years
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
                    <!--   commented q5 and q6 by vidhya - 04012019 -->
                    <!--  
					<div id="disp_q5">
						<div class="questionrow">
							<div class="question">
								5. How long have you lived where you are living in now?
								<div style="padding: 5px 0px; font-size: 90%;"><strong>Interviewer Instruction:</strong> Code 00 if less than 1 year.</div>
							</div>
							<div class="answer">
								<input type="text" name="q5" id="q5" value="<?php echo $q5; ?>" maxlength="3" onkeyup="return acceptDigits(this);" size="5" /> Years
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
					<div id="disp_q6">
						<div class="questionrow">
							<div class="question">6. Do you have any plans to move from here in the next two years?</div>
							<div class="answer">
								<?php echo $common->generateSelect('q6', $yes_no_dk, $q6); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
                    -->
					<div id="disp_q7">
						<div class="questionrow">
							<div class="question">7. Have you ever injected drugs for non-medicinal reasons?</div>
							<div class="answer">
								<?php echo $common->generateSelect('q7', $yes_no, $q7, 'onchange="return enb_q7(this.value);"'); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
                   
                    
                      
					<div id="disp_q8">
						<div class="questionrow">
							<div class="question">
								12. When was the last time you injected drugs?
								<div style="padding: 5px 0px; font-size: 90%;">
									<strong>Interviewer Instruction:</strong> Ask question and let participant respond openly. Then fit their answer into one of the options.
								</div>
							</div>
							<div class="answer">
								<?php echo $common->generateSelect('q8', $q8_default, $q8); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
                     <!--   commented q9 q10 q11 q12 by vidhya - 04012019 - renumbered q8 as q12 -->
                    <!--  
					<div id="disp_q9">
						<div class="questionrow">
							<div class="question">9. Do you consider yourself to be?</div>
							<div class="answer">
								<?php $common->dropdown('gen_code', 'gen_name', 'custom_gender_master', '', 'gen_id', 'q9', $q9_ans); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
					<div id="disp_q10">
						<div class="questionrow">
							<div class="question">10. Have you ever been incarcerated?</div>
							<div class="answer">
								<?php echo $common->generateSelect('q10', $yes_no_dk, $q10); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
					<div id="disp_q11">
						<div class="questionrow">
							<div class="question">11. Have you ever been tested for HIV?</div>
							<div class="answer">
								<?php echo $common->generateSelect('q11', $yes_no_dk, $q11); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
					<div id="disp_q12">
						<div class="questionrow">
							<div class="question">12. Have you ever received opiate substitution therapy (ADDNOK, sublingual, etc.)?</div>
							<div class="answer">
								<?php echo $common->generateSelect('q12', $yes_no_dk, $q12); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
                    -->
					<div id="disp_q13">
						<div class="questionrow">
							<div class="question">Participant Eligibility</div>
							<div class="answer">
								<?php echo $common->generateSelect('q13', $yes_no, $q13, "style='background: #cfcfcf;'"); ?>
							</div>
						</div>
						<div class="clear_form">
						</div>
                    </div>
                    <div class="questionrow">
                        <div style="padding: 5px;" align="center">
                            <input type="submit" name="Submit" value="Submit" />
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<?php
	if($q1 != "" && $q1 == "1")
	{
		?>
		<script>
			enb_q1(<?php echo $q1; ?>);
		</script>
		<?php
	}
	if($q3 != "" && $q3 == "0")
	{
		?>
		<script>
			enb_q3(<?php echo $q3; ?>);
		</script>
		<?php
	}
?>