/*
Copyright (c) 2006 Author
Author: Anil Sharma for the book "Real-World AJAX: Secrets of the Masters"

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

var debugElement = null;
var logElement = null;
var logCount = 0;

var _Error_Str = "Error - ";
var _Warning_str = "Warning - ";
var _Info_Str = "Info - ";


if (!window.Node || !window.Node.ELEMENT_NODE) {
   var Node = { ELEMENT_NODE: 1, ATTRIBUTE_NODE: 2, TEXT_NODE: 3, CDATA_SECTION_NODE: 4, ENTITY_REFERENCE_NODE: 5,
      ENTITY_NODE: 6, PROCESSING_INSTRUCTION_NODE: 7, COMMENT_NODE: 8, DOCUMENT_NODE: 9, DOCUMENT_TYPE_NODE: 10,
   DOCUMENT_FRAGMENT_NODE: 11, NOTATION_NODE: 12 };
}

function enableDebug() {
   debugElement = document.getElementById('debug');
   if (debugElement == null) {
      var c = document.createElement("center");
      var d = document.createElement("fieldset");
      var legend = document.createElement("legend");
      legend.appendChild(document.createTextNode('Debug'));
      d.appendChild(legend);
      d.className = 'Debug';
      d.id = "debugArea";
      var l = document.createElement("ilayer");
      l.id = "debugLayer";
      d.appendChild(l);
      var t = document.createElement("textArea");
      t.style.width = '100%';
      t.rows = 10;
      t.className = 'DebugTextArea';
      t.id = "debug";
      debugElement = t;
      l.appendChild(t);
      c.appendChild(d);
      var b = document.getElementsByTagName("body");
      if (b.length > 0) {
         b = b[0];
      }
      b.appendChild(c);
      
   }
   clearDebugArea();
}

function enableLog() {
   logCount = 0;
   logElement = document.getElementById('logger');
   if (logElement == null) {
      logElement = debugElement;
   }
}

function log(code, severity, src, message) {
   if (logElement) {
      logCount++;
      logElement.value += '\n' + logCount + '.   ' + severity + src + ' - ' + message;
      } else {
      logCount++;
      alert(logCount + '.   ' + severity + src + ' - ' + message);
   }
}

function debug(s) {
   if (debugElement) {
      debugElement.value += s + '\n';
   }
}

function debugA(s) {
   if (debugElement) {
      debugElement.value += s + '\n';
   }
}

function clearDebugArea() {
   if (debugElement) {
      debugElement.value = '';
   }
}


function removeAll(node) {
   var l = node.childNodes.length;
   for (var i=0; i<l; i++) {
      node.removeChild(node.childNodes[0]);
   }
}

// browser version
var is_major = null;
var is_minor = null;
// netscape?
var is_ns  = null;
// ns6.2?
var is_ns62 = null;
var is_ns62_up = null;

// is ns 7.0 and 7.xup?
var is_ns7 = null;
var is_ns7_up = null;

// internet explorer?
var is_ie   = null;
var is_ie3  = null;
var is_ie4  = null;
var is_ie5    = null;
var is_ie5_5up =null;

var is_firefox = null;

var agent = setBrowserType();

var systemProps = new Object();
systemProps.is_dev_env = true;

function setBrowserType() {
   agent = navigator.userAgent.toLowerCase();
   // browser version
   is_major = parseInt(navigator.appVersion);
   is_minor = parseFloat(navigator.appVersion);
   // netscape?
   is_ns  = ((agent.indexOf('mozilla') != -1) && (agent.indexOf('spoofer') == -1)
      && (agent.indexOf('compatible') == -1) && (agent.indexOf('opera') == -1)
   && (agent.indexOf('webtv') == -1) && (agent.indexOf('hotjava') == -1));
   // ns6.2?
   is_ns62 = ( (is_ns) && (is_major == 5) && (agent.indexOf('netscape6/6.2')!=-1) );
   is_ns62_up = ( (is_ns62) && (is_major >= 5) );
   
   // is ns 7.0 and 7.xup?
   is_ns7 = ( (is_ns) && (is_major == 5) && (agent.indexOf('netscape/7.0')!=-1) );
   is_ns7_up = ( (is_ns7) && (is_major >= 5) );
   
   is_firefox = ( (is_ns) && (agent.indexOf('firefox')!=-1) );
   
   // internet explorer?
   is_ie   = ( (agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1) );
   is_ie3  = ( (is_ie) && (is_major < 4) );
   is_ie4  = ( (is_ie) && (is_major == 4) && (agent.indexOf("msie 4")!=-1) );
   is_ie5    = ( (is_ie) && (is_major == 4) && (agent.indexOf("msie 5.0") != -1) );
   is_ie5_5up =( (is_ie) && (!is_ie3) && (!is_ie4) && (!is_ie5) );
   
   return agent;
}

function checkBrowser() {
   
   if ( is_ie5_5up  || is_ns62_up || is_firefox) {
      return true;
      } else {
      document.write("<p><b>AjaxFace is supported IE 6.0 and higher versions. Firefox support is under development.");
      document.write(" Please check back after 1/15/2006. We regret the inconvenience.</b></p>");
      return false;
   }
}

function setElementFromHtml(html, targetId, srcElementId) {
   var target = document.getElementById(targetId);
   if (target == null) {
      alert("Target: " + targetId + " does not exists");
      return;
   }
   removeAll(target);
   target.innerHTML = html;
   
   var element = document.getElementById(srcElementId);
   removeAll(target);
   target.appendChild(element);
   return element;
}

function attachElement(targetId, element) {
   var target = document.getElementById(targetId);
   if (target == null) {
      alert("Target: " + targetId + " does not exists");
      return;
   }
   removeAll(target);
   target.appendChild(element);
}

function detachAll(targetId) {
   var target = document.getElementById(targetId);
   if (target == null) {
      return;
   }
   removeAll(target);
}

var __selectionColor = '#ccccff';
