DOM = document.getElementById ? true : false;
NS4 = document.layers ? true : false;
IE = document.all ? true : false;

if (!document.getElementById) {
  if (document.all) {
    document.getElementById = function(id) {
      return document.all[id];
    }
  } else {
    document.getElementById = function(id) {
      var o = document.layers[id];
      if (o)
        return o;
      o = document.links[id];
      return o;
    }
  }
}

String.prototype.trim = function() {
  return this.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

var cmni_nocache = Math.random()*100000;
var loaded = false;

String.prototype.addsep = function(psep,pstr)
{
	return this+(this.length ? psep:'')+pstr;
}

String.prototype.replace_params = function(pwhat)
{
	for (i in pwhat) {
		eval('var '+pwhat[i]);
	}
	
	var pos1 = this.indexOf('%',0);
	var pos2 = 0;
	var i = 0;
	
	str = this.slice(pos2,pos1);
	
	while(pos1 != -1 && i < this.length)
	{
		pos2 = this.indexOf('%',pos1+1);
		
		if (pos2 != -1)
		{
			vkey = this.slice(pos1+1,pos2);
			eval('try { str += '+vkey+'; } catch(e) { str += this.slice(pos1,pos2--); }');
			pos1 = this.indexOf('%',pos2+1);
			if (pos1 != -1)
				str += this.slice(pos2+1,pos1);
			else
				str += this.slice(pos2+1);
		} else
		{
			str += this.slice(pos1);
			pos1 = -1;
		}
		i++;
	}
	return str;
}

String.prototype.urlencode = function()
{
	var ret = null;
	var i = 0;
	
	ret = "";
	for(i=0;i<this.length;$i++)
	{
		alert(this.charCodeAt(i));
		if (this.charAt(i) > 128)
		{
			//
		}
	}
	
}

String.prototype.replace_props = function(pwhat,debug)
{
	var pos1 = this.indexOf('%',0);
	var pos2 = 0;
	var i = 0;
	var val = null;
	
	if (pos1 == -1)
		return this;

	str = this.slice(pos2,pos1);
	
	while(pos1 != -1 && i < 1000)
	{
		pos2 = this.indexOf('%',pos1+1);
		if (pos2 != -1)
		{
			vkey = this.slice(pos1+1,pos2);
			if (vkey.substr(0,2) == '==')
			{
				//alert('#aaa#'.replace(/[#]/g,'pwhat.'));
				//alert('p:'+pwhat.disabled+':'+pwhat.Click);
				//alert('v:'+vkey.substr(2).replace(/[#]/g,'pwhat.'));
				eval('try { val = '+vkey.substr(2).replace(/[#]/g,'pwhat.')+'; } catch(e) { val = vkey; }');
				//alert('r:'+val);
				if (val != val)
					val = null;
			} else if (vkey.match(/^([a-z_][a-z0-9_]+)$/i))
			{	
				if (debug) alert(vkey);
				eval('try { val = pwhat.'+vkey+'; } catch(e) { val = null; }');
			} else val = null;
			if (val || val == '' || val == 0)
				str += val;
			else
				str += this.slice(pos1,pos2--);
			pos1 = this.indexOf('%',pos2+1);
			if (pos1 != -1)
				str += this.slice(pos2+1,pos1);
			else
			{
				str += this.slice(pos2+1);
			}
		} else
		{
			str += this.slice(pos1);
			pos1 = -1;
		}
		i++;
	}
	
	return str;
}


function odef(obj,def)
{
	var ret=null;
	if (!def && def != '' && def != 0)
		def = null;
	if (obj || obj==0)
		ret = obj;
	else
		ret = def;
	return ret;
}

SetVisibility = function(id, show) {
  var o = document.getElementById(id);
  if (!o)
    return false;
  var s = !o.style ? o : o.style;
  s.visibility = show ? (!o.style ? "show" : "visible") : (!o.style ? "hide" : "hidden");
  return true;
}

SetDisplay = function(id, show) {
  var o = document.getElementById(id);
  if (!o)
    return false;
  var s = !o.style ? o : o.style;
  s.display = show ? (!o.style ? "block" : (IE ? "block":"")) : (!o.style ? "none" : "none");
  return true;
}

/*
SetHidden(frm, elem, v)
{ 
	if (!frm || !elem)
		return false;
	frm.elements[elem].value = v;
	return true;
}
*/

function GetOffset(obj,uobj)
{
	if (!obj || !obj.offsetParent)
		return null;
	objLeft		= obj.offsetLeft;
	objTop		= obj.offsetTop;
	objParent	= obj.offsetParent;
	while(objParent && (!uobj || objParent != uobj)) {
		objLeft		+= objParent.offsetLeft;
		objTop		+= objParent.offsetTop;
		objParent	 = objParent.offsetParent;
	}
	return Array(objLeft, objTop);
}

// Attention!
//   if you move an absolute div, set the posx, posy property to reflect the new position

SelectRemover.selects = Array();
SelectRemover.objects = Array();
SelectRemover.process = function()
{
	if (!IE)
		return true;
	sdb = 0;
	ddb = 0;
	for(i in this.selects)
	{
		var item = this.selects[i];
		
		var conflict = false;
 		var robj = item.refobj;
 		
 		if (robj && robj.style.visiblity == 'hidden')
 			continue;

		itemw = item.offsetWidth;
		itemh = item.offsetHeight;
		if (robj)
		{
			item.posx = item.origx + robj.posx;
			item.posy = item.origy + robj.posy;
			if ((robj.posx + robj.offsetWidth) < (item.posx + itemw))
				itemw = robj.offsetWidth - item.posx;
			if ((robj.posy + robj.offsetHeight) < (item.posy + itemh))
				itemh = robj.offsetHeight - item.posy;
		} else
		{
			item.posx = item.origx;
			item.posy = item.origy;
		}	
		sdb++;

		item.overobj = Array();
		for(d in this.objects)
		{
			ddb++;
			div = this.objects[d];
			if (div == robj)
				continue
			if (div.style.visibility == 'hidden')
				continue;
			if (conflict = !((item.posx > div.posx+div.offsetWidth || div.posx > item.posx+itemw) || (item.posy > div.posy+div.offsetHeight || div.posy > item.posy+itemh) || (robj && (robj == div || robj.style.zIndex > div.style.zIndex))))
				break;
		}
		item.style.visibility = (conflict ? 'hidden':'');
	}
}

function SelectRemover()
{
	if (!IE)
		return true;

	SelectRemover.selects = Array();
	sels = document.all.tags('SELECT');
	for(i=0; i<sels.length; i++)
	{
		// find container absolute div if exist...
		var robj = sels[i].offsetParent;
		//alert(sels[i].name);
		while(robj && (!robj.style || (robj.style.position != 'absolute')))
		{
			robj = robj.offsetParent;
		}
		//if (robj) alert(robj.id);
		if (robj)
		{
			p = GetOffset(sels[i],robj);
			sels[i].origx = p[0];
			sels[i].origy = p[1];
		} else
		{
			p = GetOffset(sels[i]);
			sels[i].origx = p[0];
			sels[i].origy = p[1];
		}
		sels[i].refobj = robj;
		SelectRemover.selects[i] = sels[i];
	}
	SelectRemover.objects = Array();
	divs = document.all.tags('DIV');
	for(i=0; i<divs.length; i++)
	{
		if (divs[i].style.position == 'absolute')
		{
			p = GetOffset(divs[i]);
			divs[i].posx = p[0];
			divs[i].posy = p[1];
			SelectRemover.objects[i] = divs[i];
		}
	}
}

