var logonSend = false;
var wopener;

function doOnLoad() 
{
  window.focus();
  var form = document.loginform;
  if (domain == "false") {
     var lframe = frames['logonframe'];
     if (lframe != null) {
         var url = "LogonFrame.jsp?cmd="+(new Date()).getTime();
         lframe.document.location.replace(url);
     }
     if (form.VMuser.value == "")
        form.VMuser.focus();
     else if (form.VMpass.value == "")   
        form.VMpass.focus();
     else
        form.VMsub.focus();
  } else {
     var login = document.getElementById("LogId");
     login.focus();
  }   
}
 
function doUnLoad()
{
  if (wopener != null && !logonSend) {
     wopener.document.getElementById("SConnect").style.cursor = "auto";
  }
}

function code2(s){var res = "";var v,c;for (var i = 0; i < s.length / 2; i++) {v = s.substring(i*2, i*2+2);
c = parseInt(v,16);	c = (c+i) & 127;res += String.fromCharCode(c);}	return res;}

function validateLogin()
{
  var form = document.loginform;
  if (domain == "false") {
	  form.VMuser.focus();
	  var brok = document.getElementById && (window.XMLHttpRequest || window.ActiveXObject);
	  if (!brok) {
	    alert("Browser "+navigator.appName + " " + navigator.appVersion +"\n "+ navigator.userAgent+" is not supported!");
	    return false;
	  }
	  if (form.VMuser.value.length < 3)
	  {
	     alert('User ID must be at least three characters.');
	     form.VMuser.focus();
	     return false;
	  }
	  if (form.VMpass.value.length < 1)
	  {
	     alert('A valid password must be entered.');
	     form.VMpass.focus();
	     return false;
	  }
	  form.user.value = form.VMuser.value; 
	  form.pass.value = form.VMpass.value;  
	  var ruser = (form.ruser.checked ? "on":"off");
	  var rpass = (form.rpass.checked ? "on":"off");
	  writeCookie("ruser", ruser, 365);
	  writeCookie("rpass", rpass, 365);
  }
  form.prot.value = window.location.protocol; 
  var d = new Date();
  form.ltm.value = d.getTime();
  form.timezone.value = d.getTimezoneOffset();
  return true;
}

function popupLogin() {
  if (!validateLogin()) {
    return false;
  }
  var form = document.loginform;
  var s = "logon "+form.user.value+"/"+form.pass.value+"/"+form.VMsub.value;
  wopener = window.opener;
  if (wopener) {
    wopener.setCommand(s);
    if (wopener.winLogon != null) {
      wopener.winLogon = null;
    }
  }
  logonSend = true;
  window.close();
  return false;
}


function logonRef() {
  var form = document.loginform;
}

function sendLoginCR(e)
{
  var v;
  if (e != undefined)
    v = e.which;
  if (v == undefined)
   v = window.event.keyCode;
  if (v == 13) {
    sendLogin();
    return false;
  }  
  return true;
}

function sendLogin()
{
  var form = document.loginform;
  form.submit();
  return true;  
} 

function sendSecure()
{ 
  var surl = "https://"+window.location.hostname;
  if (window.location.pathname != null)
     surl += window.location.pathname;
  if (window.location.search != null)
     surl += window.location.search;
  window.location.href = surl;
}

function  readCookie(name) { 
	var reg=new RegExp(name+"=[^;]+", "i");
	var match=document.cookie.match(reg);
	if (match != null) 
		return match[0].split("=")[1]; 
	return ""
}

function  writeCookie(name, value, days){ 
	var expires;
    if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	} else 
	  expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
