/**************************************************
 ** Global Variables                             **
 **************************************************/

/**************************************************
 ** Signup Functions                             **
 **************************************************/

/**     
 * doSignup
 *
 * Load's a help article into the help window.
 *
 */
function doSignup() {
  var request = createXMLHttpRequest();
  var f = document.getElementById("openaprs_form_signup_first");
  var l = document.getElementById("openaprs_form_signup_last");
  var e = document.getElementById("openaprs_form_signup_email");
  var e2 = document.getElementById("openaprs_form_signup_emailver");
  var c = document.getElementById("openaprs_form_signup_callsign");
  var p = document.getElementById("openaprs_form_signup_password");

  if (f == null || l == null || e == null || e2 == null || c == null
      || p == null)
    return;

  // reset each fields error message span
  changeById('errFirst', '');
  changeById('errLast', '');
  changeById('errEmail', '');
  changeById('errCallsign', '');
  changeById('errPassword', '');

  if (e.value != e2.value) {
    changeById('errEmail', '<-- Email addresses do not match, please check them and try it again.');
    changeById('statusSignup', "There was an error processing your signup, see the fields with red error messages next to them to correct the problem.");
    return;
  } // if

  if (p.value.length < 4) {
    changeById('errPassword', '<-- Password must be 4 or more characters long.');
    changeById('statusSignup', "There was an error processing your signup, see the fields with red error messages next to them to correct the problem.");
    return;
  } // if

  changeById('wasHelpful', "Saving your account information...");
  disableField('openaprs_form_signup_submit');

  var queryString = "/ajax/signup/do.php?f="
                    + encodeURIComponent(f.value)
                    + "&l="
                    + encodeURIComponent(l.value)
                    + "&e="
                    + encodeURIComponent(e.value)
                    + "&c="
                    + encodeURIComponent(c.value)
                    + "&p="
                    + encodeURIComponent(hex_md5(p.value));

  request.open("GET", queryString, true);
  request.onreadystatechange = function() {

    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;

      var retData = new Array();
      xmlArray("/openaprs/signup", xmlDoc, retData);
      for (i=0; i < retData.length; i++) {
        if (retData[i]["done"] == "yes")
          changeById('helpContainer', retData[i]["response"]);
        else {
          changeById('wasHelpful', "There was an error processing your signup, see the fields with red error messages next to them to correct the problem.");
          changeById(retData[i]["field"], retData[i]["response"]);
        } // else
      } // for

      enableField('openaprs_form_signup_submit');
    } // if

  } // function()

  request.send(null);
} // doSignup

/**     
 * doSignupView
 *
 * Load's a help article into the help window.
 *
 */
function doSignupView(isPass) {
  var request = createXMLHttpRequest();

  showWindow('helpOverlay');

  if (!loggedIn) { 
    changeById('Signup:Error', 'You must be logged in to use this feature.');
    return;
  } // if

  if (isPass)
    var queryString = "/ajax/signup/passview.php?h="
                      + seed;
  else
    var queryString = "/ajax/signup/view.php?h="
                      + seed;

  request.open("GET", queryString, true);
  request.onreadystatechange = function() {

    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;

      var retData = new Array();
      xmlArray("/openaprs/signup", xmlDoc, retData);
      for (i=0; i < retData.length; i++) {
        if (retData[i]["done"] == "yes")
          changeById('helpContainer', retData[i]["response"]);
        else {
          changeById('wasHelpful', "There was an error processing your signup, see the fields with red error messages next to them to correct the problem.");
          changeById(retData[i]["field"], retData[i]["response"]);
        } // else
      } // for

    } // if

  } // function()

  request.send(null);
} // doSignupView

/**     
 * doSignupChange
 *
 * Load's a help article into the help window.
 *
 */
function doSignupChange() {
  var request = createXMLHttpRequest();
  var f = document.getElementById("Signup:Form:First");
  var l = document.getElementById("Signup:Form:Last");
  var e = document.getElementById("Signup:Form:Email");
  var c = document.getElementById("Signup:Form:Callsign");

  if (!loggedIn) { 
    changeById('Signup:Error', 'You must be logged in to use this feature.');
    return;
  } // if

  if (f == null || l == null || e == null || c == null)
    return;

  // reset each fields error message span
  changeById('Signup:Error:First', '');
  changeById('Singup:Error:Last', '');
  changeById('Signup:Error:Email', '');
  changeById('Signup:Error:Callsign', '');
  changeById('Signup:Error', '');

  changeById('wasHelpful', "Saving your account information...");
  disableField('Signup:Form:Submit');

  var queryString = "/ajax/signup/change.php?h="
                    + seed
                    + "&f="
                    + encodeURIComponent(f.value)
                    + "&l="
                    + encodeURIComponent(l.value)
                    + "&e="
                    + encodeURIComponent(e.value)
                    + "&c="
                    + encodeURIComponent(c.value);

  request.open("GET", queryString, true);
  request.onreadystatechange = function() {

    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;

      var retData = new Array();
      xmlArray("/openaprs/signup", xmlDoc, retData);
      for (i=0; i < retData.length; i++) {
        if (retData[i]["done"] == "yes") {
          changeById('helpContainer', retData[i]["response"]);
          logout();
        } // if
        else {
          changeById('wasHelpful', "There was an error processing your signup, see the fields with red error messages next to them to correct the problem.");
          changeById(retData[i]["field"], retData[i]["response"]);
        } // else
      } // for

      enableField('Signup:Form:Submit');
    } // if

  } // function()

  request.send(null);
} // doSignupChange

/**     
 * doPassChange
 *
 * Load's a help article into the help window.
 *
 */
function doPassChange() {
  var request = createXMLHttpRequest();
  var o = document.getElementById("Signup:Form:Old");
  var n = document.getElementById("Signup:Form:New");
  var n2 = document.getElementById("Signup:Form:New2");

  if (!loggedIn) { 
    changeById('Signup:Error', 'You must be logged in to use this feature.');
    return;
  } // if

  if (o == null || n == null || n2 == null)
    return;

  // reset each fields error message span
  changeById('Signup:Error:Old', '');
  changeById('Signup:Error:New', '');
  changeById('Signup:Error:New2', '');
  changeById('Signup:Error', '');

  if (n.value != n2.value) {
    changeById('Signup:Error', 'New passwords don\'t match.');
    return;
  } // if

  var hash = hex_md5(hex_md5(o.value) + seed);

  changeById('wasHelpful', "Saving your account information...");
  disableField('Signup:Form:Submit');

  var queryString = "/ajax/signup/passchange.php?h="
                    + seed
                    + "&o="
                    + encodeURIComponent(hash)
                    + "&n="
                    + encodeURIComponent(hex_md5(n.value));

  request.open("GET", queryString, true);
  request.onreadystatechange = function() {

    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;

      var retData = new Array();
      xmlArray("/openaprs/signup", xmlDoc, retData);
      for (i=0; i < retData.length; i++) {
        if (retData[i]["done"] == "yes") {
          changeById('helpContainer', retData[i]["response"]);
          logout();
        } // if
        else {
          changeById('wasHelpful', "There was an error processing your signup, see the fields with red error messages next to them to correct the problem.");
          changeById(retData[i]["field"], retData[i]["response"]);
        } // else
      } // for

      enableField('Signup:Form:Submit');
    } // if

  } // function()

  request.send(null);
} // doPassChange

/**     
 * doPassReset
 *
 * Load's a help article into the help window.
 *
 */
function doPassReset() {
  var request = createXMLHttpRequest();
  var e = document.getElementById("Signup:Form:Email");

  if (e == null)
    return;

  if (loggedIn) { 
    changeById('Signup:Error', 'You are alreay logged in and do not need to reset your password.');
    return;
  } // if

  // reset each fields error message span
  changeById('Signup:Error:Email', '');
  changeById('Signup:Error', '');

  changeById('wasHelpful', "Saving your account information...");
  disableField('Signup:Form:Submit');

  var queryString = "/ajax/signup/passreset.php?e="
                    + encodeURIComponent(e.value);

  request.open("GET", queryString, true);
  request.onreadystatechange = function() {

    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;

      var retData = new Array();
      xmlArray("/openaprs/signup", xmlDoc, retData);
      for (i=0; i < retData.length; i++) {
        if (retData[i]["done"] == "yes") {
          changeById('helpContainer', retData[i]["response"]);
          logout();
        } // if
        else {
          changeById('wasHelpful', "There was an error processing your signup, see the fields with red error messages next to them to correct the problem.");
          changeById(retData[i]["field"], retData[i]["response"]);
        } // else
      } // for

      enableField('Signup:Form:Submit');
    } // if

  } // function()

  request.send(null);
} // doPassReset

/**     
 * doDeleteAccount
 *
 * Load's a help article into the help window.
 *
 */
function doDeleteAccount() {
  var request = createXMLHttpRequest();
  var e = document.getElementById("Signup:Form:Email");

  if (e == null)
    return;

  // reset each fields error message span
  changeById('Signup:Error:Email', '');
  changeById('Signup:Error', '');

  changeById('wasHelpful', "Attempting to delete your account information...");
  disableField('Signup:Form:Submit');

  var queryString = "/ajax/signup/delete.php?h="
                    + seed
                    + "&e="
                    + encodeURIComponent(e.value);

  request.open("GET", queryString, true);
  request.onreadystatechange = function() {

    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;

      var retData = new Array();
      xmlArray("/openaprs/signup", xmlDoc, retData);
      for (i=0; i < retData.length; i++) {
        if (retData[i]["done"] == "yes") {
          changeById('helpContainer', retData[i]["response"]);
          logout();
        } // if
        else {
          changeById('wasHelpful', "There was an error processing your request, see the fields with red error messages next to them to correct the problem.");
          changeById(retData[i]["field"], retData[i]["response"]);
        } // else
      } // for

      enableField('Signup:Form:Submit');
    } // if

  } // function()

  request.send(null);
} // doDeleteAccount

/**     
 * doResend
 *
 * Load's a help article into the help window.
 *
 */
function doResend() {
  var request = createXMLHttpRequest();
  var e = document.getElementById("openaprs_form_signup_email");

  if (e == null)
    return;

  // reset each fields error message span
  changeById('errEmail', '');

  if (e.value.length < 1) {
    changeById('errEmail', '<-- Please give us the email address you signed up with.');
    changeById('statusSignup', "There was an error processing your request, see the fields with red error messages next to them to correct the problem.");
    return;
  } // if

  changeById('statusSignup', "Attempting to resend your activation key...");
  disableField('openaprs_form_signup_submit');

  var queryString = "/ajax/signup/resend.php?e="
                    + encodeURIComponent(e.value);

  request.open("GET", queryString, true);
  request.onreadystatechange = function() {

    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;

      var retData = new Array();
      xmlArray("/openaprs/signup", xmlDoc, retData);
      for (i=0; i < retData.length; i++) {
        if (retData[i]["done"] == "yes")
          changeById('helpContainer', retData[i]["response"]);
        else {
          changeById('wasHelpful', "There was an error processing your request, see the fields with red error messages next to them to correct the problem.");
          changeById(retData[i]["field"], retData[i]["response"]);
        } // else
      } // for

      enableField('openaprs_form_signup_submit');
    } // if

  } // function()

  request.send(null);
} // doResend

