<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

Class EmailAddress {
	var $ms_email;
	var $ms_box;
	var $ms_domain;
	
	function EmailAdress ( $ps_box, $ps_domain = false){
		if($ps_domen){
			$this->setBox($ps_box);
			$this->setDomain($ps_domain);
			$this->makeEmail();
		}else{
			$this->setEmail(ps_box);
		}		
	}
	
	function setBox( $ps_box){
		$this->ms_box = trim($ps_box);
		return 	$this->ms_box;
	}
	
	function getBox (){
		return 	$this->ms_box;
	}
	
	function setDomain( $ps_domain){
		$this->ms_domain = trim( $ps_domain );
		return $this->ms_domain;
	}
	
	function getDomain() {
		return $this->ms_domain;
	}

	function setEmail( $ps_email){
		$la_chunks = explode("@", $ps_email,2);
		$this->setBox($la_chunks[0]);
		$this->setDomain($la_chunks[1]);
		$this->makeEmail();
		return $this->ms_email;
	}

	function getEmail(){
		return $this->ms_email;
	}
	
	function makeEmail(){
		if($this->ms_domain){
			$this->ms_email = $this->ms_box.'@'.$this->ms_domain;
		}else{
			$this->ms_email = $this->ms_box;
		}
		return $this->ms_email;
	}
	
	function isBoxValid( $ps_box=false ){
		global $DEBUG;
		if(!$ps_box){
			$ps_box = $this->ms_box;
		}
		if($DEBUG){
			if(!$ps_box) {
				$msg="empty box";
			}elseif(ereg("@",$ps_box)){
				$msg="there is '@'";
			}elseif(ereg("\.\.",$ps_box)){
				$msg="there is '..'";
			}elseif(ereg("^[^a-zA-Z0-9]",$ps_box)){
				$msg="bad first symbol";
			}elseif(ereg("[^a-zA-Z0-9]$",$ps_box)){
				$msg="bad last symbol";
			}elseif(ereg("[^-_.a-zA-Z0-9]",$ps_box)){
				$msg="there is a bad symbol";
			}else{
				return true;
			}		
			echo "EmailAdress: box '$ps_box' is not valid ($msg)<br>\n";
			return false;
		}else{	
			if( (!$ps_box) ||
					ereg("@",$ps_box) || 
					ereg("\.\.",$ps_box) || 
					ereg("^[^a-zA-Z0-9]",$ps_box) ||
					ereg("[^a-zA-Z0-9]$",$ps_box) ||
					ereg("[^-_.a-zA-Z0-9]",$ps_box)
				){
				return false;
			}
		}
		return true;
	}
	
	function isDomainValid( $ps_domain=false){
		if(!$ps_domain){
			$ps_domain = $this->ms_domain;	
		}	
		if( ereg("@",$ps_domain) ||              ## @
				ereg("\.\.",$ps_domain) ||           ## duble point
				ereg("^[^a-zA-Z0-9]",$ps_domain) ||  ## first char
				ereg("[^a-zA-Z0-9]$",$ps_domain) ||  ## last char 
				ereg("[^-_.a-zA-Z0-9]",$ps_domain)   ## all chars
			){
			return false;
		}
		return true;
	}
	
	function isEmailValid ($ps_email = false){
		
		global $DEBUG;
		
		if($ps_email){
			$la_chunks = explode("@", $ps_email,2);
			$ls_box = $la_chunks[0];
			$ls_domain = $la_chunks[1];
		}else{
			$ls_box = $this->ms_box;
			$ls_domain = $this->ms_domain;
		}
		if($DEBUG){
			if($this->isBoxValid($ls_box)){
				if($this->isDomainValid($ls_domain)){
					return true;	
				}else{
					echo "EmailAdress: domain is not valid: '$ls_domain'<br>\n";
				}
			}else{
				echo "EmailAdress: box is not valid: '$ls_box'<br>\n";
			}
			return flase;
		}else{
			return $this->isBoxValid($ls_box) && $this->isDomainValid($ls_domain);
		}	
	}
}
?>
