//Basic Object Manipulation Functions
//--------------------------------------
var ns4 = document.layers;
var op5 = (navigator.userAgent.indexOf("Opera 5")!=-1) 
	||(navigator.userAgent.indexOf("Opera/5")!=-1);
var op6 = (navigator.userAgent.indexOf("Opera 6")!=-1) 
	||(navigator.userAgent.indexOf("Opera/6")!=-1);
var agt=navigator.userAgent.toLowerCase();
var mac = (agt.indexOf("mac")!=-1);
var ie = (agt.indexOf("msie") != -1); 
var mac_ie = mac && ie;


function getElem(id){
	return document.getElementById(id);
}

function getObject(id){
	// If given parameter is an object, then return it, if it's ID, 
	// then return object associated with that ID
	if (typeof(id) == "object"){
		return id;
	}
	else{
		return getElem(id);
	}
}

function getElemID(id){
	return (getObject(id).id) ? getObject(id).id : "";
}

function getElemClass(id){
	return (getObject(id).className) ? getObject(id).className : "";
}

function getElemStyle(id){
	return (getObject(id).style) ? getObject(id).style : "";
}

function show(id){
	getElemStyle(id).visibility = 'visible';
	getElemStyle(id).position ='static';
}

function hide(id){
	getElemStyle(id).visibility = 'hidden';
	getElemStyle(id).position ='absolute';	
	getElemStyle(id).left = "-1000px";
	getElemStyle(id).top = "-1000px";
}

function isHidden(id){
	if (getElemStyle(id).visibility == 'hidden') return true;
	else return false;
}

function toggleDiv(id){
	if (getElemStyle(id).display == '') getElemStyle(id).display = 'none';
	else getElemStyle(id).display = '';
}

function swapClass(id, klase1, klase2){
	if (klase1 == getElemClass(id)){
		getElem(id).className = klase2;
	}
	else{
		getElem(id).className = klase1;
	}
}

function getElemHeight(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

function getElemWidth(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}

function addEvent( obj, type, fn ) { 
  if ( obj.attachEvent ) { 
    obj['e'+type+fn] = fn; 
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
    obj.attachEvent( 'on'+type, obj[type+fn] ); 
  } else 
    obj.addEventListener( type, fn, false ); 
} 
function removeEvent( obj, type, fn ) { 
  if ( obj.detachEvent ) { 
    obj.detachEvent( 'on'+type, obj[type+fn] ); 
    obj[type+fn] = null; 
  } else 
    obj.removeEventListener( type, fn, false ); 
} 


function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	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;
}

function pad(Num, pad) { // pad is zeros of desired width
	var str = Num.toString().split(/\./);
	if(!str[1]) str[1]='0';
	while (str[1].length<pad)
		str[1] = str[1]+'0';
	return str[0]+'.'+str[1];
  }
function FormatFloat(pFloat, pDp){
    var m = Math.pow(10, pDp);
    return pad(parseInt(pFloat * m, 10) / m, 2);
}

//-----------------------------------------------------------
//-------------- Created by Viktop (c) 2006 08 23 -----------
//--------- Formats given number by various options ---------
//-------If you use it, please leave this message here ------
//-----------------------------------------------------------
function format_number(number, decimals, decpoint, thousandsep){
	// Nustatymai pagal nutylejima
	if (isNaN(decimals)) decimals = 2;
	if (isNaN(decpoint)) decpoint = ",";
	if (isNaN(thousandsep)) thousandsep = ".";
	if (typeof(number) != "number")
		number = number * 1;

	// Sveika dalis
	fl = Math.floor(number);
	
	// Sudeliojami tukstanciu skirtukai po kas trecio skaitmens
	num = fl;
	var parts = new Array();
	num += '';
	/*Dalinam skaiciu kaip stringa, kadangi tai a)greciau b)korektiskai dalinama */
	while (num.length>3){
		parts[parts.length] = num.substr(num.length-3,3);
		num = num.substr(0,num.length-3);
	}
	parts.push(num);
	
	var joined = "";
	for (i=parts.length-1;i>-1;i--){
		joined += parts[i]+thousandsep;
	}
	
	joined=joined.substring(0,joined.length-1);
	
	if (decimals>0){
		// Trupmenine dalis
		power = Math.pow(10,decimals);
		decpart=Math.round(power*(number - fl));
		//decpart=parseFloat(number) - parseInt(number); // somewhy doesn't work
		// Trupmena int->string
		decpart+='';
		// Pridedama nuliu i gala tiek, kiek truksta
		while (decpart.length<decimals){
			decpart += '0';
		}
		// Prie sveikos dalies prilipdoma desimtaine dalis
		joined=joined+decpoint+decpart;		
	}	
	
	return joined;
}