<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");
				
				if(tab1)
					$("#tab_id").val("1");
				
				if ( oTable.length > 0 ) {
					oTable.fnAdjustColumnSizing();
				}
			}
		} );
	} );
	
	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.survey_id.value == "")
			{
				alert("Please Provide Survey");
				frmname.survey_id.focus();
				return false;
			}
			lab_mstr = document.getElementsByName('lab_mstr[]');
			len = lab_mstr.length;
			var flag = true;
			
			for(i=0; i<len; i++)
			{
				if(lab_mstr[i].value == "")
				{
					flag = false;
					break;
				}
			}
			if(!flag)
			{
				alert("Please Provide Test");
				lab_mstr[i].focus();
				return false;
			}
			
			spcmn_id = document.getElementsByName('spcmn_id[]');
			len = spcmn_id.length;
			var flag = true;
			
			for(i=0; i<len; i++)
			{
				if(spcmn_id[i].value == "")
				{
					flag = false;
					break;
				}
			}
			if(!flag)
			{
				alert("Please Provide Specimen");
				spcmn_id[i].focus();
				return false;
			}
			else
			{
				for(i=0; i<len; i++)
				{
					if(lab_mstr[i].value != "")
					{
						for(j=0; j<i; j++)
						{
							if(spcmn_id[j].value == spcmn_id[i].value && lab_mstr[j].value == lab_mstr[i].value)
							{
								// alert(spcmn_id[j].value+" == "+spcmn_id[j].value+" && "+lab_mstr[j].value+" == "+lab_mstr[i].value);
								// alert("Please Provide Unique Test and Specimen");
								// alert("Duplicate Test and Specimen Entry Found");
								alert("Duplicate Test and Specimen Entry Not Allowed");
								return false;
							}
						}
					}
				}
			}
		}
	}
	
	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';
			
			if((colCount-1) == i)
			{
				newcell.align = 'center';
				/*newcell.style.width = '9%';
				newcell.style.padding = '0 0 0 4px';*/
			}
			else
                        {
				/*newcell.style.width = '44%';
				newcell.style.padding = '0 0 0 5px';*/
                        }
		}
		/*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;
		//}
		
	}
	
	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 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>
<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 Test Mapping&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">Survey</div>
                        <div class="answer">
                            <?php
								if($survey_id == "")
									$survey_id = SURVY;
								
								if(SURVYFXD == 'Y')
								{
									echo '<input type="hidden" name="survey_id" id="survey_id" value="'.SURVY.'" />';
									echo SURVYTITLE;
								}
								else
								{
									$common->dropdown('sur_code', 'sur_title', 'custom_survey_master', 'sur_code NOT IN (SELECT lab_mapng_sur_id FROM custom_lab_mapping)', 'sur_title', 'survey_id', $survey_id, 'class="required"');
								}
                            ?>
                        </div>
                    </div>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <br/>
                    	<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 40px;" id="dataTable" align="center">
                        	<tr>
                            	<td class="question_title" width="50%">Test</td>
                                <td class="question_title">Specimen</td>
                                <td class="question_title" align="center">
                                	<a style="cursor: pointer;" onclick="addRow_dtl('dataTable');">
	                                	<img src="includes/images/add.png" />
                                    </a>
                                </td>
                            </tr>
                            <tr>
                            	<td class="question">
									&nbsp;
                                	<?php
										$common->dropdown('lab_id', 'lab_test_name', 'custom_lab_master', 'status = "Y"', 'lab_test_name', 'lab_mstr[]', $lab_mstr, 'class="required" style="max-width: 310px;"');
									?>
                                </td>
                                <td class="question">
									&nbsp;
                                	<?php
										$common->dropdown('lab_spcmn_id', 'lab_spcmn_name', 'custom_lab_spceimen_master', 'status = "Y"', 'lab_spcmn_name', 'spcmn_id[]', $spcmn_id, 'class="required"');
									?>
                                </td>
                                <td class="question" 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">
                        <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>