Query={
 ajaxify:true,
 getList:function(obj){
  var list=[];
  for(var stuff in obj)
   list.push(encodeURIComponent(stuff) + '=' + encodeURIComponent(obj[stuff]));
  return list.join("&");
 },
 toggleFSEP:function(select){
   select.options[select.selectedIndex].value=="other"? $('.othersep').show():$('.othersep').hide();
 },
 toggleExport:function(index){
   $('.exportOutput.container select').attr('selectedIndex',index);
   $('.exportOutput').slideDown('slow');return false;
 },
 updtHdrs:function(){
   $.ajax({url:'grab/?hdrs',dataType:"script",type:'POST'});
 },
 scrollHistory:function(forward){
   i=forward?++qhistory.index:--qhistory.index;
   qhistory.index=i<=0?qhistory.stack.length-1:i%qhistory.stack.length;
   $('.query.container form textarea').attr('value',qhistory.stack[qhistory.index]);
   if(i) $('.history small').html(1+qhistory.index+" <br/>of "+qhistory.stack.length).fadeIn("slow",function(){});
   return false;
 },
 eexport:function(isDownload,isMail){
  if(isDownload){
    $(".mail").slideUp(600,function(){
      a=[ $("select[name='exportType']").val(),
          $(".queryform select[name='forcedownload']").val()
      ]
      document.location =a.join('&&');
      });
   }else{
    if(isMail){
      var f=$(".mail form").get(0).address,dest=$.trim(f.value);
      if(dest){
        $.ajax({url:$("select[name='exportType']").val()+'&'+dest,dataType:"script",cache:false});
      }else{
        f.select();
      }
    }else{
      $(".mail").slideToggle("slow");
   } 
   }return false;
 },
 run:function(obj){
  options={
   q:obj.query.value,
   sep:obj.fieldSep.value,
   osep:obj.outfieldSep.value,
   skip:obj.ignoreLines.value,
   casesense:obj.casesensitivity.value,
   removeCommas:obj.removeCommas.value,
   tablize:obj.wrapIntable.checked
  };

 if(!this.ajaxify)
  $("#queryResult").html("<iframe src='q/?"+this.getList(options)+"&degrade'></iframe>")
 else
  $.ajax({
    type:'POST',
    url:'get/',
    async:true,
    cache:false,
    dataType:"text",
    stamp:new Date().getTime(),
    data:options,
    success:function(res){
      Query.onSuccess(res,this.stamp);
    },
    error:function(res){
      $("#queryResult").addClass('bad userinfo').html(res);
    }});
  return false;
 },
 onSuccess:function(res,stamp){
  //$(".exportOutput").show();
  //$("#queryResult").html();
  document.getElementById('queryResult').innerHTML=res;
  //console.log('done in '+((new Date().getTime()-stamp)/1000)+' seconds(s)');
 },
 dload:function(obj){
  $("#fetchResult").append("Contacting server.. hold on..");
  $(".download.container input.submit").attr("disabled", true);
  $.ajax({
    type:'POST',
    url:'download/',
    dataType:"script",
    data:{
      url:obj.url.value,
      sep:obj.fieldSep.options[obj.fieldSep.selectedIndex].value,
      sep2:obj.othersep.value,startline:obj.firstline.value
    },
    success:function(res){
      //$("#fetchResult").html(res);
      $(".download.container input.submit").attr("disabled", false);  
    },
    error:function(res){
      $("#fetchResult").html(res).addClass("error");
    }});
  $(".download.container").get(0).scrollIntoView();
  return false;
 },
 tabu:function(which){
  //$(".tab").hide().end().find("."+which).show();
  $(".tab").removeClass("activetab").end().find("."+which).addClass("activetab");
  document.location.hash=which.substring(0,3);
  return false;
 },
 toggleImport:function(klass,selectedIndex){
  $("#QuerifyContainer").attr("class",klass);
  $('.'+klass+'.container .fieldSep').attr('selectedIndex',selectedIndex);
  return false;
 }
}

