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