
var systemUsers = new Object();

function getUserData(name)
{
  return systemUsers[name];
}

function setUserData(name,val)
{
  systemUsers[name]=val;
}

function describeUser(u,parent)
{
  var s = "";

  if (u.dt)
   if (u.dt!='') s+="Since: "+u.dt+"</br/>";

  if (u.icq)
   if (u.icq!='') s+="ICQ: <a style='text-decoration:none' title='ICQ page' href='http://www.icq.com/"+u.icq+"' target='_blank'>"+u.icq+"</a><br/>";

  if (u.yahooid)
   if (u.yahooid!='') s+="Yahoo: "+u.yahooid+"<br/>";

  if (u.msn)
   if (u.msn!='') s+="MSN: "+u.msn+"<br/>";

  if (u.jabber)
   if (u.jabber!='') s+="Jabber: "+u.jabber+"<br/>";

  if (u.skype)
   if (u.skype!='') s+="Skype: "+u.skype+"<br/>";
   
  if (u.gtalk)
   if (u.gtalk!='') s+="Gtalk: "+u.gtalk+"<br/>";   

  if (u.gizmo)
   if (u.gizmo!='') s+="Gizmo: "+u.gizmo+"<br/>";

  if (u.twitter)
   if (u.twitter!='')
   {
      s+="Twitter: <a style='text-decoration:none' title='Go to twitter' href='http://twitter.com/"+u.twitter+"' target='_blank'>"+u.twitter+"</a>";
      s+=" <a style='text-decoration:none' title='Show timeline in Twitter' href='javascript:void(0)' onClick=\"showHideBlock('twittertimeline');return false;\">-></a>";
      s+="<br/>";

      s+="<span id='twittertimeline' style='display:none'></span><br/>";
   }

  if (u.meebo)
   if (u.meebo!='') 
   { 
     if (u.meebo.indexOf("meebo.com")>=0) s+="<a style='text-decoration:none' title='Chat with '"+u.name+"' href='"+u.meebo+"' target='_blank'>Chat</a> ";
     else s+="<a style='text-decoration:none' title='Chat with '"+u.name+"' href='javascript:void(0)' onClick=\"chatWithUser('"+u.name+"','"+u.meebo+"');return false;\">Chat</a> ";
   }

  if (u.tokbox)
   if (u.tokbox!='') 
   {
     if (u.tokbox.indexOf("tokbox.com")>=0) s+="<a style='text-decoration:none' title='VideoChat with '"+u.name+"' href='"+u.tokbox+"' target='_blank'>VideoChat</a> ";
     else s+="<a style='text-decoration:none' title='VideoChat with '"+u.name+"' href='javascript:void(0)' onClick=\"videoChatWithUser('"+u.name+"');return false;\">VideoChat</a> ";
   } 

   s+="<br/><br/>";

   if (eval(u.latitude)>0)
    if (eval(u.longitude)>0)
    {
     s+="Home: ("+u.latitude+","+u.longitude+") <"
       +"a style='text-decoration:none' title='Go to user home' href='javascript:void(0)' onClick='";
     if (parent)
      s+="parent.";
     s+="moveMap("
       +u.latitude+","+u.longitude;

     i = eval(u.zoom);        
     if (i>=5 && i<=20)
       s+=","+u.zoom;

     var i = eval(u.mt);
     if (i>=0 && i<3)
      s+=","+u.mt;

     s+=");return false;'>-><"+"/a>"; 

     s+"</br>";    
   }

  return s;
}

function registerUser(_e)
{
 var e = _e;if (!e) {if(document.parent) e=document.parent.location.href; else e=document.location.href;}if (!e) e='';var s = URL+"newuser.jsp";
 var t = new Date().getMilliseconds();  
 s+="?e="+encodeURIComponent(e)+"&fict="+t;
 var wn="newuser_"+t; var wt='Registration';
 var regWindow = dhtmlwindow.open(wn, "iframe", s, wt, "width=300px,height=350px,resize=1,scrolling=1,center=1");
 regWindow.moveTo("middle", "middle");regWindow.show();

 return true;
}

function updateCurrentUser(usr)
{
  parent.setCurrentUser(usr);
}

function closeUserWindow(fict)
{
  var obj = parent.document.getElementById("newuser_"+fict);

  if (obj) obj.hide();

  return true;
}

function createNewUser()
{
  var name = document.forms['formMessage'].user.value;
  var pwd = document.forms['formMessage'].pwd.value;
  var pwd1 = document.forms['formMessage'].pwd1.value;
  var email = document.forms['formMessage'].email.value;
  var answer = document.forms['formMessage'].answer.value;

  if (name=='') return;
  if (pwd=='' || pwd1=='') 
  { 
     alert("Please, set a password");
     return false;
  }

  if (pwd!=pwd1)
  {
    alert("Passwords do not match");
    return false;
  }

  if (pwd.length<5)
  {
    alert("Password is too short (5 symbols min)");
    return false;
  }

  if (email=='')
  {
    alert("Please, provide valid email address");
    return false;
  }

  if (answer=='')
  {
    alert("Please, answer the question");
    return false;
  }

  if (!validEmail(email))
  {
    alert("Invalid email address");
    return false;
  }

  if (!validName(name))
  {
    alert("Invalid name: "+name);
    return false;
  }

  showBlock("loading");
  document.forms['formMessage'].submit();
}

function loginUser()
{
  var name = document.getElementById('loginUserName').value;
  var pwd = document.getElementById('loginUserPassword').value;

  if (name=='' || pwd=='') return false;

  if (!validName(name))
  {
    alert("Invalid name: "+name);
    return false;
  }

 cjAjaxEngine("/servlet/GeoData?f=lu&user="+name+"&pwd="+pwd,
              handlerLogin,errorLogin,
              mainBeforeAction,
              mainAfterAction);
 
 return true;  
}

function logoutUser()
{
  cjAjaxEngine("/servlet/GeoData?f=lgu",
              handlerLogout,errorLogout,
              mainBeforeAction,
              mainAfterAction);
}

function handlerLogin(txt, xmlDoc)
{
  if (txt.indexOf("{")<0) alert(txt);
  else
  {
    setCurrentUser(eval("("+txt+")")); 
  }
}


function handlerLogout(txt, xmlDoc)
{
  setCurrentUser(null);
}

function errorLogin()
{
  alert("Login error! Could not connect to server");
}

function errorLogout()
{
  alert("Logout error! Could not connect to server");
}

