var row_count=1;
var now_active=new Array();
var parb_1=false
var do_expand=true;
var field_count=17;
var do_ch=true;
//var data=new Array();


$(window).keydown(function(event){
//conr(event.keyCode)

if(do_ch==false &&  event.keyCode==13){
$('#hints').animate({height:"0px"},300)
el('lst_'+now_active['x']+'_'+now_active['y']).value=el('endExpand').value;
do_ch=true;
}


if(event.keyCode==13){

if(now_active['x']=='6' && el('lst_6_'+now_active['y']).value!="" && el('lst_4_'+now_active['y']).value!=""){

$.ajax({
beforeSend:function(){$('#loader').css({display:"block"})},
url: "ajax/ajax.php?func=get_km&from="+escape(el('lst_4_'+now_active['y']).value)+'&to='+el('lst_6_'+now_active['y']).value,
dataType: "json",
success: function(json){
alert(json);
el('lst_9_'+now_active['y']).value=json;


},
complete:function(){$('#loader').css({display:"none"})}
});


}

if(parseInt(now_active['x'])+1 >17){
newnumber=1;
newnumber1=parseInt(now_active['y'])+1;}else{
newnumber=parseInt(now_active['x'])+1;
newnumber1=now_active['y'];}
if(document.getElementById('lst_'+newnumber+'_'+newnumber1)){
$('#lst_'+newnumber+'_'+newnumber1).focus();
}else{add_more();$('#lst_'+newnumber+'_'+newnumber1).focus();}
return false;
}



if(do_ch!=false && event.keyCode!=13 && event.keyCode!=40 && event.keyCode!=38 && event.keyCode!=38){
if(now_active['x']=='3' || now_active['x']=='5'){
$('#endExpand').empty();
for(i in firmas){
if(firmas[i].indexOf(el('lst_'+now_active['x']+'_'+now_active['y']).value) =='0' ){
$('#endExpand').append('<option value="' + firmas[i] + '">' + firmas[i] + '</option>');
}}
if(el('hints').style.height!='150px'){$('#hints').animate({height:"150px"},300)}
}
if(now_active['x']=='4' || now_active['x']=='6'){

$('#endExpand').empty();
for(i in adreses){
if(adreses[i].indexOf(el('lst_'+now_active['x']+'_'+now_active['y']).value) =='0' ){
$('#endExpand').append('<option value="' + adreses[i] + '">' + adreses[i] + '</option>');
}}
if(el('hints').style.height!='150px'){$('#hints').animate({height:"150px"},300)}

//hering
//conr('klausos');
}


}



if((event.keyCode==40 || event.keyCode==38)  &&  do_ch==true && (now_active['x']=='4' || now_active['x']=='6' || now_active['x']=='3' || now_active['x']=='5' )){
//conr('ok');
do_ch=false;
$('#endExpand').focus();
}



});


function do_it(id,id_y){
if(do_expand==true){
$(".title:not(#title_"+id+")").css("color","#BABABA");
$("#title_"+id).css("color","black");



$("#main > .expand,.title").animate({width: "40px"},100 )
.fadeTo("slow",0.33);


$("#lst_"+id+",#title_"+id).animate({width: "200px"}, 300)
.fadeTo("slow",100);
}
$(".input_i:not(#lst_"+id+'_'+id_y+")").css({background:"white"});
$("#lst_"+id+'_'+id_y).css({background:"#cdedc2"});
$("#main > .expand:not(#lst_"+id+"),.title:not(#title_"+id+")").fadeTo("fast",0.33);
$("#lst_"+id+",#title_"+id).fadeTo("fast",100);

now_active['x']=id;
now_active['y']=id_y;


}

function add_more(id){
row_count++;
for (i=1;i<=field_count;i++){
$("#lst_"+i).append('<input id="lst_'+i+'_'+row_count+'" onfocus="do_it('+"'"+i+"'"+','+"'"+row_count+"'"+')" type="text" class="input_i"  />');
if(i==1){
el('lst_'+i+'_'+row_count).value=datums_yyyy;
}
}
}

function send(){
data="";
for (i=1;i<=row_count;i++){
for (g=1;g<=field_count;g++){
data=data+'_!!_'+el('lst_'+g+'_'+i).value;
}
if(i==row_count){
}else{
data=data+'_splt_'}
}

//conr(data);
//conr(escape(data));
data=escape(data)

$.ajax({
beforeSend:function(){$('#loader').css({display:"block"})},
url: "ajax/ajax.php?func=send&data="+data,
dataType: "json",
success: function(json){
},
complete:function(){$('#loader').css({display:"none"})}
});
}

function search_n(sm,bg){
var search=new Array();
var parb=false;
one="";
for (i=1;i<=row_count;i++){
//conr('for row count i='+i);
//conr('el value '+el('lst_'+bg+'_'+i).value)
if(el('lst_'+bg+'_'+i).value==""){
//conr('if value == 0');
if(parb==true){
one=one+'_!!_'+el('lst_'+sm+'_'+i).value+'_sp_'+i
}else{
one=el('lst_'+sm+'_'+i).value+'_sp_'+i
parb=true
}}}
//conr('send: '+one)

$.ajax({
beforeSend:function(){$('#loader').css({display:"block"})},
url: "ajax/ajax.php?func=search&one="+escape(one),
dataType: "json",
success: function(json){
parb=false;
//conr(json);
for(i in json){
el('lst_'+bg+'_'+json[i]['row']).value=json[i]['adress'];
}
if(parb_1==false){
parb_1=true;
search_n('5','6');
}

},
complete:function(){$('#loader').css({display:"none"})}
});}


function expand_all(){
$("#contener").animate({width: "2550px"}, 300)
$("#exp_thiz,#exp_thiz_1").animate({width: "2510px"}, 300)
$(".expand,.title").animate({width: "140px"}, 300)
$(".title").css({color:"black"});
do_expand=false;
// pa 40 vairak
//pirmst 950,  +650 !!

//id="contener"


}








