/**
	$Id: lib.js 59 2007-01-28 23:37:11Z christian $
**/


/** Is the browser IE **/
function isIE()
{
	return navigator.userAgent.toLowerCase().indexOf('msie') + 1;
}

function remove(s, t) {
	/*
	**  Remove all occurrences of a token in a string
  	**    s  string to be processed
  	**    t  token to be removed
  	**  returns new string
  	*/
  	i = s.indexOf(t);
  	r = "";
  	if (i == -1) return s;
  		r += s.substring(0,i) + remove(s.substring(i + t.length), t);
  	return r;
}


var textNumber = 1;
function addTextBox(form, afterElement) {
  // Increment the textbox number
  textNumber++;
  // Create the label
  var label = document.createElement("div");
    // Create the textbox
  var textField = document.createElement("input");
  textField.setAttribute("type","file");
  textField.setAttribute("name","userfiles[]");
  textField.setAttribute("size","40");
  textField.setAttribute("id","fil"+textNumber);
  // Add the label's text
  label.appendChild(document.createTextNode("Fil #"+textNumber+": "));
  // Put the textbox inside
  label.appendChild(textField);
  // Add it all to the form
  form.insertBefore(label,afterElement);
  return false;
}
function removeTextBox(form) {
  if (textNumber > 1) { // If there's more than one text box
    // Remove the last one added
    form.removeChild(document.getElementById("fil"+textNumber).parentNode);
    textNumber--;
  }
}


function toggleTop( target )
{
	var tabs = getElementsByClass( 'topTab' );
	for( i = 0; i < tabs.length; i++ )
	{
		if( tabs[i].id == 'fane_'+ target )
		{
			tabs[i].className="topTabCurrent"
			tabs[i].style.backgroundImage='url(/images/layout/tabs/'+tabs[i].getAttribute('id')+'.gif)';
		}
	}
}


/** Make a modal window **/
function modalVindue( sURL, sName, wHeight, wWidth )
{
	if( isIE() > 0 )
		window.showModalDialog( sURL,'','dialogHeight:'+wHeight+'px; dialogWidth:'+wWidth+'px;');
	else
	{
		leftVal = ( screen.width - wWidth ) / 2;
		topVal  = ( screen.height - wHeight) / 2;
		window.open(sURL, sName, 'modal=yes, left='+leftVal+'px, top='+topVal+'px, height='+wHeight+'px,width='+wWidth+'px, menubar=no, toolbar=no, status=no, resizable=no');
	}
}
/** Toggle show/view an element **/
function showView( target ) { x = document.getElementById( target ); if( HasClass( x, 'show' ) ) ReplaceClass( x, 'show', 'hidden'); else ReplaceClass( x, 'hidden', 'show'); }

/** Make a timer **/
function makeTimer( method, delay ) { window.setTimeout( method, delay); }

/** Adds a new class to an object, preserving existing classes **/
function AddClass(obj,cName){ KillClass(obj,cName); return obj && (obj.className+=(obj.className.length>0?' ':'')+cName); }

/** Removes a particular class from an object, preserving other existing classes. **/
function KillClass(obj,cName){ return obj && (obj.className=obj.className.replace(new RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),'')); }

/** Returns true if the object has the class assigned, false otherwise **/
function HasClass(obj,cName){ return (!obj || !obj.className)?false:(new RegExp("\\b"+cName+"\\b")).test(obj.className) }

/** Replace a class with another **/
function ReplaceClass(obj,oldName,newName) { KillClass(obj,oldName); AddClass(obj,newName); }

/** Change selected index in a dropdown **/
function dropdownChangeSelectedByInnerHTML( eid, findValue )
{
	object = document.getElementById( eid );
	length = object.options.length;
	for( i = 0; i < length; i++ )
	{
		optionData = object.options[i].innerHTML;
		if( optionData == null) continue;
		if( optionData == findValue)
			object.value = i;
	}
}

/** Change selected index in a dropdown **/
function dropdownChangeSelectedByValue( eid, findValue )
{
	document.getElementById( eid ).value = findValue;
}

/** Find the value of the selected option **/
function dropdownGetSelectedValue( destID )
{
	var obj 	= document.getElementById( destID );
	var index 	= obj.selectedIndex;
	if( index < 0 ) return index;	// dropdown er tom

	var data 	= obj.options[ index ].value;
	return data;

}
/** Find the value of the selected option **/
function dropdownGetSelectedValueFromObj( obj )
{
	var index 	= obj.selectedIndex;
	if( index < 0 ) return index;	// dropdown er tom

	var data 	= obj.options[ index ].value;
	return data;
}

/** Find all elements with a given class name */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	var i = 0;

	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/** Do we have IE **/
function isIE() { return navigator.userAgent.toLowerCase().indexOf('msie') + 1; }

/** Make window **/
var winStack = [];
function showWindow( sURL, sName, wHeight, wWidth )
{
	var leftVal = Math.round( ( screen.width - wWidth ) / 2 );
	var topVal  = Math.round( ( screen.height - wHeight) / 2 );


	var winExist = null;
	// find out if the window exists
	for (var i=0;i<winStack.length;i++) {
		if (winStack[i].name == sName) winExist = i;
	}

	// if the window does exist, check if it is still open
	if (winExist!=null) {
	 	if (winStack[winExist].win&&!winStack[winExist].win.closed) {
			//the window has been used, and it is still open
			winStack[winExist].win.location = dest;
			winStack[winExist].win.focus();
			return winStack[winExist];
		} else {
			// the window has existed but is now closed so create a new window but assign it to the variable it used last time
			winStack[winExist].win = window.open(sURL, sName, 'left='+leftVal+'px, top='+topVal+'px, height='+wHeight+'px, width='+wWidth+'px, menubar=no, toolbar=no, status=0, resizable=yes, scroll=yes, scrollbars=yes');
			winStack[winExist].name = sName;
			winStack[winExist].win.focus();
			return winStack[winExist];
		}
	} else {
		// the window has never existed, so make a new entry in the stack
		var winIndex = winStack.length-1;
		winStack[winIndex] = {win:null,name:null }
		winStack[winIndex].win = window.open(sURL, sName, 'left='+leftVal+'px, top='+topVal+'px, height='+wHeight+'px, width='+wWidth+'px, menubar=no, toolbar=no, status=0, resizable=yes, scroll=yes, scrollbars=yes');
		winStack[winIndex].name = sName;
		winStack[winIndex].win.focus();
		return winStack[winIndex];
	}
}

function trim(str) { return str.replace(/^(\s+)?(\S*)(\s+)?$/, '$2'); 	}
function ltrim(str){ return str.replace(/^\s*/, '');					}
function rtrim(str){ return str.replace(/\s*$/, '');					}

var current = 1;
function changeFan(id)
{
	if(!document.getElementById(id)) return;
	document.getElementById('content_'+current).className  = 'contentHide';
	document.getElementById('content_'+id).className  = 'content';
	current = id;
}

function toggleLayer( id )
{
    obj = document.getElementById('sub_'+ id);
	if( HasClass( obj, 'hiddenSub' ) ) {
		ReplaceClass(obj, 'hiddenSub','showSub');
		obj.style.display = '';
		document.getElementById(id).src='/images/n.gif';
	} else {
		obj.style.display = 'none';
		ReplaceClass(obj, 'showSub','hiddenSub');
		document.getElementById(id).src='/images/plusik.gif';
	}
}

function emptyDropdown( obj )
{
	obj.options.length = 0;
}
