<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;
			}
			loc_mstr = document.getElementsByName('loc_mstr[]');
			len = loc_mstr.length;
			var flag = true;
			
			for(i=0; i<len; i++)
			{
				if(loc_mstr[i].value == "")
				{
					flag = false;
					break;
				}
			}
			if(!flag)
			{
				alert("Please Provide Location");
				loc_mstr[i].focus();
				return false;
			}
			else
			{
				for(i=0; i<len; i++)
				{
					if(loc_mstr[i].value != "")
					{
						for(j=0; j<i; j++)
						{
							if(loc_mstr[j].value == loc_mstr[i].value)
							{
								alert("Duplicate Location 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('loc_mstr[]');
		if(x[pos].value == "")
		{
			alert("Please Provide Location");
			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)
			{
				str = str.replace('id=loc_mstr1', 'id=loc_mstr'+rowCount+'');
			}
			else
			{
				str = str.replace('id="loc_mstr1"', 'id="loc_mstr'+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';*/
			}
		}
		loc_mstr = "loc_mstr"+rowCount+"";
		document.getElementById(loc_mstr).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;Location 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 loc_mapng_sur_id FROM custom_location_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: 60%; line-height: 40px;" id="dataTable" align="center">
                        	<tr>
                            	<td class="question_title" width="50%">Location</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('loc_code', array('loc_name', 'loc_code'), 'custom_location', '', 'loc_name', 'loc_mstr[]', $loc_mstr, 'class="required" style="max-width: 310px;"');
									?>
                                </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('loc_mstr[]');
</script>