<?php

class clsError
{
    var $m_Message;
    var $m_Field;

    function clsError($message,$field)
    {
        $this->SetMessage($message);
        $this->SetField($field);
    }

    function GetMessage()
    {
        return $this->m_Message;
    }

    function SetMessage($value)
    {
        $this->m_Message = $value;
    }

    function GetField()
    {
        return $this->m_Field;
    }

    function SetField($value)
    {
        $this->m_Field = $value;
    }

}

class clsErrorManager
{

    var $m_errorsMap = array();
    var $m_Errors;
    var $m_Language;
    var $m_CurrentError;
    var $m_UserErrors = array();

    function clsErrorManager()
    {
        $this->SetLanguage("english");
    }

    function NumErrors()
    {
      return count($this->m_Errors);
    }

    function GetErrors()
    {
      return $this->m_Errors;
    }

	function Get($property)
	{
	  return $this->config[$property];
	}

	function Set($property, $value)
	{
      $varname = "m_".$property;
	  $this->$varname = $value;
	}


    function ClearErrors()
    {
      unset($this->m_Errors);
      $this->m_CurrentError = 0;
      $this->m_UserErrors = array();
    }

    function SetLanguage($lang)
    {
        $this->m_Language=$lang;
    }

    function AddAdminUserError($errorLang)
    {
    	$this->m_UserErrors[] = $errorLang;
    }

    function GetAdminUserErrors()
    {
    	return $this->m_UserErrors;
    }

    function AddError($errorPhrase, $errorField=NULL,$arg1 = NULL,$arg2 = NULL,$arg3 = NULL,$arg4 = NULL,$fatal=FALSE)
    {
        global $g_DebugMode, $g_ErrorLog;


        $errorText = "";

		//$arg3::$arg4\t$errorPhrase\t$arg1 #arg2
  		$errorText = "%s::%s\t%s\t%s %s\n";
        $errorMessage = sprintf($errorText,$arg3,$arg4,$errorPhrase,$arg1,$arg2);

        $error = new clsError($errorMessage, $errorField);
        $this->m_Errors[] = $error;
        if($g_DebugMode)
            echo ("Error code: $errorMessage, Field: $errorField");
        if(strlen($g_ErrorLog))
        {
        	if(is_writable($g_ErrorLog))
        	{
        		$fp = fopen($g_ErrorLog,"a");
        		if($fp)
        		{
        			$datestamp = adodb_date("Y-m-d H:i:s");
        			fputs($fp,$datestamp."\t",$errorMessage." ".$errorField);
        			fclose($fp);
        		}
        	}
        }
    }

    function GetNextErrorText()
    {
      if($this->NumErrors()>$this->m_CurrentError)
      {
        return $this->m_Errors[$this->m_CurrentError++]->GetMessage();
      }
      else
      {
          return "";
      }
    }

    function GetNextError()
    {
       if($this->NumErrors()>$this->m_CurrentError)
       {
         return $this->m_Errors[$this->m_CurrentError++];
       }
       else
           return FALSE;
    }


    function HandleErrors($postBackUrl)
    {

        $this->Set("Current_Error",0);
        $Global_Error = "";
        $e=$this->GetNextError();
        while($e)
        {
          if($e->GetField() != "")
          {

            $message = urlencode($e->GetMessage());
                   if(strstr($postBackUrl,"?") == NULL)
                       $postBackUrl = $postBackUrl."?".$e->GetField()."_Error=".$e->GetMessage();
                   else
                       $postBackUrl = $postBackUrl."&".$e->GetField()."_Error=".$e->GetMessage();
           }
           else
           {
               $Global_Error = $e->GetMessage();
           }
        }

        if(isset($Global_Error) && $Global_Error != "")
        {
           if(strstr($postBackUrl,"?") == NULL)
                 $postBackUrl .= "?Global_Error=$message";
           else
                 $postBackUrl .= "&Global_Error=$message";
        }

           $compleateUrl = GetCompleteUrl($postBackUrl);

           echo('<script language="javascript">');
           echo('location.href="'.$compleateUrl.'"');
           echo('</script>');

   //        header ("Location: $compleateUrl"); /* Redirect browser
   //                                                     to PHP web site */
        exit;                 /* Make sure that code below does
                               not get executed when we redirect. */
        $e=$this->GetNextError();
     }
}


function Permission_Error($message)
{
	global $PermissionError;

    $PermissionError = $message;
    /* here we can do some logging or other cool stuff */
}


?>