   /*<![CDATA[*/
  var gTimer;
  var gProgress;
  function progress(){
    if (gProgress){
        gProgress.val(gProgress.val()*1+1);
    }
      gTimer = setTimeout('progress()',1000);
  }
  function clearProgress(){
        gProgress.val(0);
        if (gTimer)    clearTimeout(gTimer);
  }
  $(document).ready(function(){
        var website = $('#website');
        var num = $('#num');
        var total_num = $('#total_num');
        var keyword = $('#keyword');
        var order = $('#order');
        var result = $('#result');
    gProgress = $('#progress');

        $('#searchForm').submit(function(e){
            var meta = $('input[name=meta]:checked');
            var data;
            if (meta.val()=='')
                data = {
                        q: keyword.val(),
                        num: num.val(),
                        total_num: total_num.val(),
                        oe: 'ISO-8859-11',
                        sa: 'N'
                };
            else
                data = {
                    hl: 'th',
                         q: keyword.val(),
                         as_qdr: 'all',
                        num: num.val(),
                        total_num: total_num.val(),
                        meta: meta.val(),
                        oe: 'ISO-8859-11',
                        sa: 'N'
                };
            $.ajax({
             dataType: 'xml',
             type: "GET",
             url: "?search",
             data: data,
             beforeSend: function() {
                    clearProgress();
                    progress();
                    order.html('');
                    result.html('');
             },
             success: function(google) {
               var strOrder='';
               var strResult='';
               var websiteTrim=website.val().replace(/^\s+|\s+$/g,'');
               $('c',google).each(function(n){
                        var site = $(this).text();
                        n++;
                    if (websiteTrim=='' || site.indexOf(websiteTrim) == -1) {
                      strResult += n+': '+site+'<br />';
                        } else {
                          strOrder += websiteTrim+' ติดอันดับที่ '+n+
                                ' ของ keyword \''+keyword.val()+'\'<br />';
                          strResult += n+': <span style="color:red;">'+site+'</span><br />';
                        }
                 });
               clearProgress();
                 result.hide();
                 if (strOrder=='') {
                  order.html('ยังไม่ติดอันดับ '+total_num.val()+' อันดับแรก');
                 }else{
                  order.html(strOrder);
                 }
                 if (strResult=='') {
                  result.html('ไม่พบผลลัพธ์จาก keyword นี้').show('normal');
                 }else{
                  result.html(strResult).show('normal');
                 }
             },
             error: function(a,b,c){
              alert(b);
             }
            });
            return false;
        });
  });
  /*]]>*/
