// Library
// Written by Dmitry Zhemchuzhin
// This script supports any DOM1-compliant browser and IE v4+

/*

Exposed global variables:

	isIE
	theCookies

Exposed global functions:

	InitLib()
	GetElementById(element_id)
	GetElementName(element)
	ShowElement(element)
	HideElement(element)
	SetElementText(element, string)
	GetWindowInnerHeight()
	GetWindowInnerWidth()

Exposed object:

	Cookies		// do not create in script, just use theCookies instance

*/

/*****************************************************************************/
// Globals

var isIE = (navigator.appVersion.indexOf("MSIE")!=-1);
var theCookies;

function GetWindowInnerHeight() {
	if(isIE) return document.body.clientHeight;
	else return window.innerHeight;
}

function GetWindowInnerWidth() {
	if(isIE) return document.body.clientWidth;
	else return window.innerWidth;
}

function InitLib()
{
	theCookies = new Cookies();
}

function GetElementById(element_id) {
	if (isIE) return document.all.item(element_id,0);
	else return document.getElementById(element_id);
}

function GetElementName(element) {
	var name = element.getAttribute("name");
	if (name == "") return null;
	else return name;
}

function ShowElement(element) {
	element.style.display = "";
}

function HideElement(element) {
	element.style.display = "none";
}

function SetElementText(element, string) {
	if (isIE) element.innerText = string;
	else element.firstChild.data = string;
}

/*****************************************************************************/
// Object Cookies

function Cookies() {
	this.getCookie = Cookies_GetCookie;
	this.setCookie = Cookies_SetCookie;
	this.removeCookie = Cookies_RemoveCookie;
	this.checkIfEnabled = Cookies_CheckIfEnabled;
	this.enabled = this.checkIfEnabled();
}

function Cookies_CheckIfEnabled() {
	var result;
	var chk_name = "chkifenbld";
	this.setCookie(chk_name,"test");
	result = this.getCookie(chk_name) == "test";
	this.removeCookie(chk_name);
	return result;
}

function Cookies_Count() {
	if (document.cookie == "") return 0;
	return document.cookie.split("; ").length;
}

function Cookies_GetCookie(name) {
	if (document.cookie == "") return null;
	var arr = document.cookie.split("; ");
	var crumb;
	for (var i=0; i<arr.length; i++)
	{
		crumb = arr[i].split("=");
		if (crumb[0] == name) return unescape(crumb[1]);
	}
	return null;
}

function Cookies_SetCookie(name, value, expires_date, path) {
	var string = name + "=" + escape(value);
	if (arguments.length >= 3 && expires_date != null)
		string += ";expires=" + expires_date.toGMTString();
	if (arguments.length >= 4) string += ";path=" + path;
	document.cookie = string;
}

function Cookies_RemoveCookie(name) {
	if (this.getCookie(name) == null) return; // the cookie doesn't exist
	//cookie is removed by setting its expires attribute to a past date
	var past = new Date(Date.parse("Fri, 31 Dec 1999 23:59:59 GMT"));
	this.setCookie(name,"none",past);
}

