var xVersion='4.0',xOp7Up,xOp6Dn,xIE4Up,xIE4,xIE5,xNN4,xUA=navigator.userAgent.toLowerCase();
if(window.opera){
	var i=xUA.indexOf('opera');
	if(i!=-1){
		var v=parseInt(xUA.charAt(i+6));
		xOp7Up=(v>=7);
		xOp6Dn=(v<7);
	}
}else if(navigator.vendor!='KDE' && document.all && xUA.indexOf('msie')!=-1) {
	xIE4Up=parseFloat(navigator.appVersion)>=4;
	xIE4=xUA.indexOf('msie 4')!=-1;
	xIE5=xUA.indexOf('msie 5')!=-1;
}else if(document.layers){
	xNN4=true;
}
xMac=xUA.indexOf('mac')!=-1;


var activeFocusValue = null;

function emptyBox(elem) {
	activeFocusValue = elem.value;
	elem.value = '';
}

function setDefaultBox(elem) {
	if (elem.value == '') { elem.value = activeFocusValue; }
}

function new_window(url, width, height, scroll)
{	
	if (scroll == 0) {
		scrollbar = 'no';
	} else {
		scrollbar = 'yes';
	}

	var top = (screen.availHeight-height)/2;
	var left = (screen.availWidth-width)/2;

	goatwin = window.open(url, '', "status=0,height="+height+",width="+width+",top="+top+",left="+left+",location=0,resizeable=0,fullscreen=0, scrollbars=auto",true);

	return goatwin;
}

function newSaWindow(url, width, height, scroll)
{	
	if (scroll == 0) {
		scrollbar = 'no';
	} else {
		scrollbar = 'yes';
	}

	var top = (screen.availHeight-height)/2;
	var left = (screen.availWidth-width)/2;

	goatwin = window.open(url, '', "status=0,height="+height+",width="+width+",top="+top+",left="+left+",location=0, scrollbars="+scrollbar,true);
}

function showModal (pageUrl, title, dialogWidth, dialogHeight, debug )
{
	var oDialogInfo = new Object();
	oDialogInfo.Title = title;
	oDialogInfo.Page = pageUrl;
	oDialogInfo.Editor = window;

	if (window.showModalDialog && debug != 1) {
		win = window.showModalDialog( 'editor/dialog.html', oDialogInfo, "dialogWidth:" + dialogWidth + "px;dialogHeight:" + dialogHeight + "px;center:yes;help:no;scroll:no;status:no") ;
	} else {
		win = window.open(pageUrl , '','width='+dialogWidth+', height='+dialogHeight+', scrollbars=0');
	}
}

function pr(pole) {
	var text = '';
	for (var key in pole) {
		text += key + ' => ' + pole[key] + "<br>";
	}
	document.getElementById('jsprint').innerHTML += text;
} 

function setMyCookie(name, value, valid) {
	var today = new Date();
	var expire = new Date();

	expire.setTime(today.getTime() + 3600000*24*valid);
	document.cookie = name+"="+escape(value)+";expires="+expire.toGMTString()+"; path=/";
}

function readCookie(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 redirectToUrl(url) {
	window.location.replace(url);

	return false;
}

function setStyleProperty(id, property, value) {
	var elem = document.getElementById(id);
	if (elem != null) {
		var cmd = "elem.style."+property+" = '" + value + "';";
		eval(cmd);
	}
}

function myConfirm(text, elem) {
	var answer = confirm(text);
	if (answer)	{
		window.location.replace(elem.href);
	} else {
		return false;
	}
}

function nw (elem, width, height, scroll) {
	if (elem == undefined) { return false; }
	if (scroll == 0) {
		scrollbar = 'no';
	} else {
		scrollbar = 'auto';
	}

	var url = elem.href;
	var top = (screen.availHeight-height)/2;
	var left = (screen.availWidth-width)/2;

	goatwin = window.open(url, '', "status=0,height="+height+",width="+width+",top="+top+",left="+left+",location=0,resizeable=0,fullscreen=0, scrollbars=auto",true);
}

function myNW(myLink) {
	var pref  = document.URL.replace(/([^\/]+)$/g, "");
	var url = myLink.href;
	url = url.slice(pref.length);
	var pos = url.indexOf('http');
	if (pos == -1) { url = 'http://' + url; }
	window.open(url, '_blank', "",true);
}

function isString() {
	if (typeof arguments[0] == 'string') return true;
	if (typeof arguments[0] == 'object') {  
		var criterion = arguments[0].constructor.toString().match(/string/i); 
 		return (criterion != null);  
	}
	return false;
}


function showImageDetail(detailSrc) {
	if (detailSrc.length > 0) {
		var today = new Date();
		var imgId = '';
		var verId = '';
		var imgIdArr = detailSrc.match(/\/([0-9]+)v/);
		var verIdArr = detailSrc.match(/\/[0-9]+v([0-9]+)_/);
		imgId = imgIdArr[1];
		verId = verIdArr[1];
		newSaWindow('/image.php?id='+today.getTime()+';id_image='+imgId+';id_version='+verId, 800, 600, 1);
	}	
}

function showOriginalDetail(imgId, verId) {
	newSaWindow('/image.php?id='+today.getTime()+';orig=1;id_image='+imgId+';id_version='+verId, 800, 600, 0);
}

function xDef(){
	for(var i=0; i<arguments.length; ++i){
		if(typeof(arguments[i])=='undefined') return false;
	}
	return true;
}

function xClientHeight()	{
	var h=0;
	if(xOp6Dn) { 
		h=window.innerHeight;
	} else if (document.compatMode == 'CSS1Compat' && !window.opera && document.documentElement && document.documentElement.clientHeight) {
		h=document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		h=document.body.clientHeight;
	} else if (xDef(window.innerWidth,window.innerHeight,document.width)) {
		h=window.innerHeight;
		if(document.width>window.innerWidth) h-=16;
	}

	return h;
}

function xClientWidth(){
	var w=0;
	if(xOp6Dn) w=window.innerWidth;
	else if(document.compatMode == 'CSS1Compat' && !window.opera && document.documentElement && document.documentElement.clientWidth)w=document.documentElement.clientWidth;
	else if(document.body && document.body.clientWidth)w=document.body.clientWidth;
	else if(xDef(window.innerWidth,window.innerHeight,document.height)) {
		w=window.innerWidth;
		if(document.height>window.innerHeight) w-=16;
	}
	return w;
}

function xScrollTop(){
	var offset=0;
	var w = window;
	if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
	else if(w.document.body && xDef(w.document.body.scrollTop)) offset=w.document.body.scrollTop;
	return offset;
}

function xScrollLeft(){
	var offset=0;
	var w = window;
	if(w.document.documentElement && w.document.documentElement.scrollLeft) offset=w.document.documentElement.scrollLeft;
	else if(w.document.body && xDef(w.document.body.scrollLeft)) offset=w.document.body.scrollLeft;
	return offset;
}