
motor_model = new Object();



/*
*
* Default settings
* 
*/

motor_model.mode = 'estate';

motor_model.PurposeIDList = new Array( );
motor_model.PurposeIDList.push(1);

motor_model.PriceRange = new Array();
motor_model.PriceRange.push(0);
motor_model.PriceRange.push( max_price_buy );


/*
*
* Methods
* 
*/
motor_model.getJSON = function() 
{
   return $.toJSON(motor_model);
}


/*
*
* Get methods
* 
*/
motor_model.getPurposeID = function()
{
  return this.PurposeIDList[0];
}

motor_model.getPriceRange = function()
{
  return new Array ( this.PriceRange[0], this.PriceRange[1] );
}

motor_model.getCategoryIDList = function ()
{
  if ( typeof(this.CategoryIDList) == 'undefined' )
  {
    return false;
  }
  else
  {
    return this.CategoryIDList;
  }
}

motor_model.getSubCategoryIDList = function ()
{
  if ( typeof(this.SubCategoryIDList) == 'undefined' )
  {
    return false;
  }
  else
  {
    return this.SubCategoryIDList;
  }
}

motor_model.getZipList = function ()
{
  if ( typeof(this.ZipList) == 'undefined' )
  {
    return false;
  }
  else
  {
    return this.ZipList;
  }
}

motor_model.getCity = function ()
{
  if ( typeof(this.City) == 'undefined' )
  {
    return false;
  }
  else
  {
    return this.City;
  }
}


motor_model.getAddress = function ()
{
  if ( typeof(this.Address) == 'undefined' )
  {
    return false;
  }
  else
  {
    return this.Address;
  }
}

motor_model.getRef = function ()
{
  if ( typeof(this.Ref) == 'undefined' )
  {
    return false;
  }
  else
  {
    return this.Ref;
  }
}


motor_model.getDistanceFromCity = function ()
{
  if ( typeof(this.DistanceFromCity) == 'undefined' )
  {
    return false;
  }
  else
  {
    return this.DistanceFromCity;
  }
}

motor_model.getOrderByFields = function ()
{
  if ( typeof(this.OrderByFields) == 'undefined' )
  {
    return false;
  }
  else
  {
    return this.DistanceFromCity;
  }
}

motor_model.isCategoryIDset = function ( id )
{
  if (this.getCategoryIDList())
  {
    if ( $.inArray( parseInt(id), this.getCategoryIDList() ) >-1 )
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  else
  {
    return false;
  }
}
/*
*
* set methods
* 
*/
motor_model.setMode = function ( mode )
{
    //console.log('changed mode: '+this.mode+' -> '+mode);    
  this.mode=mode;
}

motor_model.setPurposeID = function( val )
{
    //console.log('changed PurposeID: '+this.PurposeIDList[0]+' -> '+val);  
  this.PurposeIDList[0]=val;
  if (this.getPurposeID()==1)
  {
    this.setPriceRange(0, max_price_buy);
  }
  if (this.getPurposeID()==2)
  {
    this.setPriceRange(0, max_price_rent);
  }  
}

motor_model.setAddress = function (address)
{
  this.Address=address;
  if ($.trim(address)=='' )
  {
    delete this.Address;
  }
}

motor_model.setRef = function (ref)
{
  this.Ref=ref;
  if ($.trim(ref)=='' )
  {
    delete this.Ref;
  }
}

motor_model.setPriceRange = function( min, max )
{
    //console.log('changed PriceRange: '+this.PriceRange[0]+'-'+this.PriceRange[1]+' -> '+min+'-'+max);  
  this.PriceRange[0]=min;
  this.PriceRange[1]=max;
}

motor_model.addCategoryID = function ( cat )
{
  if ( typeof( this.CategoryIDList ) == 'undefined' )
  {
    this.CategoryIDList = new Array();
      //console.log('categoryIDList array created');    
  }
  
  if (!( $.inArray(parseInt(cat),this.CategoryIDList) > -1 ))
  {
    this.CategoryIDList.push( parseInt(cat) );
      //console.log('CategoryID added');
  }else
  {
      //console.log('CategoryID already in array');
  }
}

motor_model.delCategoryID = function ( cat )
{
  if ( typeof( this.CategoryIDList ) == 'undefined' )
  {
    //console.log('CategoryIDList does not exist');
  }
  else
  {
    if (!( $.inArray(parseInt(cat),this.CategoryIDList) > -1 ))
    {
      //console.log('CategoryID not in array');
    }
    else
    {
      index = $.inArray(parseInt(cat),this.CategoryIDList);
      this.CategoryIDList.splice(index,1);
      
      this.delSubCategoryFromCategoryID( cat );
      
      //console.log('CategoryID removed from array');
      
      if ( this.CategoryIDList.length == 0 )
      {
        delete this.CategoryIDList;
        //console.log('CategoryIDList removed');
      }
    }
  }
}

motor_model.addSubCategoryID = function ( cat, par )
{
  if ( typeof( this.SubCategoryIDList ) == 'undefined' )
  {
    this.SubCategoryIDList = new Array();
      //console.log('SubCategoryIDList array created');    
  }
  
  exists = false;
  parent_exists = false;  
  for (i=0;i<this.SubCategoryIDList.length;i++)
  {
    if ( this.SubCategoryIDList[i][1] == cat )
    {
      exists = i;
      //console.log('SubCategoryID already in array');     
    }
    if ( this.SubCategoryIDList[i][0] == par )
    {
      parent_exists = i;
      //console.log('parent CategoryID already in array');      
    }
  }
  
  if ( parent_exists === false )
  {
    SubCategorie = new Array( par, cat );
    this.SubCategoryIDList.push( SubCategorie );
      //console.log('SubCategoryID added');
  }
  else
  {
    if ( exists === false )
    {
      this.SubCategoryIDList[parent_exists][1]=cat;
        //console.log('existing parent updated');
    }
    else
    {
      //console.log('CategoryID and SubCategoryID already in array');
    }
  }
}

motor_model.delSubCategoryFromCategoryID = function ( par )
{
  if ( typeof( this.SubCategoryIDList ) == 'undefined' )
  {
    //console.log('SubCategoryIDList does not exist');
  }
  else
  {
    parent_exists = false;
    for (i=0;i<this.SubCategoryIDList.length;i++)
    {
      if ( this.SubCategoryIDList[i][0] == par )
      {
        parent_exists = i;
          //console.log('parent CategoryID has subcategory set');      
      }
    }
    
    if ( parent_exists !== false )
    {
      this.SubCategoryIDList.splice(parent_exists, 1);
        //console.log('SubCategory deleted');
      
      if (this.SubCategoryIDList.length==0)
      {
        delete this.SubCategoryIDList;
        //console.log('SubCategoryIDList removed');
      }
      
    }
    else
    {
      //console.log('parent does not have SubCategory set');
    }
  }
}

motor_model.setDistanceFromCity = function ( distance )
{
  if ( typeof( this.DistanceFromCity ) == 'undefined' )
  {
    if ( parseInt(distance) != 0 )
    {
      this.DistanceFromCity = parseInt(distance);
        //console.log('DistanceFromCity created');
    }
  }
  else
  {
    if ( parseInt(distance) != 0 )
    {
      this.DistanceFromCity = parseInt(distance);
        //console.log('DistanceFromCity updated');
    }
    else
    {
      delete this.DistanceFromCity;
        //console.log('DistanceFromCity removed');
    }
  }  
}

motor_model.setZipList = function ( list )
{  
  if ( list.length == 0 )
  {
    if ( typeof( this.ZipList ) != 'undefined' )
    {
      delete this.ZipList;
        //console.log('ZipListF removed');
    }
  }
  else
  {
    this.ZipList = list;
      //console.log('ZipListF set');
  }
}

motor_model.setCity = function ( city )
{  
  if (city=='')
  {
    if ( typeof( this.City ) != 'undefined' )
    {
      delete this.City;
        //console.log('City removed');
    }
  }
  else
  {
    this.City=city;
      //console.log('City set');
  }
}

motor_model.setGarden = function (val)
{
  this.Garden = parseInt(val);
}

motor_model.setTerrace = function (val)
{
  this.Terrace = parseInt(val);
}

motor_model.setParking = function (val)
{
  this.Parking = parseInt(val);
}

motor_model.setElevator = function (val)
{
  this.Elevator = parseInt(val);
}

motor_model.setPool = function (val)
{
  this.Pool = parseInt(val);
}

motor_model.setStables = function (val)
{
  this.Stables = parseInt(val);
}

motor_model.setSidebuilding = function (val)
{
  this.Sidebuilding = parseInt(val);
}

motor_model.setOffice = function (val)
{
  this.Office = parseInt(val);
}

motor_model.unsetGarden = function ()
{
  delete this.Garden;
}

motor_model.unsetTerrace = function ()
{
  delete this.Terrace;
}

motor_model.unsetParking = function ()
{
  delete this.Parking;
}

motor_model.unsetElevator = function ()
{
  delete this.Elevator;
}

motor_model.unsetPool = function ()
{
  delete this.Pool;
}

motor_model.unsetStables = function ()
{
  delete this.Stables;
}

motor_model.unsetSidebuilding = function ()
{
  delete this.Sidebuilding;
}

motor_model.unsetOffice = function ()
{
  delete this.Office;
}

motor_model.setMinRooms = function (val)
{
  if ( parseInt(val)==0 )
  {
    delete this.MinRooms;
  }
  else
  {
    this.MinRooms = parseInt(val);
  }
  
}

motor_model.setBathRooms = function (val)
{
  if ( parseInt(val)==0 )
  {
    delete this.BathRooms;
  }
  else
  {
    this.BathRooms = parseInt(val);
  }
}

motor_model.setOrderByFields = function ( val )
{
  this.OrderByFields = val;
}

motor_model.setByJSON = function (json)
{
  json = $.parseJSON(json);
  
  if (typeof(json.PurposeIDList) != 'undefined')
  {
    this.PurposeIDList = json.PurposeIDList;
  }
  
  if (typeof(json.BathRooms) != 'undefined')
  {
    this.BathRooms = json.BathRooms;
  }
  else
  {
    delete this.BathRooms;
  }
  
  if (typeof(json.CategoryIDList) != 'undefined')
  {
    this.CategoryIDList = json.CategoryIDList;
  }
  else
  {
    delete this.CategoryIDList;
  }
  
  if (typeof(json.City) != 'undefined')
  {
    this.City = json.City;
  }
  else
  {
    delete this.City;
  }
  
  if (typeof(json.DistanceFromCity) != 'undefined')
  {
    this.DistanceFromCity = json.DistanceFromCity;
  }
  else
  {
    delete this.DistanceFromCity;
  }
  
  if (typeof(json.Garden) != 'undefined')
  {
    this.Garden = json.Garden;
  }
  else
  {
    delete this.Garden;
  }
  
  if (typeof(json.MinRooms) != 'undefined')
  {
    this.MinRooms = json.MinRooms;
  }
  else
  {
    delete this.MinRooms;
  }
  
  if (typeof(json.Parking) != 'undefined')
  {
    this.Parking = json.Parking;
  }
  else
  {
    delete this.Parking;
  }
  
  if (typeof(json.PriceRange) != 'undefined')
  {
    this.PriceRange = json.PriceRange;
  }
  
  if (typeof(json.SubCategoryIDList) != 'undefined')
  {
    this.SubCategoryIDList = json.SubCategoryIDList;
  }
  else
  {
    delete this.SubCategoryIDList;
  }
  
  if (typeof(json.Terrace) != 'undefined')
  {
    this.Terrace = json.Terrace;
  }
  else
  {
    delete this.Terrace;
  }
  
  if (typeof(json.ZipList) != 'undefined')
  {
    this.ZipList = json.ZipList;
  }
  else
  {
    delete this.ZipList;
  }
  
  if (typeof(json.Elevator) != 'undefined')
  {
    this.Elevator = json.Elevator;
  }
  else
  {
    delete this.Elevator;
  }
  
  if (typeof(json.Pool) != 'undefined')
  {
    this.Pool = json.Pool;
  }
  else
  {
    delete this.Pool;
  }
  
  if (typeof(json.Stables) != 'undefined')
  {
    this.Stables = json.Stables;
  }
  else
  {
    delete this.Stables;
  }
  
  if (typeof(json.Sidebuilding) != 'undefined')
  {
    this.Sidebuilding = json.Sidebuilding;
  }
  else
  {
    delete this.Sidebuilding;
  }
  
  if (typeof(json.Office) != 'undefined')
  {
    this.Office = json.Office;
  }
  else
  {
    delete this.Office;
  }
  
  if (typeof(json.mode) != 'undefined')
  {
    this.mode = json.mode;
  }
  
  if (typeof(json.Address) != 'undefined')
  {
    this.Address = json.Address;
  }
  
  if (typeof(json.Ref) != 'undefined')
  {
    this.Ref = json.Ref;
  }
  
}

motor_model.reset = function ()
{
  delete this.BathRooms;
  delete this.CategoryIDList;
  delete this.City;
  delete this.DistanceFromCity;
  delete this.Garden;
  delete this.MinRooms;
  delete this.Parking;
  delete this.PriceRange;
  delete this.PurposeIDList;
  delete this.SubCategoryIDList;
  delete this.Terrace;
  delete this.ZipList;
  delete this.Elevator;
  delete this.Pool;
  delete this.Stables;
  delete this.Sidebuilding;
  delete this.Office;
  delete this.Address;
  delete this.Ref;
  
  motor_model.mode = 'estate';
  
  motor_model.PurposeIDList = new Array( );
  motor_model.PurposeIDList.push(1);
  
  motor_model.PriceRange = new Array();
  motor_model.PriceRange.push(0);
  motor_model.PriceRange.push(max_price_buy);
}
