//utils
function emptySelect(id){
  mySelect = document.getElementById(id);
  if(mySelect && mySelect.options){
    mySelect.options.length = 0;
  }
}

// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function tbodyHasRowIds(tbodyId, rowIds){
  var obj = document.getElementById(tbodyId);
  if(obj){
    var nChildren = obj.rows.length;
    var nChildNames = rowIds.length;
    for(var i=0; i<nChildren; i++){
      for(var j=0; j<nChildNames; j++){
	    if(obj.rows[i].id.indexOf(rowIds[j]) >= 0){
		  return true;
		}
	  }
    }
  }
  return false;
}

//Upload de fichero
function handleResponse(documentObject,stringToEvaluate) {
	window.eval(stringToEvaluate);
	if (documentObject.location){ // Should be usefull for IE only .. but I cannot test it
		if(documentObject.location != "") documentObject.location.replace('about:blank');
	}		
}

//finanzia
function finanzia_search(id, number){
  new Ajax.Request(
    '/vehicles/search/' + id + '?scaffold_id=vehicle&vehicle_number=' + number, 
    { asynchronous: true, 
      evalScripts: true, 
      onLoading: function(request){
        Element.show('vehicle-search-' + id + '-loading-indicator');
      }
    }
  ); 
}
function pda_finanzia_search(id, number){
  document.location.href = '/vehicles/search/' + id + '?scaffold_id=vehicle&vehicle_number=' + number;
}

//google
function google_asearch(){
  a = document.getElementById('google_address');
  if (a && a.value != ''){
    str = a.value
    window.open('http://maps.google.com/maps?f=d&hl=es&saddr=' + 
      str + 
      '&daddr=sepulveda+10,+alcobendas&sll=37.0625,-95.677068&sspn=29.025693,82.265625&layer=&ie=UTF8&z=11&om=1', 'google');
  }
}

//windows
function modal_window(str_title, str_url, str_url_params, i_width, i_height){
  if (!i_width ) i_width = 700;
  if (!i_height ) i_height = 450;
    
  win = new Window('window_id', {title: str_title, width: i_width, height: i_height, resizable: false, closable: true, maximizable: false,minimizable: false, url: str_url + '?' + str_url_params});                         
  win.setDestroyOnClose(); 
  win.showCenter(true);
};

//bargain
function vehicleToForm(brand, model, number, promotion){
  $('contact_brand').value = brand;
  $('contact_model').value = model;
  $('contact_number').value = number;
  if (promotion!=null) 
    $('contact_promotion').value = promotion;
  
  $('contact_name').focus();
  return false;
}

//Show photos in gallery
var first_on = 0;
var last_on = 2;
var n_photos = 0;
function photos_to_the_right(){
  if(last_on < (n_photos-1)) {
    Element.show('photo_' + (last_on + 1));
    Element.hide('photo_' + (first_on));
    first_on += 1;
    last_on += 1;
  }
}
function photos_to_the_left(){
  if(first_on > 0){
    Element.show('photo_' + (first_on - 1));
    Element.hide('photo_' + (last_on));
    first_on -= 1;
    last_on -= 1;
  }
}
function showPhoto(name,id) {
  $('selected_photo').src="/files/download?file_name="+name;
  $('selected_photo').setAttribute('id_photo',id);
}
function zoomPhoto() {
   modal_window("Zoom", "/vehicle_attachments/zoom/"+$('selected_photo').getAttribute('id_photo'), "");
}
