/*
	===========================================================
 	PQUB Content Management Framework                         
	===========================================================
	by Eric Colvin   
	Copyright (c) 2007 PQUB
	All Rights Reserved
	http://www.pqub.com
	===========================================================
	This application package is not free software and you must 
	obtain the proper license. You may not copy, sublicense, 
	redistribute, or modify this package except as expressly
	provided under this license. By using this software you
	agree that you have read and understand this license. 
	http://www.pqub.com/license
	===========================================================

	
	===========================================================
	Filename		: pqub.js
	Created On		: 03/15/07
	Last Modified	: 08/24/07
	Modified By		: EDC
	Revision		: 40
	
	Description:
		This is the javascript core of the PQUB Framework.
		
	***********************************************************	
	****** This file should never be modified in any way! *****
	***********************************************************
	
	===========================================================
*/

/* New Function Testing Area */

function stripSlashes(str)
{
return str.replace("/", '');
}


// ***** This method of document initialization does not work in Opera and probably Safari.
// Note to Self: Use jQuery document.ready(fn) or prototypical solution

// Initialize document.body object
if (document.body == null)
{
//	document.write('$');
//	document.body.removeChild(document.body.firstChild);
}

function $text(string) { return document.createTextNode(string); }

// Append function (element) appends to document.body or use (parent, element)
function $a ()
{
	if (arguments.length >= 2)
	{
		if (typeof(arguments[1]) == Array)
		{
		} else {
			if (arguments.length == 2)
			{
				return arguments[0].appendChild(arguments[1]);
			} else 
			{
				for (i=1;i<=arguments.length-1;i++)
				{
					arguments[0].appendChild(arguments[i]);
				}
			}
		}
	} else {
/*		return document.body.appendChild(arguments[0]); */
	}
}

// Clone element - faster than element creation
function $clone()
{
	return arguments[0].cloneNode(false);
}

// Move this function to framework files
function insertAfter(parent, node, referenceNode)
{
	parent.insertBefore(node, referenceNode.nextSibling);
} 
var $ia = insertAfter;

// NOTE: add ajax javascript upload errors to mysql log database.

// Custom Error Debugger


// XML Parsing
function parseXML(xml) {
   var dom = null;
   if (window.DOMParser) {
      try { 
         dom = (new DOMParser()).parseFromString(xml, "text/xml"); 
      } 
      catch (e) { dom = null; }
   }
   else if (window.ActiveXObject) {
      try {
         dom = new ActiveXObject('Microsoft.XMLDOM');
         dom.async = false;
         if (!dom.loadXML(xml)) // parse error ..

            window.alert(dom.parseError.reason + dom.parseError.srcText);
      } 
      catch (e) { dom = null; }
   }
   else
      alert("cannot parse xml string!");
   return dom;
}

// Import XML ***
var xmlDoc;
function importXML()
{
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = function ()
		{
			alert('ada');
		}
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4)
			{
//				alert(xmlDoc.responseXML);
//				convertToJson(xmlDoc.responseText);
				createTable();
			}
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load(arguments[0]);
}


function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }


// Essential Functions

// Get Target Element. Accepts Event
function getTarg (e)
{
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
		
	return targ;
}

function isArray(thing) { 
	if (thing == null) return false; 
	if (typeof(thing) != 'object') return false;
	if (typeof(thing.length) == 'undefined') return false; 
	return true; 
} 

function isObject(thing) { 
	if (thing == null) return false; // null is not a f-ing object!
	if (typeof(thing) != 'object') return false; 
	if (typeof(thing.length) != 'undefined') return false; 
	return true; 
}

function isTypeOf(thing){
	var t = typeof(thing);
	if (t == 'object'){ 
		if (isArray(thing)) return 'array';  // an object can be an array too!
		if (thing == null) return 'null';
	}
	return t
}

function makeUrl(jsonpath, text){
	return "<a id=\"a"+li+"\" href=\"#\" onclick=\"getValue('" + jsonpath + "', this);return false\">" + text + "</a>";
}

// JavaScript Error Debugger
var debugConsoleHandler = Class.create({
	initialize: function(options) {
		options ? this.options = options : this.options = {};
		this.container = new Element('div', {id: 'debugConsole'} );

		this.header = new Element('div', {className: 'header'} );
		this.minBox = new Element('div', {className: 'minBox'} );
		this.minBox.innerHTML = "-";
		Event.observe(this.minBox, "click", function () { if (this.body.style.display == "none") this.body.show(); else this.body.hide(); }.bind(this) );
		
		this.exitBox = new Element('div', {className: 'exitBox'} );
		this.exitBox.innerHTML = "X";
		Event.observe(this.exitBox, "click", function () { this.container.hide(); }.bind(this) );
	
		$a(this.header, this.exitBox);
		$a(this.header, this.minBox);
 		$a(this.header, $text('Debug Console'));
		
		this.body = new Element('div', {className: 'body'} );
		
		$a(this.container, this.header);
		$a(this.container, this.body);
		
		$a(this.container);
	},
	
	log: function(errlang, errmsg, errline, errfile) {
		obj = new Element('div', {className: 'message'} );
//		obj.innerHTML = message;
		if (errlang == "js") errlang = "JavaScript";
		if (errlang == "php") { errlang = "PHP"; obj.style.backgroundColor = "#E8ECEB"; }
		obj.innerHTML = "Error caught in " + errlang + "<br> Error: " + errmsg + "<br>Line: " + errline + "<br>URL: " + errfile;
		
		while ( obj.scrollHeight != obj.offsetHeight)
		{
		obj.style.height = '70px';
		}
		
		$a(this.body, obj);
	}
}); 

var debugConsole;

window.onerror = function () {
	if (!debugConsole)
		debugConsole = new debugConsoleHandler;
	
	debugConsole.log("js", arguments[0], arguments[1], arguments[2]);
	return true;
}