// $Id: misc.js,v 1.8 2006/11/29 10:56:35 tkepski Exp $

function PopupWindow(url,width,height,name){
    if (name==null){
      name = new String(Math.round(Math.random()*10000000));
    }

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

    var win = window.open(url,name,"toolbar=no,location=no,directories=no,"+
      "status=no,menubar=no,scrollbars=no,resizable=no,"+
      "copyhistory=no, width="+width+",height="+height+",left="+left+",top="+top);
     if (win) win.focus();
}

function popup(title,width,height)
{
	if (!width) width = 350;
	if (!height) height = 450;

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

    var win = window.open('/popup.xml?' + title,'popup',"toolbar=no,location=no,directories=no,"+
      "status=no,menubar=no,scrollbars=yes,resizable=yes,"+
      "copyhistory=no, width="+width+",height="+height+",left="+left+",top="+top);
     if (win) win.focus();
}

function PopupImage(url,width,height,title,name)
{
    if (name==null){
      name = new String(Math.round(Math.random()*10000000));
    }
    var windowheight = height+40;
    var windowwidth = width+20;

    var left = (screen.width - windowwidth)/2;
    var top = (screen.height - windowheight)/2 - 18;
    
    var scrollbars = "no";
    
    if (left<0) { scrollbars = "yes"; left = 32; windowwidth = screen.width - 2*left; }
    if (top<0) { scrollbars = "yes"; top = 32; windowheight =  screen.height - 2*top - 18; top = top - 18; }

    var win = window.open("",name,"toolbar=no,location=no,directories=no,"+
        "status=no,menubar=no,scrollbars=" + scrollbars + ",resizable=no,"+
        "copyhistory=no, width="+windowwidth+",height="+windowheight+",left=" + left + ",top=" + top);
    win.document.write("\<HEAD\>\<link rel=\"stylesheet\" type=\"text\/css\" href=\"\/style.css\"\/\>\<meta http-equiv=\"pragma\" content=\"no-cache\"\>\<TITLE\>"+title+"\<\/TITLE\>");
    win.document.write("\<link rel=\"STYLESHEET\" type=\"text/css\" href=\"css/style.css\"\>");
    win.document.write("\<\/HEAD\>");
    win.document.write("\<BODY topmargin=0 leftmargin=0\>\<center\>\<img src=\""+url+"\" width=\""+width+"\" height=\""+height+"\" border=0 vspace=\"10\" hspace=\"10\"\>\<BR\>\<a href=\"javascript:window.close();\" class=\"popup\"\>Close\<\/a\>\<\/center\>\<\/BODY\><\/HTML\>");
}

function jmShowHelp(url, w, h)
{
    var width  = (w==null) ? 750 : w;
    var height = (h==null) ? 580 : h;
    var left   = (screen.width - width)/2;
    var top    = (screen.height - height)/2

    var win = window.open(url,null,"toolbar=no,location=no,directories=no,"+
      "status=no,menubar=no,scrollbars=yes,resizable=yes,"+
      "copyhistory=no, width="+width+",height="+height+",left=" + left + ",top=" + top);
   
    return false;
}

function addOnFocus(){
	allNodes = document.getElementsByClassName("hidedefault");
	for(i = 0; i < allNodes.length; i++) {
		var elem = allNodes[i];
		elem.defaultText = elem.value;
		addEvent(elem, 'focus', clearDefault, false);
	    addEvent(elem, 'blur', restoreDefault, false);
	}
}

function clearDefault(e) {
	var info = document.getElementById("info");
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!(target && target.value == target.defaultText)) return;
    
    if (target.name != "password") {
        target.value = '';
        return;
    }

	if (window.event){ //IE CODE
		var _parent = target.parentNode;
		_parent.removeChild( target );
	    _parent.innerHTML = '<input type="password" name="' + target.getAttribute('name') + '" id="' + target.id + '" class="' + target.className + '" onFocus="clearDefault(this)" onBlur="restoreDefault(this)"/>';

		_target = document.getElementById("password");
		_target.defaultText = target.defaultText;
		_target.focus();
		_target.select();
		target = null;  // remove any reference to the old element 
		return;
	}
	target.value = "";
	target.type="password";

}

function restoreDefault(e) {
	var info = document.getElementById("info");
    var target = window.event ? window.event.srcElement : e ? e.target : null;

    if (!(target && target.value == '' && target.defaultText)) return;

    if (target.name != "password") {
        target.value = target.defaultText;
	    return;
    }
	if (window.event){ //IE CODE
		var _parent = target.parentNode;
		_parent.removeChild( target );
	    _parent.innerHTML = '<input type="text" name="' + target.getAttribute('name') + '" value="' + target.defaultText + '" id="' + target.id + '" class="' + target.className + '" onFocus="clearDefault(this)" onBlur="restoreDefault(this)"/>';
		document.getElementById("password").defaultText = target.defaultText;
		target = null;
		return;
	}
	target.type="text";
	target.value = target.defaultText;
}

function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}
function extendMenu(){
	var menu = document.getElementById("menu");
	if (menu==null) return;
	menu.style.height = (Element.getHeight("contentContainer") -  parseInt(Element.getStyle("menu", "padding-bottom")))+ "px";
}

addEvent(window, 'load', addOnFocus, false);
addEvent(window, 'load', extendMenu, false);

