// ************************************************************************************
// CustomPropertyOption
// ************************************************************************************
// namespace
Type.registerNamespace("Competir.MiEmpresa");

// constructor
Competir.MiEmpresa.CustomPropertyOption = function(generic)
{
	Competir.MiEmpresa.CustomPropertyOption.initializeBase(this);
	this._Key = "";
	this._Value = "";
	if (generic)
	{
		this.parseGeneric(generic);
	}
};

// prototype
Competir.MiEmpresa.CustomPropertyOption.prototype =
{
	// methods
	writeXml: function(sb)
	{
		sb.append(String.format("<option key=\"{0}\">{1}</option>", this.get_Key(), this.get_Value()));
	},
	parseGeneric: function(obj)
	{
		this.set_Key(obj.Key);
		this.set_Value(obj.Value);
		obj = null;
	},

	// properties
	get_Key: function()
	{
		return this._Key;
	},
	set_Key: function(value)
	{
		if (this._Key !== value)
		{
			this._Key = value;
			this.raisePropertyChanged("Key");
		}
	},
	get_Value: function()
	{
		return this._Value;
	},
	set_Value: function(value)
	{
		if (this._Value !== value)
		{
			this._Value = value;
			this.raisePropertyChanged("Value");
		}
	},

	// JSON
	descriptor: function()
	{
		properties: [{Key: "Key", Type: String}, {Key: "Value", Value: String}];
	}
};

// registration
Competir.MiEmpresa.CustomPropertyOption.registerClass("Competir.MiEmpresa.CustomPropertyOption", Sys.Component);



// ************************************************************************************
// CustomPropertyOptions
// ************************************************************************************
// namespace
Type.registerNamespace("Competir.MiEmpresa");

// constructor
Competir.MiEmpresa.CustomPropertyOptions = function(generic)
{
	Competir.MiEmpresa.CustomPropertyOptions.initializeBase(this);
	this._arr = new Array();
	if (generic)
	{
		this.parseGeneric(generic);
	}
};

// prototype
Competir.MiEmpresa.CustomPropertyOptions.prototype =
{
	// methods
	clear: function()
	{
		while (this._arr.length != 0)
		{
			this._arr.pop();
		}
	},
	add: function(obj)
	{
		this._arr.push(obj);
	},
	addComplete: function(key, value)
	{
		var cpo = new Competir.MiEmpresa.CustomPropertyOption();
		cpo.set_Key(key);
		cpo.set_Value(value);
		this._arr.push(cpo);
	},
	indexOf: function(index)
	{
		var rv = -1;
		for (var i = 0; i < this._arr.length; i ++)
		{
			if (this._arr[i] == obj)
			{
				rv = i;
				break;
			}
		}
		return rv;
	},
	indexOfKey: function(key)
	{
		var rv = -1;
		for (var i = 0; i < this._arr.length; i ++)
		{
			if (this._arr[i].get_Key() == key)
			{
				rv = i;
				break;
			}
		}
		return rv;
	},
	getItem: function(index)
	{
		return this._arr[index];
	},
	get_Count: function()
	{
		return this._arr.length;
	},
	parseGeneric: function(obj)
	{
		this.clear();
		if (obj != null && obj.length != null)
		{
			for (var i = 0; i < obj.length; i++)
			{
				var cpo = new Competir.MiEmpresa.CustomPropertyOption();
				cpo.parseGeneric(obj[i]);
				this.add(cpo);
			}
		}
		obj = null;
	}
};

// registration
Competir.MiEmpresa.CustomPropertyOptions.registerClass("Competir.MiEmpresa.CustomPropertyOptions", Sys.Component);



// ************************************************************************************
// CustomProperty
// ************************************************************************************
// namespace
Type.registerNamespace("Competir.MiEmpresa");

// constructor
Competir.MiEmpresa.CustomProperty = function(generic)
{
	Competir.MiEmpresa.CustomProperty.initializeBase(this);
	this._Name = "";
	this._Value = null;
	this._Section = "system";
	this._IsXml = false;
	this._Label = "";
	this._Options = new Competir.MiEmpresa.CustomPropertyOptions();
	if (generic)
	{
		this.parseGeneric(generic);
	}
};

// prototype
Competir.MiEmpresa.CustomProperty.prototype =
{
	// methods
	ToString: function()
	{
		return String.format("Name: {0}, Label: {1}, IsXml: {2}, Section: {3}", this._Name, this._Label, this._IsXml, this._Section);
	},
	toXml: function()
	{
		return this.writeXml(new Sys.StringBuilder(""));
	},
	writeXml: function(sb)
	{
		sb.append(String.format("<property name=\"{0}\" label=\"{1}\" isxml=\"{2}\">", this._Name, this._Label, this._IsXml));
		if (this._Value)
		{
			sb.append("<value><![CDATA[" + this._Value + "]]></value>");
		}
		if (this._Option != null && this._Options.get_Count() > 0)
		{
			sb.append("<options>");
			for (var i = 0; i < this._Options.get_Count(); i++)
			{
				var o = this._Options.get_Item(i);
				o.writeXml(sb);
				i++;
			}
			sb.append("</options>");
		}
		sb.append("</property>");
		return sb.toString();
	},
	parseGeneric: function(obj)
	{
		this.set_Name(obj.Name);
		this.set_Value(obj.Value);
		this.set_Section(obj.Section);
		this.set_IsXml(obj.IsXml);
		this.set_Label(obj.Label);
		this.get_Options().parseGeneric(obj.Options);
		obj = null;
	},

	// properties
	get_Name: function()
	{
		return this._Name;
	},
	set_Name: function(value)
	{
		if (this._Name !== value)
		{
			this._Name = value;
			this.raisePropertyChanged("Name");
		}
	},
	get_Value: function()
	{
		return this._Value;
	},
	set_Value: function(value)
	{
		if (this._Value !== value)
		{
			this._Value = value;
			this.raisePropertyChanged("Value");
		}
	},
	get_Section: function()
	{
		return this._Section;
	},
	set_Section: function(value)
	{
		if (this._Section !== value)
		{
			this._Section = value;
			this.raisePropertyChanged("Section");
		}
	},
	get_IsXml: function()
	{
		return this._IsXml;
	},
	set_IsXml: function(value)
	{
		if (this._IsXml !== value)
		{
			this._IsXml = value;
			this.raisePropertyChanged("IsXml");
		}
	},
	get_Label: function()
	{
		return this._Label;
	},
	set_Label: function(value)
	{
		if (this._Label !== value)
		{
			this._Label = value;
			this.raisePropertyChanged("Label");
		}
	},
	get_Options: function()
	{
		return this._Options;
	},
	set_Options: function(value)
	{
		if (this._Options !== value)
		{
			this._Options = value;
			this.raisePropertyChanged("Options");
		}
	}
};

// registration
Competir.MiEmpresa.CustomProperty.registerClass("Competir.MiEmpresa.CustomProperty", Sys.Component);



// ************************************************************************************
// CustomProperties
// ************************************************************************************
// namespace
Type.registerNamespace("Competir.MiEmpresa");

// constructor
Competir.MiEmpresa.CustomProperties = function(generic)
{
	Competir.MiEmpresa.CustomProperties.initializeBase(this);
	this._arr = new Array();
	if (generic)
	{
		this.parseGeneric(generic);
	}
};

// prototype
Competir.MiEmpresa.CustomProperties.prototype =
{
	// methods
	addNew: function(section, name, value)
	{
		var rv = new Competir.MiEmpresa.CustomProperty();
		rv.set_Section(section);
		rv.set_Name(name);
		rv.set_Value(value);
		this.add(rv);
		return rv;
	},
	clear: function()
	{
		while (this._arr.length != 0)
		{
			this._arr.pop();
		}
	},
	add: function(obj)
	{
		this._arr.push(obj);
	},
	addComplete: function(key, value)
	{
		var cpo = new Competir.MiEmpresa.CustomPropertyOption();
		cpo.set_Key(key);
		cpo.set_Value(value);
		this._arr.push(cpo);
	},
	indexOf: function(obj)
	{
		var rv = -1;
		for (var i = 0; i < this._arr.length; i ++)
		{
			if (this._arr[i] == obj)
			{
				rv = i;
				break;
			}
		}
		return rv;
	},
	indexOfName: function(section, name)
	{
		var rv = -1;
		for (var i = 0; i < this._arr.length; i ++)
		{
			if (this._arr[i].get_Name() == name && this._arr[i].get_Section() == section)
			{
				rv = i;
				break;
			}
		}
		return rv;
	},
	getItem: function(index)
	{
		return this._arr[index];
	},
	getItemByName: function(section, name)
	{
		return this._arr[this.indexOfName(section,name)];
	},
	toXml: function()
	{
		return this.writeXml(new Sys.StringBuilder(""));
	},
	writeXml: function(sb)
	{
		sb.append("<properties>");
		var arrSections = new Array();
		for (var i = 0; i < this.get_Count(); i++)
		{
			var item = this.getItem(i);
			if (!Array.contains(arrSections, item.get_Section()))
			{
				Array.add(arrSections, item.get_Section());
			}
		}
		for (var i = 0; i < arrSections.length; i++)
		{
			var sectionName = arrSections[i];
			sb.append("<" + sectionName + ">");
			for (var j = 0; j < this.get_Count(); j++)
			{
				var item = this.getItem(j);
				if (item.get_Section() == sectionName)
				{
					item.writeXml(sb);
				}
			}
			sb.append("</" + sectionName + ">");
		}
		sb.append("</properties>");
		return sb.toString();
	},
	parseGeneric: function(obj)
	{
		this.clear();
		if (obj != null && obj.length != null)
		{
			for (var i = 0; i < obj.length; i++)
			{
				this.add(new Competir.MiEmpresa.CustomProperty(obj[i]));
			}
		}
		obj = null;
	},
	get_Count: function()
	{
		return this._arr.length;
	}
};

// registration
Competir.MiEmpresa.CustomProperties.registerClass("Competir.MiEmpresa.CustomProperties", Sys.Component);