/* -------------------------------------- */
/*  JS   C o m m o n   f u n c t i o n s  */
/* -------------------------------------- */
function split_by(s, by) {
	var parts = new Array();
	var i = 0, j = 0, index = 0;
	while ((i=s.indexOf(by, j))>0) {
		if (i>j) {
			parts[index] = s.substring(j, i);
			index++;
		}
		j = (i+=by.length);
		if (i>=s.length-1) return;
	}
	
	if (index>0) {
		if (i<=s.length-1) 
			parts[index] = s.substring(j, s.length);
	} else parts[0] = s;
	return parts;
}

function html_out_wglink(wgl, culture_name) {
	var lc = (culture_name?culture_name:"en");
	var l = wgl.toLowerCase().replace("\x30", "");
	var parts = split_by(wgl, ":");
	if (l.indexOf('module:search:search:')==0) return "search/"+lc+"_search.html";
	if (l.indexOf('file:core:file:')==0) return parts[3];
	if (l.indexOf('page:core:page:')==0) {
		if (html_page_names) return unescape(html_page_names[parts[3]]);
		else return "page-"+parts[3]+".html";
	}
	return "";
}

function get_url_afterhash() {
	var u = document.location.href;
	try { if (!u) u = self.navigate; } catch(e) {}
	var i = u.lastIndexOf('#');
	if (i>0 && i<u.length-1) return u.substring(i+1, u.length-1);
	else return "";
}

function confirmurl(u, s) { if (confirm(s)) location=u; }
function popupWin(u,w,h) {
	try { 
		var rkey = 1;
		try{ rkey=2130706431; rkey=Math.floor(n_max*Math.random()); } 
		catch(e){ rkey=1 }
		var u_refreshed = u;
		if (u.indexOf('#')<1) {
			if (u.indexOf('?')>0) u_refreshed+='&refresh_key='+rkey;
			else u_refreshed+='?refresh_key='+rkey;
		}
		window.showModalDialog(u,document,'dialogHeight:'+h+'px;dialogWidth:'+w+'px;center:yes;edge:raised;resizable:yes;scroll:yes;status:no;'); 
	} catch (eh) {
		try { window.open(u,'wgpopup','location=0,menubar=0,scrollbars=1,status=0,toolbar=0,top=150,left=200,height='+h+',width='+w+',titlebar=0,resizable=1'); }
		catch (eh) { alert("ERROR: - Can't open an popup window!"); }
	}
}

function popupStdWin(u,w,h) {
	try { window.open(u,'wgpopup','location=0,menubar=0,scrollbars=1,status=0,toolbar=0,top=150,left=200,height='+h+',width='+w+',titlebar=0,resizable=1'); }
	catch (eh) { alert("ERROR: - Can't open an popup window!"); }
}

function ord(s) { try { return s.charCodeAt(0); } catch(e) { alert('ERROR in je():\n\n- '+e.message); } }
function chr(c) { try { return String.fromCharCode(c); } catch(e) { alert('ERROR in je():\n\n- '+e.message); } }
function hexdigit(n) { if (n<10) return n+''; else return chr(ord('A') + n - 10); }
function hexbyte(n) { return hexdigit(Math.floor(n/16)) + hexdigit(n % 16); }

// returned C-style slashed string
function je(s){
	try {
		var r='', c = 0;
		for(i=0; i<s.length; i++){
			c = s.charCodeAt(i);
			if ( (c>=ord('0') && c<=ord('9')) || (c>=ord('a') && c<=ord('z')) || (c>=ord('A') && c<=ord('Z')) ) r+= chr(c);
			else r+= '\\' + 'x' + hexbyte(c);
		}
		return r;
	} catch(e) { return s; }
}	

function initHoverImages() {
	try {
		if (!document.getElementById) return;
		var imgOriginSrc, j=0;
		var imgPreLoadHovers = new Array();
		var imgPreLoadPushed = new Array();
		var imgarr = document.getElementsByTagName('img');
		for (var i = 0; i < imgarr.length; i++) 
			if (imgarr[i].getAttribute('hover')) {
				
				// preload hover(pushed) images, for fast displaying afterwards
				imgPreLoadHovers[i] = new Image();
				imgPreLoadHovers[i].src = imgarr[i].getAttribute('hover');
				if (imgarr[i].getAttribute('pushed')) {
					imgPreLoadPushed[j] = new Image();
					imgPreLoadPushed[j++].src = imgarr[i].getAttribute('pushed');
				}
				
				// assign extra properties to store settings
				imgarr[i].is_on = 0;
				imgarr[i].hover_src = imgarr[i].getAttribute('hover');
				imgarr[i].origin_src = imgarr[i].getAttribute('src');
				if (imgarr[i].getAttribute('pushed')) imgarr[i].pushed_src = imgarr[i].getAttribute('pushed');
				else imgarr[i].pushed_src = imgarr[i].hover_src;
				
				// setup mouse events
				imgarr[i].onmouseover = function() { try{ if (this.is_on==0) this.src=this.hover_src; } catch(e) {} }
				imgarr[i].onmouseout = function() { try{ if (this.is_on==0) this.src=this.origin_src; } catch(e) {} }
				imgarr[i].onclick = function() { 
					try { 
						if (this.is_on==0) {
							var imgarr = document.getElementsByTagName('img');
							for (var i = 0; i < imgarr.length; i++)
								if (imgarr[i].getAttribute('hover') && imgarr[i].is_on==1) {
									imgarr[i].src = imgarr[i].origin_src;
									imgarr[i].is_on = 0;
								}
							this.src = this.pushed_src;
							this.is_on = 1;
						}
					} catch(e) {}
				}
			}
	} catch(e) {}
}

function preloadImg() {
	if (document.images) {
		if (typeof(document.pIMGS) == 'undefined'){
			document.pIMGS = new Object();
		}
		document.pIMGS.loadedImages = new Array();
		var argLength = preloadImg.arguments.length;
		for(arg=0;arg<argLength;arg++) {
			document.pIMGS.loadedImages[arg] = new Image();
			document.pIMGS.loadedImages[arg].src = preloadImg.arguments[arg];
		}
	}
}	

if (location.href.toLowerCase().indexOf('/admin/')>0) {
	try { window.onload=on_doc_load; } catch (e) {}
	try { document.onload=on_doc_load; } catch (e) {}
	try { this.onload=on_doc_load; } catch (e) {}
	function on_doc_load() { 
		try { /* window.focus(); */ forms[0].all[0].select(); } catch (e) { } 
		try { document.focus(); forms[0].all[0].select(); } catch (e) { } 
		try { formdata.focus(); formdata.all[0].select(); } catch (e) { } 
	}
}

// place error message in the window status bar
function on_page_error() { 
	try { 
		var a, s = 'Error!'; window.status = s;
		for(arg=0;arg<argLength;arg++) {
			a = on_page_error.arguments[arg];
			try { s+=' '+a.message; } 
			catch(e) { try { s+=' '+a; } catch(es){} }
		}
		window.status = s.replace('\n',' ').replace('\r',' ').replace('\t',' ')
	}catch(ee){} 
}

try { document.on_error = on_page_error; } catch(e) {}
try { document.onerror = on_page_error; } catch(e) {}
try { window.on_error = on_page_error; } catch(e) {}
try { window.onerror = on_page_error; } catch(e) {}