/**************************************************************************************
*                           COMPANION COMPUTERS LIMITED | CCL 
*           www.companioncomputers.co.uk | info@companioncomputers.co.uk
***************************************************************************************
*              www.compan.net | www.ccloe.co.uk | www.codebean.co.uk
***************************************************************************************
*   Application : Compan Web Portal
*        Author : Andrew Scott (andrew@compan.net) | km0|km0ti0n (km0ti0n@gmail.com)
*          Date : 18/06/2007
*       Version : 2.0.1
*   Description : Bass Class for javascript OOP.
*     Copyright : Companion Computers Ltd. | CCL (c) 1987 / 2007 All rights Reserved.
***************************************************************************************
*       Licence : The duplication, distribution and modification for this code is  
*               : prohibited under international, and UK, law.  This code can only be 
*               : used with prior written persission and concent of CCL.  This header
*               : header must be retained, with the sole exception of compression,
*               : where any peer javascript libs / files too must be compressed.
***************************************************************************************
*    Dependancy : n/a
***************************************************************************************/
function Base(){ 
	this.Controls			= (new Controls()).instanceOf();
}

Base.prototype						= new Object();	
Base.prototype.constructor			= Base;
Base.prototype.ClassName			= "Base";
Base.prototype.CSSClass				= "";
Base.prototype.Parent				= null;	
Base.prototype.ParentElement		= null;	
Base.prototype.MultiChildElements	= false;	
Base.prototype.TopElement			= null;	
Base.prototype.IdCounter			= 0;
Base.prototype.Events				= new Array();
Base.prototype.Init					= function (){};
Base.prototype.ToString				= function () {	return this.ClassName;	}
Base.prototype.toString				= function () { return this.ToString();	}
Base.prototype.Dependant			= [];



Base.prototype.PreInit			= function ()
{
	//document.body.onselectstart = function (  ){return false;}	
	this.CSSClass = this.ClassName;
	for( var i = 0; i < this.Events.length; i++)
	{
		this.AddEvent( this.Events[i] );
	}
	this.Base = this.prototype;
}

// Creates and Appends a new element of type tagName to parent
Base.prototype.NewObj = function (tagName, props, parent, text, doc )
{
	var o;
	if (!doc){ doc = document}
	try{o = doc.createElement(tagName)}catch(e)
  { 
    if( tagName.match(/\<input\sname\=\"(.*)\"/).length > 1 )
    {
      o = doc.createElement("input");
      o.name = tagName.match(/\<input\sname\=\"(.*)\"/)[1];
    }
  }
	if (props) for (var p in props) o[p]=props[p];
	if (parent) parent.appendChild(o);
	if (text) o.appendChild(doc.createTextNode(text));
	// Adding FindXY functionality to ALL new Objs
	o.FindXY = function ()
	{
		var obj = this, x=0,y=0;
		while (obj!=null)
		{
			x	+=	obj.offsetLeft-obj.scrollLeft;
			y	+=	obj.offsetTop-obj.scrollTop;
			obj	=	obj.offsetParent;
		}
		return {x:x,y:y};
	}
	return o;
}

// Generates a Unique ID for an Element
Base.prototype.NewId = function (cString)
{ 
	if( !cString ) { cString = this.ToString(); }
	this.IdCounter++;
	return cString + this.IdCounter;
}

// Generates a string that represents the object 
Base.prototype.toStruct = function (bFunc){ 
    if( bFunc === undefined ){ bFunc = false; }
    var cResult = ""; 
    for( cProp in this ){
        if( !bFunc && typeof this[cProp] != "function" ){
            cResult += cProp/*.padLeft(50)*/ + " : " + this[cProp] + "\n";
        }
    }
    return cResult;
}

// Generates a Unique ID for an Element
var __IdCounter = 0;
function NewId(cString)
{ 
	__IdCounter++;
	return cString + __IdCounter;
}

function FindXY(obj)
{
	var x=0,y=0;
	while (obj!=null)
	{
		x	+=	obj.offsetLeft-obj.scrollLeft;
		y	+=	obj.offsetTop-obj.scrollTop;
		obj	=	obj.offsetParent;
	}
	return {x:x,y:y};
}

// Creates a New Event Caller & Handler
Base.prototype.AddEvent = function (cEvent)
{
	var self = this;
	this[cEvent.substring(2)] = new Event(self, cEvent);
	
	var lNew	= true;
	for( var i = 0; i < this.Events.length; i++)
	{
		if( this.Events[i] == cEvent  )
		{
			lNew = false;
		}
	}
	if ( lNew )
	{
		this.Events[this.Events.length] = cEvent;
	}
	self = null;
}

// Clears all Events and remover circular refferences to parent objects;
Base.prototype.Dispose = function ()
{
	this.PreDispose();
	// Insert Custom Class Dispose code here;
	this.PostDispose();
}

Base.prototype.PreDispose = function ()
{
	for( var i = 0; i < this.Events.length; i++)
	{
		this[this.Events[i]] = null;
		this[this.Events[i].substring(2)] = null;
	}
	for( var i = 0; i < this.Controls.length; i++)
	{
		this.Controls[i].Dispose();
		this.Controls[i] = null;
	}
	
}
Base.prototype.PostDispose = function ()
{
	if(this.ParentElement && !this.MultiChildElements)
	{	
		this.ParentElement.innerHTML = "";
	}
	else if( this.TopElement )
	{	
		this.ParentElement.removeChild( this.TopElement );
	}

	this.Parent			= null;	
	this.ParentElement	= null;	
}




/* INC Scripts */

Base.prototype.AddScript = function ( cUrl, lForce, fnTest )
{
	var eScript = document.createElement('script');
	eScript.type = 'text/javascript'; 
	eScript.src = cUrl; 
	if( lForce ){ eScript.src + "?RND=" + new Date();  }
	document.body.appendChild(eScript);
	
	return fnTest();
}



