function ClientLogIn(lang){
  //if(lang=='en') inn='/en/services/registration/loader.php';
  //else inn='/services/registration/loader.php';
    
  $.ajax({type: "POST", url: '/services/registration/loader/', traditional: true, timeout: 30000, cache: false,
        data: { pw:$("#auth_cpw").val(), usern: $("#auth_cusername").val(), wht: "login" },
        success: function(data, txtstat, req){
            if(data=="ok"){
              location.reload();
              return false;
            }else{
              $("#auth_err").html(data);
              return false;
            }
        },
        error: function(req,txtstat,err){
            $("#auth_err").html(("Ошибка<br />"+txtstat));            
        }
   });
  return false;
}

function ClientLogOut(lang){
  //if(lang=='en') inn='/en/services/registration/loader.php';
  //else inn='/services/registration/loader.php';
  $.ajax({type: "POST", url: '/services/registration/loader/', traditional: true, timeout: 30000, cache: false, data: { wht: "logout" },
        success: function(data, txtstat, req){ location.reload(); },
        error: function(req,txtstat,err){ }
   });
  return false;
}

function get_mail(part1,part2,part3,part4){
document.write('<a href=mailto:');document.write(part1);document.write('@');document.write(part2);var myclass="";
if(part3 && part3!=""){myclass=" class="+part3;document.write(myclass);}
document.write('>');
if(part4){subtext=part4;}else{subtext=part1+'@'+part2;}
document.write(subtext);
document.write("<\/a>");
}

// для копирования в буфер
function initCopy() {
    document.clip = null;
    document.clip = new ZeroClipboard.Client();
    document.clip.setHandCursor( true );
    document.clip.addEventListener("mouseOver", my_mouse_over);
    document.clip.addEventListener("complete", my_complete);
    document.clip.glue( "d_clip_button" );
}
function my_mouse_over(client){ document.clip.setText( $("#txtcalc").text() ); }
function my_complete(client, text){ alert("Текст скопирован в буфер обмена."); }

function str_pad( input, pad_length, pad_string, pad_type ) {
    // Pad a string to a certain length with another string
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // + namespaced by: Michael White (http://crestidg.com)
    // ПРИМЕР: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
    var half = '', pad_to_go;
    var str_pad_repeater = function(s, len){
            var collect = '', i;
            while(collect.length < len) collect += s;
            collect = collect.substr(0,len);
            return collect;
        };

    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
        else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }

    return input;
}

function mkClipboard(colss){
  tb=$("#tbl_res th");
  first_maxlength=0;
  ms_length=[0,0,0]; // 2,3,4 - столбец
  tmp="";
  for(i=0;i<tb.length;i++){
    if(i==0) first_maxlength=tb.eq(i).text().length;
    if(i==(tb.length-1)) tmp+=tb.eq(i).text();
    else tmp+=tb.eq(i).text()+"||";
  }
  tmp+="@@";

  tb=$("#tbl_res td");
  for(i=0,j=0;i<tb.length;i++){
    if(i%colss==0 && i!=0){
        tmp+="@@";
        j=0;
    }
    if(j==0 && tb.eq(i).text().length>first_maxlength) first_maxlength=tb.eq(i).text().length;
    if(j!=0 && ms_length[(j-1)]<tb.eq(i).text().length ) ms_length[(j-1)]=tb.eq(i).text().length;
    tmp+=tb.eq(i).text()+"||";
    j++;
  }

  out="";
  // выбираем параметры авто
  tbl=$("#dvcalculator tr");
  maxlenn=0;
  tmp2="";
  for(i=0;i<tbl.length;i++){
     tm=tbl.eq(i).find("td");
     if(maxlenn<tm.eq(0).text().length) maxlenn = tm.eq(0).text().length;

     ss=tm.eq(1).find("select");
     if(ss.length){
        tmp2+=tm.eq(0).text()+"||"+ss.get(0).options[ss.get(0).options.selectedIndex].text+"@@";
     }else{
        break;
     }
     if(tm.length==1) break;
  }

  parts=tmp2.split("@@");
  for(i=0;i<parts.length;i++){
    if(parts[i]=="") break;
    pts = parts[i].split("||");
    out+= str_pad(pts[0], (maxlenn+2), " ", "STR_PAD_RIGHT")+pts[1]+"\n";
  }
  out+="\n\n";

  parts=tmp.split("@@");
  // начинаем рисовать таблицу
  for(i=0; i<parts.length; i++){
        if(i==0){ // заголовки
            pts = parts[i].split("||");
            for(j=0;j<pts.length;j++){
                if(j>=colss) break;
                if(j==0) out+=str_pad(pts[j], (first_maxlength+5), " ", "STR_PAD_BOTH");
                else{
                    out+=str_pad(pts[j], (ms_length[j-1]+5), " ", "STR_PAD_BOTH");
                }
            }
            out+="\n";
            continue;
        }
        if(parts[i]=="") break;

        pts = parts[i].split("||");
        for(j=0;j<pts.length;j++){
          if(j>=colss) break;
          if(j==0) out+=str_pad(pts[j], (first_maxlength+5), " ", "STR_PAD_RIGHT");
          else  out+=str_pad(pts[j], (ms_length[j-1]+5), " ", "STR_PAD_LEFT");
        }
        out+="\n";
  }
  $("#txtcalc").text(out);
}
