<script language="javascript">
	var asInitVals = new Array();
	
	$(document).ready(function() {
		
		$("#partcpnt_id").autocomplete("autocomplete.php?from=lab", {
			width: 260,
			matchContains: true,
			highlight: false,
			autoFill: false,
			scroll: true,
			scrollHeight: 180
		});
                
		$("#partcpnt_id, #survey_id").blur(function(){
			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));
			partid = $("#partcpnt_id").val();
			ctrlid = this.id;
			
			if(partid != "")
			{
				$.ajax({
					type: "POST",
					data: "partid="+partid,
					url: url+"getsurvey.php",						
					error: function(msg){
						alert(msg.status );
					},
					success: function(msg){
						if(msg != '')
						{
							if(ctrlid == 'survey_id' && msg != $("#survey_id").val())
							{
								$("#survey_id").val(msg);
								$("#dataTable").html('');
							}
							else
							{
								$("#survey_id").val(msg);
							}
							$("#survey_id option").each(function(){
								if(this.value != msg)
									$(this).remove();
							}); 
							specimen_data(msg, 'add');
						}
					}
				});
			}
		});
		
		$("#tabs").tabs( {
			"show": function(event, ui) {
				var oTable = $('div.dataTables_scrollBody>table.display', ui.panel).dataTable();
				var tab1 = $("#tabs-1").is(":visible");
				
				if(tab1)
					$("#tab_id").val("1");
				
				if ( oTable.length > 0 ) {
					oTable.fnAdjustColumnSizing();
				}
			}
		} );
	} );
	
	function amt_dis(chk, id)
	{
		if(chk)
			$("#reimbrsmnt_amt_"+id).hide();
		else
			$("#reimbrsmnt_amt_"+id).show();
	}
	
	function specimen_data(surid, mode)
	{
		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));
		partid = $("#partcpnt_id").val();
		
		if(surid != "" && partid != "")
		{
			$.ajax({
				type: "POST",
				data: "frm_name=lab_mapping&surid="+surid+"&partid="+partid+"&mode="+mode,
				url: url+"speciman.php",						
				error: function(msg){
					alert(msg.status );
				},
				success: function(msg){
					// alert(msg);
					if(msg != '')
					{
						$("#dataTable").html(msg);
						if(mode == 'edit')
						{
							// nme = document.getElementsByName('spcmn_type_hdn[]')
							var nme = $(document).find("input[type=checkbox]");
							len = nme.length;
							sel_len = $("select").length;
							
							if(len == 0)
								$("#updt_vchr").val('false');
							
							if(len == 0 && sel_len == 1)
							{
								//$("#acs_div").hide();
								$("#submt_div").hide();
							}
							else
							{
								//$("#acs_div").show();
								$("#submt_div").show();
							}
						}
					}
				}
			});
		}
	}
	
	function enb_cols(pos, mode)
	{
		// alert(chk);
		accs_no = $('#accs_no_txt').val();
		
		/*if(accs_no == '')
		{
			alert('Please Provide Accession Number');
			$('#accs_no_txt').focus();
			return false;
		}
		else
		{
			$('#partcpnt_id').attr("readonly", true);
			$('#accs_no_txt').attr("readonly", true);
		}*/
		
		nme = document.getElementsByName('lab_mstr[]');
		len = nme.length;
		chk = false;
		
		// alert(ctrl.name+" "+len);
		
		$('#ascn_no_'+pos).val(accs_no);
		$('#spn_ascn_no_'+pos).html(accs_no);
		date_val = $('#date_'+pos).val();
		$('#spn_date_'+pos).html(date_val);
		
		if(mode == "add")
		{
			chk = false;
			for(i=0; i<len; i++)
			{
				spcmn_type = 'spcmn_type_'+i;
				spcmn_chk = $('#'+spcmn_type).is(':checked');
				date_id = 'date_col_'+i;
				acsn_id = 'acsn_col_'+i;
				rslt_id = 'result_col_'+i;
				
				if(spcmn_chk)
				{
					chk = true;
				}
				/*$("#"+date_id).show();
				$("#"+acsn_id).show();
				$("#"+rslt_id).show();*/
			}
			
			if(chk)
			{
				$("#div_date_hdr").show();
				$("#div_acsn_hdr").show();
				$("#div_result_hdr").show();
			}
			else
			{
				$("#div_date_hdr").hide();
				$("#div_acsn_hdr").hide();
				$("#div_result_hdr").hide();
			}
			spcmn_type = 'spcmn_type_'+pos;
			spcmn_chk = $('#'+spcmn_type).is(':checked');
			lab_spot_test = 'lab_spot_test_'+pos;
			lab_spot_test_val = $('#'+lab_spot_test).val();
			
			date_id = 'date_col_'+pos;
			acsn_id = 'acsn_col_'+pos;
			rslt_id = 'result_col_'+pos;
			spn_date_id = 'spn_date_'+pos;
			spn_acsn_id = 'spn_ascn_no_'+pos;
			spn_rslt_id = 'result_id'+pos;
			
			if(spcmn_chk) // || spcmn_type_hdn_val == 1)
			{
				// chk = true;
				
				$("#"+spn_date_id).show();
				$("#"+spn_acsn_id).show();
				
				if(lab_spot_test_val == "Y")
					$("#"+spn_rslt_id).show();
				else
					$("#"+spn_rslt_id).hide();
				// break;
			}
			else
			{
				$("#"+spn_date_id).hide();
				$("#"+spn_acsn_id).hide();
				$("#"+spn_rslt_id).hide();
			}
			
			if(spcmn_chk)
			{
				$("#"+date_id).show();
				$("#"+acsn_id).show();
				$("#"+rslt_id).show();
			}
			else
			{
				$("#"+date_id).hide();
				$("#"+acsn_id).hide();
				$("#"+rslt_id).hide();
			}
		}
		else
		{
			spcmn_type = 'spcmn_type_'+pos;
			spcmn_chk = $('#'+spcmn_type).is(':checked');
			lab_spot_test = 'lab_spot_test_'+pos;
			lab_spot_test_val = $('#'+lab_spot_test).val();
			
			spn_date_id = 'spn_date_'+pos;
			spn_acsn_id = 'spn_ascn_no_'+pos;
			spn_rslt_id = 'result_id'+pos;
			
			if(spcmn_chk) // || spcmn_type_hdn_val == 1)
			{
				// chk = true;
				$("#"+spn_date_id).show();
				$("#"+spn_acsn_id).show();
				
				if(lab_spot_test_val == "Y")
					$("#"+spn_rslt_id).show();
				else
					$("#"+spn_rslt_id).hide();
				// break;
			}
			else
			{
				$("#"+spn_date_id).hide();
				$("#"+spn_acsn_id).hide();
				$("#"+spn_rslt_id).hide();
			}
		}
	}
        
	/*function frm_validation()
	{
		if(navigator.appName != "Microsoft Internet Explorer")
			frmname = document.survey;
		
		var tab1 = $("#tabs-1").is(":visible");
		
		if(tab1)
			$("#tab_id").val("1");
		
		if(tab1)
		{
			if(frmname.accs_no.value == "")
			{
				alert("Please Provide Accession Number");
				frmname.accs_no.focus();
				return false;
			}
			if(frmname.tst_mstr.value == "")
			{
				alert("Please Provide Lab Test");
				frmname.tst_mstr.focus();
				return false;
			}
			lab_spcmn = document.getElementsByName('spcmn_type[]');
			if(lab_spcmn.length == 0)
			{
				alert("There is No Specimen for the Selected Lab Test Please Select Any Other");
				frmname.tst_mstr.focus();
				return false;
			}
			else
			{
				if(checkbox_chk_count('spcmn_type[]', 'name') == 0)
				{
					alert("Please Select Any One Specimen");
					lab_spcmn[0].focus();
					return false;
				}
			}
		}
	}*/
	
	function frm_validation()
	{
		if(navigator.appName != "Microsoft Internet Explorer")
			frmname = document.survey;
		
		var tab1 = $("#tabs-1").is(":visible");
		
		if(tab1)
			$("#tab_id").val("1");
		
		if(tab1)
		{
			if(frmname.partcpnt_id.value == "")
			{
				alert("Please Provide Participant ID");
				frmname.partcpnt_id.focus();
				return false;
			}
			if(frmname.survey_id.value == "")
			{
				alert("Please Provide Survey");
				frmname.survey_id.focus();
				return false;
			}
			/*if(frmname.accs_no_txt.value == "")
			{
				alert("Please Provide Accession Number");
				frmname.accs_no_txt.focus();
				return false;
			}*/
			
			lab_mstr = document.getElementsByName('lab_mstr[]');
			len = lab_mstr.length;
			var flag = false;
			
			for(i=0; i<len; i++)
			{
				spcmn_type = 'spcmn_type_'+i;
				spcmn_chk = $('#'+spcmn_type).is(':checked');
				spot_test = 'result_id'+i;
				spot_test_val = $('#'+spot_test).val();
				// alert(spot_test_val);
				
				acsn_id = 'acsn_col_'+i; // added by Vidhya - 20-10-2017
				asc_val = document.getElementById(acsn_id).innerText;
				
				if(spcmn_chk || asc_val.trim() != '') // added by Vidhya - 20-10-2017
				{
					flag = true;
					break;
				}
			}
			
			var nme = $(document).find("input[type=checkbox]");
			len = nme.length;
		
			if(!flag && len != 0)
			{
				alert("Please provide any one sample");
				$('#spcmn_type_0').focus();
				return false;
			}
			else
			{
				for(i=0; i<len; i++)
				{
					spcmn_type = 'spcmn_type_'+i;
					spcmn_chk = $('#'+spcmn_type).is(':checked');
					result_id = 'result_id'+i;
					result_vsb = $('#'+result_id).is(':visible');
					// result_id = $('#'+result_id).val();
					
					if($('#'+result_id).val() == "" && spcmn_chk && result_vsb)
					{
						alert("Please provide Test Result");
						$('#'+result_id).focus();
						return false;
					}
				}
			}
		}
	}
	
	function assign_partid(val)
	{
		// alert(val);
		$("#partcpnt_id").val(val["reg_id"]);
		close_frame();
		
		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));
		partid = $("#partcpnt_id").val();
		survyid = val["sur_id"];
		ctrlid = this.id;
		
		if(partid != "")
		{
			$.ajax({
				type: "POST",
				data: "partid="+partid+"&survyid="+survyid,
				url: url+"getsurvey.php",						
				error: function(msg){
					alert(msg.status );
				},
				success: function(msg){
					if(msg != '')
					{
						if(ctrlid == 'survey_id' && msg != $("#survey_id").val())
						{
							$("#survey_id").val(msg);
							$("#dataTable").html('');
						}
						else
						{
							$("#survey_id").val(msg);
						}
						$("#survey_id option").each(function(){
							if(this.value != msg)
								$(this).hide();
							else
								$(this).show();
						}); 
						specimen_data(msg, 'add');
					}
				}
			});
		}
	}
	
	function frm_edit_val(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["lab_part_id"] != "")
		{
			$.ajax({
				type: "POST",
				data: "page="+getUrlVars()["page"]+"&mode=edit&id="+frmval["lab_part_id"]+"&surid="+frmval["lab_sur_id"],
				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();
				}
			});
		}
	}
</script>
<style>
	#dataTable {
		line-height: 15px !important
	}
</style>
<div style="width: 95%; margin: 0 auto; padding: 10px;">
	<div id="frm_display">
        <div id="tabs">
            <ul>
                <li class="active"><a href="#tabs-1">&nbsp;Lab Sample Collection&nbsp;</a></li>
            </ul>
            
            <form class="form" name="survey" id="survey" method="post" action="<?php echo $action; ?>" onsubmit="return frm_validation();">
                <div id="tabs-1">
                    <div class="questionrow">
                        <div class="question">Participant ID</div>
                        <div class="answer">
                        	<input type="text" name="partcpnt_id" id="partcpnt_id" value="" readonly />
							<img src="includes/images/search_img.png" style="vertical-align: middle;" onclick="open_frame('autocomplete.php?from=lab');">
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div class="question">Survey</div>
                        <div class="answer">
                        	<input type="hidden" name="tab_id" id="tab_id" value="" />
                        	<input type="hidden" name="updt_vchr" id="updt_vchr" value="true" />
                            <?php
                                $common->dropdown('sur_code', 'sur_title', 'custom_survey_master', '', 'sur_title', 'survey_id', $survey_id, 'class="required" onchange="return specimen_data(this.value);"');
                            ?>
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow" id="acs_div" style="display: none;">
                        <div class="question">Accession Number</div>
                        <div class="answer">
                        	<input type="text" name="accs_no_txt" id="accs_no_txt" value="" />
                        </div>
                    </div>
                    <div class="clear_form" style="display: none;">
                    </div>
                    <div class="questionrow">
                        <br/>
                    	<table border="0" cellpadding="0" cellspacing="0" style="margin-left: -23%; width: 146%;" id="dataTable" align="center">
                        	<!--<tr>
                            	<td class="question" style="width: 35%;">Test</td>
                                <td class="question" style="width: 35%;">Specimen</td>
                                <td class="question" style="width: 15%;">Collected?</td>
                                <td class="question" style="width: 10%;" align="center">
                                	<a style="cursor: pointer;" onclick="addRow_dtl('dataTable');">
	                                	<img src="includes/images/add.png" />
                                    </a>
                                </td>
                            </tr>
                            <tr>
                            	<td class="question" style="width: 35%;">
                                	<?php
										// $common->dropdown('lab_id', 'lab_test_name', 'custom_lab_master', '', 'lab_test_name', 'lab_mstr[]', $lab_mstr, 'class="required"');
									?>
                                </td>
                                <td class="question" style="width: 35%;">
                                	<?php
										// $common->dropdown('lab_spcmn_id', 'lab_spcmn_name', 'custom_lab_spceimen_master', '', 'lab_spcmn_name', 'spcmn_id[]', $spcmn_id, 'class="required"');
									?>
                                </td>
                                <td class="question" style="width: 15%;" align="center">
                                	<input type="checkbox" value="1" name="spcmn_type[]" id="spcmn_type1" />
                                </td>
                                <td class="question" style="width: 10%;" align="center">
                                	<a style="cursor: pointer;" onclick="whichRow('dataTable', this);">
	                                	<img src="includes/images/delete.png" />
                                    </a>
                                </td>
                            </tr> -->
                        </table>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow" id="submt_div">
                        <br/>
                        <div style="padding: 5px;" align="center">
                            <input type="submit" name="Submit" value="Submit" />
                        </div>
                       
                    </div>
                    
		</div>
            </form>
		</div>
	</div>
</div>
<script language="javascript">
	arr_id_change('lab_mstr[]');
	arr_id_change('spcmn_id[]');
</script>