﻿//-----------------------------------------------------------------------------
// XBoolean
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

XBoolean.prototype = new XObject;
XBoolean.prototype.constructor = XBoolean;

//=============================================================================
// Constructor

function XBoolean(
   bool,
   create
)
{
   bool = (bool == null) ? null : bool;
   create = (create == null) ? true : false;

   if (create)
      return new XBoolean(bool, false);

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   this.valueOf = function()
   {

      return oBool;
   }

   //--------------------------------------------------------------------------

   this.setObjectValue = function(
      bool
   )
   {
      bool = (bool == null) ? null : bool;

      oBool = XBoolean.getBooleanFrom(bool);

      return oBool;
   }

   //--------------------------------------------------------------------------
   // Initialization

   var oBool = this.setObjectValue(bool);

}

XBoolean.prototype.objectClass = "XBoolean";

//=============================================================================
// Static Interface

XBoolean.STYLE_TRUE_FALSE           = "true_false";
XBoolean.STYLE_YES_NO               = "yes_no";
XBoolean.STYLE_ON_OFF               = "on_off";

XBoolean.TRUE                       = "true";
XBoolean.FALSE                      = "false";
XBoolean.YES                        = "yes";
XBoolean.NO                         = "no";
XBoolean.ON                         = "on";
XBoolean.OFF                        = "off";

//-----------------------------------------------------------------------------

XBoolean.getBooleanFrom = function(
   value
)
{
   value = (value == null) ? null : value;

   if (value == null)
      return false;

   switch (typeof(value))
   {
      case "string":
         value = XString.trim(value);
         switch (value.toLowerCase())
         {
            case "":
               return false;
               break;
            case "0":
               return false;
               break;
            case "1":
               return true;
               break;
            case XBoolean.TRUE:
               return true;
               break;
            case XBoolean.FALSE:
               return false;
               break;
            case XBoolean.YES:
               return true;
               break;
            case XBoolean.NO:
               return false;
               break;
            case XBoolean.ON:
               return true;
               break;
            case XBoolean.OFF:
               return false;
         }
         return true;
      case "number":
         return (value > 0) ? true : false;
         break;
      case "boolean":
         return value;
         break;
      case "function":
         try
         {
            return XBoolean.getBooleanFrom(value());
         }
         catch (error)
         {
            return true;
         }
         break;
      case "object":
         try
         {
            if (value.length == 0)
               return false;
            else
               return true;
         }
         catch (error)
         {
            return true;
         }
         break;
   }

   return false;
}

//=============================================================================
// Public Interface

XBoolean.prototype.toString = function(
   style
)
{
   style = (style == null) ? XBoolean.STYLE_TRUE_FALSE : style;

   var bool = this.valueOf();
   var text = (bool) ? "true" : "false";
   switch (style)
   {
      case XBoolean.STYLE_TRUE_FALSE:
         text = (bool) ? XBoolean.TRUE : XBoolean.FALSE;
         break;
      case XBoolean.STYLE_YES_NO:
         text = (bool) ? XBoolean.YES : XBoolean.NO;
         break;
      case XBoolean.STYLE_ON_OFF:
         text = (bool) ? XBoolean.ON : XBoolean.OFF
         break;
   }

   return text;
}

//-----------------------------------------------------------------------------

XBoolean.prototype.toBoolean = function()
{

   return this.valueOf();
}

//=============================================================================



