﻿//-----------------------------------------------------------------------------
// XCheckBox
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

XCheckBox.prototype = new XControl;
XCheckBox.prototype.constructor = XCheckBox;

//=============================================================================
// Constructor

function XCheckBox(
   id,
   create
)
{
   id = (id == null) ? null : id;
   create = (create == null) ? true : false;

   if (create)
      return new XCheckBox(id, false);

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   this.valueOf = function()
   {

      return oSpec;
   }

   //--------------------------------------------------------------------------

   this.setObjectValue = function(
      id
   )
   {
      id = (id == null) ? null : id;

      oSpec = XCheckBox.getSpecFrom(id);

      return oSpec;
   }

   //--------------------------------------------------------------------------
   // Initialization

   var oSpec = this.setObjectValue(id);

   XControl.controls[oSpec.id] = this;

}

XCheckBox.prototype.objectClass = "XCheckBox";

//=============================================================================
// Static Interface

XCheckBox.getSpecFrom = function(
   id
)
{
   id = (id == null) ? XUtils.generateId("urn:xcential-com:checkbox:", XUtils.SCHEME_RANDOM) : id;

   var spec = new Array();
   spec.id = id;
   spec.className = "XCheckBox";
   spec.htmlNode = null;
   spec.mouseLeave = false;
   spec.visible = true;
   spec.left = 10;
   spec.top = 10;
   spec.height = 18;
   spec.shadowSize = 0;
   spec.label = "";
   spec.tristate = false;
   spec.hasFocus = false;
   spec.events = [];
   spec.value = 0;

   return spec;
}

//-----------------------------------------------------------------------------

XCheckBox.replaceHTML = function(
   rootElement
)
{

   rootElement = (rootElement == null) ? document : rootElement;
   var elements = rootElement.getElementsByTagName("input");
   for (var i=0; i<elements.length; i++)
   {
      var element = elements[i];
      var type = element.getAttribute("type").toLowerCase();
      var tristate = element.getAttribute("tristate", false);
      var onFocus = element.getAttribute("onfocus");
      var onBlur = element.getAttribute("onblur");
      var onClick = element.getAttribute("onclick");
      var onKeyDown = element.getAttribute("onkeydown");
      if (type == "checkbox" && (tristate || true))
      {
         if (element.id)
         {
            var xElement = XCheckBox(element.id);
            xElement.setValue(element.checked);
            xElement.setEvent("onfocus", onFocus);
            xElement.setEvent("onclick", onClick);
            xElement.setEvent("onblur", onBlur);
            xElement.setEvent("onkeydown", onKeyDown);
            xElement.setTristate();
            XUI(element.id).setOuterHTML(xElement.toHTML());
            xElement.synchHtmlNode();
         }
      }
   }

}

//=============================================================================
// Event Handlers

XCheckBox.prototype.doHighlight = function(
   event
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   try
   {
      if (spec.htmlNode)
      {
         XUI(spec.id + "." + ((spec.value > 0) ? "on" : (spec.value < 0) ? "ex" : "off") + ".image").hide();
         XUI(spec.id + "." + ((spec.value > 0) ? "on" : (spec.value < 0) ? "ex" : "off") + ".highlight.image").show("inline");
      }
      spec.hasFocus = true;
   }
   catch (error)
   {
      XApp.logEvent(XApp.EVENT_ERROR, error);
      XMsg.alert(error);
   }

}

//-----------------------------------------------------------------------------

XCheckBox.prototype.doUnhighlight = function(
   event
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   try
   {
      if (spec.htmlNode)
      {
         XUI(spec.id + "." + ((spec.value > 0) ? "on" : (spec.value < 0) ? "ex" : "off") + ".image").show("inline");
         XUI(spec.id + "." + ((spec.value > 0) ? "on" : (spec.value < 0) ? "ex" : "off") + ".highlight.image").hide();
      }
      spec.hasFocus = false;
   }
   catch (error)
   {
      XApp.logEvent(XApp.EVENT_ERROR, error);
      XMsg.alert(error);
   }

}

//-----------------------------------------------------------------------------

XCheckBox.prototype.doClick = function(
   event
)
{
   event = (event == null) ? window.event : event;

   var spec = this.valueOf();

   try
   {
      if (spec.htmlNode)
      {
         if (spec.value > 0)
            this.setValue((spec.tristate) ? -1 : 0);
         else if (spec.value < 0)
            this.setValue(0);
         else
            this.setValue(1);
      }
      spec.hasFocus = false;
   }
   catch (error)
   {
      XApp.logEvent(XApp.EVENT_ERROR, error);
      XMsg.alert(error);
   }

}

//=============================================================================
// Public Interface

XCheckBox.prototype.setClass = function(
   className
)
{
   className = (className == null) ? "XCheckBox" : className;

   var spec = this.valueOf();
   spec.className = className;

   return className;
}

//-----------------------------------------------------------------------------

XCheckBox.prototype.hasFocus = function()
{

   var spec = this.valueOf();

   return spec.hasFocus;
}

//-----------------------------------------------------------------------------

XCheckBox.prototype.getTristate = function(
   castAs
)
{
   castAs = (castAs == null) ? null : castAs;

   var spec = this.valueOf();

   return (spec.tristate == null) ? null : (castAs == null) ? spec.tristate : castAs(spec.tristate);
}

//-----------------------------------------------------------------------------

XCheckBox.prototype.setTristate = function(
   tristate
)
{
   tristate = (tristate == null) ? true : XBoolean(tristate).toBoolean();

   var spec = this.valueOf();

   spec.tristate = tristate;

   return tristate;
}

//-----------------------------------------------------------------------------

XCheckBox.prototype.getLabel = function(
   castAs
)
{
   castAs = (castAs == null) ? null : castAs;

   var spec = this.valueOf();

   return (spec.label == null) ? null : (castAs == null) ? spec.label : castAs(spec.label);
}

//-----------------------------------------------------------------------------

XCheckBox.prototype.setLabel = function(
   label
)
{
   label = (label == null) ? "" : label.toString();

   var spec = this.valueOf();

   spec.label = label;
   if (spec.htmlNode)
   {
      var labelNode = spec.htmlNode.ownerDocument.getElementById(spec.id + ".label");
      if (labelNode)
         labelNode.innerHtml = "&nbsp;" + label;
   }
}

//-----------------------------------------------------------------------------

XCheckBox.prototype.setValue = function(
   value
)
{
   value = (value == null) ? 0 : value;

   var spec = this.valueOf();

   if (typeof(value) == "string")
      value = (value.toLowerCase() == "off") ? 0 : 1;

   if (value != spec.value)
   {
      spec.value = value;
      XUI(spec.id).setValue(value);

      if (spec.htmlNode)
      {
         XUI(spec.id + ".off.image").hide();
         XUI(spec.id + ".off.highlight.image").hide();
         XUI(spec.id + ".on.image").hide();
         XUI(spec.id + ".on.highlight.image").hide();
         XUI(spec.id + ".ex.image").hide();
         XUI(spec.id + ".ex.highlight.image").hide();

         if (spec.value > 0)
            XUI(spec.id + "." + ((spec.value > 0) ? "on" : (spec.value < 0) ? "ex" : "on") + ".image").show("inline");
         else if (spec.value < 0)
            XUI(spec.id + "." + ((spec.value > 0) ? "on" : (spec.value < 0) ? "ex" : "ex") + ".image").show("inline");
         else
            XUI(spec.id + "." + ((spec.value > 0) ? "on" : (spec.value < 0) ? "ex" : "off") + ".image").show("inline");
      }

      if (this.onChange)
         this.onChange(this);

   }

}

//-----------------------------------------------------------------------------

XCheckBox.prototype.getValue = function()
{

   var spec = this.valueOf();

   return spec.value;
}

//-----------------------------------------------------------------------------

XCheckBox.prototype.setPosition = function(
   left,
   top
)
{
   left = (left == null) ? null : left;
   top = (top == null) ? null : top;

   var spec = this.valueOf();

   spec.left = (left) ? left : spec.left;
   spec.top = (top) ? top : spec.top;

   if (spec.htmlNode)
   {
      if (top || left)
         this.applyPositioning();
      else
         XUI(spec.htmlNode).setRuntimeStyle("position", "static");
   }

   return spec.htmlNode;
}

//-----------------------------------------------------------------------------

XCheckBox.prototype.toHTML = function(
   state
)
{
   state = (state == null) ? [] : state;

   var spec = this.valueOf();

   var controlHTML = "";

   controlHTML += "<span " +
      "id=\"" + spec.id + ".ext\" " +
      "class=\"" + spec.className + "\" " +
      ((spec.tabIndex != null && !XApp.isBrowser("Firefox",3.0)) ? "tabindex=\"" + spec.tabIndex + "\" " : "") +
      "style=\"" +
         ((spec.width) ? "left: 0px; " : "") +
         ((spec.width) ? "width: " + XUtils.dimText(spec.width) + "; " : "") +
         ((spec.height) ? "height: " + XUtils.dimText(spec.height) + "; " : "") +
      "\" " +
   ">";
   controlHTML += "<span " +
      "id=\"" + spec.id + ".icon\" " +
      "class=\"" + spec.className + "Icon\" " +
      ((spec.tabIndex != null && !XApp.isBrowser("Firefox",3.0)) ? "tabindex=\"" + spec.tabIndex + "\" " : "") +
      ((XApp.isBrowser("MSIE")) ? "onmouseenter=\"XControl.controls['" + spec.id + "'].doMouseEnter(event);XControl.controls['" + spec.id + "'].doHighlight(event)\" " : "") +
      ((!XApp.isBrowser("MSIE")) ? "onmouseover=\"XControl.controls['" + spec.id + "'].doMouseOver(event);XControl.controls['" + spec.id + "'].doHighlight(event)\" " : "") +
      ((!XApp.isBrowser("MSIE")) ? "onmouseout=\"XControl.controls['" + spec.id + "'].doMouseOut(event);XControl.controls['" + spec.id + "'].doUnhighlight(event)\" " : "") +
      ((XApp.isBrowser("MSIE")) ? "onmouseleave=\"XControl.controls['" + spec.id + "'].doMouseLeave(event);XControl.controls['" + spec.id + "'].doUnhighlight(event)\" " : "") +
      "onfocus=\"XControl.controls['" + spec.id + "'].doFocus(event)" + ((spec.events["onfocus"]) ? ";" + spec.events["onfocus"] : "") + "\" " +
      "onblur=\"XControl.controls['" + spec.id + "'].doBlur(event)" + ((spec.events["onblur"]) ? ";" + spec.events["onblur"] : "") + "\" " +
      "onclick=\"XControl.controls['" + spec.id + "'].doClick(event)" + ((spec.events["onclick"]) ? ";" + spec.events["onclick"] : "") + "\" " +
      "onkeydown=\"XControl.controls['" + spec.id + "'].doKeyDown(event)" + ((spec.events["onkeydown"]) ? ";" + spec.events["onkeydown"] : "") + "\" " +
   ">";
   controlHTML += "<img id=\"" + spec.id + ".off.image\" class=\"" + spec.className + "Image " + ((spec.value == 0) ? "" : "Hidden") + "\" src=\"/app/pkgs/xfw/images/CheckBox.Off.gif\"/>";
   controlHTML += "<img id=\"" + spec.id + ".off.highlight.image\" class=\"" + spec.className + "Image Hidden\" src=\"/app/pkgs/xfw/images/CheckBox.Off.Highlight.gif\"/>";
   controlHTML += "<img id=\"" + spec.id + ".on.image\" class=\"" + spec.className + "Image " + ((spec.value > 0) ? "" : "Hidden") + "\" src=\"/app/pkgs/xfw/images/CheckBox.On.gif\"/>";
   controlHTML += "<img id=\"" + spec.id + ".on.highlight.image\" class=\"" + spec.className + "Image Hidden\" src=\"/app/pkgs/xfw/images/CheckBox.On.Highlight.gif\"/>";
   controlHTML += "<img id=\"" + spec.id + ".ex.image\" class=\"" + spec.className + "Image " + ((spec.value < 0) ? "" : "Hidden") + "\" src=\"/app/pkgs/xfw/images/CheckBox.Ex.gif\"/>";
   controlHTML += "<img id=\"" + spec.id + ".ex.highlight.image\" class=\"" + spec.className + "Image Hidden\" src=\"/app/pkgs/xfw/images/CheckBox.Ex.Highlight.gif\"/>";
   controlHTML += "</span>";
   controlHTML += "<span " +
      "id=\"" + spec.id + ".label\" " +
      "class=\"" + spec.className + "Label\" " +
      "style=\"" +
      "\" " +
   ">" + ((XString(spec.label).isSomething()) ? "&nbsp;" + spec.label : "") + "</span>";
   controlHTML += "<input type=\"hidden\" id=\"" + spec.id + "\" name=\"" + spec.id + "\" value=\"" + spec.value + "\"/>";
   controlHTML += "</span>";

   return controlHTML;
}

//=============================================================================

