// Common JS functions

//
// set DHTML support
//
var DHTML = (document.getElementById || document.all || document.layers); 

//
// get DOM object
//
function getObj(name) {
	if (document.getElementById) {
		return document.getElementById(name);
	} else if (document.all) {
		return document.all[name];
	} else if (document.layers) { 
		return document.layers[name]; 
	}
} 

//
//include CSS
//
function IncludeCustomCss(url_, media_) 
{
    // We are preventing loading a file already loaded
    // var _links = document.getElementsByTagName("link");
    var _head = document.getElementsByTagName("head")[0];                
   
    // Loop through the length of the links
    //for( i = 0; _links.length > i; i++)
    //{
       // If the href is already present, remove it
    //    if (_links[i]["href"] == url_)
    //    {
    //        _head.removeChild(_links[i]);
    //    }
    //}

    // Optional parameters check
    var _media = media_ === undefined || media_ === null ? "all" : media_;
   
    // Build link element
    var _elstyle = document.createElement("link");
    _elstyle.setAttribute("rel", "stylesheet");
    _elstyle.setAttribute("type", "text/css");
    _elstyle.setAttribute("media", _media);
    _elstyle.setAttribute("href", url_);

    // Add style
    _head.appendChild(_elstyle);
}

//
//include JS file
//
function IncludeCustomJs(url_) 
{
    var _head = document.getElementsByTagName("head")[0];                
  
    // Build script element
    var _elscript = document.createElement("link");
    _elscript.setAttribute("language", "JavaScript1.4");
    _elscript.setAttribute("type", "text/javascript");
    _elscript.setAttribute("src", url_);

    // Add style
    _head.appendChild(_elscript);
}

//
// CSS Functions
//

function ChangeElementClass(elementId, className) {
	element = document.getElementById(elementId);
	element.className = className;
}

function ToggleElementVisibility(elementId) {
	element = document.getElementById(elementId);
	element.style.display = (element.style.display == "none") ? "block" : "none";
}

function ChangeElementVisibility(elementId, visible) {
	element = document.getElementById(elementId);
	element.style.display = (visible) ? "block" : "none";
}

function ToggleRowElementVisibility(elementId) {
	element = document.getElementById(elementId);
	element.style.display = (element.style.display == "") ? "none" : "";
}

function ChangeElementContent(elementId, content) {
	element = document.getElementById(elementId);
	element.innerHTML = content;
}

function ToggleElementContent(elementId, contentOne, contentTwo) {
	element = document.getElementById(elementId);
	element.innerHTML = (element.innerHTML == contentOne) ? contentTwo : contentOne;
}

// 
// positioning functions
//

	function getWindowHeight() {
			var windowHeight = 0;
			if (typeof(window.innerHeight) == 'number') {
				windowHeight = window.innerHeight;
			}
			else {
				if (document.documentElement && document.documentElement.clientHeight) {
					windowHeight = document.documentElement.clientHeight;
				}
				else {
					if (document.body && document.body.clientHeight) {
						windowHeight = document.body.clientHeight;
					}
				}
			}
			return windowHeight;
		}

	function setContent(containerElement) {
			if (document.getElementById) {
				var windowHeight = getWindowHeight();
				if (windowHeight > 0) {
					var contentElement = document.getElementById(containerElement);
					var contentHeight = contentElement.offsetHeight;
					if (windowHeight - contentHeight > 0) {
						contentElement.style.position = 'relative';
						contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px';
					}
					else {
						contentElement.style.position = 'static';
					}
				}
			}
		}
		
	function setPageContent()
	{
		if (document.getElementById) {
				var windowHeight = getWindowHeight();
				if (windowHeight > 0) {
					var contentElement = document.getElementById("pageContent");
					contentElement.style.height = (windowHeight - 145) + 'px';
			}
			}
	}

	function setFooter() {
			if (document.getElementById) {
				var windowHeight = getWindowHeight();
				if (windowHeight > 0) {
					var contentElement = document.getElementById('pageContainer');
					var contentHeight = contentElement.offsetHeight;
					var footerElement = document.getElementById('footer');
					var footerHeight  = footerElement.offsetHeight;
					if (windowHeight - (contentHeight + footerHeight) >= 0) {
						footerElement.style.position = 'absolute';
						footerElement.style.top = (windowHeight - footerHeight) + 'px';
					}
					else {
						footerElement.style.position = 'static';
					}
				}
			}
		}