﻿if ( !window.Entities )
    window.Entities = {};
    
Entities.Car = function( xmlData )
{
    if ( xmlData != undefined )
    {
        for ( var i=0; i<xmlData.childNodes.length; i++ )
        {
            if ( xmlData.childNodes[i].nodeName == "company" )
                this.company = xmlData.childNodes[i].childNodes[0].nodeValue;
            if ( xmlData.childNodes[i].nodeName == "brand" )
                this.brand = xmlData.childNodes[i].childNodes[0].nodeValue;
            if ( xmlData.childNodes[i].nodeName == "model" )
                this.model = xmlData.childNodes[i].childNodes[0].nodeValue;
            if ( xmlData.childNodes[i].nodeName == "price" )
                this.price = xmlData.childNodes[i].childNodes[0].nodeValue;
            if ( xmlData.childNodes[i].nodeName == "class" )
                this._class = xmlData.childNodes[i].childNodes[0].nodeValue;
        }
    }
}

Entities.Car.prototype.company = null;
Entities.Car.prototype.brand = null;
Entities.Car.prototype.model = null;
Entities.Car.prototype.price = null;
Entities.Car.prototype._class = null;

Entities.Car.prototype.getCompany = function()
{
    return this.company;
}
Entities.Car.prototype.getBrand = function()
{
    return this.brand;
}
Entities.Car.prototype.getModel = function()
{
    return this.model;
}
Entities.Car.prototype.getPrice = function()
{
    return this.price;
}
Entities.Car.prototype.get_Class = function()
{
    return this._class;
}

Entities.Car.prototype.isCar = function( o )
{
    if (Object.getTypeName(o) == "Object")
    {
        var me = [ "company","brand","model","price","_class" ];
        var he = this.getKeys( o );
        if ( me.length != he.length )
            return false;
        else
        {
            for ( var i=0; i<he.length; i++ )
                if ( !this.isInArray(me,he[i]) )
                    return false;
            return true;
        }
    }
    return false;
}

Entities.Car.prototype.getKeys = function( o )
{
    if (o==undefined) o = this;
    var arr = [];
    for ( var j in o )
        arr.push(j);
    return arr;
}

Entities.Car.prototype.isInArray = function(arr,item)
{
    for (var i=0; i<arr.length; i++)
        if ( arr[i] == item )
            return true;
    return false;
}