function la_translate() {
    var from=document.getElementById('l1').value;
    var to=document.getElementById('l2').value;
    var la_num=document.getElementById('la_num').value;
    document.getElementById('tr_form').style.display = "none";
    document.getElementById('loading').style.display = "block";  
    var sm_res;
    var answ='';
    for (var i=1; i<=la_num; ++i){
        if (answ!=''){answ+='. ===. ';}
        sm_res=document.getElementById('la'+i).innerHTML;
        answ+=sm_res;
    }
    var num_input=google_la_tr(from, to, answ);
    google.language.getBranding('br_google');
    window.setTimeout(function() {
        sm_res=document.getElementById('gr'+num_input).value;
        if (sm_res=='') {
            window.setTimeout(function() {
               sm_res=document.getElementById('gr'+num_input).value;
               if (sm_res=='') {
                    window.setTimeout(function() {
                        render(num_input, la_num);  
                    }, 1000);
               }
               else {render(num_input, la_num);}
        }, 1000);
       } else {render(num_input, la_num);}
    },
    2000);
    return false;
}

function render(num_input, la_num){
   var num;
  var la=new Array();
  var stra=new Array();
  var sm_res;
  var text='';
  var f_num;
  for (var i=0; i<=num_input; ++i){
      text+=document.getElementById("gr"+i).value;
    }
  text+=' ';
  la=text.split(' ===. ');
    for ( i=1; i<=la_num; ++i){
       document.getElementById("tr_"+i).innerHTML='<p class="red">'+la[i-1].substr(0,la[i-1].length-1)+'</p>';
    }
    document.getElementById('loading').style.display = "none";
    document.getElementById('tr_form').style.display = "block";

}

function google_la_tr(from, to, text){
   text = text.replace(/&amp;/, " and ");
   var chunk='';
   var dotpos;
   var num_chunk=-1;
   var num_chunk1=-1; 
   if(from == 'en') {var maxchars = 1000;} else {var maxchars = 300;};
   if(from == 'nb') {from='no';};
   if(to == 'nb') {to='no';};
    if(text.length < maxchars){
        num_chunk++;
        add_input(num_chunk);
        google.language.translate(text, from, to,
             function(result) {
                if (!result.error) {document.getElementById('gr'+num_chunk).value=result.translation;};
              }
        );
     }
     else
      {
        text+='. ===. ';
        chunk = text.substr(0, maxchars); 
        while(chunk.length>7)
        {
            num_chunk++;
            add_input(num_chunk);
            dotpos = chunk.lastIndexOf('. ===. ');
            if(dotpos <1) dotpos=chunk.lastIndexOf('.')+1;
            if(dotpos <1) dotpos=chunk.lastIndexOf(',')+1;
            if(dotpos <1) dotpos=chunk.lastIndexOf(' ')+1;
            chunk = chunk.substr(0, dotpos);
            google.language.translate(chunk, from, to,
                function(result)
                {
                    if (!result.error){num_chunk1++;document.getElementById('gr'+num_chunk1).value=result.translation;}
                  }
            );
            text = text.substr(chunk.length);
            chunk = text.substr(0, maxchars);
        }
      }
      return num_chunk;
  }

function add_input(id){
 var field = document.createElement("input");
field.setAttribute("type","hidden");
field.setAttribute("value",'');
field.setAttribute("id","gr"+id);
field.setAttribute("name","gr"+id);
document.getElementById("gr_transl").appendChild(field);
}
