// Absolute min/max for currency values in SQL server
//var MAX_CURRENCY = 922337203685477.5807;
//var MIN_CURRENCY = -922337203685477.5808;
// Current min/max for currency values in SQL server
var MAX_CURRENCY = 1000000000.00;
var MIN_CURRENCY = -1000000000.00;
var MAX_CURR_STRING = "1000000000.00";
var MIN_CURR_STRING = "0.00";

// Absolute min/max value for ints
//var MIN_INTEGER = Math.pow(2, 32)/2 * -1;
//var MAX_INTEGER = Math.pow(2, 32)/2 - 1;
var MAX_INTEGER = 1000000000.00;
var MIN_INTEGER = -1000000000.00;

function validateForm(form) {
  if (!form) {
    alert("Please pass in the form to validate.");
    return false;
  }
  var successful = true;
  var elements = form.elements;
  for (var i = 0; successful && i < elements.length; i++) {
    if (elements[i].validate) {
      successful = elements[i].validate();
    }
  }
  return successful;
}

function validateAlert(message, field, ignoreCustom) {
  if (!ignoreCustom && field.message) {
    alert(field.message);
  } else {
    var fieldTitle = ((field.title) ? "the \"" + field.title + "\"" : "this") + " field";
    message = message.replace(/%f/, fieldTitle);
    alert(message);
  }
}

function validateSelect() {
  var si = this.selectedIndex;
  if (!this.mandatory && (si < 0 || this.options[si].value == null || this.options[si].value == ''))
    return true;

	if (si < 0 || this.options[si].value == null || this.options[si].value == ''){
		this.focus();
	  validateAlert("Please select an option in %f.", this);
	  return false;
	}
	return true;
}

function validateText() {
  var value = this.value.replace(/^\s*(.*)\s*$/, '$1');

  if (!this.mandatory && value == "")
    return true;

  if (!value || value == "") {
    this.focus();
    this.select();
    validateAlert("Please enter some information into %f.", this);
    return false;
  }
  
  if (this.maxlength) {
    if (this.value) {
      if (this.value.length > this.maxlength) {
        this.select();
        this.focus();
        validateAlert("You cannot exceed "+this.maxlength+" characters in %f.", this);
        return false;
      }
    }
  }
  
  if (this.pattern) {
    var pattern = this.pattern;
    var found = this.value.search(pattern);
    if (found < 0) {
      var valueType = (this.valueType) ? this.valueType + " " : "value";
      this.select();
      this.focus();
      validateAlert("Please enter a valid " + valueType + " into %f.", this);
      return false;
    }
  }
  
  return true;
}

function validateInteger() {
  if (!this.mandatory && this.value =="")
    return true;

  var pattern = /^(([0-9]+)|([1-9][0-9]{0,2}(,[0-9]{3})+))$/;

  if ((!this.value || this.value == "") || this.value.search(pattern) < 0) {
    this.select();
    this.focus();

    var message;
    if (isNaN(parseInt(this.value.replace(/,/g, '')))) {
      message = "Please enter a number (in digits, not words) into %f.";
    } else {
      message = "Please enter a non-decimal positive number in digits (not words) without anything else into %f.";
    }
    validateAlert(message, this);
    return false;
  }

  var num = parseInt(this.value);
  var minValue = Number.MIN_VALUE;
  var maxValue = Number.MAX_VALUE;

  if (!isNaN(this.minValue))
    minValue = Math.max(this.minValue, minValue);
  if (!isNaN(this.maxValue))
    maxValue = Math.min(this.maxValue, maxValue);

  if (num < minValue) {
    this.select();
    this.focus();
    validateAlert("Please enter a number (in digits, not words) greater than or equal to " + minValue + " in %f.", this);
    return false;
  } else if (num > this.maxValue) {
    this.select();
    this.focus();
    validateAlert("Please enter a number less than or equal to " + maxValue + " into %f.", this);
    return false;
  }
  return true;
}

function validateCurrency() {
  if (!this.mandatory && this.value == "")
    return true;

  var pattern = /^(Nil|[+\-]?(([0-9]+)|([1-9][0-9]{0,2}(,[0-9]{3})+))([\.-]([0-9]){2}))$/i;
  var found = this.value.search(pattern);

  if (found < 0) {
    this.select();
    this.focus();
    validateAlert("Please enter a valid currency value (i.e. in digits, not words, showing dollars and cents separated by a decimal place and without typing in the dollar sign and greater than \'0.00\' e.g. \'1.00\') into %f.", this);
    return false;
  }

  var minValue = (!isNaN(this.minValue)) ? Math.max(this.minValue, MIN_CURRENCY) : MIN_CURRENCY;
  var maxValue = (!isNaN(this.maxValue)) ? Math.min(this.maxValue, MAX_CURRENCY) : MAX_CURRENCY;

  var value = this.value.replace(/(.)\-/g, '$1.');

  if (value < minValue) {
    this.select();
    this.focus();
    validateAlert("Please enter a number greater than or equal to " + MIN_CURR_STRING + " into %f.", this);
    return false;
  } else if (value > maxValue) {
    this.select();
    this.focus();
    validateAlert("Please enter a number less than or equal to " + MAX_CURR_STRING + " into %f. (This is an arbitrary maximum imposed by Incorporator.)", this);
    return false;
  }

  return true;
}

function validateEmail() {

  // return if blank an not mandatory
  if ((!this.value || this.value == "") && !this.mandatory)
    return true;

  emailStr = String(this.value);

  // checks if the e-mail address is valid
  var emailPat = /^(\".*\"|[A-Za-z0-9\_][A-Za-z0-9\.\-\_]*)@(\[\d{1,3}(\.\d{1,3}){3}]|([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,3})$/;
  var matchArray = emailStr.match(emailPat);
  if (matchArray == null) {
    this.select();
    this.focus();
    validateAlert("Please enter a valid email address into %f.\n(check the '@' and '.' characters)", this);
    return false;
  }
  // make sure the IP address domain is valid
  var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
  if (IPArray != null) {
    for (var i=1; i<=4 ;i++) {
      if (IPArray[i]>255) {
        this.select();
        this.focus();
        validateAlert("Please enter a valid destination IP address into %f.", this)
        return false;
      }
    }
  }
  return true;
}

function validateDecimal() {

  if (!this.mandatory && this.value == "")
    return true;

  if (isNaN(this.value)) {
    this.select();
    this.focus();
    validateAlert("Please enter a number into %f.", this);
    return false;
  }
  else {
    var decimals = (this.decimals) ? this.decimals : 0;
    var regExp = '^[\-+]?[0-9]+' + ((decimals) ? '(\.[0-9]{1,' + decimals + '})?' : '') + '$'
    var matchArray = this.value.match(regExp);

    if (matchArray == null) {
      this.select();
      this.focus();
      validateAlert("Please enter a number with " + ((decimals) ? "up to " + decimals : "no") + " decimal places into %f.", this);
      return false;
    }

    var minValue = (isNaN(this.minValue)) ? Number.negativeInfinity : Number(this.minValue);
    var maxValue = (isNaN(this.maxValue)) ? Number.positiveInfinity : Number(this.maxValue);

    if (this.value < minValue) {
      this.select();
      this.focus();
      validateAlert("Please enter a number greater than or equal to " + minValue + " into %f. (This is an arbitrary minimum imposed by Incorporator.)", this);
      return false;
    } else if (this.value > maxValue) {
      this.select();
      this.focus();
      validateAlert("Please enter a number less than or equal to " + maxValue + " into %f. (This is an arbitrary maximum imposed by Incorporator.)", this);
      return false;
    }
  }
  return true;
}

function validateChecked() {
  // Get the array for this checkbox/radio button group
  var checkboxes = this.form[this.name];
  
  if (checkboxes[0] && checkboxes[0].mandatory && !checkboxes[0].disabled) {
    for (var i = 0; i < checkboxes.length && !checkboxes[i].checked; i++);

    if (i >= checkboxes.length) {
      checkboxes[0].focus();

      //alert("Please tick the relevant name(s).");
      if (this.message)
		    alert(this.message);
		  else
        validateAlert("Please select an option in %f.", this);
      return false;
    }
  } else {
    if (checkboxes.mandatory && !checkboxes.checked && !checkboxes.disabled) {
      checkboxes.focus();

      //alert("Please tick the relevant name(s).");
      if (this.message)
		    alert(this.message);
		  else
        validateAlert("Please select an option in %f.", this);
      return false;
    }
  }
  return true;
}<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4804158-6");
pageTracker._initData();
pageTracker._trackPageview();
</script>