<%@ include file="/includes.jsp"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<link rel="stylesheet" href="../css/draggable.css" type="text/css" />
<jsp:useBean id="qbFormFields" class = "com.ardeo.util.QBFormFieldsMappingBean" />
	<script type="text/javascript">
			function update() {
				dispatch("update");
				document.getElementById("qbForm").submit();
			}
			var mutex = false;
			
			function saveQueryBuilderMatchesOrder(){
				if(!mutex) {
					mutex = true;
					const table = document.getElementById("queryBuilderMatchesTblId");
					const rowCount = table.rows.length;
					let order = "";
					if (rowCount > 0) {
						for (let i = 1; i < rowCount; i++) {
							if(order.length>0)
								order = order + "," + table.rows[i].cells[0].innerHTML+"="+i;
							else
								order= table.rows[i].cells[0].innerHTML+"="+i;
						}
					}
					dispatch("move");
					document.getElementById("matchesOrder").value=order;
					document.getElementById("qbForm").submit();
				}
				mutex = false;
			}
			
			function removefield(selectedQFMId) {
				if(!mutex) {
					mutex = true;
					if(confirmAndDispatch("remove", "Are you sure you wish to remove the selected fields?")) {
						document.getElementById("selectedQFMId").value=selectedQFMId;
						document.getElementById("qbForm").submit();
					}
					mutex = false;
				}
			}
			
			function select_inner_logical_oprGrp(innerLogic) {
			    var id = innerLogic.id;
                var index, indexOfIndex, outerLogicElement;
                indexOfIndex = id.indexOf("[");
                index = id.substr(indexOfIndex+1,1);
                outerLogicElement = "matches[" + index + "].outerLogicalOpr"; 
                
                if(innerLogic.value !== "0") {
                    document.getElementById(outerLogicElement).value = "0"
                    document.getElementById(innerLogicElement).value = innerLogic.value;
                    document.getElementById(outerLogicElement).disabled = true;
                }
                
                if(innerLogic.value === "0")
                document.getElementById(outerLogicElement).disabled = false;
                
            }
            function select_outer_logical_oprGrp(outerLogic) {
                var id = outerLogic.id;
                var index, indexOfIndex, innerLogicElement;
                indexOfIndex = id.indexOf("[");
                index = id.substr(indexOfIndex+1,1);
                innerLogicElement = "matches[" + index + "].innerLogicalOpr"; 
                
                if(outerLogic.value !== "0") {
                    document.getElementById(innerLogicElement).value = "0";
                    document.getElementById(outerLogicElement).value = outerLogic.value;
                    document.getElementById(innerLogicElement).disabled = true;
                }
                if(outerLogic.value === "0")
                document.getElementById(innerLogicElement).disabled = false;
            }
            
			function select_inner_logical_opr() {
			    if(document.getElementById("newMatch.innerLogicalOpr").value !== "0")
                document.getElementById("newMatch.outerLogicalOpr").disabled = true;
                if(document.getElementById("newMatch.innerLogicalOpr").value === "0")
                document.getElementById("newMatch.outerLogicalOpr").disabled = false;
                
            }
            function select_outer_logical_opr() {
                if(document.getElementById("newMatch.outerLogicalOpr").value !== "0")
                document.getElementById("newMatch.innerLogicalOpr").disabled = true;
                if(document.getElementById("newMatch.outerLogicalOpr").value === "0")
                document.getElementById("newMatch.innerLogicalOpr").disabled = false;
            }
            
          var sortBySelectionFlag = false
           function checkForSortbySelection(sortbyLogic){
                var id = sortbyLogic.id;
                var index, indexOfIndex;
                indexOfIndex = id.indexOf(".");
                index = id.substring(7,indexOfIndex);
                var alreadySelectedIndex = document.getElementById("sortbySelectedId").value;
                
                if(sortBySelectionFlag){
                // if the sorting column is already selected.
                    if(alreadySelectedIndex === index){
	                        if(document.getElementById(id).value === 0){
	                            sortBySelectionFlag = false;
	                            return;
	                        }
                        }else{
                            alert("Only one field can be selected for sorting");
                            document.getElementById(id).value = 0;
                            return;
                        }
                    }
                    else{
	                    if(document.getElementById(id).value === 1 || document.getElementById(id).value === 2){
	                        sortBySelectionFlag = true;
	                        document.getElementById("sortbySelectedId").value = index;
	                    }
                    }
                
                }
                
	</script>
	
<div class="breadcrumb">
You are here: <a href="../emdt/emdtHome.form"><img src="../emdt/images/16x16/icon.png" style="vertical-align: top;"/> eMDT</a> &gt; <a href="../emdt/reportMenu.jsp">Reports</a> &gt; <a href="../emdt/queries.jsp">Query Builder</a> &gt; <a class="selected" >Edit Query</a>
</div>
	<smvc:form id="qbForm" action="editQueryBuilder.form" modelAttribute="queryBuilder">
		<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
		<smvc:hidden path="matchIndex" id="matchIndex"/>
		<smvc:hidden path="moveMatchAmmount" id="moveMatchAmmount"/>
		<smvc:hidden path="actionHint" id="dispatchMethod"/>
		<smvc:hidden path="selectedQFMId" id="selectedQFMId"/>
		<c:set var="selectedMatchId" value="${queryBuilder.selectedQFMId}" />
		<input type="hidden" id="matchesOrder" name="matchesOrder" />
        <input type="hidden" id="sortbySelectedId" name="sortbySelectedId" value="" />

		<fieldset>
		<legend>Edit Query</legend>
			<table border="0" width="100%" >
				<tr>
					<th align="left">Query name</th>
					<td>
						<smvc:input path="description" maxlength="240" size="60" />
						<div class="error"><smvc:errors path="description"/></div>
					</td>
				</tr>
				<tr>
					<td colspan="2"></td>
				</tr>
				<tr>
					<th align="left">Patients</th>
					<td><smvc:radiobutton path="patients_Filter" value="all"  /> For All Patients
					<smvc:radiobutton path="patients_Filter" value="current" /> Only Current Patient
					<smvc:radiobutton path="patients_Filter" value="currentHospital" /> Only Patients in my Current Hospital
					<div class="error"><smvc:errors path="patients_Filter"/> </div>
					 </td>

				</tr>
				<tr>
					<th align="left">Forms</th>
					<td><smvc:radiobutton path="records_Filter" value="all" /> For All Forms
					<smvc:radiobutton path="records_Filter" value="recent" /> Only Most Recent Form
					<div class="error"><smvc:errors path="records_Filter"/></div>
					</td>

				</tr>
	
				<tr>
					<td>&#160;</td>
				</tr>
    
    			<tr>
                    <th align="left">Show headers as</th>
                    <td><smvc:radiobutton path="header_Filter" value="fieldNames" /> Field names
                    <smvc:radiobutton path="header_Filter" value="fieldLabels" /> Field labels
                    <div class="error"><smvc:errors path="header_Filter"/></div>
                    </td>

                </tr>
				<tr>
					<td colspan="2"></td>
				</tr>
			
				<tr>
					<td>&#160;</td>
				</tr>

    			<tr>
					<th align="left">Fields</th>
					<td>
					</td>

				</tr>
				<tr>
					<td colspan="2" style="border: 1px lightgray solid;">
						<c:if test="${empty queryBuilder.matches}">None.</c:if>
						<c:if test="${not empty queryBuilder.matches}">
						<table border="0" width="100%" id="queryBuilderMatchesTblId" >
							<thead>
								<tr class="tableHeader">
									<th style="display: none"></th>
									<th class="tableColHeader"></th>
									<th class="tableColHeader">Output</th>
									<th class="tableColHeader">Form</th>
									<th class="tableColHeader">Field</th>
									<th class="tableColHeader">Condition</th>
									<th class="tableColHeader">Value</th>
									<th class="tableColHeader">Logic1</th>
									<th class="tableColHeader">Logic2</th>
	                                <th class="tableColHeader">Sort by</th>
	                                <c:if test="${queryBuilder.selectedQFMId eq null}">
										<th class="tableColHeader">Edit</th>
										<th class="tableColHeader">Delete</th>
									</c:if>
									<th class="tableColHeader"></th>
								</tr>
							</thead>
							<tbody>
							<c:forEach var="qfm" items="${queryBuilder.matches}" varStatus="s">
								<c:if test="${empty (selectedMatchId)  or (not empty (selectedMatchId) and selectedMatchId eq qfm.id)}">
									<tr class="grabableRow <c:out value="${s.index % 2 == 0 ? 'evenRow' : 'oddRow'}"/>" >
										<td style="display: none"><c:out value="${qfm.id}"/></td>
										<td class="grabable" title="Drag and drop to change ordering"  style="size: 50px;"></td>
										<td>
											<c:choose>
												<c:when test="${not empty selectedMatchId}">
													<smvc:select path="selectedMatch.output" cssClass="smallFont">
														<c:forEach var="entry" items="${queryBuilder.outputMap}">
															<smvc:option value="${entry.key}">${entry.value}</smvc:option>
														</c:forEach>
													</smvc:select>
												</c:when>
												<c:otherwise>
													<c:out value="${queryBuilder.outputMap[qfm.output]}"/>
												</c:otherwise>
											</c:choose>
										</td>
										<td>
	                                        <c:choose>
	                                            <c:when test="${not empty selectedMatchId}">
	                                                <smvc:select path="selectedMatch.fieldParentName" onchange="update()"  cssClass="smallFont">
	                                                    <smvc:option value="patient">Patient</smvc:option>
	                                                    <smvc:option value="carespell">Carespell</smvc:option>
	                                                    <smvc:option value="meeting">Meeting</smvc:option>
	                                                    <c:forEach var="entry" items="${queryBuilder.getFormDefPathLabelMapByGroup()}">
	                                                    	<optgroup label="${queryBuilder.getFormGroupPathLabelMap()[entry.key]}">
	                                                    	 	<c:forEach var="entry1" items="${entry.value}">
	                                                    	 	 	<smvc:option value="${entry1.key}"><c:out value="${entry1.value}"/></smvc:option>
	                                                    		</c:forEach>
	                                                    	</optgroup>
				           								</c:forEach>
	                                                </smvc:select>
	                                            </c:when>
	                                            <c:otherwise><c:out value="${qfm.fieldParentNameStr}"/></c:otherwise>
	                                        </c:choose>
										</td>
										<td>
											<c:choose>
												<c:when test="${not empty selectedMatchId}">
													<smvc:select path="selectedMatch.fieldName" cssClass="smallFont">
														<smvc:option value="" />
														<c:set var="beanName" value="${queryBuilder.matches[s.index].fieldParentName}"/>
														<c:choose>
															<c:when test="${beanName == 'patient'}">
																<c:forEach var="patientInfo" items="${qbFormFields.patientsMap}">
																	<smvc:option value="${patientInfo.key}"><c:out value="${patientInfo.value}"/></smvc:option>
																</c:forEach>
															</c:when>
															<c:when test="${beanName == 'carespell'}">
																<c:forEach var="careSpellsInfo" items="${qbFormFields.careSpellsMap}">
																	<smvc:option value="${careSpellsInfo.key}"><c:out value="${careSpellsInfo.value}"/></smvc:option>
																</c:forEach>
															</c:when>
															<c:when test="${beanName == 'meeting'}">
																<c:forEach var="meetingsInfo" items="${qbFormFields.meetingsMap}">
																	<smvc:option value="${meetingsInfo.key}"><c:out value="${meetingsInfo.value}"/></smvc:option>
																</c:forEach>
															</c:when>
															<c:otherwise>
																<c:forEach var="formGroup" items="${emdt.formDomain.children}">
																	<c:forEach var="formDef" items="${formGroup.children}">
																		<c:if test="${formDef.path == beanName}">
																			<c:forEach var="formPage" items="${formDef.children}">
																				<c:forEach var="formSection" items="${formPage.children}">
																					<c:forEach var="formField" items="${formSection.children}">
																						<smvc:option value="${formField.name}"><c:out value="${formField.name}"/></smvc:option>
																					</c:forEach>
																				</c:forEach>
																			</c:forEach>
																		</c:if>
																	</c:forEach>
																</c:forEach>
															</c:otherwise>
														</c:choose>
													</smvc:select>
												</c:when>
												<c:otherwise><c:out value="${qfm.fieldNameStr}"/></c:otherwise>
											</c:choose>
										</td>
										<td>
											<c:choose>
												<c:when test="${not empty selectedMatchId}">
													<smvc:select path="selectedMatch.op" cssClass="smallFont">
														<c:forEach var="entry" items="${queryBuilder.opMap}">
															<smvc:option value="${entry.key}">${entry.value}</smvc:option>
														</c:forEach>
													</smvc:select>
												</c:when>
												<c:otherwise><c:out value="${queryBuilder.opMap[qfm.op]}"/></c:otherwise>
											</c:choose>
										</td>
										<td><c:choose>
											<c:when test="${not empty selectedMatchId}">
												<smvc:input cssClass="smallFont" size="15" path="selectedMatch.value" maxlength="255" />
											</c:when>
											<c:otherwise><c:out value="${qfm.value}"/></c:otherwise>
										</c:choose>
										</td>
										<td>
											<c:choose>
												<c:when test="${not empty selectedMatchId}">
													<smvc:select path="selectedMatch.innerLogicalOpr" cssClass="smallFont" onchange="select_inner_logical_oprGrp(this)">
														<c:forEach var="entry" items="${queryBuilder.innerLogicalOprMap}">
															<smvc:option value="${entry.key}">${entry.value}</smvc:option>
														</c:forEach>
													</smvc:select>
												</c:when>
												<c:otherwise><c:out value="${queryBuilder.innerLogicalOprMap[qfm.innerLogicalOpr]}"/></c:otherwise>
											</c:choose>
										</td>
										<td width="1">
											<c:choose>
												<c:when test="${not empty selectedMatchId}">
													<smvc:select path="selectedMatch.outerLogicalOpr" cssClass="smallFont" onchange="select_outer_logical_oprGrp(this)">
														<c:forEach var="entry" items="${queryBuilder.outerLogicalOprMap}">
															<smvc:option value="${entry.key}">${entry.value}</smvc:option>
														</c:forEach>
													</smvc:select>
												</c:when>
												<c:otherwise><c:out value="${queryBuilder.outerLogicalOprMap[qfm.outerLogicalOpr]}"/></c:otherwise>
											</c:choose>
										</td>
										<td width="1">
											<c:if test="${qfm.sortByOrder eq '1' || qfm.sortByOrder eq '2'}">
												<script>
													sortBySelectionFlag = true;
													document.getElementById("sortbySelectedId").value = <c:out value="${s.index}"/>;
												</script>
											</c:if>
											<c:choose>
												<c:when test="${not empty selectedMatchId}">
													<smvc:select path="selectedMatch.sortByOrder" cssClass="smallFont" onchange="checkForSortbySelection(this);">
														<c:forEach var="entry" items="${queryBuilder.sortByOrderMap}">
															<smvc:option value="${entry.key}">${entry.value}</smvc:option>
														</c:forEach>
													</smvc:select>
												</c:when>
												<c:otherwise><c:out value="${queryBuilder.sortByOrderMap[qfm.sortByOrder]}"/></c:otherwise>
											</c:choose>
										</td>
										<c:if test="${empty selectedMatchId}">
											<td style="text-align: center"><img src="../images/16x16/edit.png" onmouseover="this.style.cursor = cursorPointer" onclick="location.href='editQueryBuilder.form?queryBuilderId=${queryBuilder.id}&selectedQFMId=${qfm.id}'"/></td>
											<td style="text-align: center"><img src="../images/delete.png" onclick="removefield('${qfm.id}')" border="0" alt="Delete" onmouseover="this.style.cursor = cursorPointer; " /></td>
										</c:if>
									</tr>
								</c:if>
							</c:forEach>
								<tr>
									<td class="error"></td>
									<td class="error"></td>
									<td class="error"><smvc:errors path="selectedMatch.fieldParentName"/></td>
									<td class="error"><smvc:errors path="selectedMatch.fieldName"/></td>
									<td class="error"><smvc:errors path="selectedMatch.op"/></td>
									<td class="error"><smvc:errors path="selectedMatch.value"/></td>
									<td class="error"></td>
									<td class="error"></td>
									<td class="error"></td>
									 <c:if test="${queryBuilder.selectedQFMId eq null}"><td></td><td></td></c:if>
									<td></td>
								</tr>
							</tbody>
						</table>
						</c:if>
					</td>
				</tr>
			</table>
		</fieldset>
		<br/><br/>
		<c:if test="${queryBuilder.selectedQFMId eq null}">	
		<fieldset>
			<legend>Add a new field...</legend>
			<table align="left" border="0" width="72%">
				<tr>
				    <th align="left" width="30px">&#160;</th>
					<th align="left">Output</th>
					<th align="left">Form</th>
					<th align="left">Field</th>
					<th align="left">Condition</th>
					<th align="left">Value</th>
					<th align="left">Logic1</th>
					<th align="left">Logic2</th>
				</tr>
				<tr>
				    <td width="1">&#160;</td>
					<td width="1">
						<smvc:select path="newMatch.output" cssClass="smallFont">
							<c:forEach var="entry" items="${queryBuilder.outputMap}">
								<smvc:option value="${entry.key}">${entry.value}</smvc:option>
							</c:forEach>
						</smvc:select>
					</td>
					<td width="1">
						<smvc:select path="newMatch.fieldParentName" onchange="update()" cssClass="smallFont">
							<smvc:option value="" />
							<smvc:option value="patient">Patient</smvc:option>
							<smvc:option value="carespell">Carespell</smvc:option>
							<smvc:option value="meeting">Meeting</smvc:option>
							<optgroup label="forms">
						    <c:forEach var="entry" items="${queryBuilder.getFormDefPathLabelMapByGroup()}">
                             	<optgroup label="${queryBuilder.getFormGroupPathLabelMap().get(entry.key)}">
                                	<c:forEach var="entry1" items="${entry.value}">
                                		<smvc:option value="${entry1.key}"><c:out value="${entry1.value}"/></smvc:option>
                                	</c:forEach>
                                 </optgroup>
 							</c:forEach>
							</optgroup>
						</smvc:select>
					</td>
					<td width="1">
						<smvc:select path="newMatch.fieldName" cssClass="smallFont">
							<smvc:option value="">&#160;</smvc:option>
							<c:set var="beanName" value="${queryBuilder.newMatch.fieldParentName}"/>
							<c:choose>
								<c:when test="${! empty beanName}">
									<smvc:option value="ALL_FIELDS">- All Fields -</smvc:option>
								</c:when>
							</c:choose>
							<c:choose>
								<c:when test="${beanName == 'patient'}">
									<c:forEach var="patientInfo" items="${qbFormFields.patientsMap}">
										<smvc:option value="${patientInfo.key}"><c:out value="${patientInfo.value}"/></smvc:option>
									</c:forEach>
								</c:when>
								<c:when test="${beanName == 'carespell'}">
									<c:forEach var="careSpellsInfo" items="${qbFormFields.careSpellsMap}">
										<smvc:option value="${careSpellsInfo.key}"><c:out value="${careSpellsInfo.value}"/></smvc:option>
									</c:forEach>
								</c:when>
								<c:when test="${beanName == 'meeting'}">
									<c:forEach var="meetingsMap" items="${qbFormFields.meetingsMap}">
										<smvc:option value="${meetingsMap.key}"><c:out value="${meetingsMap.value}"/></smvc:option>
									</c:forEach>
								</c:when>
								<c:otherwise>
									<c:forEach var="formGroup" items="${emdt.formDomain.children}">
										<c:forEach var="formDef" items="${formGroup.children}">
											<c:if test="${formDef.path == beanName}">
												<c:forEach var="formPage" items="${formDef.children}">
													<c:forEach var="formSection" items="${formPage.children}">
														<c:forEach var="formField" items="${formSection.children}">
															<c:if test="${formField.inputType != 0}">
																<smvc:option value="${formField.name}"><c:out value="${formField.name}"/></smvc:option>
															</c:if>
														</c:forEach>
													</c:forEach>
												</c:forEach>
											</c:if>
										</c:forEach>
									</c:forEach>
								</c:otherwise>
							</c:choose>
						</smvc:select>
					</td>
					<td width="1">
						<smvc:select path="newMatch.op" cssClass="smallFont">
							<c:forEach var="entry" items="${queryBuilder.opMap}">
								<smvc:option value="${entry.key}">${entry.value}</smvc:option>
							</c:forEach>
						</smvc:select>
					</td>
					<td>
						<smvc:input size="15" path="newMatch.value" maxlength="255" cssClass="smallFont"/>
						<smvc:errors cssClass="error" path="newMatch"/>
					</td>
					<td width="1">
                      	<smvc:select path="newMatch.innerLogicalOpr" cssClass="smallFont" onchange="select_inner_logical_opr()">
							<c:forEach var="entry" items="${queryBuilder.innerLogicalOprMap}">
								<smvc:option value="${entry.key}">${entry.value}</smvc:option>
							</c:forEach>
						</smvc:select>
                    </td>
                    <td width="1">
                        <smvc:select path="newMatch.outerLogicalOpr" cssClass="smallFont" onchange="select_outer_logical_opr()">
							<c:forEach var="entry" items="${queryBuilder.outerLogicalOprMap}">
								<smvc:option value="${entry.key}">${entry.value}</smvc:option>
							</c:forEach>
						</smvc:select>
                    </td>
				</tr>
				<tr>
				    <td class="error" width="1"></td>
					<td class="error" width="1"></td>
					<td class="error" width="1"><smvc:errors path="newMatch.fieldParentName"/></td>
					<td class="error" width="1"><smvc:errors path="newMatch.fieldName"/></td>
					<td class="error" width="1"><smvc:errors path="newMatch.op"/></td>
					<td class="error" width="1"><smvc:errors path="newMatch.value"/></td>
					<td class="error" width="1"><smvc:errors path="newMatch.innerLogicalOpr"/></td>
					<td class="error" width="1"><smvc:errors path="newMatch.outerLogicalOpr"/></td>
				</tr>
				<tr>
				<td colspan="7" >&#160;
                </td>
				<td width="1"><div style="text-align: left">
                    <input type="submit" name="addNewMatch" value="Add" onclick="dispatch('addNewMatch')"/>
                </div>
				</td>
				</tr>
			</table>
		</fieldset>
	</c:if>
		<c:if test="${not empty queryBuilder.groupingCodes }">
			<c:if test="${fn:length(queryBuilder.groupingCodes) gt 1 and queryBuilder.records_Filter ne 'recent'  }">
			<fieldset>
				<legend>Grouping</legend>
				<table align="left" border="0" width="45%">
					<tr>
					<td width="2">&#160;</td>
					<td class="error" colspan="2"><smvc:errors path="groupingCodes"/></td>
					</tr>
					<tr>
						<th align="left" width="30px">&#160;</th>
						<th align="left">Form</th>
						<th align="left">Field</th>
					</tr>
					<c:forEach var="gc" items="${queryBuilder.groupingCodes}" varStatus="s">
						<c:set var="beanName" value="${gc.key}" />
						<tr ><td width="2" colspan="2" >&#160;</td><td colspan="2" class="error" width="1"><smvc:errors path="groupingCodes[${beanName}]"/></td></tr>
						<tr>
							<td width="2">&#160;</td>
							<td width="2"><c:forEach var="entry" items="${queryBuilder.getFormDefPathLabelMapByGroup()}">
									<c:forEach var="entry1" items="${entry.value}">
										<c:if test="${entry1.key eq beanName}">
											<input type="text" value="<c:out value="${entry1.value}"/>" size="40" disabled="disabled" class="smallFont" />
										</c:if>
									</c:forEach></c:forEach>
							</td>
							<td width="1">
							<smvc:select path="groupingCodes[${beanName}]" cssClass="smallFont">
									<smvc:option value="">&#160;</smvc:option>
									<c:forEach var="formGroup" items="${emdt.formDomain.children}">
										<c:forEach var="formDef" items="${formGroup.children}">
											<c:if test="${formDef.path == beanName}">
												<c:forEach var="formPage" items="${formDef.children}">
													<c:forEach var="formSection" items="${formPage.children}">
														<c:forEach var="formField" items="${formSection.children}">
															<c:if test="${formField.inputType != 0}">
																<smvc:option value="${formField.name}"><c:out value="${formField.name}"/></smvc:option>
															</c:if>
														</c:forEach>
													</c:forEach>
												</c:forEach>
											</c:if>
										</c:forEach>
									</c:forEach>
								</smvc:select>
							</td>
						</tr>
						<tr>
				    		<td class="error" width="2"></td>
				    		<td class="error" width="2"></td>
							<td class="error" width="2"></td>
						</tr>
					</c:forEach>
				</table>
			</fieldset></c:if>
		</c:if>
		<fieldset class="buttonFieldSet">
		<table border="0" align="left" width="85%">
			<tr>
			<td width="65%"></td>
				<c:if test="${queryBuilder.selectedQFMId eq null}">
					<td><input type="submit" name="createCopy" value="Create copy" onclick="dispatch('createCopy')" /></td>
					<td><input type="submit" name="cancelButton" value="Cancel" /> </td>
					<td><input type="reset" value="Reset" /></td>
				</c:if>
				<c:if test="${queryBuilder.selectedQFMId ne null}">
					<input type="button" name="return" value="return" onclick="javascript:location.href='editQueryBuilder.form?queryBuilderId=${queryBuilder.id}'" />
				</c:if>
				<td><input type="submit" name="save" value="Save" onclick="dispatch('save')" /></td>
			</tr>
		</table>
		</fieldset>
		<smvc:errors/>
	</smvc:form>
<script type="text/javascript">
	$('#queryBuilderMatchesTblId tbody').sortable({
		opacity: 0.5,
		update: function(event, ui) {saveQueryBuilderMatchesOrder()}
	});
</script>