var g_SiteRoot;
function trackJsError() {
	window.onerror = logError;
}

function logError(msg, url, lno) {
	if (document.forms.length > 0) { 
		eVar = document.forms[0]['__JS_ERROR'];
		eVar.value = msg + ", " + url + ", " + lno;
	}
	return true;
}

function getObjAbsolutePos(obj)
{
	x = obj.offsetLeft;
	//IE fix for center or right aligned spans

	if ((obj.tagName == 'SPAN') && browser.isIE) {
		for (i=0; i<obj.children.length; i++) {
			x = Math.min(x, obj.children[i].offsetLeft)
		}
//		window.status = obj.children[0].innerText
		//x = Math.min(x, obj.lastChild.offsetLeft)
	}

	y = obj.offsetTop;
	parentObj = obj.offsetParent;
	while (parentObj.tagName != 'BODY')
	{
		x += parentObj.offsetLeft;
		y += parentObj.offsetTop;
		parentObj = parentObj.offsetParent;
	}
	return new Point(x, y);
}
function Point(x, y) {
	this.x = x;
	this.y = y;
}

function Pair(a, b) {
	this.first = a;
	this.second = b;
}

function Triplet(a, b, c) {
	this.first = a;
	this.second = b;
	this.third = c;
}

function Void() {
}
function ReplaceNode(oldNode,newNode){
	oldNode.parentNode.insertBefore(newNode,oldNode);
	oldNode.parentNode.removeChild(oldNode);
}
function GetEventKey(e) {
	if (!e) e = window.event
	return e.keyCode
}

function WriteLayer(ID, sText) {
 	if (document.layers) {
		var oLayer;
		o = document.layers[ID]
		if(o) {
			o.document.open();
			o.document.write(sText);
			o.document.close();
		}
	} else if (parseInt(navigator.appVersion)>=5&&navigator.appName=="Netscape") {
		o=document.getElementById(ID)
		if(o) o.innerHTML = sText;
	} else if (document.all) {
 		o=document.all[ID]
		if(o) o.innerHTML = sText;
	}
}

function PrintFrame(window, id) {
	w = GetFrame(window, id)
	if (w) {
		w.focus()
		w.print()
	}
}
function addHidden(name,value){
	var h=document.createElement("input");
	h.type='hidden';
	h.value=(value!=null) ? value : '';
	h.name=name;
	h.id=name;
	document.forms[0].insertBefore(h,document.forms[0].firstChild);
}

function GetFrame (window, id) {
	if (window.document.frames) {
		return window.document.frames[id]
	} else if (window.frames) {
		return window.frames[id]
	} else
		return null;
}

function getObjById(name)
{
	_obj = null;
	if (document.getElementById)
  	{
  		_obj = document.getElementById(name);
  	}
  	else if (document.all)
  	{
		_obj = document.all[name];
  	}
  	else if (document.layers)
  	{
   		_obj = document.layers[name];
  	}
  	return _obj;
}

function getChildById(parentobj, childName)
{
	_obj = null;
	if (parentobj.getElementById)	{
  		_obj = parentobj.getElementById(childName);
  	}
  	else if (parentobj.all) {
		alert(childName)
  		for (i=0; i<parentobj.all.length; i++) {
  			if (parentobj.all[i].id == childName) {
  				_obj = parentobj.all[i];
  				break;
  			}
  		}
  	} else {
		obj = SearchNodes(parentobj, childName)  	
		if (obj != null) {
			window.status='ok'	
			_obj = obj;
		}
  	}
  	return _obj;
}

function SearchNodes(node, childId) 
{
	var i;
	var obj;
	for (i=0; i<node.childNodes.length;i++) {
//		alert(nodeList[i].tagName + ', ' + nodeList[i].id + ', looking for: ' + nodeId + ', i=' + i + ', ' + nodeList.length+ ' nodes')
		if (node.childNodes[i].id == childId) {
			return node.childNodes[i];
		} else if (node.childNodes[i].childNodes.length > 0) {
			obj = SearchNodes(node.childNodes[i], childId);
			if (obj != null)
				return obj;
		}
	}
	return null
}
function PopUp(URL, Title, Width, Height, params)
{
	var pre = "toolbar=no,scrollbars=yes,resizable=yes,width=" + Width + ",height=" + Height;
	var pre = pre.split(',');
	var parammap = new Object();
	for(i=0;i<pre.length;i++) {
		var p=pre[i].split('=');
		parammap[p[0]]=p[1];
	}
	if (params!=null) {
		params = params.split(',');
		for(i=0;i<params.length;i++) {
			p=params[i].split('=');
			parammap[p[0]]=p[1];
		}
	};
	var wparams='';
	for(p in parammap) {
		if(wparams.length>0)
			wparams+=',';
		wparams+=p+'='+parammap[p];
	}
	w = window.open(URL , Title, wparams);
	w.focus();
}

function MainPopUp(URL, Title, Width, Height)
{
	var sParams = "status=yes,toolbar=no,scrollbars=no,resizable=yes,width=" + Width + ",height=" + Height;
	URL = escape(URL)
	while(URL.indexOf('/') >= 0) 
		URL = URL.replace('/','%2F')
	mainUrl = g_SiteRoot + 'main.aspx?url=' + URL
	w = window.open(mainUrl , Title, sParams);
	w.focus();
}

function FrameNavigate(frame, url) {
	if (frame == null)
		frame = window;
	frame.location.href = url;
}

/**
* Performs menu state update according to specified menu identifier.
* The apropriate menu branch is shown.
* @author hab
*/

function updateMenu(menuId) {
	if (menuId!='') {
		if (top.menu != null)
			if (top.menu.RollDown != null)
				top.menu.RollDown(menuId);
	}
}

/**
*
* Opens help window and sets the help page. 
* @author hab
*/
function showHelp() {
	linkObj = document.getElementById('helpLink');
	helpURL = g_SiteRoot + "help/index.html"
//	alert(helpURL);
	newWnd = window.open(helpURL,"helpWindow","height=600,width=650,addressbar,resizable");
	setTimeout("newWnd.focus()", 500);
	if (linkObj!=null) {
		link = linkObj.value;
		if (link!=''){
			setTimeout("newWnd.loadHelpPage('" + link + "')", 500);
		}
	} 
}

/**	SIDropList **/
var tmpBLOrigMDown
var tmpBLCurObj
function ButtonList(obj) {
	if (document.onmousedown)
		document.onmousedown()
	tmpBLOrigMDown = document.onmousedown;
	tmpBLCurObj = obj.getElementsByTagName('DIV')[0];
	tmpBLCurObj.style.display = 'block';
	document.onmousedown = ButtonListMouseDown;
	tmpBLCurObj.firstChild.focus();
}
function ButtonListMouseDown(evt) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
    	var elem = (evt.target) ? evt.target : 
        	((evt.srcElement) ? evt.srcElement : null);
	    if (elem ) {
	    	if (elem.tagName== 'OPTION')
	    	  elem = elem.parentNode
    	    // perform all event processing here
			if (tmpBLCurObj) {
    	    	if (elem.parentNode == tmpBLCurObj.parentNode)			
    	    		return;
	    		if (elem.id == tmpBLCurObj.firstChild.id)
    				return;
    			if (elem.id == tmpBLCurObj.firstChild.id + "_btn")
    	    		return;
			}
	    }
	}
	if (tmpBLCurObj) 
		ButtonListHide();
}
function ButtonListEvent(sender) {
	val=''
	ButtonListHide();
	if (sender) {
		if (sender.selectedIndex >=0) {
			val = sender.options[sender.selectedIndex].value;
			s = String(val)
			if ((s==null) || (s==''))
				logError('list data invalid', window.location.href, sender.id)
			__doPostBack(sender.name,s)
		}
	}
}
function ButtonListKey(sender,evt) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
		var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
 	   	if (charCode == 13) 
			ButtonListEvent(sender)
	}
}

function ButtonListHide() {
	document.onmousedown = tmpBLOrigMDown;
	if (tmpBLCurObj)
		tmpBLCurObj.style.display = 'none';
}


/******************************************************************
*	SIEditableCombo
******************************************************************/
var tmpECCurObj = null;
var tmpECOrigMDown = null;

/* documentMouseDown */
function EditableComboMouseDown(evt) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
    	var elem = (evt.target) ? evt.target : 
        	((evt.srcElement) ? evt.srcElement : null);
	    if (elem ) {
	    	if (elem.tagName== 'OPTION')
				elem = elem.parentNode
			if (tmpECCurObj) {
    	    	if (elem.parentNode.parentNode == tmpECCurObj.parentNode.parentNode)			
    	    		return;
    	    	if (elem.parentNode == tmpECCurObj.parentNode.parentNode)			
    	    		return;
			}
	    }
	}
	EditableComboHideList();
}
/* showList*/
function EditableComboShowList(obj) {
	if (obj.firstChild)
		current = obj.firstChild.firstChild;
	else
		current = obj.children[0].children[0];	//this for buggy crappy IE
				
	if (tmpECCurObj)
		if (tmpECCurObj.id != current.id)
			EditableComboHideList()

	if (document.onmousedown) 
		document.onmousedown()
	tmpECOrigMDown = document.onmousedown;
			
	if (current) {
		if (obj.style.width != '')
			current.style.width = obj.style.width;
		else
			current.style.width = obj.offsetWidth + 'px';	
		current.parentNode.style.display = 'block';
		document.onmousedown = EditableComboMouseDown;
		current.focus();
		tmpECCurObj = current;
	}
}
/* hideList */
function EditableComboHideList() {
	if (tmpECCurObj != null) {
		tmpECCurObj.parentNode.style.display='none'
		document.onmousedown = tmpECOrigMDown;
		tmpECCurObj = null;
	}
}
/* processKey */
function EditableComboProcessKey(evt,elem,postback) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
		var charCode = (evt.charCode) ? evt.charCode : 
    	    	((evt.which) ? evt.which : evt.keyCode);

 	   	if (charCode == 40 )  {
			EditableComboShowList(elem.parentNode);
		}
 	   	else if (charCode == 13) {
			__doPostBack(elem.name,'')
		}
	}
}
/* copyValue */
function EditableComboCopyValue(sender,doPostBack) {
	elem = sender.parentNode.nextSibling;
	if (sender) {
		if (sender.selectedIndex >=0) {
			elem.value = sender.options[sender.selectedIndex].firstChild.nodeValue;
		}
	}
	EditableComboHideList();
	if (doPostBack)
		__doPostBack(sender.name,'')
}

/**
* Processes enter key press by copying the calling option element value to the target input.
* @param evt An event to be processed
  @param elem Calling element 
  @param target A target input
*/
function EditableComboProcessEnter(evt,sender,doPostBack) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
		var charCode = (evt.charCode) ? evt.charCode : 
    	    	((evt.which) ? evt.which : evt.keyCode);
		//targetObj = getObjById(objName);
 		elem = sender.parentNode.nextSibling;
	   	if (charCode == 13 )  {
 	   		
 	   		EditableComboCopyValue(sender,doPostBack)
			if(elem) elem.focus();
		} else if (charCode == 27 )  {
			EditableComboHideList();
			if(elem) elem.focus();
		}
	}
	return true;
}

/**
* Performs java version checking based on information provided by applet. 
* If JVM version is to low displays error status information and hides applet.
  @param applet Applet Id
  @param appletContainer Applet's container Id
  @param elementsToShow list ( ; separated) of elements' IDs to show (include error info here)
  @param elementsToHide list ( ; separated) of elements' IDs to hide (include applet here)
*/
function checkJava(applet, appletContainer, elementsToShow , elementsToHide) {

	//window.onerror = "JavaFailed('" + elementsToShow + "','" + elementsToHide + "');return 1;"
	appletRef = eval('document.'+applet);
	container = getObjById(appletContainer);
	if (appletRef==null) {
		if (tryouts < 10) {
			tryouts++;
			setTimeout("checkJava('" + applet +"','" + elementsToShow + "','"+elementsToHide+ "')",2000);
		} else {
			JavaFailed(elementsToShow , elementsToHide)
			container.style.visibility = "hidden";
		}
	} else {
		
		jVersion = 0;
		try {
			jVersion = appletRef.getJava();
			required = appletRef.getRequiredJava() ;
		
			if (jVersion >= required) {
				container.style.visibility = "visible";
				repaintApplet(applet)
			} else {
				JavaFailed(elementsToShow , elementsToHide)
				container.style.visibility = "hidden";
			}
		}
		catch(e) {
			JavaFailed(elementsToShow , elementsToHide)
			container.style.visibility = "hidden";
		}
	}
}

function repaintApplet(applet)
{
	appletRef = eval('document.'+applet);
	if (appletRef!=null) {
		appletRef.repaint();
		//alert('redrawing');
	}
	isResizing= false;
}

function JavaFailed(elementsToShow , elementsToHide) {
	show = elementsToShow.split(';');
	for (i=0; i<show.length; i++) {
		obj = getObjById(show[i]);
		if (obj != null)
			obj.style.display = "block";
	}
	hide = elementsToHide.split(';');
	for (i=0; i<hide.length; i++) {
		obj = getObjById(hide[i]);
		if (obj != null)
			obj.style.display = "none";
	}
}

/**
 * Function shows the context help for the specified topic.
 * @param id Resource identifier of the help
 * @author Pawel Milczynski
 */
function ShowContextHelp(sFilePath, nResId)
{
	var nWidth = 300;
	var nHeight = 300;
	var sParams = "toolbar=no,scrollbars=yes,resizable=yes,width=" + nWidth + ",height=" + nHeight;
	newWnd = window.open(sFilePath + "?id=" + nResId, "context_help_window", sParams);
	setTimeout("newWnd.focus()", 500);
}

function CopyTextToTranslation(sSrc, sTarget)
{
	var oSrcObject = getObjById(sSrc);
	var oTargetObject = getObjById(sTarget);
	//oTargetObject.value = oSrcObject.innerText;
	oTargetObject.value = getInnerText(oSrcObject);
}
/**
* Gecko compilant innerText counterparts.
* As NN and Mozilla does not support innerText attribute. 
* These methods perform the necessary taskts
*/
/*
* Sets element's innerText to passed in the second argument.
*/
function setInnerText(element,text) {
	element.removeChild(element.firstChild);
	var newText=document.createTextNode(text);
	element.appendChild(newText);
}
/**
* For nodes which do not contain text or have non text child nodes 
* the behaviour is undefined.
*/
function getInnerText(elem) {
	return elem.firstChild.data;
}

function ShowPreview(sInputObjId, sPreviewObjId, sSpanObjId, sNoPreview)
{
	var img = document.getElementById(sPreviewObjId);
	var oSpan = document.getElementById(sSpanObjId);
	var file = document.getElementById(sInputObjId);
	var parts = file.value.split('.');
	var extension = '';
	if (parts.length>0)
	{
		extension = parts[parts.length - 1];
	}
	else
	{
		extension = '';
	}
	if (extension == 'jpg' || extension == 'jpeg' ||extension == 'gif' || extension == 'png')
	{
		img.src = file.value;
		img.style.display = 'inline';
		oSpan.style.display = 'none';
	}
	else
	{
		img.style.display = 'none';
		oSpan.innerText = sNoPreview;
		oSpan.style.display = 'inline';
	}
}
function resetMenu() {
	setTimeout("if(top.menu) if(top.menu.unselect_item) top.menu.unselect_item(0)", 100);
	
}

function ShowListBoxInfo(obj) {
	if (browser.isIE) {
		obj.previousSibling.firstChild.innerText = obj.options[obj.selectedIndex].text
		obj.previousSibling.firstChild.style.display='block'
		w = obj.previousSibling.firstChild.offsetWidth - obj.scrollWidth
		if (obj.options.length > obj.size)
			w+=20
		if (w<0)
			obj.previousSibling.firstChild.style.display='none'
	}
}

function HideListBoxInfo(obj) {
	if (browser.isIE) {
		obj.previousSibling.firstChild.style.display='none'
	}
}

function ClientWait(waitInfo, hidden) {
	wait = getObjById(waitInfo);
	if (wait) wait.style.display='block';
	for (i=0; i<hidden.length; i++) {
		obj = getObjById(hidden[i]);
		if (obj) obj.style.display='none';
	}
	return true;
}

var gCurrentBodyOnClick=null;
var gMenuOpened;
function openDynamicMenu(id){
	var menuObj = document.getElementById(id)
	if (menuObj) {
		if (document.body.onclick!=null) document.body.onclick()
		gCurrentBodyOnClick = document.body.onclick
		menuObj.style.display='inline'
		document.body.onclick = function(){closeDynamicMenu(id)}
		menuObj.onclick=function(){closeDynamicMenu(id)}
		gMenuOpened=true;
	}
}
function closeDynamicMenu(id) {
	var menuObj = document.getElementById(id)
	if (menuObj) {
		if (gMenuOpened)
			gMenuOpened=false;
		else {		
			document.body.onclick = gCurrentBodyOnClick;
			menuObj.style.display='none'
			menuObj.onclick=null;
		}
	}
}
function StringTrim(s) {
	if (typeof s != "string") 
		return s;
	var rx=/ /;
	rx=rx.compile('^[\\s' + unescape('%A0') + ']*([^\\s' + unescape('%A0') + ']*)[' + unescape('%A0') + '\\s]*$')
	return s.replace(rx,"$1");
}
var g_FixScrollObjId
function FixScroll(id) {
	if (typeof(id)=='string') {
		g_FixScrollObjId=id;
		if (!browser.isIE){
			window.setTimeout(FixScroll, 50);
		}
	}
	else	
		id=g_FixScrollObjId;
	var obj=getObjById(id);
	var op=obj.offsetParent;
	var cw=op.offsetWidth;
	var ch=op.offsetHeight;
	obj.style.display='none';
	if (!browser.isIE) {
		obj.style.height='10px';
		document.body.style.height='100%'
	}
	var rw=op.offsetWidth;
	var rh=op.offsetHeight;
	obj.style.display='block';
	var d=cw-rw;
	if (d>0) 
		obj.style.width=rw + 'px';
	if (!browser.isIE) {
		obj.style.height=(rh-20)+'px';
		obj.style.width=(rw-20)+'px';
		window.onresize = FixScroll;
	}
}