/*
 * A car-object retrieved from the ccdb-database.
 */
function Car(kenteken, dealernummer) {
  /* The required fields which the class assumes is set */
  if(  arguments.length < 2)
    return null;
  var kenteken     = kenteken;
  var dealernummer   = dealernummer;
  var theme       = ccdb2_theme;
  
  
  /* The undefined fields */
  var imageExist     = [];
  var cachedInfo    = [];
  var voertuigXML;
  
  //Getters
  this.getKenteken     =   function(){ return kenteken;     }
  this.getDealernummer   =   function(){ return dealernummer;   }
  this.getKenDsl       =   function(){ return this.getKenteken() + "_" + this.getDealernummer() }
  this.getKenNumberDsl   =  function(number){ return this.getKenteken() + number + "_" + this.getDealernummer() }
  
  //Functions
  
  //Check if the image exists on the specified index
  this.imageExists = function(index){
    if(imageExist[index] == undefined){
        var lExists = false;
        $.ajax({
        type: "GET",
        url: serverurl + "/imagestore/exists.aspx?filename=" + kenteken + index + "_" + dealernummer + ".jpg",
        dataType: "text",
        async: false,
        success: function( text ) {
          imageExist[index] = (text == "true");
        }
        }); 
    }
    return imageExist[index];
  }
  //Get the XML file from a car
  this.fillCarInformation = function(){
    if(    kenteken     == undefined || 
        dealernummer  == undefined )
      return false;
      $.ajax({
      type: "GET",
      url: serverurl + ccdb2dll + "/actGetVoertuigen?site_id=" + dealernummer + "&theme=" + ccdb2_theme + "&kenteken=" + kenteken,
      dataType: "xml",
      async: false,
      success: function(xml){
        voertuigXML = $(xml);
      }
    })
  }
  //Load the information from an xml file from multiple cars
  this.carInfoFromXML = function(XML){
    $(XML).find("VOERTUIG").each(function(){
      if($(this).find("KENTEKEN").text() == kenteken && $(this).attr("dslnummer") == dealernummer){
        voertuigXML = $(this);
      }
    })
  }
  //Load information from a given <VOERTUIG></VOERTUIG> tag
  this.carInfoFromVoertuigXML = function(XML){
    voertuigXML = $(XML);  
  }
  //get the content of a field value
  this.getFieldInformation = function(fieldname){
    if(cachedInfo[fieldname] == undefined)
      cachedInfo[fieldname] = $(voertuigXML).find(fieldname).text();
    return cachedInfo[fieldname];
  }
  //get the xml-code from a field value
  this.getFieldXML = function(fieldname){
    return $(voertuigXML).find(fieldname);
  }
  //get the image url of this car
  this.getImageUrl = function(index, width, height){
    return "/imagestore/get.aspx?filename=" + kenteken + index + "_" + dealernummer + ".jpg&width=" + width + "&height=" + height;  
  }
}

Array.prototype.getCar = function(KenDsl){
  var result;
  $.each(this, function(){
    if(this.getKenDsl() == KenDsl)
      result = this;               
  })
  return result;
}

Array.prototype.getKenDsl = function(){
  var tempArray = [];
  $.each(this, function(){
    tempArray.push(this.getKenDsl());
  })
  return tempArray;
}
