﻿// JScript File

/* Created by: Francis Cocharrua :: http://scripts.franciscocharrua.com/ */
function Validate_String(string, return_invalid_chars) {
  valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  invalid_chars = '';
  if(string == null || string == '')
     return(true);

  //For every character on the string.   
  for(index = 0; index < string.length; index++) {
    char = string.substr(index, 1);                        
     
    //Is it a valid character?
    if(valid_chars.indexOf(char) == -1) {
      //If not, is it already on the list of invalid characters?
      if(invalid_chars.indexOf(char) == -1) {
        //If it's not, add it.
        if(invalid_chars == '')
          invalid_chars += char;
        else
          invalid_chars += ', ' + char;
      }
    }
  }

  //If the string does not contain invalid characters, the function will return true.
  //If it does, it will either return false or a list of the invalid characters used
  //in the string, depending on the value of the second parameter.
  if(return_invalid_chars == true && invalid_chars != '') {
    last_comma = invalid_chars.lastIndexOf(',');
    if(last_comma != -1)
      invalid_chars = invalid_chars.substr(0, $last_comma) + 
      ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
    return(invalid_chars);
    }
  else
    return(invalid_chars == ''); 
}

function Validate_Email_Address(email_address) {
  //Assumes that valid email addresses consist of user_name@domain.tld or user.name@domain.tld
  at = email_address.indexOf('@');
  dot = email_address.lastIndexOf('.');

  if(at == -1 || 
    dot == -1 || 
    dot <= at + 1 ||
    dot == 0 || 
    dot == email_address.length - 1)
    return(false);
     
  user_name = email_address.substr(0, at);
  domain_name = email_address.substr(at + 1, email_address.length);                  

  if(Validate_String(user_name) === false || 
    Validate_String(domain_name) === false)
    return(false);                     

  return(true);
}


//
//	---------------------------------------
//


/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Travis Beckham :: http://www.squidfingers.com | http://www.podlob.com */

// |||||||||||||||||||||||||||||||||||||||||||||||||||||
//
// Coded by Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
// If want to use this code, feel free to do so, but
// please leave this message intact.
//
// |||||||||||||||||||||||||||||||||||||||||||||||||||||
// --- version date: 11/21/02 --------------------------

// returns true if the string is empty
function isEmpty(str){
	return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return Validate_Email_Address(str)
//	return re.test(str);
}
// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains characters 0-9
function isNumeric(str){
	var re = /[\D\.,\-]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
	var re = /[^a-zA-Z0-9\.,\s\-]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string's length equals "len"
function isLength(str, len){
	return str.length == len;
}
// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max){
	return (str.length >= min)&&(str.length <= max);
}
// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isUSPhoneNumber(str){
	var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
	return re.test(str);
}
// returns true if the string is a valid US date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isUSDate(str){
	var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
	if (!re.test(str)) return false;
	var result = str.match(re);
	var m = parseInt(result[1]);
	var d = parseInt(result[2]);
	var y = parseInt(result[3]);
	if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
	if(m == 2){
		var days = ((y % 4) == 0) ? 29 : 28;
	}else if(m == 4 || m == 6 || m == 9 || m == 11){
		var days = 30;
	}else{
		var days = 31;
	}
	return (d >= 1 && d <= days);
}
// returns true if the string is a valid date formatted as...
// dd mm yyyy, dd/mm/yyyy, dd.mm.yyyy, dd-mm-yyyy
function isDate(str){
	var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
	if (!re.test(str)) return false;
	var result = str.match(re);
	var d = parseInt(result[1]);
	var m = parseInt(result[2]);
	var y = parseInt(result[3]);
	if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
	if(m == 2){
		var days = ((y % 4) == 0) ? 29 : 28;
	}else if(m == 4 || m == 6 || m == 9 || m == 11){
		var days = 30;
	}else{
		var days = 31;
	}
	return (d >= 1 && d <= days);
}// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2){
	return str1 == str2;
}
// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
function isWhitespace(str){ // NOT USED IN FORM VALIDATION
	var re = /[\S]/g
	if (re.test(str)) return false;
	return true;
}
// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
function stripWhitespace(str, replacement){// NOT USED IN FORM VALIDATION
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if(str.search(re) != -1){
		result = str.replace(re, replacement);
	}
	return result;
}
// validate the form
function validateForm(f, preCheck){
	var errors = '';
	if(preCheck != null) errors += preCheck;
	var i,e,t,n,v;
	for(i=0; i < f.elements.length; i++){
		e = f.elements[i];
		if(e.optional) continue;
		t = e.type;
		n = e.name;
		v = e.value;
		if(t == 'text' || t == 'password' || t == 'textarea'){
			if(isEmpty(v)){
				errors += n+' kan ikke være tom.\n'; continue;
			}
			if(v == e.defaultValue){
				errors += n+' skal udfyldes, kan ikke være standard værdi.\n'; continue;
			}
			if(e.isAlpha){
				if(!isAlpha(v)){
					errors += n+' kan kun indeholder følgende tegn A-Z a-z.\n'; continue;
				}
			}
			if(e.isNumeric){
				if(!isNumeric(v)){
					errors += n+' kan kun indeholde følgende tegn 0-9.\n'; continue;
				}
			}
			if(e.isAlphaNumeric){
				if(!isAlphaNumeric(v)){
					errors += n+' kan kun indeholde følgende tegn A-Z a-z 0-9.\n'; continue;
				}
			}
			if(e.isEmail){
				if(!isEmail(v)){
					errors += v+' er ikke en rigtig email adresse.\n'; continue;
				}
			}
			if(e.isLength != null){
				var len = e.isLength;
				if(!isLength(v,len)){
					errors += n+' der skal være '+len+' tegn.\n'; continue;
				}
			}
			if(e.isLengthBetween != null){
				var min = e.isLengthBetween[0];
				var max = e.isLengthBetween[1];
				if(!isLengthBetween(v,min,max)){
					errors += n+' der skal være mellem '+min+' og '+max+' tegn.\n'; continue;
				}
			}
			if(e.isUSPhoneNumber){
				if(!isUSPhoneNumber(v)){
					errors += v+' er ikke et valid US telefon nummer.\n'; continue;
				}
			}
			if(e.isUSDate){
				if(!isUSDate(v)){
					errors += v+' er ikke en valid dato i US format (mm dd yyyy).\n'; continue;
				}
			}
			if(e.isDate){
				if(!isDate(v)){
					errors += v+' er ikke en valid dato (dd mm yyyy).\n'; continue;
				}
			}
			if(e.isMatch != null){
				if(!isMatch(v, e.isMatch)){
					errors += n+' er ikke éns.\n'; continue;
				}
			}
		}
		if(t.indexOf('select') != -1){
			if(isEmpty(e.options[e.selectedIndex].value)){
				errors += n+' mindst én linie skal være valgt.\n'; continue;
			}
		}
		if(t == 'file'){
			if(isEmpty(v)){
				errors += n+' der skal være en fil for at kunne oploade.\n'; continue;
			}
		}
	}
	if(errors != '') alert(errors);
	return errors == '';
}

/*
The following elements are not validated...
	button		type="button"
	checkbox	type="checkbox"
	hidden		type="hidden"
	radio		type="radio"
	reset		type="reset"
	submit		type="submit"

All elements are assumed required and will only be validated for an
empty value or defaultValue unless specified by the following properties.

	isEmail = true;				// valid email address
	isAlpha = true;				// A-Z a-z characters only
	isNumeric = true;			// 0-9 characters only
	isAlphaNumeric = true;		// A-Z a-z 0-9 characters only
	isLength = number;			// must be exact length
	isLengthBetween = array;	// [lowNumber, highNumber] must be between lowNumber and highNumber
	isUSPhoneNumber = true;		// valid US phone number. See "isUSPhoneNumber()" comments for the formatting rules
	isUSDate = true;			// valid US date. See "isUSDate()" comments for the formatting rules
	isDate = true;				// valid date. See "isDate()" comments for the formatting rules
	isMatch = string;			// must match string
	optional = true;			// element will not be validated
*/

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// --------------------------------------------------
// ||||||||||||||||||||||||||||||||||||||||||||||||||
// All of the previous JavaScript is coded to process
// any form and should be kept in an external file if
// multiple forms are being processed.
// ||||||||||||||||||||||||||||||||||||||||||||||||||
// --------------------------------------------------
// ||||||||||||||||||||||||||||||||||||||||||||||||||

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// This function configures the previous
// form validation code for this form.
//	function configureValidation(f){
//	  f.firstname.isAlphaNumeric = true;
//	  f.lastname.isAlphaNumeric = true;
//	  f.email.isEmail = true;
//	  f.phone.isUSPhoneNumber = true;
//	  f.birthday.isDate = true;
//	  f.password1.isLengthBetween = [4,255];
//	  f.password2.isMatch = f.password1.value;
//	  f.comments.optional = true;
//	  var preCheck = (!f.infohtml.checked && !f.infocss.checked && !f.infojs.checked) ? 'select at least one checkbox.\n' : null;
//	  return validateForm(f, preCheck);
//	}

