/*    ./javascript/ajax.js    */

help_list = new Array();
var xhr = new Array();

ORIG_NS_change = new Array();
PREK_NS_change = new Array();

// Inicializace Ajaxu
function Ajax() {
  // For IE7+, Firefox, Chrome, Opera, Safari
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest();
  }
  // For IE6, IE5
  if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
}


function showLoad(size) {
  document.getElementById(size+'_load').style.display="block";
}

function hideLoad(size) {
  document.getElementById(size+'_load').style.display="none";
}


// Funkce pro práci s Ajaxem
function zpracuj_vystup(field, type, odpoved) {
  switch (type) {
    case 'language':
      field.value = odpoved;
      if (odpoved=="neznámý jazyk") {
        field.style.background="#ffbbbb";
      } else {
        field.style.background="#ddffbb";
      }
      break;
      
    case 'username':
    case 'password':
      if (odpoved!="ok") {
        document.getElementById('chyby').innerHTML=odpoved;
        field.style.background="#ffbbbb";
      } else {
        document.getElementById('chyby').innerHTML="";
        field.style.background="#ddffbb";
      }
      break;
      
    case 'customer':
      zakaznici = odpoved.split("\n");
      document.getElementById('customer_help').innerHTML=" ";
      for (i=0; zakaznici[i]!=""; i+=2) {
        document.getElementById('customer_help').innerHTML=document.getElementById('customer_help').innerHTML+
          '<a href="customer" id="customer_help_'+((i/2)+1)+'" class="odkaz_kontext'+(i==0?'_hover':'')+'" onmouseover="if (help_list[\'customercurrent\']>0) {document.getElementById(\'customer_help_\'+help_list[\'customercurrent\']).className=\'odkaz_kontext\';} help_list[\'customercurrent\']=\''+((i/2)+1)+'\'; this.className=\'odkaz_kontext_hover\';" onclick="document.getElementById(\'customer\').value=\''+zakaznici[i+1]+'\'; document.getElementById(\'customer_GUID\').value=\''+zakaznici[i]+'\'; document.getElementById(\'customer_help\').style.display=\'none\'; window.document.getElementById(\'person_name\').innerHTML=\'\'; window.document.getElementById(\'person_GUID\').value=\'\'; return false;">'+zakaznici[i+1]+'</a>';
      }
      help_list['customercount']=(i/2);
      help_list['customercurrent']=1;
      
      if (i>0) {
        document.getElementById('customer_help').style.display='block';
      }      
      break;
      
    case 'translator':
      prekladatele = odpoved.split("\n");
      document.getElementById('translator_help').innerHTML=" ";
      for (i=0; prekladatele[i]!=""; i+=2) {
        document.getElementById('translator_help').innerHTML=document.getElementById('translator_help').innerHTML+
          '<a href="translator" id="translator_help_'+((i/2)+1)+'" class="odkaz_kontext'+(i==0?'_hover':'')+'" onmouseover="if (help_list[\'translatorcurrent\']>0) {document.getElementById(\'translator_help_\'+help_list[\'translatorcurrent\']).className=\'odkaz_kontext\';} help_list[\'translatorcurrent\']=\''+((i/2)+1)+'\'; this.className=\'odkaz_kontext_hover\';" onclick="document.getElementById(\'translator\').value=\''+prekladatele[i+1]+'\'; document.getElementById(\'translator_GUID\').value=\''+prekladatele[i]+'\'; document.getElementById(\'translator_help\').style.display=\'none\'; return false;">'+prekladatele[i+1]+'</a>';
      }
      help_list['translatorcount']=(i/2);
      help_list['translatorcurrent']=1;
      
      if (i>0) {
        document.getElementById('translator_help').style.display='block';
      }
      break;
      
  };
}

function check_field(field, type, load, editformular) {

  xhr['other'] = Ajax();

  if (load!=false) {load=true;}

  if (xhr['other']) {

    xhr['other'].open("GET", "./index.php?page=new&subpage=check_field&value="+field.value+"&type="+type+"&charset=č", true);

    xhr['other'].onreadystatechange=function() {
      switch (xhr['other'].readyState) {
        case 1:
          if (load) {showLoad('small');}
          break;
        case 2:
          if (load) {hideLoad('small');}
          break;
        case 4:
          if(xhr['other'].status == 200) {zpracuj_vystup(field, type, xhr['other'].responseText); editformular.submit();}
          if (load) {hideLoad('small');}
          break;
      };
    };

    xhr['other'].send(null);

  }

}

function key_press(field, type, load, e) {

  if (e.keyCode=="40" || e.keyCode=="38") {
    if (help_list[type+'current']>0) {document.getElementById(type+'_help_'+help_list[type+'current']).className='odkaz_kontext';}
    if (e.keyCode=="38") {help_list[type+'current']-=1;}
    if (e.keyCode=="40") {help_list[type+'current']+=1;}
    if (help_list[type+'current']<=0) {help_list[type+'current']=help_list[type+'count'];}
    if (help_list[type+'current']>help_list[type+'count']) {help_list[type+'current']=1;}
    document.getElementById(type+'_help_'+help_list[type+'current']).className='odkaz_kontext_hover';
    return false;
  } else {
    if (help_list[type+'current']>0 && e.keyCode=="13") {
      document.getElementById(type).value=zakaznici[((help_list[type+'current']-1)*2)+1];
      document.getElementById(type+'_GUID').value=zakaznici[((help_list[type+'current']-1)*2)];
      document.getElementById(type+'_help').style.display='none';
      if (type=="customer") {
        document.getElementById('person_name').innerHTML='';
        document.getElementById('person_GUID').value='';
      }
      return false;
    }
    check_field(field, type, load);
    help_list[type+'current']=0;
    return true;
  }
}

function lock_order(order, time) {

  xhr['lock'] = Ajax();

  if (xhr['lock']) {
    xhr['lock'].open("GET", "./index.php?page=ajax&subpage=lock-order&ID="+order+"&time="+time+"&charset=č", true);
    
    xhr['lock'].onreadystatechange=function() {
      switch (xhr['lock'].readyState) {
        case 1:
          break;
        case 2:
          break;
        case 4:
          if(xhr['lock'].status == 200) {setTimeout('lock_order("'+order+'", "'+xhr['lock'].responseText+'");', 40000);}
          break;
      };
    };
    
    xhr['lock'].send(null); 
  }
}

function lock_address(company, time) {

  xhr['lock'] = Ajax();

  if (xhr['lock']) {
    xhr['lock'].open("GET", "./index.php?page=ajax&subpage=lock-address&GUID_AD="+company+"&time="+time+"&charset=č", true);
    
    xhr['lock'].onreadystatechange=function() {
      switch (xhr['lock'].readyState) {
        case 1:
          break;
        case 2:
          break;
        case 4:
          if(xhr['lock'].status == 200) {setTimeout('lock_address("'+company+'", "'+xhr['lock'].responseText+'");', 40000);}
          break;
      };
    };
    
    xhr['lock'].send(null); 
  }
}

function search_translator(kam, from, to) {

  xhr['translator'] = Ajax();

  if (xhr['translator']) {
    xhr['translator'].open("GET", "./index.php?page=ajax&subpage=search-translator&from="+from+"&to="+to+"&charset=č", true);
    
    xhr['translator'].onreadystatechange=function() {
      switch (xhr['translator'].readyState) {
        case 1:
          document.getElementById(kam).innerHTML='Načítám...!';
          break;
        case 2:
          break;
        case 4:
          if(xhr['translator'].status == 200) {document.getElementById(kam).innerHTML=xhr['translator'].responseText;}
          else {document.getElementById(kam).innerHTML='Chyba při načítání souboru!';}
          document.getElementById('prekladatel_href').style.display='none';
          document.getElementById(kam).style.display='block';
          break;
      };
    };
    
    xhr['translator'].send(null); 
  }
}



function loadFileinfo(file) {

  xhr['file'] = Ajax();

  if (xhr['file']) {


    if (0 < xhr['file'].readyState && xhr['file'].readyState < 4) {
      xhr['file'].abort();
    }
    parameters="ling=ajax";

    xhr['file'].open("POST", './index.php?page=ajax&subpage=file-info&file='+file+'&charset=č', true);
    xhr['file'].setRequestHeader("User-Agent", "Ling/1.0");
    xhr['file'].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr['file'].setRequestHeader("Content-length", parameters.length);
    xhr['file'].setRequestHeader("Connection", "close");

    xhr['file'].onreadystatechange=function() {
      switch (xhr['file'].readyState) {
        case 1:
          window.document.getElementById('file-info-load').style.display='block';
          window.document.getElementById('file-info-data').style.visibility='hidden';
          break;
        case 2:
          break;
        case 4:
          window.document.getElementById('file-info-load').style.display='none';
          if(xhr['file'].status == 200) {
            window.document.getElementById('file-info-data').innerHTML=xhr['file'].responseText;
            window.document.getElementById('file-info-caption').innerHTML='Informace o souboru <a href="#" onclick="window.document.getElementById(\'file-info-data\').innerHTML=\'<table style=\\\'width: 100%;\\\'><tr><td>Není vybrán žádný soubor.</td></tr></table>\'; window.document.getElementById(\'file-info-caption\').innerHTML=\'Informace o souboru\'; return false;">(skrýt)</a>';
          } else {
            window.document.getElementById('file-info-data').innerHTML='<b>Chyba skriptu!</b>';    
          }
          window.document.getElementById('file-info-data').style.visibility='visible';
          break;
      };
    };

    xhr['file'].send(parameters);

  }
}

function guid_calc(old_guid) {
  new_guid = old_guid.substr(0, 8);
  new_guid += old_guid.substr(9, 4);
  new_guid += old_guid.substr(14, 4);
  new_guid += old_guid.substr(19, 4);
  new_guid += old_guid.substr(24, 12);
  return new_guid;
}
