<script language="javascript">
	var asInitVals = new Array();
	
	$(document).ready(function() {
		
		$("#partcpnt_id").autocomplete("autocomplete.php?from=incentive", {
			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=incentive&surid="+surid+"&partid="+partid+"&mode="+mode+"&incentive_id="+$("#incentive_id").val(),
				url: url+"speciman.php",						
				error: function(msg){
					alert(msg.status );
				},
				success: function(msg){
					// alert(msg);
					if(msg != '')
					{
						// alert(msg);
						$("#dataTable").html(msg);
						
						var n=msg.indexOf("Participant Not Eligible"); 
						if(n < 0)
						{
							$("#div_submit").show();
							issue_val = document.getElementsByName('issue_val[]');
							len = issue_val.length;
							// alert(len);
							j = 0;
							for(i=0; i<len; i++)
							{
								if(issue_val[i].value == "NA")
									j++;
							}
							// alert(len+" "+j);
							
							/*if(len == j)
								$("#div_submit").hide();
							else
								$("#div_submit").show();*/
							
							var len = $("input[type=checkbox]").length;
							
							if(len == 0)
								$("#save").hide();
						}
						else
							$("#div_submit").hide();
					}
				}
			});
		}
	}
	
	function frm_validation(in_val)
	{
		if(navigator.appName != "Microsoft Internet Explorer")
			frmname = document.survey;
		
		if(in_val == 1)
		{
			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;
				}
				/*reimburs_amt = document.getElementsByName('reimburs_amt[]');
				len = (reimburs_amt.length-1);
				var flag = true;
				
				for(i=0; i<len; i++)
				{
					if(reimburs_amt[i].value == "")
					{
						flag = false;
						break;
					}
				}
				if(!flag)
				{
					alert("Please Provide Reimbursement Amount");
					reimburs_amt[i].focus();
					return false;
				}*/
								
							
				reimburs_amt = document.getElementsByName('reimburs_amt[]');
				issue = document.getElementsByName('issue[]');
				len = issue.length;
				var flag = false;
				
				for(i=0; i<len; i++)
				{
					issue_type = 'issue_'+i;
					isuue_chk = $('#'+issue_type).is(':checked');
					
					if(isuue_chk)
					{
						if(reimburs_amt[i].value == "")
						{
							alert("Please Provide Incentive Amount");
							reimburs_amt[i].focus();
							return false;
						}
						if(isNaN(reimburs_amt[i].value))
						{
							alert("Incentive Amount should be Number");
							reimburs_amt[i].focus();
							return false;
						}
						if(parseFloat(reimburs_amt[i].value) <= 0)
						{
							alert("Incentive Amount should be greater than Zero");
							reimburs_amt[i].focus();
							return false;
						}				
						
					}
				}
				
				for(i=0; i<len; i++)
				{
					spcmn_type = 'issue_'+i;
					spcmn_chk = $('#'+spcmn_type).is(':checked');
					
					if(spcmn_chk)
					{
						if(reimburs_amt[i].value == "")
						{
							alert("Please Provide Incentive Amount");
							reimburs_amt[i].focus();
							return false;
						}
						if(isNaN(reimburs_amt[i].value))
						{
							alert("Incentive Amount should be Number");
							reimburs_amt[i].focus();
							return false;
						}
						if(parseFloat(reimburs_amt[i].value) <= 0)
						{
							alert("Incentive Amount should be greater than Zero");
							reimburs_amt[i].focus();
							return false;
						}
											
						flag = true;
						break;
					}
				}
				
				if(!flag)
				{
					alert("Please provide any one issue");
					$('#issue_0').focus();
					return false;
				}
				
				$.post('<?php echo $action; ?>', $('#survey').serialize(), function(data){
					<?php $act = str_replace("submit", "print", $action); ?>
					<?php $link = str_replace("&mode=submit", "", $action); ?>									
			    	//	window.open("<?php echo $act; ?>&id="+frmname.partcpnt_id.value+"&sur_id="+frmname.survey_id.value+"&incentive_id="+incentive_id, "print", 'width=700,height=600');
					location.href = '<?php echo $link; ?>';
					// $("#question_output").html(data);
					// alert(data);
					// window.self.close();
				});
				return false;
			}
		}
		else
		{
			// alert($("input[name=print_enb[]]").length);
			/*$("input[type=checkbox]").each(function(index) {
				alert(index + ': ' + $(this).attr("name"));
			});*/
			/*if($('input[name=print_enb[]]:checked').length == 0)
			{
				msg = "Please Select Any One Option to Print\n";
				alert (msg);
				return false;
			}*/
			// alert($('input[name=print_enb[]]:checked').val());
			
			//incentive_id = $('input[name=print_enb[]]:checked').val();
			incentive_id = frmname.incentive_id.value;
		
			<?php $act = str_replace("submit", "print", $action); ?>
			<?php $link = str_replace("&mode=submit", "", $action); ?>
			 window.open("<?php echo $act; ?>&id="+frmname.partcpnt_id.value+"&sur_id="+frmname.survey_id.value+"&incentive_id="+incentive_id, "print", 'width=700,height=600');
		}
	}
	
	function addRow_dtl(tableID)
	{
		appVersion = navigator.appVersion;
		gen_appVer = appVersion.split(" ");
		gen_appVer = parseInt(gen_appVer[0]);
		
		appVer = appVersion.split(";");
		for(i=0; i<appVer.length; i++)
		{
			n = appVer[i].indexOf("MSIE");
			if(n > 0)
			{
				appVer[i] = parseInt(appVer[i].replace('MSIE ', ''));
				brswr_version = appVer[i];
			}
		}
		
		var table = document.getElementById(tableID);
		var rowCount = table.rows.length;
		pos = (rowCount-2);
		
		var x = document.getElementsByName('lab_mstr[]');
		if(x[pos].value == "")
		{
			alert("Please Provide Test");
			x[pos].focus();
			return false;
		}
		
		var x = document.getElementsByName('spcmn_id[]');
		if(x[pos].value == "")
		{
			alert("Please Provide Specimen");
			x[pos].focus();
			return false;
		}

		var row = table.insertRow(rowCount);
		var colCount = table.rows[1].cells.length;

		for(var i=0; i<colCount; i++)
		{
			var newcell = row.insertCell(i);
			str = table.rows[1].cells[i].innerHTML;
			
			if(navigator.appName == "Microsoft Internet Explorer" && brswr_version < 9)
			{
				// alert(str);
				str = str.replace('id=lab_mstr1', 'id=lab_mstr'+rowCount+'');
				str = str.replace('id=spcmn_id1', 'id=spcmn_id'+rowCount+'');
			}
			else
			{
				str = str.replace('id="lab_mstr1"', 'id="lab_mstr'+rowCount+'"');
				str = str.replace('id="spcmn_id1"', 'id="spcmn_id'+rowCount+'"');
			}
			
			newcell.innerHTML = str;
			newcell.className = 'question';
			
			// alert((colCount-1)+" == "+i+" || "+(colCount-3)+" == "+(i-1));
			
			if((colCount-3) == (i-1))
			{
				newcell.align = 'center';
				newcell.style.width = '15%';
			}
			else if((colCount-1) == i)
			{
				newcell.align = 'center';
				newcell.style.width = '10%';
			}
			else
				newcell.style.width = '35%';
		}
		if(navigator.appName == "Microsoft Internet Explorer")
		{
			lab_mstr = "lab_mstr"+rowCount+"";
			document.getElementById(lab_mstr).selectedIndex = 0;
			spcmn_id = "spcmn_id"+rowCount+"";
			document.getElementById(spcmn_id).selectedIndex = 0;
		}
		arr_id_change('spcmn_type[]');
	}
	
	function whichRow(tableID, obj)
	{ 
		var par=obj.parentNode; 
		while(par.nodeName.toLowerCase()!='tr')
			par=par.parentNode; 
		
		rowid = par.rowIndex+1;
		rowid = par.rowIndex;
		deleteRow_dtl(tableID, rowid)
		// return (par.rowIndex+1); 
	}

	function deleteRow_dtl(tableID, rowid)
	{
		try {
		var table = document.getElementById(tableID);
		var rowCount = table.rows.length;
		
		if(rowid == rowCount)
			rowid--;
		
		if(rowCount <= 2) {
			alert("Cannot delete all the rows.");
			return false;
		}
		table.deleteRow(rowid);
		}catch(e) {
			alert(e);
		}
	}
	
	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 != "undefined")
		{
			$.ajax({
				type: "POST",
				data: "page="+getUrlVars()["page"]+"&mode=edit&id="+frmval["lab_smple_part_id"]+"&incentive_id="+frmval["lab_smple_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();
				}
			});
		}
	}
	
	function frm_print_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 != "undefined")
		{
			$.ajax({
				type: "POST",
				data: "page="+getUrlVars()["page"]+"&mode=edit_print&id="+frmval["lab_smple_part_id"]+"&incentive_id="+frmval["lab_smple_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").hide();
				}
			});
		}
	}
</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;Incentive&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">Particpant ID</div>
                        <div class="answer">
                        	<input type="text" name="partcpnt_id" id="partcpnt_id" value="" readonly />
                        	<input type="hidden" name="incentive_id" id="incentive_id" value="" />
							<img src="includes/images/search_img.png" style="vertical-align: middle;" onclick="open_frame('autocomplete.php?from=incentive');">
                        </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="" />
                            <?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">
                    	<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 40px;" 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="question_output">
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div style="padding: 5px; display: none;" align="center" id="div_submit">
                            <input type="button" id="save" name="save" value="Save" onclick="return frm_validation('1');" />
                            <input type="button" id="print" name="print" value="Print" onclick="return frm_validation('2');" />
                        </div>
                    </div>
		</div>
            </form>
		</div>
	</div>
</div>
<script language="javascript">
	arr_id_change('lab_mstr[]');
	arr_id_change('spcmn_id[]');
	$("#print").hide();
	$("#print_img").show();
</script>