// Trillian Software Solutions 2000 - www.trillian.se
// This code is free and can be used anywhere!
// Please include this header if using the code.

function validateRequired(s) {
  return s.match( /[^\s]+/ );
}

function validatePhone(s) {
  return s.match( /^\s*0[1-9][0-9]{0,2}(\s*|-)?[0-9]{5,}\s*$/ );
}

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

function validateZip(s) {
  return s.match( /^\s*[1-9][0-9]{2}(\s*|-)?[0-9]{2}\s*$/ );
}

function validateOrgNumber(s) {
  orgnumber = s.replace( /[^0-9]/, '' );
  if ( orgnumber.length != 10 )
    return false;

  sum = 0;
  for (i=0; i<orgnumber.length-1; i++){
    num = orgnumber.charAt(i) - '0';
    if (i % 2 == 0) num = num * 2;
    if (num > 9) {
      sum += num % 10;
      num = Math.floor(num / 10);
    }
    sum += num;
  }
  sum = (10 - (sum % 10));
  if (sum == 10) sum = 0;

  return (orgnumber.charAt(9) - '0') == sum;
}
