cid, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             name=\"" . convert_xml($this->name, ENT_QUOTES, "UTF-8") . "\"\n");	
		fwrite($fp, "             co=\"" . convert_xml($this->co, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             address=\"" . convert_xml($this->address, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             city=\"" . convert_xml($this->city, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             country=\"" . convert_xml($this->country, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             phone=\"" . convert_xml($this->phone, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             phone2=\"" . convert_xml($this->phone2, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             email=\"" . convert_xml($this->email, ENT_QUOTES, "UTF-8") . "\"\n");	
		fwrite($fp, "             email2=\"" . convert_xml($this->email2, ENT_QUOTES, "UTF-8") . "\"\n");	
		fwrite($fp, "             url=\"" . convert_xml($this->url, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             url2=\"" . convert_xml($this->url2, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             essential=\"" . convert_xml($this->essential, ENT_QUOTES, "UTF-8") . "\">");
		fwrite($fp, convert_xml($this->notes, ENT_QUOTES, "UTF-8"));
		fwrite($fp, "\n");
	}
	public function show()
	{
		echo "
\n";
		//		echo "\tcid: " . $this->cid . "
\n";
		echo "\tName: " . $this->name . "
\n";
		if($this->co) echo "\tc/o: " . $this->co . "
\n";
		if($this->address) echo "\tAddress: " . $this->address . "
\n";
		if($this->city) echo "\tCity: " . $this->city . "
\n";
		if($this->country) echo "\tCountry: " . $this->country . "
\n";
		if($this->phone) echo "\tPhone: " . $this->phone . "
\n";
		if($this->phone2) echo "\tPhone2: " . $this->phone2 . "
\n";
		if($this->email) echo "\tEmail: email . "\">" . $this->email . "
\n";
		if($this->email2) echo "\tEmail2: email2 . "\">" . $this->email2 . "
\n";
		if($this->url) echo "\tURL: url . "\">" . $this->url . "
\n";
		if($this->url2) echo "\tURL2: url2 . "\">" . $this->url2 . "
\n";
		if($this->notes) echo "\tNotes: 
\n" . parse($this->notes) . "
\n";
		echo "\tcid . "\">Edit\n";
		echo "
\n";
	}
	public function showshort()
	{
		echo "";
	}
	function Contact($cid, $name, $co, $address, $city, $country, $phone, $phone2, $email, $email2, $url, $url2, $essential, $notes)
	{
		$this->cid = $cid;
		$this->name = $name;
		$this->co = $co;
		$this->address = $address;
		$this->city = $city;
		$this->country = $country;
		$this->phone = $phone;
		$this->phone2 = $phone2;
		$this->email = $email;
		$this->email2 = $email2;
		$this->url = $url;
		$this->url2 = $url2;
		$this->essential = $essential;
		$this->notes = $notes;
	}
}
class ContactGroup {
	public $gid;
	public $name;
	private $contacts = array();
	public function getContact($cid)
	{
		return $this->contacts[$cid];
	}
	public function write($fp) {
		fwrite($fp, "  gid, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "                name=\"" . convert_xml($this->name, ENT_QUOTES, "UTF-8") . "\">\n");
		foreach($this->contacts as $contact) {
			$contact->write($fp);
		}
		fwrite($fp, "  \n");
	}
	public function getMaxCID()
	{
		$maxcid = 1;
		foreach($this->contacts as $contact) {
			if($maxcid < $contact->cid) $maxcid = $contact->cid;
		}
		return $maxcid;
	}
	public function add($contact) {
		$key = $contact->cid;
		$this->contacts[$key] = $contact;
	}
	public function show()
	{
		echo "  \n";
	}
	public function showshort()
	{
		global $client_is_mobile_device;
		$ness = 0;
		foreach($this->contacts as $contact) {
			if($contact->essential) $ness += 1;
		}
		echo "  \n";
	}
	function ContactGroup($gid,
												$name)
	{
		$this->gid = $gid;
		$this->name = $name;
	}
}
class Contacts {
	private $file;
	private $contactgroups = array();
	public function show()
	{
		echo "\n";
		foreach($this->contactgroups as $contactgroup) {
			$contactgroup->showshort();
		}
		echo "
\n";
	}
	public function getNextCID()
	{
		$nextcid = 1;
		foreach($this->contactgroups as $contactgroup) {
			$cid = $contactgroup->getMaxCID();
			if($nextcid < $cid) $nextcid = $cid;
		}
		return $nextcid + 1;
	}
	public function getNextGID()
	{
		$nextgid = 1;
		foreach($this->contactgroups as $contactgroup) {
			if($nextgid < $contactgroup->gid) $nextgid = $contactgroup->gid;
		}
		return $nextgid + 1;
	}
	public function getContact($cid)
	{
		foreach($this->contactgroups as $contactgroup) {
			if($contactgroup->getContact($cid)) return $contactgroup->getContact($cid);
		}
		return false;
	}
	public function getContactGroup($gid)
	{
		return $this->contactgroups[$gid];
	}
	public function add($contactgroup) {
		$key = $contactgroup->gid;
		$this->contactgroups[$key] = $contactgroup;
	}
	
	public function write()
	{
		$fp = fopen($this->file, "w");
    $block = TRUE;
    flock($fp, LOCK_EX, $block); // do an exclusive lock
		fwrite($fp, "\n");
		fwrite($fp, "\n");
		foreach($this->contactgroups as $contactgroup) {
			$contactgroup->write($fp);
		}
		fwrite($fp, "\n");
		fclose($fp);
	}
	
	private function read()
	{
		$dom = new DomDocument;
		$dom->preserveWhiteSpace = TRUE;
		$dom->load($this->file);
		$contactgroups = $dom->getElementsByTagName('contactgroup');
		foreach ($contactgroups as $cg) {
			$contactgroup = new ContactGroup($cg->getAttribute('gid'),
																			 $cg->getAttribute('name'));
			$contacts = $cg->getElementsByTagName('contact');
			foreach ($contacts as $c) {
				$contact = new Contact($c->getAttribute('cid'),
															 $c->getAttribute('name'),
															 $c->getAttribute('co'),
															 $c->getAttribute('address'),
															 $c->getAttribute('city'),
															 $c->getAttribute('country'),
															 $c->getAttribute('phone'),
															 $c->getAttribute('phone2'),
															 $c->getAttribute('email'),
															 $c->getAttribute('email2'),
															 $c->getAttribute('url'),
															 $c->getAttribute('url2'),
															 $c->getAttribute('essential'),
															 $c->textContent);
				$contactgroup->add($contact);
			}
			$this->add($contactgroup);
		}
	}
	public function Contacts($file)
	{
		$this->file = $file;
		if(file_exists($file)) $this->read();
	}
}
?>