
var row_active = null;
var row_active_background_color = null;

function clicklab_trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function is_defined(o)
{
	if(o==null) return false;
	var_str=eval("' "+o+"'");
	if (var_str==" undefined") return false;
	else return true;
}//clicklab_is_defined

function add_handler(object, event, handler)
{
  if (typeof object.addEventListener != 'undefined')
    object.addEventListener(event, handler, false);
  else if (typeof object.attachEvent != 'undefined') {
  	
    object.attachEvent('on' + event, handler);
  }
  else
  {
    var handlersProp = '_handlerStack_' + event;
    var eventProp = 'on' + event;
    if (typeof object[handlersProp] == 'undefined')
    {
      object[handlersProp] = [];
      if (typeof object[eventProp] != 'undefined')
        object[handlersProp].push(object[eventProp]);
      object[eventProp] = function(e)
      {
        var ret = true;
        for (var i = 0; ret != false && i < object[handlersProp].length; i++)
          ret = object[handlersProp][i](e);
        return ret;
      }
    }
    object[handlersProp].push(handler);
  }
}


function row_rollover(id, mouse_over)
{
	// mouse_over is 1 for onmouseover, 0 for onmouseout
	var row = document.getElementById('tr_' + id);
	if(mouse_over) {
		row_active = row;
		row_active_background_color = row.style.backgroundColor;
	}
	else {
		row_active = null;
	}
	row.style.backgroundColor = (row == row_active) ? '#FFFFD9' : row_active_background_color;
}

function beta_feature(feature_name)
{
	w = 400;
  h = 200;
  l = (screen.availWidth-10)/2-250;
  t = (screen.availHeight-20)/2-250
  features = "width="+w+",height="+h;
  features += ",left="+l+",top="+t+",screenX=0,screenY=0";
	window.open("/?m=beta-feature&feature-name="+feature_name,"BetaFeature",features);
}
function change_class(n, class_name)
{
	if(is_defined(get_element(n)) && is_defined(get_element(n).className)) {
		get_element(n).className=class_name;
	}

}

function get_element(n)
{
	return document.getElementById(n);
}

function popup(nam,lnk,add,h,w,sb)
{
	if (isNaN(parseInt(h)) || parseInt(h) <= 0)
	{
		h = screen.height - 80;
	}
	if (isNaN(parseInt(w)) || parseInt(w) <= 0)
	{
		w = screen.width - 50;
	}
	var ih = 1;
	var iw = 1;
	if( !sb ) sb = "no";
	//sb = 1;
	wnd = window.open(lnk+add, nam , "top="+ih+", left="+iw+", height="+h+", width="+w+", scrollbars="+sb+", status=1, toolbar=no, menubar=no, location=no");
	if(wnd&&wnd.focus) wnd.focus();
}

function mod10(number) {
	var total = 0;
	var flag = 0;
	for (var i=(number.length - 1);i>=0; i--) {
		if (flag == 1) {
			if(number.charAt(i)>="0" && number.charAt(i)<="9") {
				var digits = number.charAt(i) * 2;
				if (digits > 9) digits -= 9;
				total += digits;
				flag = 0;
			}
		} else {
			if(number.charAt(i)>="0" && number.charAt(i)<="9") {
				total = total + parseInt(number.charAt(i));
				flag = 1;
			}
		}
	}

	if (total>0 && (total%10) == 0) {
		return true;
	} else {
		return false;
	}
}


function set_cookie(name,value,days)
{
	if(days)	{
	var date=new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires='; expires='+date.toGMTString();
	}
	else {
		expires='';
	}
	document.cookie=name+'='+value+expires+'; path=/';
}


function get_cookie(name)
{
	var nameEQ=name+'=';
	var ca=document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c=ca[i];
		while(c.charAt(0)==' ')c=c.substring(1,c.length);
		if(c.indexOf(nameEQ)==0)return c.substring(nameEQ.length,c.length);
	}
	return null;
}



function _read_site_pref()
{

	cookie = get_cookie('site_prefs');

	if(cookie) {

		tmp = cookie.split('|||');
		if( tmp.length<2 || tmp.length%2!=0 ) return false;
		prefs = new Array();
		for(i=0; i<tmp.length; i+=2) {
			prefs[tmp[i]] = tmp[i+1];
		}//for
		return prefs;
	}
	else return false;

}//_read_site_pref


function _write_site_prefs(prefs)
{
	cookie = '';
	tmp = new Array();
	j=0;
	for(i in prefs) {
		tmp[j] = i;
		tmp[j+1] = prefs[i];
		j+=2;
	}//for
	cookie = tmp.join('|||');
	set_cookie('site_prefs', cookie, 500);

}//_write_site_prefs


function set_site_pref(name,value)
{
	if( !(prefs = _read_site_pref()) ) prefs = new Array();
	prefs[name] = value;
	_write_site_prefs(prefs);
}




function get_site_pref(name)
{
	if( !(prefs = _read_site_pref()) ) return false;
	return prefs[name];
}

// for support links
function hide_show(id_obj, id_button, speed, steps_count, start_s, end_s)
{
	step_size = Math.round((start_s - end_s)/steps_count);
	obj = document.getElementById(id_obj);
// 	obj.style.display = '';
	direc = start_s == parseInt(obj.style.height) ? -1 : +1;
	autom = new Automation(steps_count, speed);
	autom.obj = obj;
	autom.onUpdate = function(a) {
		if(this.length==this.step)
			a.style.height = direc==1 ? start_s : end_s;
		else
			a.style.height = parseInt(parseInt(a.style.height) + direc*step_size)+ "px";
	};
	autom.onStop=function(){
		if(obj.style.height=='1px')
		{
			obj.style.display = 'none';
		};
	};
	autom.onStart=function(){
		if ( direc>0 ) {
			// bar is visible
			SetSitePref(id_obj, '1');
			document.getElementById(id_button).src="img/hide_on.gif";
			document.getElementById(id_button).alt="Hide Search Bar";
			obj.style.display = '';
		}
		else {
			// bar is hidden
			SetSitePref(id_obj, '0');
			document.getElementById(id_button).src="img/hide.gif";
			document.getElementById(id_button).alt="Show Search Bar";
		}
	};
	autom.start();
}


// class Automation
// run some action automatically ([length] times every [speed] ms)
function Automation(length, speed)
	{
		this.length = length; // length
		this.speed = speed;// speed
		this.step = -1;     // current step ; -1 begin;
		this.timerId = null; // timer id
		this.obj = null;
	}
		Automation.prototype = {
			clearTimer:function()
			{
				(this.timerId) && clearInterval(this.timerId);
				this.timerId=null;
			},
			start:function()
			{

				if(this.step < 0||this.step>=this.length) {
					this.step = 0;
					this.onStart();
					this.start();
				}
				else {
					var self=this;
					this.clearTimer();
					this.timerId = setInterval( function(){ self.update(); }, this.speed);
				};
			},
			update:function()
			{
				if(this.step<this.length && this.step>=0) {
					this.step++;
					this.onUpdate(this.obj);
				}
				else {
					this.onUpdate(this.obj);
					this.stop();
				}
			},
			stop:function()
			{
				this.onStop();
				this.clearTimer();
			},

			onStart:function(){},
			onStop:function(){},
			onUpdate:function(){}
		};//prototype

function SetSitePref(name,value)
{
	if( !(prefs = _read_site_pref()) ) prefs = new Array();
	prefs[name] = value;
	_write_site_prefs(prefs);
}


function GetSitePref(name)
{
	if( !(prefs = _read_site_pref()) ) return false;
	return prefs[name];
}//GetSitePref

function _write_site_prefs(prefs)
{
	cookie = '';
	tmp = new Array();
	j=0;
	for(i in prefs) {
		tmp[j] = i;
		tmp[j+1] = prefs[i];
		j+=2;
	}//for
	cookie = tmp.join('|||');
	SetCookie('site_prefs', cookie, 500);
}//_write_site_prefs

function SetCookie(name,value,days)
{
	if(days)	{
	var date=new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires='; expires='+date.toGMTString();
	}
	else {
		expires='';
	}
	document.cookie=name+'='+value+expires+'; path=/';
}


function mark_all(obj)
{
	var els = document.getElementsByTagName('input');
	for(var i = 0; i< els.length; i++) {
		if(els[i].type=='checkbox') {
			els[i].checked = obj.checked;
		}
	}

}