var appletWin = false;
// Determines if an element has a style class
function hasClass ( theClass, element ) {
  return ( element.className.indexOf( theClass ) > -1 );
}

// Adds a CSS class name to an elements existing space separated class list
function addClass ( newClass, element ) {
  if ( element.className.indexOf ( newClass ) > -1 )
    return;
  element.className = element.className + " " + newClass;
}
// Removes a CSS class from an elements existing space separated class list
function removeClass( oldClass, element ) {
  var cn = element.className;
  if ( cn.indexOf( oldClass ) < 0 )
    return;
  var names = cn.split(" ");
  var newNames = "";
  for ( i = 0; i < names.length; i++ ) {
    if ( names[i] != oldClass ) {
      newNames += names[i];
    }
    if ( i < names.length ) newNames += " ";
  }
  element.className = newNames;
}


function popUp(URL) {
  appletWin = window.open(URL, "cdcApplet", 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=1,width=270,height=145');
  if(appletWin != null && typeof(appletWin)!="undefined") { appletWin.focus(); }
  return appletWin;
}

function quickSubmit( ff ) {
  if ( !isValidKey( ff.elements["key"].value ) ) { markInvalidKey(ff.elements["key"]);return false; }
  markValidKey( ff.elements["key"] );
  var pop = popUp( "scapplet.vm?quickConnect=true&key=" + ff.elements["key"].value );
  if (!pop || pop==null || typeof(pop)== "undefined") { blockedPopup(); }
  return false;
}
function quickSubmitAppletWindows( ff ) {
  if ( !isValidKey( ff.elements["key"].value ) ) { markInvalidKey(ff.elements["key"]);return false; }
  markValidKey( ff.elements["key"] );
  appletLauncher( ff.elements["key"].value );
  return false;
}
function markInvalidKey( el ){
  try {
  addClass("invalid", el);
  document.getElementById("keyMessage").style.visibility = "visible";
  el.select();
  el.focus();
  }catch(noEl){}
}
function markValidKey( el ){
  try{
  removeClass("invalid", el);
  document.getElementById("keyMessage").style.visibility = "hidden";
  }catch(noEl){}
}
function blockedPopup() {
  addNotice("The SecureLink connection popup window was blocked.  Click the <b>Connect</b> button below to manually connect.  Please set your popup blocker to allow popups from the SecureLink server.");
}
function isValidKey( kk ) { 
  kk = kk.trim();
  if ( kk.length < 8 || kk.substring(0, 2) != "01" || kk.substring(kk.length -4, kk.length -2) == "10" || !/^[\da-f]+$/i.test(kk) ){return false;}return true;
}
function addNotice( str ) {
 var p = document.createElement("p");
 p.innerHTML = str;
 p.className = "notice"; 
 p.setAttribute("style", "position: relative; top: -10px;")
 document.getElementById("notices").appendChild( p );
 return p;
}

