function popwin(mypage, myname, w, h, scroll) {
	LeftPosition = (screen.width) ? (screen.width - w) / 2 : 0;
	TopPosition = (screen.height) ? (screen.height - h) / 2 : 0;
	settings = 'height=' + h + ',width=' + w + ',top=' + TopPosition + ',left='
			+ LeftPosition + ',scrollbars=' + scroll + ',resizable';
	win = window.open(mypage, myname, settings)
	if (win.opener == null) {
		win.opener = self;
	}
}

function lockFormSubmit(lock) {
	if (document.all || document.getElementById) {
		for (i = 0; i < lock.length; i++) {
			if (lock.elements[i]) {
				var tempobj = lock.elements[i];
				if (tempobj.type.toLowerCase() == "submit"
						|| tempobj.type.toLowerCase() == "reset"
						|| tempobj.type.toLowerCase() == "button") {
					tempobj.disabled = true;
				}
			}
		}
	}
}

function move(index, to) {
	var list = document.form.list;
	var total = list.options.length - 1;
	if (index == -1) {
		return false;
	}
	if (to == +1 && index == total) {
		return false;
	}
	if (to == -1 && index == 0) {
		return false;
	}
	var items = new Array;
	var values = new Array;
	for (i = total; i >= 0; i--) {
		items[i] = list.options[i].text;
		values[i] = list.options[i].value;
	}
	for (i = total; i >= 0; i--) {
		if (index == i) {
			list.options[i + to] = new Option(items[i], values[i + to], 0, 1);
			list.options[i] = new Option(items[i + to], values[i]);
			i--;
		} else {
			list.options[i] = new Option(items[i], values[i]);
		}
	}
	list.focus();
}

function newCaptcha(width, height, url) {
	var now = new Date();
	document.captcha.src = url + '/image.php?mode=captcha&width=' + width
			+ '&height=' + height + '&u=' + now.getTime();
	return (false);
}

function vaultpop(URL) {
	var day = new Date();
	var id = day.getTime();
	eval("page"
			+ id
			+ " = window.open(URL, '"
			+ id
			+ "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=650,height=600,left = 50,top = 50');");
}

function grayOut(vis, options) {
	var options = options || {};
	var zindex = options.zindex || 50;
	var opacity = options.opacity || 70;
	var opaque = (opacity / 100);
	var bgcolor = options.bgcolor || '#000000';
	var dark = document.getElementById('darkenScreenObject');
	if (!dark) {
		var tbody = document.getElementsByTagName("body")[0];
		var tnode = document.createElement('div');
		tnode.style.position = 'absolute';
		tnode.style.top = '0px';
		tnode.style.left = '0px';
		tnode.style.overflow = 'hidden';
		tnode.style.display = 'none';
		tnode.id = 'darkenScreenObject';
		tbody.appendChild(tnode);
		dark = document.getElementById('darkenScreenObject');
	}
	if (vis) {
		if (document.body
				&& (document.body.scrollWidth || document.body.scrollHeight)) {
			var pageWidth = document.body.scrollWidth + 'px';
			var pageHeight = document.body.scrollHeight + 'px';
		} else if (document.body.offsetWidth) {
			var pageWidth = document.body.offsetWidth + 'px';
			var pageHeight = document.body.offsetHeight + 'px';
		} else {
			var pageWidth = '100%';
			var pageHeight = '100%';
		}
		dark.style.opacity = opaque;
		dark.style.MozOpacity = opaque;
		dark.style.filter = 'alpha(opacity=' + opacity + ')';
		dark.style.zIndex = zindex;
		dark.style.backgroundColor = bgcolor;
		dark.style.width = pageWidth;
		dark.style.height = pageHeight;
		dark.style.display = 'block';
	} else {
		dark.style.display = 'none';
	}
}

function jframe(j_div, j_url) {
	if (window.XMLHttpRequest) {
		var xobj = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		var xobj = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return (false);
	}
	xobj.open("GET", j_url, true);
	xobj.onreadystatechange = function() {
		if (xobj.readyState == 4) {
			document.getElementById(j_div).innerHTML = xobj.responseText;
		}
	}
	xobj.send(null);
}

var re_dt = /^(\d{1,2})\-(\d{1,2})\-(\d{4})$/, re_tm = /^(\d{1,2})\:(\d{1,2})\:(\d{1,2})$/, a_formats = {
	'alpha' : /^[a-zA-Z\.\-]*$/,
	'alphanum' : /^\w+$/,
	'unsigned' : /^\d+$/,
	'integer' : /^[\+\-]?\d*$/,
	'real' : /^[\+\-]?\d*\.?\d*$/,
	'email' : /^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/,
	'phone' : /^[\d\.\s\-]+$/,
	'date' : function(s_date) {
		if (!re_dt.test(s_date))
			return false;
		if (RegExp.$1 > 31 || RegExp.$2 > 12)
			return false;
		var dt_test = new Date(RegExp.$3, Number(RegExp.$2 - 1), RegExp.$1);
		if (dt_test.getMonth() != Number(RegExp.$2 - 1))
			return false;
		return true;
	},
	'time' : function(s_time) {
		if (!re_tm.test(s_time))
			return false;
		if (RegExp.$1 > 23 || RegExp.$2 > 59 || RegExp.$3 > 59)
			return false;
		return true;
	}
}, a_messages = [
		'No form name passed to validator construction routine',
		'No array of "%form%" form fields passed to validator construction routine',
		'Form "%form%" can not be found in this document',
		'Incomplete "%n%" form field descriptor entry. "l" attribute is missing',
		'Can not find form field "%n%" in the form "%form%"',
		'Can not find label tag (id="%t%")',
		'Can not verify match. Field "%m%" was not found',
		'"%l%" is a required field',
		'Value for "%l%" must be %mn% characters or more',
		'Value for "%l%" must be no longer than %mx% characters',
		'"%v%" is not valid value for "%l%"', '"%l%" must match "%ml%"']

function validator(s_form, a_fields, o_cfg) {
	this.f_error = validator_error;
	this.f_alert = o_cfg && o_cfg.alert ? function(s_msg) {
		alert(s_msg);
		return false
	} : function() {
		return false
	};

	if (!s_form)
		return this.f_alert(this.f_error(0));
	this.s_form = s_form;

	if (!a_fields || typeof(a_fields) != 'object')
		return this.f_alert(this.f_error(1));
	this.a_fields = a_fields;

	this.a_2disable = o_cfg && o_cfg['to_disable']
			&& typeof(o_cfg['to_disable']) == 'object'
			? o_cfg['to_disable']
			: [];

	this.exec = validator_exec;
}

function validator_exec() {
	var o_form = document.forms[this.s_form];
	if (!o_form)
		return this.f_alert(this.f_error(2));

	b_dom = document.body && document.body.innerHTML;

	for (var n_key in this.a_fields) {
		this.a_fields[n_key]['n'] = n_key;
		if (!this.a_fields[n_key]['l'])
			return this.f_alert(this.f_error(3, this.a_fields[n_key]));
		o_input = o_form.elements[n_key];
		if (!o_input)
			return this.f_alert(this.f_error(4, this.a_fields[n_key]));
		this.a_fields[n_key].o_input = o_input;
	}

	if (b_dom)
		for (var n_key in this.a_fields)
			if (this.a_fields[n_key]['t']) {
				var s_labeltag = this.a_fields[n_key]['t'], e_labeltag = get_element(s_labeltag);
				if (!e_labeltag)
					return this.f_alert(this.f_error(5, this.a_fields[n_key]));
				this.a_fields[n_key].o_tag = e_labeltag;
				e_labeltag.className = 'tfvNormal';
			}

	for (var n_key in this.a_fields) {
		var s_value = '';
		o_input = this.a_fields[n_key].o_input;
		if (o_input.type == 'checkbox')
			s_value = o_input.checked ? o_input.value : '';
		else if (o_input.value)
			s_value = o_input.value;
		else if (o_input.options)
			s_value = o_input.selectedIndex > -1
					? o_input.options[o_input.selectedIndex].value
					: null;
		else if (o_input.length > 0)
			for (var n_index = 0; n_index < o_input.length; n_index++)
				if (o_input[n_index].checked) {
					s_value = o_input[n_index].value;
					break;
				}
		this.a_fields[n_key]['v'] = s_value.replace(/(^\s+)|(\s+$)/g, '');
	}

	var n_errors_count = 0, n_another, o_format_check;
	for (var n_key in this.a_fields) {
		o_format_check = this.a_fields[n_key]['f']
				&& a_formats[this.a_fields[n_key]['f']]
				? a_formats[this.a_fields[n_key]['f']]
				: null;

		this.a_fields[n_key].n_error = null;

		if (this.a_fields[n_key]['r'] && !this.a_fields[n_key]['v']) {
			this.a_fields[n_key].n_error = 1;
			n_errors_count++;
		} else if (this.a_fields[n_key]['mn']
				&& this.a_fields[n_key]['v'] != ''
				&& String(this.a_fields[n_key]['v']).length < this.a_fields[n_key]['mn']) {
			this.a_fields[n_key].n_error = 2;
			n_errors_count++;
		} else if (this.a_fields[n_key]['mx']
				&& String(this.a_fields[n_key]['v']).length > this.a_fields[n_key]['mx']) {
			this.a_fields[n_key].n_error = 3;
			n_errors_count++;
		} else if (this.a_fields[n_key]['v']
				&& this.a_fields[n_key]['f']
				&& ((typeof(o_format_check) == 'function' && !o_format_check(this.a_fields[n_key]['v'])) || (typeof(o_format_check) != 'function' && !o_format_check
						.test(this.a_fields[n_key]['v'])))) {
			this.a_fields[n_key].n_error = 4;
			n_errors_count++;
		} else if (this.a_fields[n_key]['m']) {
			for (var n_key2 in this.a_fields)
				if (n_key2 == this.a_fields[n_key]['m']) {
					n_another = n_key2;
					break;
				}
			if (n_another == null)
				return this.f_alert(this.f_error(6, this.a_fields[n_key]));
			if (this.a_fields[n_another]['v'] != this.a_fields[n_key]['v']) {
				this.a_fields[n_key]['ml'] = this.a_fields[n_another]['l'];
				this.a_fields[n_key].n_error = 5;
				n_errors_count++;
			}
		}

	}
	var s_alert_message = '', e_first_error;

	if (n_errors_count) {
		for (var n_key in this.a_fields) {
			var n_error_type = this.a_fields[n_key].n_error, s_message = '';

			if (n_error_type)
				s_message = this
						.f_error(n_error_type + 6, this.a_fields[n_key]);

			if (s_message) {
				if (!e_first_error)
					e_first_error = o_form.elements[n_key];
				s_alert_message += s_message + "\n";
				if (b_dom && this.a_fields[n_key].o_tag)
					this.a_fields[n_key].o_tag.className = 'tfvHighlight';
			}
		}
		alert(s_alert_message);
		if (e_first_error.focus && e_first_error.type != 'hidden'
				&& !e_first_error.disabled)
			eval("e_first_error.focus()");
		return false;
	}

	for (n_key in this.a_2disable)
		if (o_form.elements[this.a_2disable[n_key]])
			o_form.elements[this.a_2disable[n_key]].disabled = true;

	return true;
}

function validator_error(n_index) {
	var s_ = a_messages[n_index], n_i = 1, s_key;
	for (; n_i < arguments.length; n_i++)
		for (s_key in arguments[n_i])
			s_ = s_.replace('%' + s_key + '%', arguments[n_i][s_key]);
	s_ = s_.replace('%form%', this.s_form);
	return s_
}

function get_element(s_id) {
	return (document.all ? document.all[s_id] : (document.getElementById
			? document.getElementById(s_id)
			: null));
}

function jrSetCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	} else
		var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function jrReadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ')
			c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function jrEraseCookie(name) {
	jrSetCookie(name, "", -1);
}

function jrEscape(text) {
	if (encodeURIComponent) {
		text = encodeURIComponent(text);
	} else {
		text = escape(text);
	}
	return text;
}
