var dtable = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','.','_');

function WriteCookie (cookieName, cookieValue, expiry) 
{
	var expDate = new Date();
	expDate.setTime (expDate.getTime() + expiry);
	document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString() + "; path=/";
}

function ReadCookie (Name)
{
	var search = Name + "="
	if (document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search)
				
		if (offset != -1)
		{
			offset += search.length 
			end = document.cookie.indexOf(";", offset) 
			if (end == -1)
				end = document.cookie.length
			return unescape(document.cookie.substring(offset, end))
		}
		else
			return ("");
		
	}
	else
			return ("");
	
}


var loginfield="";
var passwordfield="";
var persistentfield="";

function storePersistentInfos(loginfield,passwordfield,persistentfield)
{
	var usrlogin = encode(loginfield.value);
	var usrpwd = encode(passwordfield.value);
	if (persistentfield)
	{
		try
		{
			if (persistentfield.checked)
			{
					WriteCookie("vdocportal_login",usrlogin,2678400000);
					WriteCookie("vdocportal_pwd",usrpwd,2678400000);
					WriteCookie("vdocportal_bpersistent","true",2678400000);
			}
			else
			{
					WriteCookie("vdocportal_login","",-2678400000);
					WriteCookie("vdocportal_pwd","",-2678400000);
					WriteCookie("vdocportal_bpersistent","false",-2678400000);
			}
		} catch(e) { alert(e); }
	}
}

function fillPersistentInfos(loginfield,passwordfield,persistentfield)
{
	var bpersistent = ReadCookie("vdocportal_bpersistent");
	
	if (loginfield && passwordfield && persistentfield)
	{
		loginfield.value = decode(ReadCookie("vdocportal_login"));
		
		passwordfield.value = decode(ReadCookie("vdocportal_pwd"));	
		
		(bpersistent == "true") ? persistentfield.checked = true : persistentfield.checked = false;
	}
}

function doLogin()
{
	storePersistentInfos();
	document.forms.loginform.submit();	
}


function inchar(d) {
  r = -1;
  for (var i = 0; i < dtable.length; i++) {
    if (d == dtable[i]) {
      r = i;
      break;
    }
  }
  return r;
}

function Trouve(n) {
while (n > 256) { n -= 256; }
return n
}

function encode(n) {
  var o1 = o2 = o3 =o4 = 0;
  var text = "";
  j = 0;
  for (var i = 0; i < n.length; i += 3) {
    t = Math.min(3, n.length - i); 
    if (t == 1) {
       x = n.charCodeAt(i); 
       text += dtable[(x >> 2)]; 
       text += dtable[((x & 0X00000003) << 4)]; 
       text += '-'; 
       text += '-'; 
    } else if (t == 2) {
       x = n.charCodeAt(i); 
       y = n.charCodeAt(i+1); 
       text += dtable[(x >> 2)]; 
       text += dtable[((x & 0X00000003) << 4) | (y >> 4)]; 
       text += dtable[((y & 0X0000000f) << 2)]; 
       text += '-'; 
    } else {
       x = n.charCodeAt(i); 
       y = n.charCodeAt(i+1); 
       z = n.charCodeAt(i+2); 
       text += dtable[x >> 2]; 
       text += dtable[((x & 0x00000003) << 4) | (y >> 4)]; 
       text += dtable[((y & 0X0000000f) << 2) | (z >> 6)]; 
       text += dtable[z & 0X0000003f]; 
    }
  }
  return text; 
}

function decode(n) {

  var p;
  var o1 = o2 = o3 = 0;
  var text = "";
  if ((n.length % 4) != 0) { return null; }
  j = 0;
  for (var i = 0; i < n.length; i += 4) {
    x1 = inchar(n.charAt(i));
    x2 = inchar(n.charAt(i+1));
    x3 = inchar(n.charAt(i+2));
    x4 = inchar(n.charAt(i+3));
    ol = 4;
    if (x4 == -1) { ol--; x4 = 0;}
    if (x3 == -1) { ol--; x3 = 0;}
    if (ol == 4) {
      o1 = ((x1 << 2) | (x2 >> 4)); ((o1 > 256) ? p=Trouve(o1) : p=o1) ; text += String.fromCharCode(p);
      o2 = ((x2 << 4) | (x3 >> 2)); ((o2 > 256) ? p=Trouve(o2) : p=o2) ; text += String.fromCharCode(p);
      o3 = ((x3 << 6) | x4); ((o3 > 256) ? p=Trouve(o3) : p=o3) ; text += String.fromCharCode(p);
    } else if (ol == 3) {
      o1 = ((x1 << 2) | (x2 >> 4)); ((o1 > 256) ? p=Trouve(o1) : p=o1) ; text += String.fromCharCode(p);
      o2 = ((x2 << 4) | (x3 >> 2)); ((o2 > 256) ? p=Trouve(o2) : p=o2); text += String.fromCharCode(p);
          } else if (ol == 2) {
      o1 = ((x1 << 2) | (x2 >> 4)); ((o1 > 256) ? p=Trouve(o1) : p=o1) ; text += String.fromCharCode(p);
    }
  }
  return text;
}