var history_array=[],select_field=[],g_index;function panel(a){a||$(".toggle_container").hide();$("h2.tiger").click(function(){$(this).toggleClass("active").next().slideToggle("slow")})}
function display(a,b){var c,d,e,h;for(d=a;d<b;d++){h=history_array[d];e=history_array[d].get_tab();for(c=0;c<d;c++)if(e>history_array[c].get_tab()){for(e=d;e>c;e--)history_array[e]=history_array[e-1];history_array[c]=h;break}}c="";for(d=0;d<history_array.length;d++){e=history_array[d].get_tab();c+='<h2 class="tiger"><a href="#">'+e+"</a></h2>";for(c+='<div class="toggle_container">\n';history_array[d].get_tab()==e;){c+='<div class="block"> <table width ="250">';c+="<thead><tr><td>";c+=history_array[d].get_and_or()?
'<img src="'+pmaThemeImage+'pmd/or_icon.png" onclick="and_or('+d+')" title="OR"/></td>':'<img src="'+pmaThemeImage+'pmd/and_icon.png" onclick="and_or('+d+')" title="AND"/></td>';c+='<td style="padding-left: 5px;" align="right">'+PMA_getImage("b_sbrowse.png","column name")+'</td><td width="175" style="padding-left: 5px">'+history_array[d].get_column_name();c+=history_array[d].get_type()=="GroupBy"||history_array[d].get_type()=="OrderBy"?'</td><td align="center">'+PMA_getImage("b_info.png",detail(d))+
'<td title="'+detail(d)+'">'+history_array[d].get_type()+"</td></td><td onmouseover=\"this.className='history_table';\" onmouseout=\"this.className='history_table2'\" onclick=history_delete("+d+")>"+PMA_getImage("b_drop.png","Delete")+"</td></tr></thead>":'</td><td align="center">'+PMA_getImage("b_info.png",detail(d))+'</td><td title="'+detail(d)+'">'+history_array[d].get_type()+"</td><td <td onmouseover=\"this.className='history_table';\" onmouseout=\"this.className='history_table2'\" onclick=history_edit("+
d+")>"+PMA_getImage("b_edit.png",PMA_messages.strEdit)+"</td><td onmouseover=\"this.className='history_table';\" onmouseout=\"this.className='history_table2'\"               onclick=history_delete("+d+')><img src="themes/original/img/b_drop.png" title="Delete"></td></tr></thead>';d++;if(d>=history_array.length)break;c+="</table></div><br/>"}d--;c+="</div><br/>"}return c}
function and_or(a){history_array[a].get_and_or()?history_array[a].set_and_or(0):history_array[a].set_and_or(1);document.getElementById("ab").innerHTML=display(0,0);panel(1)}
function detail(a){var b=history_array[a].get_type(),c;if(b=="Where")c="Where "+history_array[a].get_column_name()+history_array[a].get_obj().getrelation_operator()+history_array[a].get_obj().getquery();if(b=="Rename")c="Rename "+history_array[a].get_column_name()+" To "+history_array[a].get_obj().getrename_to();if(b=="Aggregate")c="Select "+history_array[a].get_obj().get_operator()+"( "+history_array[a].get_column_name()+" )";if(b=="GroupBy")c="GroupBy "+history_array[a].get_column_name();if(b==
"OrderBy")c="OrderBy "+history_array[a].get_column_name();if(b=="Having"){c="Having ";if(history_array[a].get_obj().get_operator()!="None"){c+=history_array[a].get_obj().get_operator()+"( "+history_array[a].get_column_name()+" )";c+=history_array[a].get_obj().getrelation_operator()+history_array[a].get_obj().getquery()}else c="Having "+history_array[a].get_column_name()+history_array[a].get_obj().getrelation_operator()+history_array[a].get_obj().getquery()}return c}
function history_delete(a){for(var b=0;b<from_array.length;b++)if(from_array[b]==history_array[a].get_tab()){from_array.splice(b,1);break}history_array.splice(a,1);document.getElementById("ab").innerHTML=display(0,0);panel(1)}
function history_edit(a){g_index=a;var b=history_array[a].get_type();if(b=="Where"){document.getElementById("eQuery").value=history_array[a].get_obj().getquery();document.getElementById("erel_opt").value=history_array[a].get_obj().getrelation_operator();document.getElementById("query_where").style.left="530px";document.getElementById("query_where").style.top="130px";document.getElementById("query_where").style.position="absolute";document.getElementById("query_where").style.zIndex="9";document.getElementById("query_where").style.visibility=
"visible"}if(b=="Having"){document.getElementById("hQuery").value=history_array[a].get_obj().getquery();document.getElementById("hrel_opt").value=history_array[a].get_obj().getrelation_operator();document.getElementById("hoperator").value=history_array[a].get_obj().get_operator();document.getElementById("query_having").style.left="530px";document.getElementById("query_having").style.top="130px";document.getElementById("query_having").style.position="absolute";document.getElementById("query_having").style.zIndex=
"9";document.getElementById("query_having").style.visibility="visible"}if(b=="Rename"){document.getElementById("query_rename_to").style.left="530px";document.getElementById("query_rename_to").style.top="130px";document.getElementById("query_rename_to").style.position="absolute";document.getElementById("query_rename_to").style.zIndex="9";document.getElementById("query_rename_to").style.visibility="visible"}if(b=="Aggregate"){document.getElementById("query_Aggregate").style.left="530px";document.getElementById("query_Aggregate").style.top=
"130px";document.getElementById("query_Aggregate").style.position="absolute";document.getElementById("query_Aggregate").style.zIndex="9";document.getElementById("query_Aggregate").style.visibility="visible"}}
function edit(a){if(a=="Rename"){if(document.getElementById("e_rename").value!=""){history_array[g_index].get_obj().setrename_to(document.getElementById("e_rename").value);document.getElementById("e_rename").value=""}document.getElementById("query_rename_to").style.visibility="hidden"}if(a=="Aggregate"){if(document.getElementById("e_operator").value!="---"){history_array[g_index].get_obj().set_operator(document.getElementById("e_operator").value);document.getElementById("e_operator").value="---"}document.getElementById("query_Aggregate").style.visibility=
"hidden"}if(a=="Where"){if(document.getElementById("erel_opt").value!="--"&&document.getElementById("eQuery").value!=""){history_array[g_index].get_obj().setquery(document.getElementById("eQuery").value);history_array[g_index].get_obj().setrelation_operator(document.getElementById("erel_opt").value)}document.getElementById("query_where").style.visibility="hidden"}if(a=="Having"){if(document.getElementById("hrel_opt").value!="--"&&document.getElementById("hQuery").value!=""){history_array[g_index].get_obj().setquery(document.getElementById("hQuery").value);
history_array[g_index].get_obj().setrelation_operator(document.getElementById("hrel_opt").value);history_array[g_index].get_obj().set_operator(document.getElementById("hoperator").value)}document.getElementById("query_having").style.visibility="hidden"}document.getElementById("ab").innerHTML=display(0,0);panel(1)}
function history(a,b,c,d,e){var h,f,l,g,m,n;this.set_column_name=function(j){g=j};this.get_column_name=function(){return g};this.set_and_or=function(j){h=j};this.get_and_or=function(){return h};this.get_relation=function(){return h};this.set_obj=function(j){f=j};this.get_obj=function(){return f};this.set_tab=function(j){l=j};this.get_tab=function(){return l};this.set_obj_no=function(j){m=j};this.get_obj_no=function(){return m};this.set_type=function(j){n=j};this.get_type=function(){return n};this.set_obj_no(d);
this.set_tab(c);this.set_and_or(0);this.set_obj(b);this.set_column_name(a);this.set_type(e)}
var where=function(a,b){var c,d;this.setrelation_operator=function(e){c=e};this.setquery=function(e){d=e};this.getquery=function(){return d};this.getrelation_operator=function(){return c};this.setquery(b);this.setrelation_operator(a)},having=function(a,b,c){var d,e,h;this.set_operator=function(f){h=f};this.setrelation_operator=function(f){d=f};this.setquery=function(f){e=f};this.getquery=function(){return e};this.getrelation_operator=function(){return d};this.get_operator=function(){return h};this.setquery(b);
this.setrelation_operator(a);this.set_operator(c)},rename=function(a){var b;this.setrename_to=function(c){b=c};this.getrename_to=function(){return b};this.setrename_to(a)},aggregate=function(a){var b;this.set_operator=function(c){b=c};this.get_operator=function(){return b};this.set_operator(a)};function unique(a){var b=[],c=0;a:for(;c<a.length;c++){for(var d=0;d<b.length;d++)if(b[d]==a[c])continue a;b[b.length]=a[c]}return b}
function found(a,b){for(var c=0;c<a.length;c++)if(a[c]==b)return 1;return-1}
function build_query(a,b){var c="SELECT ",d;for(i=0;i<select_field.length;i++){d=check_aggregate(select_field[i]);if(d!=""){c+=d;d=check_rename(select_field[i]);c+=d+","}else{d=check_rename(select_field[i]);c+=select_field[i]+d+","}}c=c.substring(0,c.length-1);c+=" FROM "+query_from();if(query_where()!=""){c+="\n WHERE";c+=query_where()}if(query_groupby()!="")c+="\nGROUP BY "+query_groupby();if(query_having()!="")c+="\nHAVING "+query_having();if(query_orderby()!="")c+="\nORDER BY "+query_orderby();
d=document.getElementById("box");document.getElementById("filter").style.display="block";document.getElementById("boxtitle").innerHTML="SELECT";if(b){gradient("box",0);fadein("box")}else d.style.display="block";document.getElementById("textSqlquery").innerHTML=c}
function query_from(){var a,b=[],c=[],d=[],e="",h="",f=[],l=[];l=from_array;var g=0;for(a=0;a<history_array.length;a++)from_array.push(history_array[a].get_tab());b=from_array=unique(from_array);h=a=b.shift();c.push(a);for(a=0;a<2;a++){for(g in contr)for(key in contr[g])for(key2 in contr[g][key]){f=key2.split(".");if(found(c,f[1])>0)for(key3 in contr[g][key][key2]){parts1=contr[g][key][key2][key3][0].split(".");if(found(b,parts1[1])>0){e+="\nLEFT JOIN ";e+="`"+parts1[0]+"`.`"+parts1[1]+"` ON ";e+=
"`"+f[1]+"`.`"+key3+"` = ";e+="`"+parts1[1]+"`.`"+contr[g][key][key2][key3][1]+"` ";d.push(parts1[1])}}}g=0;d=unique(d);c=add_array(d,c);b=remove_array(d,b);d=[];for(g in contr)for(key in contr[g])for(key2 in contr[g][key]){f=key2.split(".");if(found(b,f[1])>0)for(key3 in contr[g][key][key2]){parts1=contr[g][key][key2][key3][0].split(".");if(found(c,parts1[1])>0){e+="\nLEFT JOIN ";e+="`"+f[0]+"`.`"+f[1]+"` ON ";e+="`"+parts1[1]+"`.`"+contr[g][key][key2][key3][1]+"` = ";e+="`"+f[1]+"`.`"+key3+"` ";
d.push(f[1])}}}d=unique(d);c=add_array(d,c);b=remove_array(d,b);d=[]}for(k in b)h+=" , `"+b[k]+"`";e=h+e;from_array=l;return e}function add_array(a,b){for(var c=0;c<a.length;c++)b.push(a[c]);return b}function remove_array(a,b){for(var c=0;c<a.length;c++)for(var d=0;d<b.length;d++)a[c]==b[d]&&b.splice(d,1);return b}
function query_groupby(){var a,b="";for(a=0;a<history_array.length;a++)if(history_array[a].get_type()=="GroupBy")b+=history_array[a].get_column_name()+", ";return b=b.substr(0,b.length-1)}
function query_having(){var a,b="(";for(a=0;a<history_array.length;a++)if(history_array[a].get_type()=="Having")if(history_array[a].get_obj().get_operator()!="None"){b+=history_array[a].get_obj().get_operator()+"("+history_array[a].get_column_name()+" ) "+history_array[a].get_obj().getrelation_operator();b+=" "+history_array[a].get_obj().getquery()+", "}else b+=history_array[a].get_column_name()+" "+history_array[a].get_obj().getrelation_operator()+" "+history_array[a].get_obj().getquery()+", ";return b=
b=="("?"":b.substr(0,b.length-2)+")"}function query_orderby(){var a,b="";for(a=0;a<history_array.length;a++)if(history_array[a].get_type()=="OrderBy")b+=history_array[a].get_column_name()+" , ";return b=b.substr(0,b.length-1)}
function query_where(){var a,b="(",c="(";for(a=0;a<history_array.length;a++)if(history_array[a].get_type()=="Where")if(history_array[a].get_and_or()==0){b+="( "+history_array[a].get_column_name()+" "+history_array[a].get_obj().getrelation_operator()+" "+history_array[a].get_obj().getquery()+")";b+=" AND "}else{c+="( "+history_array[a].get_column_name()+" "+history_array[a].get_obj().getrelation_operator()+" "+history_array[a].get_obj().getquery()+")";c+=" OR "}c=c!="("?c.substring(0,c.length-4)+")":
"";b=b!="("?b.substring(0,b.length-5)+")":"";if(c!="")b=b+" OR "+c+" )";return b}function check_aggregate(a){var b;for(b=0;b<history_array.length;b++)if("`"+history_array[b].get_tab()+"`.`"+history_array[b].get_column_name()+"`"==a&&history_array[b].get_type()=="Aggregate")return history_array[b].get_obj().get_operator()+"("+a+")";return""}
function check_rename(a){var b;for(b=0;b<history_array.length;b++)if("`"+history_array[b].get_tab()+"`.`"+history_array[b].get_column_name()+"`"==a&&history_array[b].get_type()=="Rename")return" AS `"+history_array[b].get_obj().getrename_to()+"`";return""}function gradient(a,b){var c=document.getElementById(a);c.style.opacity=b;c.style.MozOpacity=b;c.style.KhtmlOpacity=b;c.style.filter="alpha(opacity="+b*100+")";c.style.display="block"}
function fadein(a){for(var b=0;b<=1;){setTimeout("gradient('"+a+"',"+b+")",b*1E3+10);b+=0.01}}function closebox(){document.getElementById("box").style.display="none";document.getElementById("filter").style.display="none"};
