//
// Copyright (c) 2008  TigerMe LLC. All rights reserved
// Identification:   "TigerMe" -0008-2007-0000-user
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//  1. A license agreement from TigerMe LLC
//

//if(navigator.userAgent.indexOf("MSIE")>0)  
//   alert("Sorry! Microsoft IE is not supported at the moment; This software is best tuned for Firefox and Safari"), 
//    document.location="http://www.mozilla.com/en-US/";

_FA0_=_FA1_=_FA2_=_FA3_=null;

_AGR_=_LGL_=_ALL_=_NEW_=  
_LNK_=_LOG_=_MOD_=_RST_=_MSG_= _CVR_= _BAR_=null;


_AGR_=document.getElementById("agree"); 
_LGL_=document.getElementById("legal"); 
_ALL_=document.getElementById("xyall"); 

_NEW_=document.getElementById("creat"); 
_LOG_=document.getElementById("login");
_LNK_=document.getElementById("alias");

_MOD_=document.getElementById("modif");
_RST_=document.getElementById("reset");

_MSG_=document.getElementById("uname");

_CVR_=document.getElementById("cover");
_BAR_=document.getElementById("loadB");



// DOCUMENTATION (progress)
// Display neat status of what is going on and block user from pushing extras 
// stuff while progress is going on. Important to display status anyway.

var _LEN_=0; 
var _INC_=5; 

function Progress(len) {
  if(_LEN_<  1)
      window.scrollTo(0,1);

  _CVR_.style.width=document.body.clientWidth+"px";  _CVR_.style.height=document.body.clientHeight+"px"; 

  if(_LEN_>201) 
     return _CVR_.style.display="none"; 

  setTimeout("Progress(0);", 25); _LEN_=_LEN_+_INC_; //+2;
}

_LEN_=0; Progress(0);

// DOCUMENTATION: (mesghttp)
// Apparently we always need an ajax interface to do thing in the
// background. Mostly used to verify user and create account through
// a XDB GET and a PUT

var _EMAIL_ = "An email was sent to you. Your account will be activated through email notification";
var _UPDAT_ = "Sorry, <b>TigerMe</b> couldn't update your password";
var _CREAT_ = "Sorry, <b>TigerMe</b> couldn't create your account";
var _INUSE_ = "Sorry, this email address is already in use";

var mesghttp = false;
if(navigator.appName=="Microsoft Internet Explorer")
   mesghttp=new ActiveXObject("Microsoft.XMLHTTP");
else
   mesghttp=new XMLHttpRequest();


function printmsg(obj) {
  window.scrollTo(0,1);

  _MSG_.style.height=document.body.clientHeight+"px"; 

  //  _MSG_.style.height=window.outerHeight+"px";

    _MSG_.style.display="block"; _CVR_.style.display="none"; 
    _MSG_.innerHTML=obj +"<div style=\"display:block; padding-top:10px; color:#BBB; font-size:70%\">(dismiss message)</span>"; 
}

// DOCUMENTATION: (logmeout)
// The ability to log someone out is very importan. A user must logout
// Perhaps an auto logout is conceivable for future releases such lock
// and security measures. THis functions is reusable all over tigerme

function logmeout(obj) {
  mesghttp.open("GET", "/", true, "login", "password");
  mesghttp.send(null);
}

function logmexin(obj) {
  mesghttp.open("GET", "/xdb?"); 
  mesghttp.send(null);
}



function LoginUser(obj) {

  _ALL_.style.display="block"; 

    _MOD_.style.display="none"; _NEW_.style.display="none"; _AGR_.style.display="none"; _RST_.style.display="none"; _LOG_.style.display="none"; 
    _LNK_.style.display="none";

  obj.style.display="block"; 
}

// DOCUMENTATION: (legalusr)
// Display legal banner and be ready for a transaction submit to database. 
// This is the step that will establish the user account. 

function LegalUser(obj) {
  _AGR_.style.display="block";
}

// DOCUMENTATION: (resetuser)
// Establish an http connection and verify if user exit on server base on 
// email address. New password is generated automatically

function ResetUser(obj) {

  _FA2_=document.getElementById(obj).value;
  if(_FA2_.length<3) return printmsg("Not valid email address");

  _CVR_.style.display="block"; _LEN_=0; Progress(0);
   
// Submit now the password entry to server and verify output if an item 
// already exist or not. If not, go to legal note before final creation;

  mesghttp.open("GET", 
      "/-DAP?"+_FA2_, false); mesghttp.send(null);

  _LEN_=201;

  if(mesghttp.status!=202) 
    return printmsg(_UPDAT_); 

  printmsg(_EMAIL_);
}

// DOCUMENTATION: (modifuser)
// Establish an http connection and verify if user exit on server base on 
// email address. New password is capture separately

function ModifUser(obj) {

  _FA3_=document.getElementById(obj).value;
  if(_FA3_.length<3) return;

  _CVR_.style.display="block"; _LEN_=0; Progress(0);
   
// Submit now the password entry to server and verify output f an item 
// already exist or not. If not, go to legal note before final creation;

  mesghttp.open("GET", 
      "/+DAP?"+_FA3_, false);  mesghttp.send(null);

  _LEN_=201;

  if(mesghttp.status!=202) 
    return printmsg(_UPDAT_); 

  _ALL_.style.display="none"; printmsg("Your password is updated");

// Now, reset the password for the user. He has to  login again with
// his new credentials. 

  xmlxhttp.open("GET", "/", false, 
                "login", "password"); xmlxhttp.send(null); 

  document.cookie="username=;";
}

// DOCUMENTATION: (checkuser)
// Establish an http connection and verify if user exit on server base on 
// email address. If user exist notify else, load legal notice before 
// submitting transaction

function CheckUser(obj) {

  _FA0_=document.getElementById("name").value; 
  _FA1_=document.getElementById("last").value; 
  _FA2_=document.getElementById("mail").value.toLowerCase();

  _FA3_=document.getElementById("pass").value;

//DOCUMENTATION (checkname)
// Now run basic check to verify if entry is valid. This section can be 
// fancy to avoid garbage entry. Names must have certain lengths and 
// specific codes. Good syntax is easier to manage.


  if(_FA0_.length<2) return printmsg("Invalid name"); 
  if(_FA1_.length<2) return printmsg("Invalid name"); 
  if(_FA3_.length<3) return printmsg("Invalid password");

// Verify email account template structure. split string into
// name@domain.arpa 

   if(_FA2_.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\..{2,2}))$)\b/gi)==null)
       return printmsg("Invalid email address");
    
  _CVR_.style.display="block"; _LEN_=0; Progress(0);

// Submit now the password entry to server and verify output if an item 
// already exist or not. If not, go to legal note before final creation;

  mesghttp.open("GET", 
      "/.DAP?"+_FA2_+"/"+_FA3_, false); mesghttp.send(null);

  _LEN_=201;

  if(obj==302) 
    return printmsg(_INUSE_); 

  LoginUser(_AGR_); 
}

// DOCUMENTATION: (createlink)
// Create XML stub and submit user to XDB database as a create transaction. 
// MD5 key passback is need to guarantee identity

function CreateLink(obj) {

  _FA2_=document.getElementById(obj).value.toLowerCase();

  if(_FA2_=="" || _FA2_==null)
    return printmsg("Sorry, <b>TigerMe</b> requires a valid Email Address");

  _CVR_.style.display="block"; _LEN_=0; Progress(0);

// Perform PUT now. Mostly write the user metadata and copy of agreement. 
// In case UI evolves and we need to keep track of.

  mesghttp.open("PUT", 
		"/@DAP?"+escape(_FA2_), false);  mesghttp.send(null);

    _LEN_=0;

    if(mesghttp.status!=201)
      return  printmsg(_CREAT_); 
    
    return printmsg("Your email address is created");
}

// DOCUMENTATION: (createuser)
// Create XML stub and submit user to XDB database as a create transaction. 
// MD5 key passback is need to guarantee identity

function CreateUser(obj) {

  if(_FA2_ == "" || _FA2_==null) 
    return printmsg("Sorry, <b>TigerMe</b> requires a valid Email Address");

   _DOC_="<xdb:user"+
            "\n name=\""+_FA0_+
          "\"\n last=\""+_FA1_+
          "\"\n mail=\""+_FA2_+"\"</xdb:user>\n"+_LGL_.innerHTML;

  _CVR_.style.display="block"; _LEN_=0; _INC_=0.1; Progress(0);

// Perform PUT now. Mostly write the user metadata and copy of agreement. In 
// case UI evolves and we need to keep track of.

  LoginUser(_NEW_);

  mesghttp.open("PUT", 
		"/*DAP?"+escape(_FA2_+"/"+_FA3_), false); mesghttp.send(_DOC_); 

  _LEN_=0; _INC_=5;  

  if(mesghttp.status!=201)
    return printmsg(_CREAT_);   printmsg(_EMAIL_); 
}

