﻿


function Company(id, name, mail, webSite, address, officePhone, cellPhone, faxNumber)
{
	this.Id = parseInt(id);
	this.Name = name;
	this.Mail = mail;
	this.WebSite = webSite;
	this.Address = address;
	this.OfficePhone = officePhone;
	this.CellPhone = cellPhone;
	this.FaxNumber = faxNumber;

	this.ToString = function ()
	{
		var company = "";
		var addBracket = false;
		this.Name = this.Name != null ? this.Name : "";
		this.Mail = this.Mail != null ? this.Mail : "";
		this.WebSite = this.WebSite != null ? this.WebSite : "";
		this.OfficePhone = this.OfficePhone != null ? this.OfficePhone : "";
		this.CellPhone = this.CellPhone != null ? this.CellPhone : "";

		if (this.Name != "")
		{
			company += this.Name;
			addBracket = true;
		}

		if (addBracket)
			company += "&#160;&#40;";

		if (this.Mail != "")
		{
			company += "<a href=\"mailto:" + this.Mail + "\">" + this.Mail + "</a>";
		}
		if (this.WebSite != "")
		{
			if (this.Mail != "")
				company += "&#44;&#160;";

			company += "<a href=\"javascript:Browser.OpenUrl ('" + this.WebSite + "')\">" + this.WebSite + "</a>";
		}
		if (this.OfficePhone != "")
		{
			if (this.Mail != "" || this.WebSite != "")
				company += "&#44;&#160;";

			company += "<a href=\"callto:" + this.OfficePhone + "\">" + this.OfficePhone + "</a>";
		}
		if (this.CellPhone != "")
		{
			if (this.Mail != "" || this.WebSite != "" || this.OfficePhone != "")
				company += "&#44;&#160;";

			company += "<a href=\"callto:" + this.CellPhone + "\">" + this.CellPhone + "</a>";
		}

		if (addBracket)
			company += "&#41;";

		return company;
	}
}

Company.FromString = function (data)
{
	var company = new Company("");
	data = data.split(";;");

	for (var i = 0; i < data.length; i++)
	{
		var value = data[i].split(":")[1];

		switch (data[i].split(":")[0])
		{
			case "id":
				company.Id = parseInt(value);
				break;
			case "name":
				company.Name = value;
				break;
			case "mail":
				company.Mail = value;
				break;
			case "webSite":
				company.WebSite = value;
				break;
			case "address":
				company.Address = Locations.Get(value);
				break;
			case "officePhone":
				company.OfficePhone = value;
				break;
			case "cellPhone":
				company.CellPhone = value;
				break;
			case "faxNumber":
				company.FaxNumber = value;
				break;
			default:
				break;
		}
	}

	return company;
}

Company.FromHTML = function (container)
{
	var properties = container.getElementsByTagName("p");
	var company = new Company("");

	for (var i = 0; i < properties.length; i++)
	{
		var value = properties[i].innerHTML;

		switch (properties[i].className)
		{
			case "Id":
				company.Id = parseInt(value);
				break;
			case "Name":
				company.Name = value;
				break;
			case "Mail":
				company.Mail = value;
				break;
			case "WebSite":
				company.WebSite = value;
				break;
			case "Address":
				company.Address = Locations.Get(value);
				break;
			case "OfficePhone":
				company.OfficePhone = value;
				break;
			case "CellPhone":
				company.CellPhone = value;
				break;
			case "FaxNumber":
				company.FaxNumber = value;
				break;
			default:
				break;
		}
	}

	return company;
}
