// Library of useful function
// Version 1.0

// Written by David J. Taylor
// © djtSoft
// Web site: http://www.djtsoft.com/

function getBrowserName() {
	var ar = new Array("Firefox", "Opera", "Netscape", "MSIE", "Mozilla"), i;
	for (i in ar) {
		if (navigator.userAgent.indexOf(ar[i]) > -1) return ar[i];
	}
}

function getBrowserVersion() {
	var ver = navigator.userAgent, ar = new Array("Mozilla/", "MSIE ", "Netscape/", "Opera/", "Opera ", "Firefox/"), i, j;
	for (i in ar) {
		j = ver.indexOf(ar[i]);
		if (j > -1) ver = ver.substring(j + ar[i].length);
	}
	return parseFloat(ver);
}

function getBrowserUpdates() {
	var upd = navigator.appMinorVersion;
	if (upd == "0") return "(none)";

	upd = upd.split(" ").join(";").split(";");
	var i, j;
	for (i = 0; i < upd.length; i++) {
		while (upd[i] == "") {
			for (j = i; j < upd.length-1; j++) upd[j] = upd[j+1];
			upd.length--;
		}
	}

	return upd.join(", ");
}

function getOS() {
	var oslist = multiSplit("Win~16~Windows 3.1|Win~95~Windows 95|Win~98~Windows 98|Win~98~Win 9x 4.90~Windows Millennium Edition|Win~NT~Windows NT|Win~NT 5.0~Windows 2000|Win~NT 5.1~Windows XP|Win~NT 5.2~Windows Server 2003|Win~CE~Windows CE|PPC~MacOS|Mac~MacOS|SunOS~SunOS/Solaris|BeOS~BeOS|Linux~Linux|HP~HP-UX|FreeBSD~FreeBSD|OpenBSD~OpenBSD|NetBSD~NetBSD|SGI~SGI", "|~"), os = "(unknown)", i, j;
	for (i in oslist) {
		for (j in oslist[i]) {
			if (j < oslist[i].length - 1) {
				if (navigator.appVersion.indexOf(oslist[i][j]) < 0) break;
			} else {
				os = oslist[i][j];
			}
		}
	}
	return os;
}

function getLang(str) {
	var i, lang = "", langlist = "af~Afrikaans~sq~Albanian~ar~Arabic~ar-dz~ (Algeria)~ar-bh~ (Bahrain)~ar-eg~ (Egypt)~ar-iq~ (Iraq)~ar-jo~ (Jordan)~ar-kw~ (Kuwait)~ar-lb~ (Lebanon)~ar-ly~ (Libya)~ar-ma~ (Morocco)~ar-om~ (Oman)~ar-qa~ (Qatar)~ar-sa~ (Saudi Arabia)~ar-sy~ (Syria)~ar-tn~ (Tunisia)~ar-ae~ (U.A.E.)~ar-ye~ (Yemen)~hy~Armenian~as~Assamese~az~Azeri (Cryllic/Latin)~eu~Basque~be~Belarusian~bn~Bengali~bg~Bulgarian~ca~Catalan~zh~Chinese~zh-hk~ (Hong Kong)~zh-cn~ (PRC)~zh-sg~ (Singapore)~zh-tw~ (Taiwan)~hr~Croatian~cs~Czech~da~Danish~nl~Dutch~nl-be~ (Belgium)~";
	langlist += "en~English~en-au~ (Australian)~en-bz~ (Belize)~en-ca~ (Canadian)~en-ie~ (Ireland)~en-jm~ (Jamaica)~en-nz~ (New Zealand)~en-ph~ (Philippines)~en-za~ (South Africa)~en-tt~ (Trinidad)~en-gb~ (United Kingdom)~en-us~ (United States)~en-zw~ (Zimbabwe)~et~Estonian~fo~Faeroese~fa~Farsi~fi~Finnish~fr~French~fr-be~ (Belgium)~fr-ca~ (Canadian)~fr-lu~ (Luxembourg)~fr-mc~ (Monaco)~fr-ch~ (Switzerland)~gd~Gaelic~ka~Georgian~de~German~de-at~ (Austrian)~de-li~ (Liechtenstein)~de-lu~ (Luxembourg)~de-ch~ (Switzerland)~el~Greek~gu~Gujarati~";
	langlist += "he~Hebrew~hi~Hindi~hu~Hungarian~is~Icelandic~id~Indonesian~it~Italian~it-ch~ (Switzerland)~ja~Japanese~kn~Kannada~kk~Kazakh~x-kok~Konkani~ko~Korean~lv~Latvian~lt~Lithuanian~mk~Macedonian (FYROM)~ms~Malaysian~ml~Malayalam~mt~Maltese~mr~Marathi~ne~Nepali~no~Norwegian~or~Oriva~pl~Polish~pt~Portuguese~pt-br~ (Brazil)~rm~Rhaeto-Romanic~ro~Romanian~ro-mo~ (Moldavia)~ru~Russian~ru-mo~ (Moldavia)~sa~Sanskrit~sr~Serbian (Cyrillic/Latin)~sk~Slovak~sl~Slovenian~sl~Slovenian~sb~Sorbian~";
	langlist += "es~Spanish~es-ar~ (Argentina)~es-bo~ (Bolivia)~es-cl~ (Chile)~es-cr~ (Costa Rica)~es-co~ (Colombia)~es-do~ (Dominican Republic)~es-ec~ (Ecuador)~es-sv~ (El Salvador)~es-gt~ (Guatemala)~es-hn~ (Honduras)~es-mx~ (Mexican)~es-ni~ (Nicaragua)~es-pa~ (Panama)~es-py~ (Paraguay)~es-pe~ (Peru)~es-pr~ (Puerto Rico)~es-uy~ (Uruguay)~es-ve~ (Venezuela)~sx~Sutu~sw~Swahili~sv~Swedish~sv-fi~ (Finland)~ta~Tamil~tt~Tatar~te~Telugu~th~Thai~ts~Tsonga~tn~Tswana~tr~Turkish~uk~Ukrainian~ur~Urdu~uz~Uzbek (Cryllic/Latin)~ve~Venda~vi~Vietnamese~xh~Xhosa~yi~Yiddish~zu~Zulu";
	langlist = langlist.split("~");
	for (i = 0; i < langlist.length; i += 2) if (str.indexOf(langlist[i]) > -1) lang += langlist[i+1];
	return lang;
}

function multiSplit(ar, sep) {
	if (typeof(sep) == "string" && sep.length > 0) {
		ar = ar.split(sep.charAt(0));
		if (sep.length > 1) for (var i in ar) ar[i] = multiSplit(ar[i], sep.substring(1));
	}
	return ar;
}

function multiJoin(ar, sep) {
	if (typeof(sep) == "string" && sep.length > 0) {
		if (sep.length > 1) for (var i in ar) ar[i] = multiJoin(ar[i], sep.substring(1));
		ar = ar.join(sep.charAt(0));
	}
	return ar;
}

function multiCmp(str, ar) {
	if (typeof(ar) == "string") ar = ar.split("|");
	for (var i in ar) if (str == ar[i]) return i;
	return -1;
}

function format_decimal(v, b, nd) {
	v = parseInt(v, 10).toString(b);
	while (v.length < nd) v = "0" + v;
	if (v.length > nd && nd > 0) v = v.substring(v.length - nd);
	return v;
}

function hex2rgb(hex) {
	return {
		r: parseInt(hex.substring(1, 3), 16),
		g: parseInt(hex.substring(3, 5), 16),
		b: parseInt(hex.substring(5, 7), 16)
	};
}

function rgb2hex(rgb) {
	return "#" + format_decimal(rgb.r, 16, 2) + format_decimal(rgb.g, 16, 2) + format_decimal(rgb.b, 16, 2);
}

function bset(b, v, f) {
	if (f) v |= 1 << b; else v &= ~(1 << b);
	return v;
}

function btst(b, v) {
	return (v & (1 << b)) ? true : false;
}

function comma(v) {
	v = parseInt(v, 10).toString();
	for (var i = v.length - 3; i > 0; i -= 3) v = v.substring(0, i) + "," + v.substring(i);
	return v;
}

function fixed(v, d, trim) {
	var i;
	if (v.toFixed != null) {
		v = v.toFixed(d);
	} else {
		v = v.toString();
		i = v.indexOf(".");
		if (i > -1) if (d <= 0) v = v.substring(0, i); else v = (parseFloat(v) + ((parseInt(v.charAt(i+d+1)) >= 5) ? Math.pow(10, -d) : 0)).toString().substring(0, i+d+1);
	}
	if (trim) {
		i = v.indexOf(".");
		if (i > -1) while ((v.charAt(v.length - 1) == "0" || v.charAt(v.length - 1) == ".") && v.length > i) v = v.substring(0, v.length-1);
	}
	return v;
}

function random(min, max) {
	return Math.floor(Math.random() * (max - min + 1) + min);
}

function sign(v) {
	if (v < 0) return -1;
	if (v > 0) return 1;
	return 0;
}

function rplce(str, a, b) {
	var i = -b.length;
	while ((i = str.indexOf(a, i + b.length)) > -1) str = str.substring(0, i) + b + str.substring(i + a.length);
	return str;
}

// Cookies

function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = document.cookie;
	var prefix = name + "=";
	var start = cookie.indexOf(prefix);
	if (start < 0) return;
	var end = cookie.indexOf(";", start + prefix.length);
	if (end < 0) end = cookie.length;
	return unescape(cookie.substring(start + prefix.length, end));
}

function deleteCookie(name) {
	setCookie(name, " ", new Date());
}

// Get Index of Checked Radio Button in a form

function getChkIndx(name) {
	for (var i in name) if (name[i].checked) return i;
}

// VML

function line(ar, flg, linecolr) {
	if (typeof(ar) != "object") ar = ar.split("|");
	var i, x1 = eval(ar[0]), y1 = eval(ar[1]), x2, y2, str = "<v:polyline points='" + x1 + "," + y1;
	if (flg) {
		ar[ar.length] = x1;
		ar[ar.length] = y1;
	}
	for (i = 2; i < ar.length; i += 2) {
		x2 = ar[i];
		y2 = ar[i+1];
		if (typeof(x2) == "string" && (x2.charAt(0) == "+" || x2.charAt(0) == "-")) x2 = x1+eval(x2);
		if (typeof(y2) == "string" && (y2.charAt(0) == "+" || y2.charAt(0) == "-")) y2 = y1+eval(y2);
		str += " " + x2 + "," + y2;
		x1 = x2;
		y1 = y2;
	}
	str += "'" + ((linecolr) ? (" strokecolor='" + linecolr + "'") : "") + " filled='false'/>";
	return str;
}

function rect(x, y, w, h, linecolr, fillcolr, fillcolr2, ang, foc) {
	return "<v:rect style='margin-left: " + x + "px; margin-top: " + y + "px; width: " + w + "px; height: " + h + "px;'" + shapecolr(linecolr, fillcolr, fillcolr2, ang, foc) + "</v:rect>";
}

function ellipse(x, y, w, h, linecolr, fillcolr, fillcolr2, ang, foc) {
	return "<v:oval style='margin-left: " + (x-(w-1)/2) + "px; margin-top: " + (y-(h-1)/2) + "px; width: " + w + "px; height: " + h + "px;'" + shapecolr(linecolr, fillcolr, fillcolr2, ang, foc) + "</v:oval>";
}

function polygon(ar, linecolr, fillcolr, fillcolr2, ang, foc) {
	if (typeof(ar) != "object") ar = ar.split("|");
	var i, x1 = eval(ar[0]), y1 = eval(ar[1]), x2, y2, lx = x1, hx = x1, ly = y1, hy = y1;
	for (i = 2; i < ar.length; i += 2) {
		x2 = ar[i];
		y2 = ar[i+1];
		if (typeof(x2) == "string" && (x2.charAt(0) == "+" || x2.charAt(0) == "-")) x2 = x1 + eval(x2);
		if (typeof(y2) == "string" && (y2.charAt(0) == "+" || y2.charAt(0) == "-")) y2 = y1 + eval(y2);
		ar[i] = x2;
		ar[i + 1] = y2;

		lx = Math.min(lx, x2);
		hx = Math.max(hx, x2);
		ly = Math.min(ly, y2);
		hy = Math.max(hy, y2);

		x1 = x2;
		y1 = y2;
	}

	var str = "<v:shape style='margin-left: " + lx + "px; margin-top: " + ly + "px; width: " + (hx-lx+1) + "px; height: " + (hy-ly+1) + "px;' coordsize='" + (hx-lx+1) + "," + (hy-ly+1) + "' path='m " + (ar[0]-lx) + "," + (ar[1]-ly) + " l";
	for (i = 2; i < ar.length; i += 2) str += " " + (ar[i]-lx) + "," + (ar[i+1]-ly);
	str += " x e'" + shapecolr(linecolr, fillcolr, fillcolr2, ang, foc) + "</v:shape>";
	return str;
}

function shapecolr(linecolr, fillcolr, fillcolr2, ang, foc) {
	return ((linecolr && linecolr != 0) ? " strokecolor='" + linecolr + "'" : "") +
	((linecolr == 0) ? " stroked='false'" : "") +
	((fillcolr) ? " fillcolor='" + fillcolr + "'" : " filled='false'") + ">" +
	((fillcolr2) ? "<v:fill type='gradient' method='linear' color2='" + fillcolr2 + "'" + ((ang) ? " angle='" + ang + "'" : "")  + ((foc) ? " focus='" + foc + "%'" : "") + " />" : "");
}

// Basic functions for DHTML positioned elements that will work on
// both Netscape Navigator and Internet Explorer (version 4.0 and above)

function getObj(name) {
	if (typeof(name) == "object") return name;
	if (typeof(name) != "string") return;
	if (document.getElementById) return document.getElementById(name);
	if (document.all) return document.all[name];
	if (document.layers) return findObj(name);
}

function findObj(name, obj) {
	if (obj == null) obj = document;
	var layer;
	for (var i in obj.layers) {
		layer = obj.layers[i];
		if (layer.name == name) return layer;
		if (layer.document.layers.length > 0) {
			layer = findLayer(name, layer.document);
			if (layer) return layer;
		}
	}
}

function getUniqueID(obj) {
	obj = getObj(obj);
	if (obj.id) return obj.id;
	if (obj.uniqueID) return obj.uniqueID;
	var id;
	while (true) {
		id = "uid" + random(0, 999999);
		if (!getObj(id)) {
			obj.id = id;
			return id;
		}
	}
}

function getParent(obj) {
	obj = getObj(obj);
	if (obj.parentElement) return obj.parentElement;
	return obj.parentNode;
}

// Inner HTML

function setInnerHTML(obj, txt) {
	obj = getObj(obj);
	if (obj.innerHTML != null) obj.innerHTML = txt;
	if (obj.document != null) {
		obj.document.open();
		obj.document.write(txt);
		obj.document.close();
	}
}

// background

function setBgColor(obj, color) {
	obj = getObj(obj);
	if (obj.style.backgroundColor != null) obj.style.backgroundColor = color;
	if (obj.bgColor != null) obj.bgColor = color;
}

function setBgImage(obj, src) {
	obj = getObj(obj);
	if (obj.style.backgroundImage != null) obj.style.backgroundImage = "url(" + src + ")";
	if (obj.background != null) obj.background.src = src;
}

// positioning

function moveObjTo(obj, x, y) {
	obj = getObj(obj);
	if (obj.style.left != null) {
		obj.style.left = x;
		obj.style.top = y;
		return;
	}
	if (obj.moveTo != null) obj.moveTo(x, y);
}

function moveObjBy(obj, dx, dy) {
	obj = getObj(obj);
	if (obj.style.pixelLeft != null) {
		obj.style.pixelLeft += dx;
		obj.style.pixelTop += dy;
		return;
	}
	if (obj.style.left != null) {
		obj.style.left = parseInt(obj.style.left) + dx;
		obj.style.top = parseInt(obj.style.top) + dy;
		return;
	}
	if (obj.moveBy != null) obj.moveBy(dx, dy);
}

function setLeft(obj, x) {
	obj = getObj(obj);
	if (obj.style.left != null) obj.style.left = x;
	if (obj.left != null) obj.left = x;
}

function setTop(obj, y) {
	obj = getObj(obj);
	if (obj.style.top != null) obj.style.top = y;
	if (obj.top != null) obj.top = y;
}

function getLeft(obj) {
	obj = getObj(obj);
	if (obj.offsetLeft != null) return obj.offsetLeft;
	if (obj.left != null) return obj.left;
}

function getTop(obj) {
	obj = getObj(obj);
	if (obj.offsetTop != null) return obj.offsetTop;
	if (obj.top != null) return obj.top;
}

function getTrueLeft(obj) {
	obj = getObj(obj);
	if (obj.offsetLeft != null) {
		var x = 0, flg = false;
		while (obj) {
			if (obj != document.body || !flg) x += obj.offsetLeft;
			if (obj.style.position == "absolute" || obj.style.position == "relative") flg = true;
			obj = obj.offsetParent;
		}
		return x;
	}
	if (obj.pageX != null) return obj.pageX;
}

function getTrueTop(obj) {
	obj = getObj(obj);
	if (obj.offsetTop != null) {
		var y = 0, flg = false;
		while (obj) {
			if (obj != document.body || !flg) y += obj.offsetTop;
			if (obj.style.position == "absolute" || obj.style.position == "relative") flg = true;
			obj = obj.offsetParent;
		}
		return y;
	}
	if (obj.pageY != null) return obj.pageY;
}

function setWidth(obj, w) {
	obj = getObj(obj);
	if (obj.style.width != null) obj.style.width = w;
	if (obj.width != null) obj.width = w;
}

function setHeight(obj, h) {
	obj = getObj(obj);
	if (obj.style.height != null) obj.style.height = h;
	if (obj.height != null) obj.height = h;
}

function setSize(obj, w, h) {
	setWidth(obj, w);
	setHeight(obj, h);
}

function getWidth(obj) {
	obj = getObj(obj);
	if (obj.clientWidth != null) return obj.clientWidth;
	if (obj.offsetWidth != null) return obj.offsetWidth;
	if (obj.document != null && obj.document.width != null) return obj.document.width;
	if (obj.clip != null) return obj.clip.right - obj.clip.left;
}

function getHeight(obj) {
	obj = getObj(obj);
	if (obj.clientHeight != null) return obj.clientHeight;
	if (obj.offsetHeight != null) return obj.offsetHeight;
	if (obj.document != null && obj.document.height != null) return obj.document.height;
	if (obj.clip != null) return obj.clip.bottom - obj.clip.top;
}

function getzIndex(obj) {
	obj = getObj(obj);
	if (obj.style.zIndex != null) return obj.style.zIndex;
	if (obj.zIndex != null) return obj.zIndex;
}

function setzIndex(obj, z) {
	obj = getObj(obj);
	if (obj.style.zIndex != null) obj.style.zIndex = z;
	if (obj.zIndex != null) obj.zIndex = z;
}

// visibility

function setVisibility(obj, v) {
	if (v < 0 || v > 2) return;
	obj = getObj(obj);
	if (obj.style.visibility != null) obj.style.visibility = new Array("hidden", "visible", "inherit")[v];
	if (obj.visibility != null) obj.visibility = new Array("hide", "show", "inherit")[v];
}

function getVisibility(obj) {
	obj = getObj(obj);
	var ie = new Array("hidden", "visible", "inherit"), nn = new Array("hide", "show", "inherit");
	for (var i in ie) {
		if (obj.style.visibility != null) if (obj.style.visibility == ie[i]) return i;
		if (obj.visibility != null) if (obj.visibility == nn[i]) return i;
	}
	return 2;
}

// clipping and scrolling

function setClip(obj, clip) {
	obj = getObj(obj);
	if (obj.style.clip != null) {
		var oclip = getClip(obj);
		for (var i in oclip) if (!clip[i]) clip[i] = oclip[i];
		obj.style.clip = "rect(" + clip.join(" ") + ")";
	}
	if (obj.clip != null) {
		if (clip[0]) obj.clip.top = clip[0];
		if (clip[1]) obj.clip.right = clip[1];
		if (clip[2]) obj.clip.bottom = clip[2];
		if (clip[3]) obj.clip.left = clip[3];
	}
}

function getClip(obj) {
	obj = getObj(obj);
	if (obj.style.clip != null) {
		var clip = obj.style.clip;
		if (clip) clip = clip.substring(clip.indexOf("(") + 1, clip.indexOf(")")).split(" "); else clip = new Array(0, getWidth(obj), getHeight(obj), 0);
		clip[4] = clip[1] - clip[3];
		clip[5] = clip[2] - clip[0];
		return clip;
	}
	if (obj.clip != null) return new Array(obj.clip.top, obj.clip.right, obj.clip.bottom, obj.clip.left, obj.clip.width, obj.clip.height);
}

function scrollObjTo(obj, x, y, bound) {
	var clip = getClip(obj);
	scrollObjBy(obj, x - clip[3], y - clip[0], bound);
}

function scrollObjBy(obj, dx, dy, bound) {
	var clip = getClip(obj);

	if (bound) {
		if (clip[3] + dx < 0) dx = -clip[3];
		if (clip[1] + dx > getWidth(obj)) dx = getWidth(obj) - clip[1];
		if (clip[0] + dy < 0) dy = -clip[0];
		if (clip[2] + dy > getHeight(obj)) dy = getHeight(obj) - clip[2];
	}

	clip[0] += dy;
	clip[1] += dx;
	clip[2] += dy;
	clip[3] += dx;

	setClip(obj, clip);
	moveObjBy(obj, -dx, -dy);
}

// Window and page properties

function getWindowWidth() {
	// if (window.innerWidth != null) return window.innerWidth;
	if (document.documentElement != null && document.documentElement.clientWidth != null && document.documentElement.clientWidth > 0) return document.documentElement.clientWidth;
	if (document.body.clientWidth != null) return document.body.clientWidth;
}

function getWindowHeight() {
	// if (window.innerHeight != null) return window.innerHeight;
	if (document.documentElement != null && document.documentElement.clientHeight != null && document.documentElement.clientHeight > 0) return document.documentElement.clientHeight;
	if (document.body.clientHeight != null) return document.body.clientHeight;
}

function getPageWidth() {
	if (document.documentElement.scrollWidth != null) return document.documentElement.scrollWidth;
	if (document.width != null) return document.width;
}

function getPageHeight() {
	if (document.documentElement.scrollHeight != null) return document.documentElement.scrollHeight;
	if (document.height != null) return document.height;
}

function getScrollLeft() {
	if (document.body.scrollLeft != null) return document.body.scrollLeft;
	if (window.pageXOffset != null) return window.pageXOffset;
}

function getScrollTop() {
	if (document.body.scrollTop != null) return document.body.scrollTop;
	if (window.pageYOffset != null) return window.pageYOffset;
}

function getMouseX(e) {
	if (event.clientX != null) return event.clientX;
	if (e.pageX != null) return e.pageX;
}

function getMouseY(e) {
	if (event.clientY != null) return event.clientY;
	if (e.pageY != null) return e.pageY;
}

// Events

function attachEventHandler(obj, event, handler) {
	obj = getObj(obj);
	if (obj.attachEvent) {
		obj.attachEvent("on" + event, handler);
	} else if (obj.addEventListener) {
		obj.addEventListener(event, handler, true);
	}
}
