// WHOIS NASEPTAVAC
// - staci u inputu pro zadani domeny pridat tridu "whois_whisperer"

// inicializace
$(document).ready(function() {
  WhoisWhisperer.init();
});

WhoisWhisperer         = new Object();
// Prodleva mezi stisknutim klavesy 
// a spustenim naseptavace v sekundach
WhoisWhisperer.timeout = 0;
WhoisWhisperer.tlds    = Array(
                          'cz',
                          'eu',
                          'co.cz',
                          'com',
                          'org',
                          'net',
                          'biz',
                          'info'/*/,
                          // ostatni
                          'name',
                          'mobi',
                          'asia',
                          'in',
                          'cc',
                          'tv',
                          'ws',
                          'bz',
                          'mn',
                          'co.uk',
                          'org.uk',
                          'me.uk',
                          'co.in',
                          'net.in',
                          'org.in',
                          'gen.in',
                          'ind.in',
                          'firm.in'
                          /**/
                         );

WhoisWhisperer.lastValue = '';

WhoisWhisperer.init    = function() {
                           if($('.whois_whisperer').get(0) == undefined) {
                             return;
                           }
                           var style = $('.whois_whisperer').attr('style');
                           $('.whois_whisperer').attr('style',style + 'position:relative;');
                           $('.whois_whisperer').attr('autocomplete','off');
                           var height = $('.whois_whisperer').get(0).scrollHeight;
                           height = height + 2;
                           $('.whois_whisperer').after(
                             '<div id="whois_whisperer_box" style="position:absolute; display:none;z-index:9;">'
                            +' <table id="whois_whisperer_table" cellspacing="0">'
                            +' </table>'
                            +'</div>'
                           );

                           $('.whois_whisperer').keyup(function() {
                             WhoisWhisperer.main();
                           });

                           $('.whois_whisperer').focus(function() {
                             WhoisWhisperer.main();
                           });

                           $('.whois_whisperer').blur(function() {
                             setTimeout(function() {
                               $('#whois_whisperer_box').hide()
                               },
                               500);
                           });

                         }

WhoisWhisperer.urlAppend = function(urlKey, hiddenId){
  return ($('#'+hiddenId).length == 0)
    ? ''
    : ('&'+urlKey+'='+$('#'+hiddenId).val())
  ;
}

WhoisWhisperer.main    = function() {
                           setTimeout(function() {
                             var value = $('.whois_whisperer').get(0).value;

                             if(value.length > 2) {
                               value = value.replace(/\s/g,'');
                               var html = '';
                               var tdId = '';
                               if(value != WhoisWhisperer.lastValue) {
                                 WhoisWhisperer.lastValue = value;
                                 value = WhoisWhisperer.repair(value);
                                 for(key in WhoisWhisperer.tlds) {
                                   var fullName = value + '.' + WhoisWhisperer.tlds[key];
                                   tdId = ('status_'+fullName).replace(/\./g,'_');
                                   var href = '/cs/objednavka';

                                   html  += '<tr '
                                         + 'onmouseover="this.bgColor=\'#E8EDF0\';"'
                                         + 'onmouseout="this.bgColor=\'\';"'
                                         + '>'
                                         + '  <td>'
                                         + '    <form method="post" action="' + href + '">'
                                         + '      <input type="hidden" name="domainName" value="' + fullName + '">'
                                         + '      <input type="hidden" name="forceStepOne" value="1"/>'
                                         + '      <button type="submit" name="execute[domainSelect]" >'
                                         + '        <span id="'+tdId+'">'
                                         + '          <img src="/cp/im/in_progress.gif" border="0">'
                                         + '        </span>'
                                         +          fullName
                                         + '      </button>'
                                         + '    </form>'
                                         + '  </td>'
                                         + '</tr>';
                                 }
                                 html  += '<tr '
                                       + 'onmouseover="this.bgColor=\'#E8EDF0\';"'
                                       + 'onmouseout="this.bgColor=\'\';"'
                                       + '>'
                                       + '  <td>'
                                       + '    <form method="post" action="' + href + '">'
                                       + '      <input type="hidden" name="domainName" value="' + value + '">'
                                       + '      <input type="hidden" name="forceStepOne" value="1"/>'
                                       + '      <button type="submit" name="execute[domainSelect]" >'
                                       + '        Zobrazit další domény'
                                       + '      </button>'
                                       + '    </form>'
                                       + '  </td>'
                                       + '</tr>';
                                 $('#whois_whisperer_table').html(html);
                                 for(key in WhoisWhisperer.tlds) {
                                   WhoisWhisperer.check(value,WhoisWhisperer.tlds[key]);
                                 }
                               }
                               $('#whois_whisperer_box').show();
                             } else {
                               $('#whois_whisperer_box').hide();
                             }
                           },
                           this.timeout * 1000);
                         }
WhoisWhisperer.check   = function(domain, tld) {
                           domain = domain+'.'+tld;
                           $.ajax({
                             type:    "POST",
                             async:   true,
                             cache:   false,
                             url:     '/web/domeny/hlavni/whois_agent_new.php', 
                             data:    ({ whois : domain }),
                             success: function(answer) {
                               var result = $.evalJSON(answer);
                               var html  = '';
                               var color = '';
                               var text  = '';
                               switch(result.message) {
                                 case 'OWN':
                                   color = 'orange';
                                   text = 'vedena u nás';
                                   break;
                                 case 'TIMEOUT':
                                   color = 'red';
                                   text = 'chyba';
                                   break;
                                 case 'TRANSFER':
                                   color = 'red';
                                   text = 'převod';
                                   break;
                                 case 'AVAILABLE':
                                   color = 'green';
                                   text = 'objednat';
                                   break;
                                 case 'FORMAT_ERROR':
                                   color = 'red';
                                   text = 'chyba';
                                   break;
                                 case 'ERROR':
                                   color = 'red';
                                   text = 'chyba';
                                   break;
                               }
                               $('#status_'+domain.replace(/\./g,'_')).html('<font color="'+color+'">'+text+'</font>');
                             }
                           });
                         }

WhoisWhisperer.repair  = function(domain) {
                           // Odstraneni diakritiky
                           sdiak = "áäčďéěíĺľňóô öŕšťúů üýřžÁÄČĎÉĚÍĹĽŇÓÔ ÖŔŠŤÚŮ ÜÝŘŽ";
                           bdiak = "aacdeeillnoo orstuu uyrzAACDEEILLNOO ORSTUU UYRZ";
                           tx = "";
                           txt = domain;
                           for(p = 0; p < txt.length; p++) {
                             if (sdiak.indexOf(txt.charAt(p)) != -1) {
                               tx += bdiak.charAt(sdiak.indexOf(txt.charAt(p)));
                             } else tx += txt.charAt(p);
                           }
                           domain = tx;
                           // Odstraneni TLD
                           domain = domain.replace(/^www\./,'');
                           var result = domain.split('.');
                           domain = result[0];
                           return domain;
                         } 

