//----------------------------------------------------------------------
//  Script: lib.form.js
// Purpose: Form verification utilities
//  Author: Daniel Evans (23 Nov 2002)
//----------------------------------------------------------------------

function defined(v) {
  var undefined;
  return (v!=undefined);
}

function empty(v) {
  return (v=="" || v==null || !defined(v));
}

function emptyText(obj) {
  return empty(obj.value);
}

function lengthText(obj) {
  return obj.value.length;
}

function emptyTextArea(obj) {
  return empty(obj.value);
}

function emptyPassword(obj) {
  return empty(obj.value);
}

function emptyFileUpload(obj) {
  return empty(obj.value);
}

function emptyCheckbox(obj) {
  var isEmpty=true;
  if (defined(obj.length)) {
    var index=0;
    while (isEmpty && index<obj.length) {
      isEmpty=!obj[index++].checked;
    }
  } else {
    isEmpty=!obj.checked;
  }
  return isEmpty;
}

function emptyRadio(obj) {
  // Radio() objects have exactly the same properties as Checkbox() objects...
  return emptyCheckbox(obj);
}

function emptySelect(obj) {
  switch (obj.type) {
    case "select-one":
      var v=obj.options[obj.selectedIndex].value.toLowerCase();
      return (v=="null" || empty(v));
      break;
    case "select-multiple":
      var index=0;
      var isEmpty=true;
      while (isEmpty && index<obj.length) {
        if (obj.options[index].selected) {
          var v=obj.options[index].value;
          isEmpty=(v=="null" || empty(v));
        }
        index++;
      }
      return isEmpty;
      break;
    default:
      window.alert("unknown <select> type");
  }
  return false;
}

function getSelectValue(obj) {
  switch (obj.type) {
    case "select-one":
      return obj.options[obj.selectedIndex].value;
      break;
    default:
      window.alert("getSelectValue: ERROR: not a Select object");
  }
  return null;
}

function validEmail(s) {
  return s.match(/^[\w\-\.]+@[\w\-]+(\.[\w\-]+)+/);
}

function validCreditCard(s) {
  var ccn="";

  // Clean the number
  for (var c=0; c<s.length; c++) {
    if (parseInt(s.charAt(c)).toString()==s.charAt(c)) {
      ccn+=s.charAt(c);
    }
  }

  // Simple length checks (a credit card number cannot be shorter than 7 digits
  // not can it be longer than 19). 
  if (ccn.length < 7 || ccn.length > 19) {
    return null;
  }

  // Perform Luhn check
  var sum=0;
  var doubleIt=false;
  for (offset=ccn.length-1; offset>=0; offset--) {
    var n=parseInt(ccn.charAt(offset));
    if (doubleIt) {
      n*=2;
      if (n>9) { n-=9; }
    }
    sum+=n;
    doubleIt=!doubleIt;
  }

  if ((sum%10)==0) {
    return ccn;
  } else {
    return null;
  }
}

function validPostCode(s) {
  var valid="";
  s=s.toUpperCase().replace(/[^A-Z0-9]/g,"");

  // Not particularly efficient, but it works...
  if (!valid) { valid=s.match(/^\w\d\d\w\w$/); }
  if (!valid) { valid=s.match(/^\w\d\d\d\w\w$/); }
  if (!valid) { valid=s.match(/^\w\w\d\d\w\w$/); }
  if (!valid) { valid=s.match(/^\w\w\d\d\d\w\w$/); }
  if (!valid) { valid=s.match(/^\w\d\w\d\w\w$/); }
  if (!valid) { valid=s.match(/^\w\w\d\w\d\w\w$/); }
  if (!valid) { valid=s.match(/^GIR0AA$/); }

  if (valid) {
    valid=valid.toString();
    var outward=valid.substring(0,valid.length-3);
    var inward=valid.substring(valid.length-3);
    return outward + " " + inward;
  } else {
    return null;
  }
}
