function get_elementX(ref) {
	var loop = ref;
	var out = 0;
	
	while (loop != null) {
		out += loop.offsetLeft;
		loop = loop.offsetParent;
	}
	
	return out;
}

function get_elementY(ref) {
	var loop = ref;
	var out = 0;
	
	while (loop != null) {
		out += loop.offsetTop;
		loop = loop.offsetParent;
	}
	
	return out;
}

function get_elementWidth(ref) {
	return ref.offsetWidth;
}

function get_elementHeight(ref) {
	return ref.offsetHeight;
}

function preview(prev_path) {
	window.open("/preview.php?prev_path=" + prev_path, "prev_wnd", "toolbar=0,statusbar=0,menubar=0,resizable=1,scrollbars=0,width=800,height=600");
}

function go_back() { history.go(-1); }

function blank() {  }

function do_print() { window.print(); }

function download(path) {
	window.open(path, "download");
}

function open_map(path) {
	window.open(path, "map");
}

function newsletter_login() {
	document.getElementById("newsletter_form").submit();
}

function newsletter_info_close() {
	document.getElementById("newsletter_info").style.display = "none";
}

function std_banner_close() {
	document.getElementById("pre_std_banner").style.display = "none";
}

var last_contact = null;

function contact_close(ident) {
	var obj = document.getElementById("contact_stuff" + ident + "no");
	if (obj != null) {
		if (last_contact == ident)
			last_contact = null;
			
		obj.style.display = "none";
	}
}

function contact_open(ident) {
	var obj = document.getElementById("contact_stuff" + ident + "no");
	if (obj != null) {
		
		if (last_contact != null)
			document.getElementById("contact_stuff" + last_contact + "no").style.display = "none";
		
		obj.style.display = "block";
		last_contact = ident;
	}
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function utf8_encode(string) {
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return utftext;
}
	
function base64(input) {
	var output = "";
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i = 0;

	input = utf8_encode(input);

	while (i < input.length) {

		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}

		output = output +
		keyStr.charAt(enc1) + keyStr.charAt(enc2) +
		keyStr.charAt(enc3) + keyStr.charAt(enc4);

	}

	return output;
}

function model_filter(href, value) {
	var pattern = encodeURI(value);
	var n_pattern = "";
	var f, ch;
	for (f = 0; f < pattern.length; f++) {
		ch = pattern.substr(f, 1);
		if (ch == '%') n_pattern += '~';
		else n_pattern += ch;
	}
	document.location.href = href + n_pattern;
}

function clone_add(a, o) {
	for (var i in o) a[i] = o[i];
}

function purge(d) {
	var a = d.attributes, i, l, n;
	if (a) {
		l = a.length;
		for (i = 0; i < l; i++) {
			n = a[i].name;
			if (typeof d[n] === 'function') d[n] = null;
		}
	}
}

function structure_destroy(ref) {
	if (ref != null) {
		var stack = new Array();
		var current, f;
		stack[stack.length] = ref;
		
		while (stack.length > 0) {
			current = stack.pop();

			if (current.childNodes != null && current.childNodes.length > 0) {
				stack[stack.length] = current;
				for (f = 0; f < current.childNodes.length; f++)
					stack[stack.length] = current.childNodes[f];
				
			} else {
				purge(current);
				if (current.parentNode != null) current.parentNode.removeChild(current);
			}
		}
	}
}

function object_destroy(ref) {
	for (var i in ref) {
		ref[i] = null;
	}
}


function new_captcha_make(ref, skip_new) {
	var link = "/captcha.php?";
	if (skip_new == null) link += "new=1&";
	link += "r="
	var univ = "qwertyuiopasdfghjklzxcvbnm0123456789";
	var f, letter, len = Math.round(Math.random() * 20) + 15;
		
	for (f = 0; f < len; f++) link += univ.substr(Math.round(Math.random() * (univ.length - 1)), 1);

	ref.src = link;
}
	
function new_captcha() {
	var ref = document.getElementById("captcha");
	if (ref != null) {
		new_captcha_make(ref);
			
	} else {
		ref = null;
		var index = 1;
			
		do {
			if ((ref = document.getElementById("captcha_" + index)) != null)
				new_captcha_make(ref, index == 1 ? null : true);
			index++;
		} while (ref != null);
	}
}
