<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 amt_dis(chk, id)
	{
		if(!chk)
        {
			$("#reimbrsmnt_amt_"+id).hide();
			$("#reimbrsmnt_amt_"+id).val('');
			$("#reimbrsmnt_type_val_"+id).val('0');
        }
		else
        {
			$("#reimbrsmnt_amt_"+id).show();
			$("#reimbrsmnt_type_val_"+id).val('1');
        }
	}
	
	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.sur_mstr.value == "")
			{
				alert("Please Provide Survey");
				frmname.sur_mstr.focus();
				return false;
			}
			remimbst_mstr_id = document.getElementsByName('remimbst_mstr_id[]');
			reimbrsmnt_type = document.getElementsByName('reimbrsmnt_type[]');
			reimbrsmnt_type_val = document.getElementsByName('reimbrsmnt_type_val[]');
			reimbrsmnt_amt = document.getElementsByName('reimbrsmnt_amt[]');
			len = reimbrsmnt_type.length;
			
			for(i=0; i<len; i++)
			{
				if(remimbst_mstr_id[i].value == "")
				{
					alert("Please Provide Reimbursement Head");
					$("#remimbst_mstr_id"+(i+1)).focus();
					return false;
				}
				if(reimbrsmnt_type_val[i].value == 1 && reimbrsmnt_amt[i].value == "")
				{
					alert("Please Provide Reimbursement Amount");
					$("#reimbrsmnt_amt_"+i).focus();
					return false;
				}
			}
			
			for(i=0; i<len; i++)
			{
				if(remimbst_mstr_id[i].value != "")
				{
					for(j=0; j<i; j++)
					{
						if(remimbst_mstr_id[j].value == remimbst_mstr_id[i].value)
						{
							alert("Duplicate Reimbursement Head 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('remimbst_mstr_id[]');
		if(x[pos].value == "")
		{
			alert("Please Provide Reimbursement Head");
			x[pos].focus();
			return false;
		}
		
		reimbrsmnt_type_val = document.getElementsByName('reimbrsmnt_type_val[]');
		var x = document.getElementsByName('reimbrsmnt_amt[]');
		if(reimbrsmnt_type_val[pos].value == 1 && x[pos].value == "")
		{
			alert("Please Provide Reimbursement Amount");
			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=remimbst_mstr_id1', 'id=remimbst_mstr_id'+rowCount+'');
				str = str.replace('id=reimbrsmnt_type_0', 'id=reimbrsmnt_type_'+(rowCount-1)+'');
				str = str.replace('id=reimbrsmnt_type_val_0', 'id=reimbrsmnt_type_val_'+(rowCount-1)+'');
				str = str.replace('onclick=return amt_dis(this.checked, \'0\');', 'onclick=return amt_dis(this.checked, \''+(rowCount-1)+'\');');
				str = str.replace('id=reimbrsmnt_amt_0', 'id=reimbrsmnt_amt_'+(rowCount-1)+'');
			}
			else
			{
				str = str.replace('id="remimbst_mstr_id1"', 'id="remimbst_mstr_id'+rowCount+'"');
				str = str.replace('id="reimbrsmnt_type_0"', 'id="reimbrsmnt_type_'+(rowCount-1)+'"');
				str = str.replace('id="reimbrsmnt_type_val_0"', 'id="reimbrsmnt_type_val_'+(rowCount-1)+'"');
				str = str.replace('onclick="return amt_dis(this.checked, \'0\');"', 'onclick="return amt_dis(this.checked, \''+(rowCount-1)+'\');"');
				str = str.replace('id="reimbrsmnt_amt_0"', 'id="reimbrsmnt_amt_'+(rowCount-1)+'"');
			}
			
			newcell.innerHTML = str;
			newcell.className = 'question';
			
			if((colCount-1) == i)
			{
				newcell.align = 'center';
				newcell.style.width = '5%';
			}
			else if(i == 1)
			{
				newcell.className = 'question_title';
				newcell.style.width = '15%';
			}
			else
				newcell.style.width = '40%';
		}
		if(navigator.appName == "Microsoft Internet Explorer")
		{
			remimbst_mstr_id = "remimbst_mstr_id"+rowCount+"";
			document.getElementById(remimbst_mstr_id).selectedIndex = 0;
			reimbrsmnt_type = "reimbrsmnt_type_"+(rowCount-1)+"";
			$("#"+reimbrsmnt_type).attr("checked", false);
			reimbrsmnt_type_val = "reimbrsmnt_type_val_"+(rowCount-1)+"";
            $("#"+reimbrsmnt_type_val).val('0');
			reimbrsmnt_amt = "reimbrsmnt_amt_"+(rowCount-1)+"";
            $("#"+reimbrsmnt_amt).show();
            $("#"+reimbrsmnt_amt).val('');
		}
        else
        {
            remimbst_mstr_id = "remimbst_mstr_id"+rowCount+"";
			document.getElementById(remimbst_mstr_id).selectedIndex = 0;
			reimbrsmnt_type = "reimbrsmnt_type_"+(rowCount-1)+"";
			$("#"+reimbrsmnt_type).attr("checked", false);
            reimbrsmnt_type_val = "reimbrsmnt_type_val_"+(rowCount-1)+"";
            $("#"+reimbrsmnt_type_val).val('0');
			reimbrsmnt_amt = "reimbrsmnt_amt_"+(rowCount-1)+"";
            $("#"+reimbrsmnt_amt).hide();
            $("#"+reimbrsmnt_amt).val('');
        }
	}
	
	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;Reimbursement 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($sur_mstr == "")
									$sur_mstr = SURVY;
								
								if(SURVYFXD == 'Y')
								{
									echo '<input type="hidden" name="sur_mstr" id="sur_mstr" value="'.SURVY.'" />';
									echo SURVYTITLE;
								}
								else
								{
									echo $common->dropdown('sur_code', 'sur_title', 'custom_survey_master', 'sur_code NOT IN (SELECT reimbrsmnt_survey_id FROM custom_reimbursement_mapping)', 'sur_title', 'sur_mstr', $sur_mstr, 'class="required"');
								}
							?>
                        </div>
                    </div>
                    <div class="clear_form">
                    	<div>&nbsp;</div>
                    </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_title" style="width: 40%;">Reimbursement Head</td>
                                <td class="question_title" style="width: 15%;">Is Fixed?</td>
                                <td class="question_title" style="width: 40%;">Reimbursement Amount</td>
                                <td class="question_title" style="width: 5%;" 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: 40%;">
									&nbsp;
                                	<?php
										$common->dropdown('reimbrsmnt_head_id', 'reimbrsmnt_head_name', 'custom_reimbursement_heads_master', '', 'reimbrsmnt_head_name', 'remimbst_mstr_id[]', $remimbst_mstr_id, 'class="required" style="max-width: 250px;"');
									?>
                                </td>
                                <td class="question_title" style="width: 15%;">
                                	<input type="checkbox" name="reimbrsmnt_type[]" id="reimbrsmnt_type_0" checked value="1" onclick="return amt_dis(this.checked, '0');" />
                                        <input type="hidden" name="reimbrsmnt_type_val[]" id="reimbrsmnt_type_val_0" value="1" />
                                </td>
                                <td class="question" style="width: 40%;">
									&nbsp;
                                	<input type="text" name="reimbrsmnt_amt[]" id="reimbrsmnt_amt_0" onkeyup="return acceptDecimal(this);" style="text-align: right;"/>&nbsp;&nbsp;
                                </td>
                                <td class="question" style="width: 5%;" align="center">
                                	<a style="cursor: pointer;" onclick="whichRow('dataTable', this);">
	                                	<img src="includes/images/delete.png" />
                                    </a>
                                </td>
                            </tr>
                        </table>
                        <script language="javascript">
							arr_id_change('remimbst_mstr_id[]');
						</script>
                    	<!--<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 40px; display: none;">
                        	<tr>
                            	<td class="question_title" style="width: 40%;">Reimbursement Head</td>
                                <td class="question_title" style="width: 15%;">Is Fixed?</td>
                                <td class="question_title" style="width: 40%;">Reimbursement Amount</td>
                            </tr>
                            <?php
								/*$qry = "SELECT * FROM custom_reimbursement_heads_master WHERE status = 'Y'";
								$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
								$rec = $common->sql->record;
								$cnt = count($rec);
								
								for($i=0; $i<$cnt; $i++)
								{
                                    echo '
									<tr>
                                        <td class="question" style="width: 40%;">
										'.$rec[$i]['reimbrsmnt_head_name'].'
										<input type="hidden" name="remimbst_mstr_id[]" id="remimbst_mstr_id_'.$i.'" value="'.$rec[$i]['reimbrsmnt_head_id'].'" />
										</td>
                                        <td class="question" style="width: 15%;">
											<input type="checkbox" name="reimbrsmnt_type[]" id="reimbrsmnt_type_'.$i.'" checked value="1" onclick="return amt_dis(this.checked, '.$i.');" />
										</td>
                                        <td class="question" style="width: 40%;">
											<input type="text" name="reimbrsmnt_amt[]" id="reimbrsmnt_amt_'.$i.'" onkeyup="return acceptDecimal(this);" />
										</td>
                                    </tr>';
								}*/
							?>
                        </table> -->
                    </div>
					<?php
						/*if(count($rec) != 0)
						{*/
                    ?>
                    <div class="clear_form">
                    </div>
                    <div class="questionrow">
                        <div style="padding: 5px;" align="center">
                            <input type="submit" name="Submit" value="Submit" />
                        </div>
                    </div>
                    <?php /*}*/ ?>
				</div>
            </form>
		</div>
	</div>
</div>