<script language="javascript">
	var asInitVals = new Array();
	
	$(document).ready(function() {
		
		$("#partcpnt_id").autocomplete("autocomplete.php?from=reimbursement", {
			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=reibmusement&surid="+surid+"&partid="+partid+"&mode="+mode,
				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();
							$("#partcpnt_id").attr("readonly", true);
						}
						else
						{
							$("#div_submit").hide();
							$("#partcpnt_id").attr("readonly", false);
						}
					}
					else
					{
						$("#dataTable").html('');
						$("#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;
				}
				if(frmname.vchr_pay_amt.value == "")
				{
					alert("Please Provide Pay Amount");
					frmname.vchr_pay_amt.focus();
					return false;
				}
				else
				{
					if(eval(frmname.vchr_pay_amt.value) > eval(frmname.vchr_amt.value))
					{
						alert("Pay Amount Should not be Greater Than the Amount");
						frmname.vchr_pay_amt.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); ?>
					$("#question_output").html(data);
					// window.open("<?php echo $act; ?>&id="+frmname.partcpnt_id.value+"&sur_id="+frmname.survey_id.value, "print", 'width=700,height=600');
					// location.href = '<?php echo $link; ?>';
					// alert(data);
					// window.self.close();
				});
				return false;
			}
		}
		else
		{
			<?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, "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);
		close_frame();
		frm_edit_val(val);
	}
	
	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));
		
		$.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();
				}
		});
	}
</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;Reimbursement Payments&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="hidden" name="id" id="id" value="" />
                        	<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=reimbursement_payments');">
                        </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="vchr_no" id="vchr_no" value="<?php echo $vchr_no; ?>" />
                            <?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">
                        <div style="padding: 5px; display: none;" align="center" id="div_submit">
                            <!--<input type="submit" name="Submit" value="Save & Print" />-->
                            <input type="button" id="save" name="save" value="Save & Print" 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>
<div id="question_output">
</div>
<script language="javascript">
	arr_id_change('lab_mstr[]');
	arr_id_change('spcmn_id[]');
	$("#print").hide();
</script>