Array.prototype.indexOf = function(value)
{
    for(i=0; i<this.length;i++)
    {
        if(this[i]==value){return i;}
    }
    return -1;
}

var Node_Open_Ids = Array();
var Node_Open_Ids_Str = get_cookie('open_nodes');
if( Node_Open_Ids_Str )
	Node_Open_Ids = Node_Open_Ids_Str.split(',');

function OnBodyLoaded()
{
}

function OpenNodeFromCookie(type, id, rand, level)
{
	if( Node_Open_Ids.indexOf(id)!=-1 )	OpenNode(type, id, rand, level);
}

function OnAddEvent2Calendar(result)
{
	if( result )
	{
		if( result=='ok' ) window.alert('Event was added to calendar.');
		if( result=='already' ) window.alert('Event is already in the calendar.');
	}
	return true;
}

function AddEvent2Calendar(event_id)
{
	HTTPsend('OnAddEvent2Calendar', "/events/add2calendar.html?ajax=1&act=add&eid="+escape(event_id));
	return false;
}

function WriteEmail(user, domain)
{
	user = unescape(user);
	domain = unescape(domain);
    document.write('<a h' + 'ref' + '="ma' + 'il' + 'to:');
	document.write(user + '@');
	document.write(domain);
	document.write('">');
	document.write(user + '@');
	document.write(domain);
    document.write('</a>');
}


/*
* Date: 29/08/07
* Author: Vladimir Luniov
* Purpose: added to disable errors, why we need this funcs don't know
*/
function menuhover(var1, var2)
{
}

function showmenu(menu, show)
{
}

/*
* Date: 06/09/07
* Author: Serg Parshin
* Purpose: opens popup windows
*/
var __IE__=1; // IE
if(navigator.userAgent.indexOf('Gecko') != -1){__IE__=0;} // MOZILLA

function positionWindow(width, height, event)
{
	var winWidth =screen.width
	var winHeight=screen.height
	if(event == null){
		var left= 150
		var top = 150
	}else{
		var left= event.screenX+15
		var top = event.screenY+15
	}
	if((left+width)>winWidth){left=winWidth-width-10;}
	if((top+height+100)>winHeight){top=winHeight-height-100;}
	return new Array(left,top);
}

function PopupWindow(url,width,height,event, full_href, nomodal)
{
	var aXY=positionWindow(width, height, event);

	var cBaseUrl='';

	if(!full_href)
	{
		cBaseUrl=new String(window.location);
		var a=cBaseUrl.split('/');
		cBaseUrl='';
		//for(var n=0; n<a.length-1; n++)
        //get only site root
        for(var n=0; n<3; n++)
        {
            cBaseUrl+=a[n]+'/';
        }
	}
    if( nomodal )
        var style = "scrollbars=1,width="+width+",height="+height+",left="+aXY[0]+",top="+aXY[1]+',resizable=yes,status=no';
    else
        var style = "modal,width="+width+",height="+height+",left="+aXY[0]+",top="+aXY[1]+',resizable=no,status=no';

	if( url.indexOf('?') >= 0 )
        var win = window.open(cBaseUrl+url+"&ajax=1", '_blank', style);
	else
		var win = window.open(cBaseUrl+url+"?ajax=1", '_blank', style);

	return win;
}


/*
* Date: 26/11/07
* Author: Vladimir Luniov
* Purpose: Opens Div
*/

function OpenDiv(id)
{
	if( div=document.getElementById(id) )
	{
		if(div.style.display=="none")
			div.style.display="block";
		else
			div.style.display="none";

		id=id.replace("div_", "box_id_");
		if( img = document.getElementById(id) )
		{
			if(div.style.display=="none")
				img.className = "category-closed";
			else
				img.className = "category-opened";
		}
	}
}

/*
* Date: 7/12/07
* Author: Vladimir Luniov
* Purpose: Composes Post from Interest Form
*/

function ComposeIntrestFormPost(prefix, count, command_button)
{
	var request_str = command_button+'=1';
        for( i=0; i < count; i++ )
        {
        	var CheckBox = document.getElementById(prefix.substr(1)+i);
                if( CheckBox && CheckBox.checked )
                {
                	request_str += '&';
                        request_str += prefix;
                        request_str += escape(CheckBox.value);
                        request_str += '=1';
                }
        }
        return request_str;
}

function CheckSubItems(prefix, startid, endid, state)
{
    var  i = 0;
    /*for( i = startid; i < endid; i++ )
    {
        var id_str = prefix+i;
        var check_box = document.getElementById(id_str);
        if(check_box)
        {
            var old_state = check_box.checked;
            check_box.checked = state;
            if( old_state!=state ) check_box.onclick();
        }
    }*/
}

function SubItems2GroupState(prefix, startid, endid, group_id)
{
    /*var  allchecked = true;
    for( i = startid; i < endid; i++ )
    {
        var id_str = prefix+i;
        var check_box = document.getElementById(id_str);
        if( check_box && !check_box.checked)
        {
            allchecked = false;
            break;
        }
    }
    var group_check_box = document.getElementById(group_id);
    if( group_check_box )
    {
        group_check_box.checked = allchecked;
        group_check_box.ondblclick();
    } */
}


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

function NodeOpened(result, key)
{
	var divid = 'connections_div' + key;
	var dest_div = GetEl(divid);
	if( dest_div )
	{
		dest_div.innerHTML = result;
		dest_div.setAttribute('id', divid);

		var show_div = GetEl('show_connections_div' + key);
		var hide_div = GetEl('hide_connections_div' + key);
		if(show_div) show_div.style.display="none";
		if(hide_div) hide_div.style.display="block";
	}

	var adiv = GetEl('arrow_div' + key);
	if( adiv ) adiv.style.display='block';

	return true;
}

function OpenNode(type, node_id, rand, level, remember)
{
	if(remember) remember = true;
	else remember = false;

	var div_id = type + node_id + rand;

	var dest_div = GetEl('connections_div' + div_id);
	if( dest_div && dest_div.innerHTML != "" )
	{
		if( dest_div.style.display=='' || dest_div.style.display=='block' )
		{
			var show_div = GetEl('show_connections_div' + div_id);
			var hide_div = GetEl('hide_connections_div' + div_id);
			if(show_div) show_div.style.display="block";
			if(hide_div) hide_div.style.display="none";
			dest_div.style.display='none';
			if(level==0) Node_Open_Ids.splice(Node_Open_Ids.indexOf(node_id));
		}
		else
		{
			var show_div = GetEl('show_connections_div' + div_id);
			var hide_div = GetEl('hide_connections_div' + div_id);
			if(show_div) show_div.style.display="none";
			if(hide_div) hide_div.style.display="block";
			dest_div.style.display='block';
			if(level==0 && remember) Node_Open_Ids.push(node_id);
		}
		if(level==0 && remember) set_cookie("open_nodes", Node_Open_Ids.toString(), 0, 0, 0, window.location.pathname.substr(0, window.location.pathname.lastIndexOf('/')+1));
		return;
	}
	if(level==0 && remember)
	{
		Node_Open_Ids.push(node_id);
		set_cookie("open_nodes", Node_Open_Ids.toString(), 0, 0, 0, window.location.pathname.substr(0, window.location.pathname.lastIndexOf('/')+1));
	}
	var url = '/connections/open.html?ajax=1&type=' + escape(type) + '&node_id=' + escape(node_id)+'&level='+level;
	HTTPsend("NodeOpened", url, '', div_id);
}

// (c) Alarsoft
// Author: Ishenko V.
// DOM function

//show/hide block by id
function ToggleDiv(block_id)
{
	var div=document.getElementById(block_id);
	if(!div){return;}

	if(div.style.display == "block"){
		div.style.display = "none";
	}else{
		if(div.style.display == "none"){
			div.style.display = "block";
		}else{
			//undefended or etc.
			div.style.display = "none";
		}
	}
	return div.style.display;
}

var Filter_Results_Default_Text = 'Filter Results';

function FilterFocus(edit)
{
	if( edit.value==Filter_Results_Default_Text ) edit.value='';
}

function FilterBlur(edit)
{
	if( edit.value=='' ) edit.value=Filter_Results_Default_Text;
}

var ConnectSelected = 0;

function OnDeleteTwoConn(result)
{
	return true;
}


function DeleteTwoConn()
{
	var url = '/connections/open.html?act=stop_connect';
	ConnectSelected = 0;
	var lefter_div = GetEl('lefter_node_div');
	if( lefter_div ) lefter_div.innerHTML = '';
	HTTPsend("OnDeleteTwoConn", url, '');
	return false;
}

function OnTwoConnect(result, div_id)
{
	if( result )
	{
		if( result=='done' )
		{
			window.location.replace('/connections/connect2.html');
			return true;
		}
		ConnectSelected = div_id;

		var lefter_div = GetEl('lefter_node_div');
		if( lefter_div ) lefter_div.innerHTML = result;
	}
}

function ConnectTwo(type, node_id, rand)
{
	var div_id = type + node_id + rand;
	if( ConnectSelected==0 )
		var url = '/connections/open.html?act=start_connect&type=' + escape(type) + '&node_id=' + escape(node_id);
	else
		var url = '/connections/open.html?act=end_connect&type=' + escape(type) + '&node_id=' + escape(node_id);
	HTTPsend("OnTwoConnect", url, '', div_id);
	return false;
}

function ApproveNode(type, node_id, rand)
{
	var div_id = type + node_id + rand;
	var url = '/connections/open.html?act=approve&type=' + escape(type) + '&node_id=' + escape(node_id);
	HTTPsend("OnApproved", url, '', div_id);
	return false;
}

function OnApproved(result, div_id)
{
	var buttondiv = GetEl('node_button_box'+div_id);
 	if( result && result=='ok' &&  buttondiv)
 	{
 		buttondiv.innerHTML = 'Approved';
 	}
}

function DisapproveNode(type, node_id, rand)
{
	var div_id = type + node_id + rand;
	var url = '/connections/open.html?act=deny&type=' + escape(type) + '&node_id=' + escape(node_id);
	HTTPsend("OnDisapprove", url, '', div_id);
	return false;
}

function OnDisapprove(result, div_id)
{
	var buttondiv = GetEl('node_button_box'+div_id);
 	if( result && result=='ok' &&  buttondiv)
 	{
 		buttondiv.innerHTML = 'Disapproved';
 	}
}

/*
* Date: 05/21/08
* Author: Eugene Kuzmin (C) Alarsoft
* Purpose: default text in login form
*/

function LoginBlur()
{
	var email = document.forms['login-box'].elements['_input_email'];
	var pass = document.forms['login-box'].elements['_input_password'];

	if(email.value == '')
		document.getElementById('login-email').style.display = 'inline';

	if(pass.value == '')
		document.getElementById('login-pass').style.display = 'inline';
}

function LoginFocus(input)
{
	var email = document.forms['login-box'].elements['_input_email'];
	var pass = document.forms['login-box'].elements['_input_password'];

	switch(input)
	{
		case 'email':
						if(pass.value == '')
							document.getElementById('login-pass').style.display = 'inline';
						document.getElementById('login-email').style.display = 'none';
						email.focus();
				break;

		case 'pass':
						if(email.value == '')
							document.getElementById('login-email').style.display = 'inline';
						document.getElementById('login-pass').style.display = 'none';
						pass.focus();
				break;
		default:
	}
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path)
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y>0 )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  document.cookie = cookie_string;
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

/**
 * Login Form validation
 * @author Konstantin Samorodov
 * @date 07.07.2009
 */
function validateLoginForm()
{
    if (document.getElementById('login_email').value == '' || document.getElementById('login_pass').value == '')
    {
        alert('Please enter a valid e-mail and password');
        return false;
    }

    return true;
}