
function init() {
  if (document.keyForm != null) {
    //cal('expiration'); 
    //cal('runtimeExpiration'); 
    refresh();
  }
}

function validate() {
  if (document.keyForm.systemName.value == "" ||
      document.keyForm.serial.value == "" ||
      document.keyForm.pgroup.value == "") 
  {
      alert("Please fill in System Name, Serial Number, and Processor Group.");
      return false;
  }
  if (document.keyForm.serial.value.length != 7) 
  {
    alert("Serial number must be 7 characters in length");
    return false;
  }
  /*if (document.getElementById("runtime").checked && product != "rpgsp") 
  {
    alert("Runtime licenses are only valid for RPGsp.");
    return false;
  }*/  
  return true;
}

function refresh() {
  var product = document.keyForm.product.value;
  if (product == "") {
    //hide("todoRow");
    hide("licenseTypeRow");
    hide("iDatalicenseTypeRow");
    hide("codeRow");
    //hide("keyTypeRow");
    //hide("expirationRow");
    //hide("runtimeExpirationRow");
    //document.getElementById("calimgexpiration").style.display = "none";    
    //document.getElementById("calimgruntimeExpiration").style.display = "none";    
    hide("systemNameRow");
    hide("serialRow");
    hide("pgroupRow");
    //hide("usersRow");
    //hide("keyRow");
    hide("submitRow");
    return;
  }
  //show("todoRow");
  //if (document.getElementById("generate").checked) {
  if (product=="rpgsp") 
    show("licenseTypeRow");
  else 
    hide("licenseTypeRow");
  if (product=="idata") 
  {
    show("iDatalicenseTypeRow");
    show("codeRow");      
  }
  else 
  {
    hide("iDatalicenseTypeRow");
    hide("codeRow");      
  }
  /*if (document.getElementById("specific").checked) {
    show("expirationRow");
    setTimeout(function() { moveCal(document.keyForm.expiration) }, 1);   
  }
  else {
    hide("expirationRow");
    document.getElementById("calimgexpiration").style.display = "none";    
  }
  if (document.getElementById("specific").checked && product == "rpgsp") {
    show("runtimeExpirationRow");
    setTimeout(function() { moveCal(document.keyForm.runtimeExpiration) }, 1);   
  }
  else {
    hide("runtimeExpirationRow");
    document.getElementById("calimgruntimeExpiration").style.display = "none";
  }*/
  //show("keyTypeRow");
  show("systemNameRow");
  show("serialRow");
  show("pgroupRow");
  //if (product=="genie" || product=="browse5250" || product == "idata") show("usersRow");
  //else hide("usersRow");
  //hide("keyRow");
  //}
  //generate is not checked. We got rid of this (remember)
  /*else {
    hide("licenseTypeRow");
    hide("iDatalicenseTypeRow");
    hide("codeRow");
    hide("keyTypeRow");
    hide("systemNameRow");
    hide("serialRow");
    hide("pgroupRow");
    hide("usersRow");
    hide("expirationRow");
    hide("runtimeExpirationRow");
    document.getElementById("calimgexpiration").style.display = "none";    
    document.getElementById("calimgruntimeExpiration").style.display = "none";    
    //show("keyRow");
  }*/
  show("submitRow");

}

function show(id) {
  document.getElementById(id).style.display = "";
}

function hide(id) {
  document.getElementById(id).style.display = "none";
}

function copyToClipboard(s) {
  // browsers don't like you to automatically copy things to the clipboard using javascript
  // this trick uses a flash file to perform the copy -- works in both IE and FireFox
  var flashcopier = 'flashcopier';
  if(!document.getElementById(flashcopier)) {
    var divholder = document.createElement('div');
    divholder.id = flashcopier;
    document.body.appendChild(divholder);
  }
  document.getElementById(flashcopier).innerHTML = '';
  var divinfo = '<embed src="/flash/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(s)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
  document.getElementById(flashcopier).innerHTML = divinfo;
}


function getActivationCode() {   
  var serial = document.keyForm.serial.value;

  if (serial.length != 7) {
    alert("Serial numbers must be 7 characters.");
    return;
  }
  
  var activationCode = 0;
  for (var i = 0; i < serial.length; i++) {
    activationCode += serial.charCodeAt(i);
  }
  
  copyToClipboard(activationCode);
  alert("The activation code has been copied to your clipboard.");

}


function validateLogin()
{
  var password = document.loginForm.password.value;
  var partnerId = document.loginForm.resellerId.value;
  if(password == "" || partnerId == "")
  {
    alert("Partner ID and Password are required fields.");
    return false;
  }
}
