/*
  (c) 2008-2014 Samnan ur Rehman
 @web        http://mywebsql.net
 @license    http://mywebsql.net/license
*/
var pendingChanges=!1,bAddingIndex=!1,newIndex=null;function setError(a,b){$("#grid-messages").html(b).addClass("error");$(selectedRow).length&&($(selectedRow).removeClass("sel"),selectedRow=null);$(a).each(function(){$(this).addClass("error")});setTimeout(function(){$(a).each(function(){$(this).removeClass("error")})},2E3)}function setMessage(a){$("#grid-messages").html(a).removeClass("error")}
function setupIndexes(){$("#grid-tabs").tabs();$("#indexlist option").remove();options="";for(index in indexInfo)options+='<option value="'+index+'">'+index+"</option>";$("#indexlist").html(options).change(selectIndex);removeFieldList();$("#btn_addfield").button({disabled:!0}).click(addField).hide();$("#btn_delfield").button({disabled:!0}).click(deleteField).hide();$("#chk_primary,#chk_unique,#chk_fulltext").click(checkIndexOptions);$("#indextype").hide();$("#btn_add").button().click(addIndex);$("#btn_edit").button().click(editIndex).hide();
$("#btn_save").button().click(saveIndex).hide();$("#btn_del").button().click(deleteIndex).hide();$("#btn_cancel").button().click(cancelChanges).hide();$("#btn_submit").button({disabled:!0}).click(validateIndexes);$("#dialog-list").dialog({autoOpen:!1,width:240,height:240,modal:!0,draggable:!1,resizable:!1,open:loadDialogValues,buttons:{Add:addFieldToIndex,Done:function(){$("#dialog-list").dialog("close")}}})}
function loadDialogValues(a,b){$("#list-items").html("");index_name=$("#indexlist").val();for(i=0;i<fieldInfo.length;i++){fieldExists=!1;fi=fieldInfo[i];for(j=0;j<newIndex.length;j++)if(newIndex[j].column==fi.fname){fieldExists=!0;break}fieldExists||(txt=""==fi.flen?fi.fname+"  [ "+fi.ftype+" ]":fi.fname+"  ["+fi.ftype+" ("+fi.flen+")]",option=$("<option></option>").val(fi.fname).text(txt),$("#list-items").append(option))}setTimeout(function(){$("#list-item").focus()},50)}
function addFieldToIndex(){if(0==$("#list-items option:selected").length)return!1;field=$("#list-items").val();flength=$("#flength").val();fieldObj={column:field,type:"BTREE",order:0,length:flength};index_name=$("#indexlist").val();newIndex.push(fieldObj);fi=getField(field);tr=$('<tr><td><input type="checkbox" /></td><td></td><td></td></tr>');td=tr.find("td");ftype=""==fi.flen?fi.ftype:fi.ftype+" ("+fi.flen+")";td.eq(1).text(fi.fname);td.eq(2).text(ftype);$("#table_grid tbody").append(tr);$("#list-items option:selected").remove();
$("#flength").val("");$("#btn_save").button({disabled:!1})}function addIndex(){jPrompt(__("Enter new index name"),"",__("Indexes"),function(a){null!=a&&a&&""!=a&&!indexExists(a)&&(newIndex=[],bAddingIndex=!0,option=$('<option value="'+a+'">'+a+"</option>"),$("#indexlist").append(option),option.prop("selected","selected"),selectIndex(),editIndex())})}
function editIndex(){index=$("#indexlist").val();bAddingIndex||(newIndex=clone(indexInfo[index]));$("#btn_add,#btn_edit,#btn_del").hide();$("#table_grid").find("input").removeAttr("disabled");$("#indexlist").attr("disabled","disabled");$("#btn_addfield").button({disabled:!1}).click(addField).show();$("#btn_delfield").button({disabled:!1}).click(deleteField).show();$("#btn_cancel").show();$("#btn_save").button({disabled:!0}).show();$("#btn_submit").button({disabled:!0});$("#indextype").find("input").removeAttr("disabled");
indexExists("PRIMARY")&&$("#chk_primary").attr("disabled","disabled");"PRIMARY"==index&&$("#chk_unique,#chk_fulltext").attr("disabled","disabled")}function deleteIndex(){index=$("#indexlist").val();""!=index&&(bAddingIndex||delete indexInfo[index_name],$("#indexlist option:selected").remove(),removeFieldList(),$("#indextype").hide(),$("#btn_addfield").hide(),$("#btn_delfield").hide(),$("#btn_edit").hide(),$("#btn_del").hide(),$("#btn_submit").button({disabled:!1}))}
function saveIndex(){if(0==newIndex.length)return jAlert(__("Index must have at least one field")),!1;bAddingIndex&&"1"==newIndex.primary&&(index_name="PRIMARY",delete newIndex.primary,$("#indexlist option:selected").val(index_name).text(index_name));index_name=$("#indexlist").val();indexInfo[index_name]=newIndex;$("#btn_add").show();$("#btn_cancel").hide();$("#btn_save").hide();$("#indexlist").removeAttr("disabled");bAddingIndex=!1;pendingChanges=!0;$("#btn_submit").button({disabled:!1});selectIndex()}
function selectIndex(){index_name=$("#indexlist").val();if(""!=index_name){$("#table_grid tr").not("#fhead").remove();primary=unique=fulltext=!1;index=bAddingIndex?newIndex:indexInfo[index_name];for(i=0;i<index.length;i++)fi=getField(index[i].column),tr=$('<tr><td><input type="checkbox" /></td><td></td><td></td></tr>'),td=tr.find("td"),ftype=""==fi.flen?fi.ftype:fi.ftype+" ("+fi.flen+")",td.eq(1).text(fi.fname),td.eq(2).text(ftype),$("#table_grid tbody").append(tr),"PRIMARY"==index_name&&(primary=
!0),"1"==index[i].unique&&(unique=!0),"FULLTEXT"==index[i].type&&(fulltext=!0);$("#chk_primary").prop("checked",primary);$("#chk_unique").prop("checked",unique);$("#chk_fulltext").prop("checked",fulltext);$("#table_grid").find("input").attr("disabled","disabled");$("#indextype").show().find("input").attr("disabled","disabled");$("#btn_addfield").button({disabled:!0}).show();$("#btn_delfield").button({disabled:!0}).show();$("#btn_edit").show();$("#btn_del").show()}}
function removeFieldList(){$("#table_grid tr").not("#fhead").remove();$("#table_grid tbody").append('<tr><td class="empty" colspan="3">'+__("Select an index to view / edit its details")+"</td></tr>")}function addField(){$("#dialog-list").dialog("open")}
function deleteField(){checked=$("#table_grid input:checked");if(0==checked.length)return!1;index_name=$("#indexlist").val();delRows=[];checked.each(function(){field=$(this).parent().next().text();delRows.push($(this).parent().parent());for(i=0;i<newIndex.length;i++)newIndex[i].column==field&&newIndex.splice(i,1)});for(i=0;i<delRows.length;i++)delRows[i].remove();pendingChanges=!0;$("#btn_save").button({disabled:!1})}
function checkIndexOptions(){checked=$(this).prop("checked");id=$(this).attr("id");index=$("#indexlist").val();if("chk_primary"==id&&checked&&indexExists("PRIMARY"))return jAlert(__("Primary key already exists")),$(this).removeAttr("checked"),!1;"chk_primary"==id&&(newIndex.primary=checked?"1":"0");for(i=0;i<newIndex.length;i++)"chk_unique"==id?newIndex[i].unique=checked?"1":"0":"chk_fulltext"==id&&(newIndex[i].type=checked?"FULLTEXT":"BTREE");$("#btn_save").button({disabled:!1});return!0}
function cancelChanges(){$("#btn_add").show();$("#btn_cancel").hide();$("#btn_save").hide();$("#indexlist").removeAttr("disabled");bAddingIndex?(deleteIndex(!0),bAddingIndex=!1,removeFieldList()):selectIndex();pendingChanges&&$("#btn_submit").button({disabled:!1})}function getField(a){for(j=0;j<fieldInfo.length;j++)if(fieldInfo[j].fname==a)return fieldInfo[j];return{}}function indexExists(a){for(index_name in indexInfo)if(index_name==a)return!0;return!1}
function validateIndexes(){json={};json.indexes=indexInfo;query=JSON.stringify(json);setMessage("Please wait...");$("#popup_overlay").removeClass("ui-helper-hidden");wrkfrmSubmit("indexes","alter",tableName,query,responseHandler)}
function responseHandler(a){result=$(a).find("#result").text();message=$(a).find("#message").html();"1"==result?(setMessage(__("Indexes Updated")),$("#tab-messages").html(message),pendingChanges=!1,$("#btn_submit").button({disabled:!1})):(setMessage(__("Error")),$("#tab-messages").html(message),$("#grid-tabs").tabs("select",1));div=0<$("#tab-messages div.sql_text").length?$("#tab-messages div.sql_text"):$("#tab-messages div.sql_error");div.length&&(code=div.html2txt(),obj_lines=$('<div class="sql_lines"></div>'),
obj_out=$('<pre class="sql_output"></pre>'),div.html("").append(obj_lines).append(obj_out),parent.commandEditor.win.highlightSql($("#tab-messages pre.sql_output"),$("#tab-messages div.sql_lines"),code));$("#popup_overlay").addClass("ui-helper-hidden")}function clone(a){var b=new a.constructor,c;for(c in a)b[c]=a[c];return b};
