// JavaScript form.js
// This script is based on examples found at The JavaScript Source!!
// http://javascript.internet.com
// modified: January 2, 2008

var number = "0123456789"
var space = " "
var point = "."
var comma = ","
var hyphen = "-"
var zero = "0"

function getCookie(name) {
  var cname = name + "="
  var dc = document.cookie

  if (dc.length > 0) {
    begin = dc.indexOf(cname)
    if (begin != -1) {   
      begin += cname.length
      end = dc.indexOf(";", begin)
      if (end == -1)
        end = dc.length
      return unescape(dc.substring(begin, end))
    } 
  }
  return null
}

function setCookie(name, value) {
  var now = new Date()
  var then = new Date(now.getTime() + 31536000000)

  document.cookie = name + "=" + escape(value) + "; expires=" + then.toGMTString() + "; path=/"
}

function getInfo(form) {
  form.info.value = "Browser Information: " + navigator.userAgent
}

function getValue(element) {
  var value = getCookie(element.name)

  if (value != null)
    element.value = value
}

function setValue(element) {
  setCookie(element.name, element.value)
}

function fixElement(element, message) {
  alert(message)
  element.focus()
}

function ltrim(string) {
  var temp = string
  var size = temp.length

  while (temp.slice(0, 1) == " ") {
    temp = temp.substr(1, size-1)
    size = temp.length
  }
  return temp
}

function rtrim(string) {
  var temp = string
  var size = string.length

  while (temp.slice(size-1, size) == " ") {
    temp = temp.substr(0, size-1)
    size = temp.length
  }
  return temp
}

function trim(string) {

  return ltrim(rtrim(string))
}

function isNumeric(string) {
  re = /[^0-9]/g
  re.input = string

  if (re.test(string))
    return false
  return true
}

function isAlpha(string){
  re = /[^a-zA-Z]/g
  re.input = string

  if (re.test(string))
    return false
  return true
}

function validate_number(field, message) {
  var valid = number + point + comma
  var string = field.value
  var size = string.length
  var count = 0
  var temp = ""

  if (! size)
    return message
  string = trim(string)
  for (var i = 0; i < string.length; i++) {
    temp = string.substring(i, i+1)
    if (temp == point || temp == comma)
      count++
  }
  for (var i = 0; i < string.length; i++) {
    temp = string.substring(i, i+1)
    if (valid.indexOf(temp) == -1 || count > 1)
      return "Dit is geen correct getal!"
  }
  temp = string.substring(0, 1)
  if (temp == point || temp == comma) {
    string = "0" + string
    size = string.length
  }
  temp = string.substring(size-1, size)
  if (temp == point || temp == comma)
    string = string + "0"
  field.value = string
  return ""
}

function validate_string(field, message) {
  var string = field.value
  var size = string.length
  var first = ""
  var temp = ""
  var i = 0

  if (! size)
    return message
  string = trim(string.toLowerCase())
  while (size > 0) {
    string = ltrim(string)
    size = string.length
    if (! size)
      break
    first = string.charAt(0)
    string = first.toUpperCase() + string.substring(1, size)
    i = string.indexOf(space)
    if (i > 0) {
      temp = temp + space + string.substring(0, i)
      string = string.substring(i, size)
    }
    else {
      temp = temp + space + string
      string = ""
    }
    size = string.length
  }

  string = temp
  size = string.length
  temp = ""
  while (size > 0) {
    string = ltrim(string)
    size = string.length
    if (! size)
      break
    first = string.charAt(0)
    string = first.toUpperCase() + string.substring(1, size)
    i = string.indexOf(point)
    if (i > 0) {
      temp = temp + string.substring(0, i) + point + space
      string = ltrim(string.substring(i+1, size))
    }
    else {
      temp = temp + string
      string = ""
    }
    size = string.length
  }


  field.value = ltrim(temp)
  return ""
}

function validate_phone(field, message) {
  var valid = number + space + hyphen
  var string = field.value
  var size = string.length
  var count = 0
  var temp = ""

  if (! size)
    return message
  string = trim(string)
  field.value = string
  for (var i = 0; i < string.length; i++) {
    temp = string.substring(i, i+1)
    if (i == 0 && temp != zero)
      return "Geef het netnummer inclusief de 0!"
    if (i == 1 && temp == zero)
      return "Dit is geen correct netnummer!"
    if (temp == space || temp == hyphen)
      count++;
    if (valid.indexOf(temp) == -1)
      return "Een telefoonnummer mag alleen de cijfers 0-9, spaties en koppeltekens bevatten!"
  }
  field.value = string
  if (string.length != 10 + count)
    return "Een telefoonnummer moet 10 cijfers bevatten!"
  return ""
}

function validate_email(field, message) {
  var emailStr = field.value

  if (! emailStr.length)
    return message
  emailStr = trim(emailStr)
  field.value = emailStr

// Original script: Sandeep V. Tamhankar (stamhankar@hotmail.com)
// The following variable tells the rest of the function whether or not
// to verify that the address ends in a two-letter country or well-known TLD.
//   1 = check it
//   0 = don't

  var checkTLD = 1

// The following is the list of known TLDs that an e-mail address must end with.

  var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/

// The following pattern is used to check if the entered e-mail address fits the user@domain format.
// It also is used to separate the username from the domain.

  var emailPat = /^(.+)@(.+)$/

// The following string represents the pattern for matching all special characters.
// We don't want to allow special characters in the address.
// These characters include ( ) < > @ , ; : \ " . [ ]

  var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]"

// The following string represents the range of characters allowed in a username or domainname.
// It really states which chars aren't allowed.

  var validChars = "\[^\\s" + specialChars + "\]"

// The following pattern applies if the "user" is a quoted string (in which case
// there are no rules about which characters are allowed and which aren't, anything goes).
// For example, "jiminy cricket"@disney.com is a legal e-mail address.

  var quotedUser = "(\"[^\"]*\")"

// The following pattern applies for domains that are IP addresses, rather than symbolic names.
// For example, joe@[123.124.233.4] is a legal e-mail address.
// NOTE: The square brackets are required.

  var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

// The following string represents an atom (basically a series of non-special characters.)

  var atom = validChars + '+'

// The following string represents one word in the typical username.
// For example, in john.doe@somewhere.com, john and doe are words.
// Basically, a word is either an atom or quoted string.

  var word = "(" + atom + "|" + quotedUser + ")"

// The following pattern describes the structure of the user.

  var userPat = new RegExp("^" + word + "(\\." + word + ")*$")

// The following pattern describes the structure of a normal symbolic domain,
// as opposed to ipDomainPat, shown above.

  var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$")

// Finally, let's start trying to figure out if the supplied address is valid.
// Begin with the coarse pattern to simply break up user@domain into different pieces
// that are easy to analyze.

  var matchArray = emailStr.match(emailPat)

// Too many/few @'s or something; basically, this address doesn't even fit
// the general mould of a valid e-mail address.

  if (matchArray == null)
    return "Dit is geen correct e-mailadres - controleer @ and .!"

  var user = matchArray[1]
  var domain = matchArray[2]

// Start by checking that only basic ASCII characters are in the strings (0-127).

  for (var i = 0; i < user.length; i++)
    if (user.charCodeAt(i) > 127)
      return "De gebruikersnaam bevat verboden karakters!"
  for (var i = 0; i < domain.length; i++)
    if (domain.charCodeAt(i) > 127)
      return "De domeinnaam bevat verboden karakters!"

// See if "user" is valid.

  if (user.match(userPat) == null)
    return "Dit is geen correcte gebruikersnaam!"

// If the e-mail address is at an IP address (as opposed to a symbolic host name)
// make sure the IP address is valid.

  var IPArray = domain.match(ipDomainPat);

  if (IPArray != null) {
    for (var i = 1; i <= 4; i++)
      if (IPArray[i] > 255)
        return "Dit is geen correct IP-adres!"
    return ""
  }

// Domain is symbolic name. Check if it's valid.
 
  var atomPat = new RegExp("^" + atom + "$")
  var domArr = domain.split(".")
  var len = domArr.length

  for (var i = 0; i < len; i++)
    if (domArr[i].search(atomPat) == -1)
      return "Dit is geen correcte domeinnaam!"

// Domain name seems valid, but now make sure that it ends in a known top-level domain
// (like com, edu, gov) or a two-letter word, representing country (uk, nl),
// and that there's a hostname preceding the domain or country.

  if (checkTLD && domArr[domArr.length-1].length != 2 && 
    domArr[domArr.length-1].search(knownDomsPat) == -1)
    return "Het hoofddomein is onbekend en evenmin een landencode van 2 letters!"

// Make sure there's a host name preceding the domain.

  if (len < 2)
    return "Het adres bevat geen host-naam!"

// If we've gotten this far, everything's valid!

  return ""
}

function validate_zip(field, message) {
  var string = field.value
  var size = string.length
  var part1 = ""
  var part2 = ""

  if (! size)
    return message
  string = trim(string.toUpperCase())
  size = string.length
  part1 = rtrim(string.substring(0, 4))

// numeric part of zip must contain 4 digits and not start with 0
// alpha part of zip must contain 2 letters

  part2 = ltrim(string.substring(4, size))
  if (isNumeric(part1) && part1.length == 4 && part1.substring(0,1) != zero &&
    isAlpha(part2) && part2.length == 2) {
    field.value = part1 + space + part2
    return ""
  }
  return "Dit is geen correcte postcode!"
}

function validate_bankgiro(field, message) {
  var valid = point + space
  var string = field.value
  var size = string.length
  var temp = string
  var sum = 0

  if (! size)
    return message

// remove leading zero's

  for (var i = 0; i < size; i++)
    if (string.substring(i, i+1) == zero) {
      temp = string.substring(i+1, size)
    }
    else
      break
  string = temp
  size = string.length
  temp = ""

// remove decimal points and spaces

  for (var i = 0; i < string.length; i++) {
    if (valid.indexOf(string.substring(i, i+1)) == -1)
      temp += string.substring(i, i+1)
  }
  string = temp
  size = string.length
  if (! isNumeric(string) || size == 0 || size > 9)
    return "Dit is geen correct bank- of gironummer!"

// bank account number must meet 11-test
// giro account number must contain < 8 digits

  if (size == 9) {
    for (var i = 0; i < size; i++)
      sum += eval(string.substring(i, i+1)) * (9-i)
    if (sum % 11)
      return "Dit is geen correct banknummer!"
  }
  else if (size == 8)
    return "Dit is geen correct gironummer!"
  field.value = string
  return ""
}

// end of form.js
