// Copyright 2007 - 2010 Steadfast Networks.  All rights reserved.

function create(f, name, value) {
   var t = document.createElement('input');
   t.setAttribute('type', 'hidden');
   t.setAttribute('name', name);
   t.setAttribute('value', value);
   f.appendChild(t);
}

function figure(b) {
  f = b.form;
  f.target = null;
  for (i=0; i < f.elements.length; i++) {
    if (f.elements[i].type == "hidden") {
      f.removeChild(f.elements[i]);
    }
  }
  if(f.id.value == "" || (f.pw.value == "" && b.name != "lostpw")) {
    alert("Please enter a username" +
          (b.name != "lostpw" ? " and password" : "") +
          ".\n\nUsername and Password to Use:\n\
VPS Servers: Your main server IP and root password.\n\
Dedicated/Colocation: Your four digit client ID or username and password.");
    return false;
  }
  if(f.id.value.match(/^\d+$/) || f.id.value.match(/[A-Za-z0-9\-]+\@[A-Za-z0-9\-]+\./)) {
    if(b.name == "login") {
      f.action = "https://manage.steadfast.net";
      create(f, 'login', f.id.value);
      create(f, 'pass', f.pw.value);
    } else if (b.name == "lostpw") {
      f.action = "https://manage.steadfast.net/popup_retrievepw.php";
      f.target = "forgotpw";
      create(f, 'var_type', 'clientid');
      create(f, 'passclick', 'Email Password');
      create(f, 'var_value', f.id.value);
    }
    return true;
  } else if (f.id.value.match(/^\d{1,3}\.\d{1,3}.\d{1,3}.\d{1,3}$/)) {
    f.action = "https://" + f.id.value + ":4643/vz/cp";
    if(b.name == "login") {
      create(f, 'DoLogin', 'Login');
      create(f, 'LoginPass', f.pw.value);
      create(f, 'tz_offset', 360);
      create(f, 'active_lang', 'en');
    } else if (b.name == "lostpw") {
      f.action += "/restore-password";
    }
    create(f, 'LoginUser', 'root');
    return true;
  } else {
    resp = confirm("If you are a shared hosting customer, your account was \
transferred to Fluid Hosting and this form should no longer be used.\n\n\
Please log in directly at https://cp.steadfast.net/ from now on.\n\n\
If you have another service, such as a dedicated server, click OK to be \
logged into the Steadfast Networks management portal.  Click Cancel to \
be redirected to https://cp.steadfast.net.");
    if (!resp) {
      f.action = "https://cp.steadfast.net/";
    } else if(b.name == "login") {
      f.action = "https://manage.steadfast.net";
      create(f, 'login', f.id.value);
      create(f, 'pass', f.pw.value);
    } else if (b.name == "lostpw") {
      f.action = "https://manage.steadfast.net/popup_retrievepw.php";
      f.target = "forgotpw";
      create(f, 'var_type', 'clientid');
      create(f, 'passclick', 'Email Password');
      create(f, 'var_value', f.id.value);
    }
    return true;
  }
}

