<?

class ajaxValidate{
	
	var $array = array();
	
	function ajaxValidate($array){
		$this->array = $array;
	}

	function val($x)
	{
		return $this->array[$x]['value'];
	}
	
	function validate()
	{
		foreach($this->array as $name => $i)
		{
			if($i['required'] && !$i['value'])
			{
				return $i['required'];
			}
			
			switch($i['valid'])
			{
				case 'email':
					if(!$this->validEmail($i['value'])) return 'Email ni pravilne oblike.';
				break;
			}
		}
		
		return false;
	}
	
	function validEmail($email)
	{
		$pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' .
		'(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';
		
		$valid = preg_match ($pattern, $email);
		
		if($valid)
		{
			return true;
		}
		
		return false;
	}



}

?>