var cur_year = new Date();
cur_year = cur_year.getFullYear();
var to = null;

function fillCars()
{
  var cat = document.getElementById( 'idb' );
 
  cat.options.length = 0;
  cat.options[ cat.options.length ] = new Option( '...', 0 );    
  
  for( var i in Cars )
  {
    var c = Cars[i];
    cat.options[ cat.options.length ] = new Option( c.t + ' ' + c.n, c.b+':'+c.m );
  }
}

function fillCars2()
{
  var cat = document.getElementById( 'idb' );
 
  cat.options.length = 0;
  cat.options[ cat.options.length ] = new Option( '...', 0 );    
  
  for( var i in Cars )
  {
    var c = Cars[i];
    c.t = c.t.replace(/&laquo;(.+)&raquo;/, '«$1»');
    cat.options[ cat.options.length ] = new Option( c.t, c.i );
    if( c.i == initial )
    {
      cat.selectedIndex = cat.options.length-1;
    } 
  }
}

function loadNotify()
{
  document.getElementById('loading').style.display = "block";
}

function readContent() 
{
  document.getElementById('loading').style.display = "none";
  $('#slowconnect').hide();
  clearTimeout( to );
  
  var Data = eval("(" + http_request.responseText + ")");
  
  Data.id_city = Locations;
  if(Cars)
  {
    Data.idb = Cars;
  }
  for(i in fields)
  {
    if(fields[i].type == 'text')
    {
      document.getElementById(fields[i].name).value = Data[fields[i].name];
    }
    else if(fields[i].type == 'textarea')
    {
        document.getElementById(fields[i].name).value = Data[fields[i].name];
    }
    else if(fields[i].type == 'select')
    {
        
      fillSelect(document.getElementById(fields[i].name), Data[fields[i].name], Data[fields[i].name + '_sel']);
    }
    else if(fields[i].type == 'checkbox')
    {
      if(Data[fields[i].type])
      {
        document.getElementById(fields[i].name).checked = true;
      }
    }
  }
}

function fillSelect( sel, data, current)
{
  sel.options.length = 0;

  if(data.length>1)
  {
    sel.options[ sel.options.length ] = new Option( '...', 0 );    
  }
  
  for( var i in data )
  {
    var c = data[i];
    sel.options[ sel.options.length ] = new Option( c.t, c.i );
    if( c.i == current )
    {
      sel.selectedIndex = sel.options.length-1;
    } 
  }
}


function sendRequest()
{
    var car = document.getElementById('idb').value.split(":");
    makeRequest('http://www.redom.ru/auto/'+(doc == 'moto' ? 'moto' : 'cars')+'/ajax/brand_model.cgi?id_model=' + car[1] + "&id_brand=" + car[0], readContent);
}

function sw_dmg(f)
{
  if(f.checked)
  {
    document.getElementById('dmg_l').style.display = "block";
    document.getElementById('damages').style.display = "block";
  }
  else
  {
    document.getElementById('dmg_l').style.display = "none";
    document.getElementById('damages').style.display = "none";
  }
}

function find_model( string, model )
{
  if( string == 'undefined' )
  {
    return;
  }
  pattern = new RegExp( string, 'i' );
  for( i = 0; i < model.options.length; i ++ )
  {
    if( model.options[i].text.match( pattern ) )
    {
      model.options[i].selected = true;
      return;
    }
  }
}

var load_to=null;

function start_load( model )
{
  load_to = setTimeout( function() { load_model( model ) }, 100 );
}

function stop_load()
{
  clearTimeout( load_to );
}

var selected_model=0;

function load_model( model )
{
  if( selected_model == model.options[ model.selectedIndex ].value )
  {
    return;
  }
  selected_model = model.options[ model.selectedIndex ].value;
  loadNotify();
  to = setTimeout( slow_connect, 5000 );
  sendRequest();
}

function findPos(obj) 
{
    var curleft = curtop = 0;
    if (obj.offsetParent)
    {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent)
        {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

function helper(field)
{
    var pos = findPos(field);
    var help = field.getAttribute('help');

    if(help != '' && help)
    {
        var div = document.createElement('div');
        div.id = field.id+'_h';
    
        div.style.left = pos[0] + field.offsetWidth/2;
        div.style.top = pos[1] - 32;
        div.innerHTML = '<p>'+ help +'</p><img src="http://i.redom.ru/help_p.gif"><!--[if lte IE 6.5]><iframe></iframe><![endif]-->';
        div.className = 'select-free help';
    
        document.body.appendChild(div);
    }
}

function set_error(field, message)
{
    var pos = findPos(field);
  
    var div = document.createElement('div');
    div.id = field.id+'_e';

    div.style.left = pos[0] + field.offsetWidth;
    div.style.top = pos[1] - 12;
    div.innerHTML = '<p>'+message+'</p><!--[if lte IE 6.5]><iframe></iframe><![endif]-->';
    div.className = 'select-free error';

    document.body.appendChild(div);
}

function remove_helper(f)
{
  document.body.removeChild(document.getElementById(f.id+'_h'));
}

function remove_error(f)
{
  var i = document.getElementById(f);
  if(i)
  {
     document.body.removeChild(i);
  }
}

function slow_connect()
{
  $('#slowconnect').show();
  clearTimeout( to );
}
