/**
 * General functions.
 **/
function trim(s) {
 return s.replace(/^\s+/,"").replace(/\s+$/,"");
}

function isFloat(n) {
 if (isNaN(n)) {
  return false;
 }
 var matches = /^-?(\d+)(\.(\d+))?$/.exec(n);
 if (matches == null) {
  return false;
 }
 var i = matches[1];
 var f = matches[3];
 var i_len = new String(i).length;
 var f_len;
 if (f == null) {
  f_len = 0;
 }
 else {
  f_len = new String(f).length;
 }
 var s = new String(n).replace(".","");
 if ((s < -0x80000000) || (s > 0x7FFFFFFF)) {
  return false;
 }
 return true;
}

function formatFloat(value,decimals) {
 if (decimals > 0) {
  var zeros = "0000000000";
  var s = new String(value);
  var i = s.lastIndexOf(".");
  if (i >= 0) {
   return s + zeros.substr(0,decimals - s.substr(i+1).length);
  }
  else {
   return s + "." + zeros.substr(0,decimals);
  }
 }
 return value;
}

/**
 * Global variables.
 **/
var loaded;


/**
 * Global constants.
 **/
var MSG_VALID_NUMBER = "A valid number contains one or more digits, optionally followed by a dot decimal seperator and one or more digits.";
var MSG_NOT_LOADED = "Please wait for the document to finish loading first.";


/**
 * User account management functions.
 **/
function checkAccount(f) {
 var err = "";
 if (f.email.value.length < 6) {
  err += "You must enter your real email address.\n";
 }
 if (err.length > 0) {
  alert("Your application cannot be handled due to the following problems:\n\n" + err);
  return false;
 }
 return true;
}

function checkNewUsr(f) {
 var err = "";
 if (f.name.value.length < 5) {
  err += "You must enter your real name.\n";
 }
 if (f.email.value.length < 6) {
  err += "You must enter your real email address.\n";
 }
 if (!f.uid.value.match(/^\w{4,15}$/)) {
  err += "You must enter a prefered user id that consists of between 4 to 15 alphanumeric characters.\n";
 }
 if (f.pwd.value != f.pwdchk.value ) {
  err += "The two passwords you entered do not match.\n";
 }
 else {
  if (!f.pwd.value.match(/^\w{4,15}$/)) {
   err += "You must enter a prefered password that consists of between 4 to 15 alphanumeric characters.\n";
  }
 }
 if (!f.agree.checked) {
  err += "You must read and agree with the terms and conditions of the DISCLAIMER before using this service.\n";
 }
 if (err.length > 0) {
  alert("Your application cannot be handled due to the following problems:\n\n" + err);
  return false;
 }
 return true;
}

function showDisclaimer() {
 alert("DISCLAIMER\n\nWhile every attempt has been made to ensure that this service performs satisfactorily, " +
       "the developer can accept no liability for any damage or loss, including special, incidental, or consequential, " +
       "caused by the use of the service, directly or indirectly.\n\n" +
       "By using this service you acknowledge that you have read this disclaimer, understand it, and agree to be bound by it's terms and conditions.");
 return false;
}


/**
 * Configuration functions.
 **/
function constructEnvelope(f) {
 f.envelope.value = "";
 var xpoints = new Array();
 var ypoints = new Array();
 var points = new Array();
 var xfactor = Math.pow(10,f.arm_decimals.options[f.arm_decimals.options.selectedIndex].value);
 var yfactor = Math.pow(10,f.arm_decimals.options[f.arm_decimals.options.selectedIndex].value);
 for (var i=0; i < f.envelope_select.options.length; i++) {
  var s = f.envelope_select.options[i].text;
  if ((s == null) || (s.length == 0)) {
    continue;
  }
  var matches = /^arm=(-?\d+(\.\d+)?), wgt=(\d+(\.\d+)?)$/.exec(s);
  xpoints.push(Math.round(matches[1] * xfactor));
  ypoints.push(Math.round(matches[3] * yfactor));
  points.push(s);
 }
 f.envelope.value = points.join("^");
 document.PolygonViewerApplet.setPolygon(xpoints.join(","),ypoints.join(","),xpoints.length);
 return xpoints.length;
}

function addPoint(select,arm,wgt) {
 if (!loaded) {
  alert(MSG_NOT_LOADED);
 }
 var err = "";
 if (!isFloat(arm.value)) {
  err += "Arm contains an invalid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (!isFloat(wgt.value)) {
  err += "Wgt contains an invalid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (err.length > 0) {
  alert("Cannot add point due to the following error(s):\n\n" + err);
  return false;
 }
 select.options[select.options.length] = new Option('arm=' + parseFloat(arm.value) + ', wgt=' + parseFloat(wgt.value), select.options.length);
 arm.value = "";
 wgt.value = "";
 constructEnvelope(select.form);
 return false;
}

function removePoint(select,arm,wgt) {
 if (!loaded) {
  alert(MSG_NOT_LOADED);
 }
 for (var i=0; i < select.options.length; i++) {
  if (select.options[i].selected) {
   var matches = /^arm=(-?\d+(\.\d+)?), wgt=(\d+(\.\d+)?)$/.exec(select.options[i].text);
   arm.value=matches[1];
   wgt.value=matches[3];
   select.options[i] = null;
   break;
  }
 }
 constructEnvelope(select.form);
 return false;
}

function constructMoments(f) {
 f.moments.value = "";
 var x = new Array();
 var result = 0;
 for (var i=0; i < f.moments_select.options.length; i++) {
  var s = f.moments_select.options[i].text;
  if ((s != null) && (s.length > 0)) {
   x.push(s);
  }
  result++;
 }
 f.moments.value = x.join("^");
 return result;
}

function addMoment(select,arm,maxwgt,minwgt,defwgt,name) {
 var MSG_CANNOT_ADD = "Cannot add moment due to the following error(s):\n\n";
 var err = "";
 if (!isFloat(arm.value)) {
  err += "Arm contains an invalid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (!isFloat(maxwgt.value)) {
  err += "Max Weight contains an invalid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (!isFloat(minwgt.value)) {
  err += "Min Weight contains an invalid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (!isFloat(defwgt.value)) {
  err += "Default Weight contains an invalid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (trim(name.value).length == 0) {
  err += "You must fill in Name (e.g. Pilot & front passengers (Lbs)).\n";
 }
 if (name.value.indexOf("^") >= 0) {
  err += "You may not use a ^ character in the Name field.\n";
 }
 if (err.length > 0) {
  alert(MSG_CANNOT_ADD + err);
  return false;
 }
 if (parseFloat(minwgt.value) >= parseFloat(maxwgt.value)) {
  alert(MSG_CANNOT_ADD + "Min Weight must be less than Max Weight.");
  return false;
 }
 if ((parseFloat(defwgt.value) < parseFloat(minwgt.value)) || (parseFloat(defwgt.value) > parseFloat(maxwgt.value))) {
  alert(MSG_CANNOT_ADD + "Default Weight must be between than Min Weight and Max Weight.");
  return false;
 }
 select.options[select.options.length] = new Option('arm=' + arm.value + ', maxwgt=' + maxwgt.value + ', minwgt=' + minwgt.value + ', defwgt=' + defwgt.value + ', name=' + name.value, select.options.length);
 arm.value = "";
 maxwgt.value = "";
 minwgt.value = "";
 defwgt.value = "";
 name.value= "";
 return false;
}

function removeMoment(select,arm,maxwgt,minwgt,defwgt,name) {
 for (var i=0; i < select.options.length; i++) {
  if (select.options[i].selected) {
   var matches = /^arm=(-?\d+(\.\d+)?), maxwgt=(\d+(\.\d+)?), minwgt=(\d+(\.\d+)?), defwgt=(\d+(\.\d+)?), name=(.+)$/.exec(select.options[i].text);
   arm.value = matches[1];
   maxwgt.value = matches[3];
   minwgt.value = matches[5];
   defwgt.value = matches[7];
   name.value= matches[9];
   select.options[i] = null;
   break;
  }
 }
 return false;
}

function checkConfig(f) {
 var err = "";
 // Validate general info
 if (f.cat_code.selectedIndex <= 0) {
  err += "You must select an aircraft category.\n";
 }
 if (trim(f.type.value).length < 3) {
  err += "You must specify a valid aircraft type.\n";
 }
 if (trim(f.reg.value).length < 4) {
  err += "You must specify a valid aircraft registration.\n";
 }
 if (trim(f.desc.value).length == 0) {
  err += "You must specify a description for this aircraft configuration.\n";
 }
 if (trim(f.arm_units.value).length == 0) {
  err += "You must specify the measuring system units for the arms. Example: Inches.\n";
 }
 if (trim(f.wgt_units.value).length == 0) {
  err += "You must specify the measuring system units for the arms. Example: Lbs.\n";
 }
 if (!isFloat(f.basic_empty_wgt.value)) {
  err += "Basic empty wgt does not contain a valid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (!isFloat(f.basic_empty_arm.value)) {
  err += "Basic empty arm does not contain a valid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (!isFloat(f.fuel_allow_wgt.value)) {
  err += "Fuel allowance wgt does not contain a valid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (!isFloat(f.fuel_wgt_max.value)) {
  err += "Fuel wgt maximum does not contain a valid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (!isFloat(f.fuel_arm.value)) {
  err += "Fuel arm does not contain a valid number. " + MSG_VALID_NUMBER + "\n";
 }
 if (f.arm_decimals.selectedIndex < 0) {
  err += "You must select the decimal precision for the arms.\n";
 }
 if (f.wgt_decimals.selectedIndex < 0) {
  err += "You must select the decimal precision for the weights.\n";
 }
 // Validate moments
 if (constructMoments(f) == 0) {
  err += "You must specify at least 1 variable moment.\n";
 }
 // Validate polygon
 if (constructEnvelope(f) < 3) {
  err += "You must specify at least 3 envelope polygon points.\n";
 }
 if (err.length > 0) {
  alert("Cannot save request due to the following error(s):\n\n" + err);
  return false;
 }
 return true;
}

function makeDesc(type,reg,desc) {
 if ((desc.value.length == 0) && (type.value.length > 0) && (reg.value.length > 0)) {
  desc.value = type.value + " (" + reg.value + ")";
 }
}