function initPage() {
// Disable the buttons
  dgeid('register').disabled = true;
  
  ruiValid = false;
  addEventHandler(dgeid("rui"),"blur",checkUserName);
  rueValid = false;
  addEventHandler(dgeid("rue"),"blur",testEmailAddress);
  rupValid = false;
  addEventHandler(dgeid("rup"),"blur",testPassword);
  rucValid = false;
  addEventHandler(dgeid("ruc"),"blur",testPassword);
  rfnValid = false;
  addEventHandler(dgeid("rfn"),"blur",testForEntry);
  rlnValid = false;
  addEventHandler(dgeid("rln"),"blur",testForEntry);
  rcoValid = false;
  addEventHandler(dgeid("rco"),"blur",testForEntry);
  rsrValid = false;
  addEventHandler(dgeid("rsr"),"blur",testForEntry);
  rcyValid = false;
  addEventHandler(dgeid("rcy"),"blur",testForEntry);
  rstValid = false;
  addEventHandler(dgeid("rst"),"blur",testForEntry);
  rzpValid = false;
  addEventHandler(dgeid("rzp"),"blur",testForEntry);
  rcnValid = false;
  addEventHandler(dgeid("rcn"),"blur",testForEntry);
  rphValid = false;
  addEventHandler(dgeid("rph"),"blur",testForEntry);
  rfxValid = false;
  addEventHandler(dgeid("rfx"),"blur",testForEntry);

  dgeid('rui').focus();
  dgeid('rui').select();
}
// End of Page Initialization function
/*
 * runOnLoad.js: portable registration for onload event handlers.
 * 
 * This module defines a single runOnLoad() function for portably registering
 * functions that can be safely invoked only when the document is fully loaded
 * and the DOM is available.
 *
 * Functions registered with runOnLoad() will not be passed any arguments when
 * invoked. They will not be invoked as a method of any meaningful object, and
 * the this keyword should not be used.  Functions registered with runOnLoad()
 * will be invoked in the order in which they were registered.  There is no
 * way to deregister a function once it has been passed to runOnLoad().
 *
 * In old browsers that do not support addEventListener() or attachEvent(),
 * this function relies on the DOM Level 0 window.onload property and will not
 * work correctly when used in documents that set the onload attribute
 * of their <body> or <frameset> tags.
 */
function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = [initPage];     // The array of functions to call
runOnLoad.loaded = false;         // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one shouldn't stop the rest */ }
    }
    
    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;
// END of onLoad programs ---------------------------------------------

var errorMessages = {
  "rup" : {
    "required"   : "Password is a required field.",
    "length"     : "Password is a minimum of 8 and a maximum of 20 characters.",
    "characters" : "Passwords must be letters, numbers, - or . only",
    "match"      : "Confirm Password must match Password."
  },
  "rfn" : {
    "required" : "First Name is a required field.",
    "length"   : "First Name is a maximum of 40 characters."
  },
  "rln" : {
    "required" : "Last Name is a required field.",
    "length"   : "Last Name is a maximum of 40 characters."
  },
  "rco" : {
    "required" : "Company Name is a required field.",
    "length"   : "Company Name is a maximum of 100 characters."
  },
  "rsr" : {
    "required" : "Street is a required field.",
    "length"   : "Street is a maximum of 50 characters."
  },
  "rcy" : {
    "required" : "City is a required field.",
    "length"   : "City is a maximum of 50 characters."
  },
  "rst" : {
    "required" : "State/Provence is a required field.",
    "length"   : "State/Provence is a maximum of 50 characters."
  },
  "rzp" : {
    "required" : "Zip/Postal Code is a required field.",
    "length"   : "Zip/Postal Code is a maximum of 10 characters."
  },
  "rcn" : {
    "required" : "Country is a required field.",
    "length"   : "Country is a maximum of 50 characters."
  },
  "rue" : {
    "required" : "Email Address is a required field.",
    "length"   : "Email Address is a maximum of 50 characters."
  },
  "rph" : {
    "required" : "Phone is a required field.",
    "length"   : "Phone is a maximum of 50 characters."
  },
  "rfx" : {
    "required" : "Fax is a required field.",
    "length"   : "Fax is a maximum of 50 characters."
  }
}

// Function to test to see if any button should be enabled and
// to enable it.
function testForEnable() {
  if (ruiValid && rueValid && rupValid &&
      rfnValid && rlnValid &&
      rcoValid && rsrValid && rcyValid && rstValid &&
      rzpValid && rcnValid && rphValid && rfxValid ) {
    dgeid("register").disabled = false;
  } else {
    dgeid("register").disabled = true;
  }
}

function checkUserName() {
  var username = dgeid("rui").value.toUpperCase();
  var para = dgeid("ruierr");
  removeChildren(para);
  if (testUserName(username)) {
    usernameRequest = createRequest();
    if (usernameRequest == null)
      alert("Unable to create request");
    else {
      var url= "checkUsername.php?username=" + username;
      usernameRequest.onreadystatechange = showUsernameStatus;
      usernameRequest.open("GET", url, true);
      usernameRequest.send(null);
    }
  }
}
function testUserName(username) {
  var msg = "";
  var para = dgeid("ruierr");
  removeChildren(para);
  if ((20 >= username.length) && // Must be between 8 and 20 chars
      ( 8 <= username.length)){
    if (fieldIsNotAlphaNumeric(username)) { // must be alphanumeric only
      ruiValid = false;
      msg = "Only letters, numbers, . and - are allowed in the User Name.";
    } else {
      var firstchar = username.charAt(0);
      if (fieldIsNotAlpha(firstchar)) {
        ruiValid = false;
        msg = "The first character of the user name must be a letter.";
      } else {
        ruiValid = true;
      }
    }
  } else {
    ruiValid = false;
    msg = "User Name is required and must be between 8 and 20 letters, numbers, . and/or - long.";
  }
  if (!ruiValid) {
    textNode = document.createTextNode(msg);
    para.appendChild(textNode);
    dgeid("rui").focus();
    dgeid("rui").select();
  }
  return ruiValid;
}
function showUsernameStatus() {
  if (usernameRequest.readyState == 4) {
    if (usernameRequest.status == 200) {
      var para = dgeid("ruierr");
      removeChildren(para);
      if (usernameRequest.responseText == "1") {
        ruiValid = true;
      } else {
        ruiValid = false;
        textNode = document.createTextNode("The User Name you selected ["+
                   dgeid("rui").value+"] is already in use.");
        para.appendChild(textNode);
        dgeid("rui").focus();
        dgeid("rui").select();
        dgeid("rui").value = '';
      }
      testForEnable();
    }
  }
}
function testPassword(e) {
  var p1    = dgeid('rup');
  var p2    = dgeid('ruc');
  var para  = dgeid('ruperr');
  removeChildren(para);
  var val1  = p1.value;
  var val2  = p2.value;
  if ("" == val1) { // empty string
    textNode = document.createTextNode(errorMessages.rup.required);
    para.appendChild(textNode);
  } else {
    if (20 < val1.length || 8 > val1.length) { // length
      textNode = document.createTextNode(errorMessages.rup.length);
      para.appendChild(textNode);
    } else {
      if (fieldIsNotAlphaNumeric(val1)) {
        textNode = document.createTextNode(errorMessages.rup.characters);
        para.appendChild(textNode);
      } else {
        if (val1 != val2) { // Match
          textNode = document.createTextNode(errorMessages.rup.match);
          para.appendChild(textNode);
        } else {
          rupValid = true;
          testForEnable();
        }
      }
    }
  }
}
function testForEntry(e) {
  var me    = getActivatedObject(e);
  var fid   = me.id;
  var errid = fid + "err";
  var para  = dgeid(errid);
  removeChildren(para);
  var val   = me.value;
  var maxl  = { "rui" : 20 ,     "rue" : 50 ,
                "rfn" : 40 ,     "rln" : 40 ,
                "rco" : 100 ,    "rsr" : 50 ,
                "rcy" : 50 ,     "rst" : 50 ,
                "rzp" : 10 ,     "rcn" : 50 ,
                "rph" : 50 ,     "rfx" : 50 };
  if ("" == val) {
    // empty string
    textNode = document.createTextNode(eval('errorMessages.'+fid+'.required'));
    para.appendChild(textNode);
  } else {
    var ml = eval('maxl.' + fid);
    if (ml < val.length) {
      textNode = document.createTextNode(eval('errorMessages.'+fid+'.length'));
      para.appendChild(textNode);
    } else {
      eval(fid+'Valid = true;');
      testForEnable();
    }
  }
}
function fieldIsNotAlpha(field) {
  var nonAlphaChars = /[^a-zA-Z]/;
  return (nonAlphaChars.test(field));
}

function fieldIsNotNumeric(field) {
  var nonNumericChars = /[^0-9]/;
  return (nonNumericChars.test(field));
}
function fieldIsNotAlphaNumeric(field) {
  var nonAlphaNumericChars = /[^a-zA-Z0-9.\-]/;
  return (nonAlphaNumericChars.test(field));
}
function testEmailAddress() {
  emailAddress = dgeid("rue").value;
  para = dgeid("rueerr");
  removeChildren(para);
  if (!/^[\w\.\-_\+]+@[\w-]+(\.\w{2,4})+$/.test(emailAddress)) {
  //if (!validateEmail(emailAddress) {
      // Invalid email address
    rueValid = false;
    textNode = document.createTextNode("Please enter a valid email address in the form of user@company.com");
    para.appendChild(textNode);
    dgeid("rue").focus();
    dgeid("rue").select();
  } else {
    //   Valid email address
    rueValid = true;
  }
  testForEnable();
}
// Function to validate the email address
function validateEmail(addr) {
var valid = true;
// Verify that there is data (i.e., the field is not empty
if (addr == '') { valid = false; }
else {
  // Test for invalid characters or non-ascii characters
  var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
  for (i = 0; i < addr.length ; i++) {  
     if ((addr.charCodeAt(i)>127) ||
         (invalidChars.indexOf(addr.charAt(i)) > -1)) {
       valid = false;
     }  
  }
  if (valid) {
    // find the at sign (@)
    var atSign = addr.indexOf('@'); 
    // it must be present
    if (atSign == -1) { valid = false; }
    // it must not be at the beginning
    if (atSign ==  0) { valid = false; }
    // there must be only one
    if (addr.indexOf('@', atSign + 1) > -1) { valid = false; }
    // domain name must contain a period
    if (addr.indexOf('.', atSign) == -1)    { valid = false; }
    // a period must not immediately follow the at sign (@)
    if (addr.indexOf('@.') != -1)           { valid = false; }
    // a period must not immediately precede the at sign (@)
    if (addr.indexOf('.@') != -1)           { valid = false; }
    // there cannot be two adjacent periods
    if (addr.indexOf('..') != -1)           { valid = false; }
    if (valid) { // Test the Top Level Domain 
      var suffix = addr.substring(addr.lastIndexOf('.')+1);
      if (suffix.length < 2) { // The minimum TLD is two characters
         valid = false; 
      }
    }
  }
}
return valid;
}

function removeChildren(node) {
  while (node.childNodes.length > 0) {
    node.removeChild(node.childNodes[0]);
  }
}