﻿// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
// xGetElementById r2, Copyright 2001-2007 Michael Foster (Cross-Browser.com)

function xGetElementById(e) {
	if (typeof (e) == 'string') {
		if (document.getElementById) e = document.getElementById(e);
		else if (document.all) e = document.all[e];
		else e = null;
	}
	return e;
}

// xNum r2, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
function xNum() {
	for (var i = 0; i < arguments.length; ++i) { if (isNaN(arguments[i]) || typeof (arguments[i]) != 'number') return false; }
	return true;
}

// xDef r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
function xDef() {
	for (var i = 0; i < arguments.length; ++i) { if (typeof (arguments[i]) == 'undefined') return false; }
	return true;
}

// xStr r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
function xStr(s) {
	for (var i = 0; i < arguments.length; ++i) { if (typeof (arguments[i]) != 'string') return false; }
	return true;
}

// xGetComputedStyle r7, Copyright 2002-2007 Michael Foster (Cross-Browser.com)
function xGetComputedStyle(e, p, i) {
	if (!(e = xGetElementById(e))) return null;
	var s, v = 'undefined', dv = document.defaultView;
	if (dv && dv.getComputedStyle) {
		s = dv.getComputedStyle(e, '');
		if (s) v = s.getPropertyValue(p);
	}
	else if (e.currentStyle) {
		v = e.currentStyle[xCamelize(p)];
	}
	else return null;
	return i ? (parseInt(v) || 0) : v;
}

// xCamelize r1, Copyright 2007-2009 Michael Foster (Cross-Browser.com)
function xCamelize(cssPropStr) {
	var i, c, a, s;
	a = cssPropStr.split('-');
	s = a[0];
	for (i = 1; i < a.length; ++i) {
		c = a[i].charAt(0);
		s += a[i].replace(c, c.toUpperCase());
	}
	return s;
}

// xColEqualizer r1, Original by moi. Modified by Mike Foster.
function xColEqualizer() {
	var i, j, h = [];
	// get heights of columns' child elements
	for (i = 1, j = 0; i < arguments.length; i += 2, ++j) {
		h[j] = xHeight(arguments[i]);
	}
	h.sort(d);
	// set heights of column elements
	for (i = 0; i < arguments.length; i += 2) {
		xHeight(arguments[i], h[0]);
	}
	return h[0];
	// for a descending sort
	function d(a, b) {
		return b - a;
	}
}

// xHeight r7, Copyright 2001-2009 Michael Foster (Cross-Browser.com)
function xHeight(e, h) {
	var css, pt = 0, pb = 0, bt = 0, bb = 0, gcs;
	if (!(e = xGetElementById(e))) return 0;
	if (xNum(h)) {
		if (h < 0) h = 0;
		else h = Math.round(h);
	}
	else h = -1;
	css = xDef(e.style);
	if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
		h = xClientHeight();
	}
	else if (css && xDef(e.offsetHeight) && xStr(e.style.height)) {
		if (h >= 0) {
			if (document.compatMode == 'CSS1Compat') {
				gcs = xGetComputedStyle;
				pt = gcs(e, 'padding-top', 1);
				if (pt !== null) {
					pb = gcs(e, 'padding-bottom', 1);
					bt = gcs(e, 'border-top-width', 1);
					bb = gcs(e, 'border-bottom-width', 1);
				}
				// Should we try this as a last resort?
				// At this point getComputedStyle and currentStyle do not exist.
				else if (xDef(e.offsetHeight, e.style.height)) {
					e.style.height = h + 'px';
					pt = e.offsetHeight - h;
				}
			}
			h -= (pt + pb + bt + bb);
			if (isNaN(h) || h < 0) return;
			else e.style.height = h + 'px';
		}
		h = e.offsetHeight;
	}
	else if (css && xDef(e.style.pixelHeight)) {
		if (h >= 0) e.style.pixelHeight = h;
		h = e.style.pixelHeight;
	}
	return h;
}

// xWidth r7, Copyright 2001-2009 Michael Foster (Cross-Browser.com)
function xWidth(e, w) {
	var css, pl = 0, pr = 0, bl = 0, br = 0, gcs;
	if (!(e = xGetElementById(e))) return 0;
	if (xNum(w)) {
		if (w < 0) w = 0;
		else w = Math.round(w);
	}
	else w = -1;
	css = xDef(e.style);
	if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
		w = xClientWidth();
	}
	else if (css && xDef(e.offsetWidth) && xStr(e.style.width)) {
		if (w >= 0) {
			if (document.compatMode == 'CSS1Compat') {
				gcs = xGetComputedStyle;
				pl = gcs(e, 'padding-left', 1);
				if (pl !== null) {
					pr = gcs(e, 'padding-right', 1);
					bl = gcs(e, 'border-left-width', 1);
					br = gcs(e, 'border-right-width', 1);
				}
				// Should we try this as a last resort?
				// At this point getComputedStyle and currentStyle do not exist.
				else if (xDef(e.offsetWidth, e.style.width)) {
					e.style.width = w + 'px';
					pl = e.offsetWidth - w;
				}
			}
			w -= (pl + pr + bl + br);
			if (isNaN(w) || w < 0) return;
			else e.style.width = w + 'px';
		}
		w = e.offsetWidth;
	}
	else if (css && xDef(e.style.pixelWidth)) {
		if (w >= 0) e.style.pixelWidth = w;
		w = e.style.pixelWidth;
	}
	return w;
}

// xWindow r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
function xWindow(name, w, h, x, y, loc, men, res, scr, sta, too) {
	var e = '', c = ',', xf = 'left=', yf = 'top='; this.n = name;
	if (document.layers) { xf = 'screenX='; yf = 'screenY='; }
	this.f = (w ? 'width=' + w + c : e) + (h ? 'height=' + h + c : e) + (x >= 0 ? xf + x + c : e) +
    (y >= 0 ? yf + y + c : e) + 'location=' + loc + ',menubar=' + men + ',resizable=' + res +
    ',scrollbars=' + scr + ',status=' + sta + ',toolbar=' + too;
	this.opened = function() { return this.w && !this.w.closed; };
	this.close = function() { if (this.opened()) this.w.close(); };
	this.focus = function() { if (this.opened()) this.w.focus(); };
	this.load = function(sUrl) {
		if (this.opened()) this.w.location.href = sUrl;
		else this.w = window.open(sUrl, this.n, this.f);
		this.focus();
		return false;
	};
}

// xDef r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
function xDef() {
	for (var i = 0; i < arguments.length; ++i) { if (typeof (arguments[i]) == 'undefined') return false; }
	return true;
}

// xClientHeight r6, Copyright 2001-2008 Michael Foster (Cross-Browser.com)
function xClientHeight() {
	var v = 0, d = document, w = window;
	if ((!d.compatMode || d.compatMode == 'CSS1Compat') /* && !w.opera */ && d.documentElement && d.documentElement.clientHeight)
	{ v = d.documentElement.clientHeight; }
	else if (d.body && d.body.clientHeight)
	{ v = d.body.clientHeight; }
	else if (xDef(w.innerWidth, w.innerHeight, d.width)) {
		v = w.innerHeight;
		if (d.width > w.innerWidth) v -= 16;
	}
	return v;
}

// xClientWidth r5, Copyright 2001-2007 Michael Foster (Cross-Browser.com)

function xClientWidth() {
	var v = 0, d = document, w = window;
	if ((!d.compatMode || d.compatMode == 'CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientWidth)
	{ v = d.documentElement.clientWidth; }
	else if (d.body && d.body.clientWidth)
	{ v = d.body.clientWidth; }
	else if (xDef(w.innerWidth, w.innerHeight, d.height)) {
		v = w.innerWidth;
		if (d.height > w.innerHeight) v -= 16;
	}
	return v;
}

// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xVisibility(e, bShow) {
	if (!(e = xGetElementById(e))) return null;
	if (e.style && xDef(e.style.visibility)) {
		if (xDef(bShow)) e.style.visibility = bShow ? 'visible' : 'hidden';
		return e.style.visibility;
	}
	return null;
}

//--------------------------------------------------------------------------------------
//http://cass-hacks.com/
function URLEncode(clearString) {
	var output = '';
	var x = 0;
	clearString = clearString.toString();
	var regex = /(^[a-zA-Z0-9_.]*)/;
	while (x < clearString.length) {
		var match = regex.exec(clearString.substr(x));
		if (match != null && match.length > 1 && match[1] != '') {
			output += match[1];
			x += match[1].length;
		} else {
			if (clearString[x] == ' ')
				output += '+';
			else {
				var charCode = clearString.charCodeAt(x);
				var hexVal = charCode.toString(16);
				output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
			}
			x++;
		}
	}
	return output;
}

function URLDecode(encodedString) {
	var output = encodedString;
	var binVal, thisString;
	var myregexp = /(%[^%]{2})/;
	while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
		binVal = parseInt(match[1].substr(1), 16);
		thisString = String.fromCharCode(binVal);
		output = output.replace(match[1], thisString);
	}
	return output;
}


//--------------------------------------------------------------------------------------

function uMinHeight(e, h) {
	var css, pt = 0, pb = 0, bt = 0, bb = 0, gcs;
	if (!(e = xGetElementById(e))) return 0;
	if (xNum(h)) {
		if (h < 0) h = 0;
		else h = Math.round(h);
	}
	else h = -1;
	css = xDef(e.style);
	if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
		h = xClientHeight();
	}
	else if (css && xDef(e.offsetHeight) && xStr(e.style.height)) {
		if (h >= 0) {
			if (document.compatMode == 'CSS1Compat') {
				gcs = xGetComputedStyle;
				pt = gcs(e, 'padding-top', 1);
				if (pt !== null) {
					pb = gcs(e, 'padding-bottom', 1);
					bt = gcs(e, 'border-top-width', 1);
					bb = gcs(e, 'border-bottom-width', 1);
				}
				else if (xDef(e.offsetHeight, e.style.height)) {
					e.style.minHeight = h + 'px';
					pt = e.offsetHeight - h;
				}
			}
			h -= (pt + pb + bt + bb);
			if (isNaN(h) || h < 0) return;
			else e.style.minHeight = h + 'px';
		}
		h = e.offsetHeight;
	}
	else if (css && xDef(e.style.pixelHeight)) {
		if (h >= 0) e.style.pixelHeight = h;
		h = e.style.pixelHeight;
	}
	return h;
}

//--------------------------------------------------------------------------------------

function TextAreaCounter(messageId, counterId, maxChars) {
	var myTextArea = xGetElementById(messageId);
	var myCounter = xGetElementById(counterId);
	var c;
	if (myTextArea.value.length > maxChars) {
		myTextArea.value = myTextArea.value.substring(0, maxChars);
		c = 0;
	}
	else {
		c = maxChars - myTextArea.value.length;
	}
	myCounter.value = 'You have ' + c + ' of ' + maxChars + ' characters left';
}

function SetUniqueRadioButton(nameregex, current) {
	re = new RegExp(nameregex);
	for (i = 0; i < document.forms[0].elements.length; i++) {
		elm = document.forms[0].elements[i]
		if (elm.type == 'radio') {
			if (re.test(elm.name)) {
				elm.checked = false;
			}
		}
	}
	current.checked = true;
}

function UnevoPopUpStart(X, Y) {
	var xx;
	var yy;

	X = X + 80;
	Y = Y + 110;

	yy = self.window.screenY + 100;
	if (isNaN(yy)) yy = self.window.screenTop + 20;

	xx = self.window.screenX + 50;
	if (isNaN(xx)) xx = self.window.screenLeft + 50;

	self.window.resizeTo(X, Y);
	self.window.focus();
}

function uWindow(sUrl, name, w, h, x, y, loc, men, res, scr, sta, too) {
	var e = '', c = ',', xf = 'left=', yf = 'top=';
	if (document.layers) { xf = 'screenX='; yf = 'screenY='; }
	var features = (w ? 'width=' + w + c : e) + (h ? 'height=' + h + c : e) + (x >= 0 ? xf + x + c : e) + (y >= 0 ? yf + y + c : e) + 'location=' + loc + ',menubar=' + men + ',resizable=' + res + ',scrollbars=' + scr + ',status=' + sta + ',toolbar=' + too;

	var uChildWindow = window.open(sUrl, name, features);
	uChildWindow.focus();
	return false;
}

function UnevoDelete() { return confirm('Do you really want to delete this record?'); }
function UnevoClose() { close(); }
