/*
* This notice must be untouched at all times.

============= META ==================
@name : jaxWidgets_Library.js  
@version : 0.11 
@copyright (c) 2005 Sarat Pediredla. All rights reserved.
@last-modified: 30/09/2005
@url : http://sarat.xcelens.co.uk/jaxWidgets
@latest-version-url : http://sarat.xcelens.co.uk/jaxWidgets/latest/
======================================

============== DESCRIPTION =============
The Library is a repository for common functions used by jaxWidgets
=========================================

============= FEATURES ==================
- Sets Style of elements
============================================

============= CHANGELOG ==================
30 September 2005 [10:44] [Sarat Pediredla]
- Changed setAttribute("className") to xbrowser compatible obj.className
18 June 2005 [08:45] [Sarat Pediredla]
- Added functions to get X & Y position of elements.
- Modified style setters to use element.style.cssText
17 June 2005 [17:46] [Sarat Pediredla]
- Modified code to replace getAttribute().toLowerCase() with tmpString
  because strangely IE 6.0 wont support it.
==========================================

LICENSE: LGPL

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

For more details on the GNU Lesser General Public License,
see http://www.gnu.org/copyleft/lesser.html
*/

// The following array IS NEEDED for widgets wishing to lock submitting forms
var submitLockedBy = new Array();

// Register object
jaxWidgets.Library = function()
{
	// Cleanup function to remove jax XML tags
	this.cleanup = function(array)
	{
		var arraylength = array.length;
		for(var i=0; i < arraylength; i++)
		{
			var element = array[0];
			element.parentNode.removeChild(element);
		}
		return true;
	}
	
	// Gets the index of an element in an array
	this.getIndex = function(element,array)
	{
		for(var i=0; i < array.length; i++)
		{
			if(array[i] == element)
				return i;
		}
	}
	// Sets the CSS style from source to target element
	this.setStyle = function(sourceElement,targetElement)
	{
		if(sourceElement.getAttribute("style") && sourceElement.getAttribute("style").length > 0)
		{
			targetElement.style.cssText = sourceElement.getAttribute("style");
			return;
		}
		if(sourceElement.getAttribute("Class") && sourceElement.getAttribute("Class").length > 0)		
		{									
			targetElement.setAttribute("class",sourceElement.getAttribute("class"));
			// Backup method
			targetElement.className = sourceElement.getAttribute("class"));
			return;
		}
	}
	
	// Returns parent form of a given element
	this.getParentForm = function(element)
	{
		for(var i=0;i < document.forms.length; i++)
		{
			if(document.forms[i][element.id] == element)
				return document.forms[i];
		}
		_Engine.writeError("jax Error : Your elements are not embedded inside a form");
		return null;
	}
	
	// Returns the submit button for a given form
	this.getSubmitElement = function(currentForm)
	{
		for(i=0;i<currentForm.length;i++)
		{
			var currentElement = currentForm.elements[i];
			var tmpString = currentElement.type;
			if(tmpString.toString().toLowerCase() == "submit")
				return currentElement;
		}
	}
	
	// Disables submitting of forms when fields not validated
	this.disableSubmit = function(element)
	{		
		form = this.getParentForm(element);
		var btnSubmit = this.getSubmitElement(form);		
		if(btnSubmit)
			btnSubmit.disabled = true;
	}
	
	this.enableSubmit = function(element)
	{		
		form = this.getParentForm(element);
		var btnSubmit = this.getSubmitElement(form);
		if(btnSubmit)
			btnSubmit.disabled = false;
	}
	
	this.lockSubmit = function(id)
	{
		var index = _Library.getIndex(id,submitLockedBy)
		if(!(index >= 0))
		{
			submitLockedBy.push(id);		
		}	
		_Library.disableSubmit(document.getElementById(id));
	}
	
	this.unlockSubmit = function(id)
	{
		var index = _Library.getIndex(id, submitLockedBy);
		if(index > -1)
		{
			submitLockedBy.pop(index);
			if(_Library.noSubmitLocks())
				_Library.enableSubmit(document.getElementById(id));
		}
	}
		
	this.noSubmitLocks = function()
	{
		if(submitLockedBy.length <= 0)
			return true;
		return false;
	}
	
	this.getXPos = function(element)
	{
			xPos = element.offsetLeft;
			tempEl = element.offsetParent;
  			while (tempEl != null) 
			{
				xPos += (tempEl.offsetLeft);
		  		tempEl = tempEl.offsetParent;
  			}
			return xPos;
	}
	
	this.getYPos = function(element)
	{		
			yPos = element.offsetTop;
			tempEl = element.offsetParent;
  			while (tempEl != null) 
			{
				yPos += tempEl.offsetTop;
		  		tempEl = tempEl.offsetParent;
  			}
			return yPos;		
	}
					
}
	