var pattern = {
    filled    : /^\s*\S+(\s+\S+)*/,
    number    : /^\s*-{0,1}\d+\s*$/,
    phone     : /^\s*(\d{3}-\d{3}-\d{4}|\(\d{3}\) \d{3}-\d{4})\s*$/,
    ssn       : /^\s*\d{3}-\d{2}-\d{4}\s*$/,
    decimal   : /^\s*-{0,1}\d*\.\d+\s*$/,    
    zip       : /^\s*\d{5}(-\d{4}){0,1}\s*$/,
    date      : /^\s*([0][1-9]|[1][012])(\/|-|\.)([0][1-9]|[12][0-9]|[3][01])(\/|-|\.)[1-9]\d{3}\s*$/,
    time      : /^\s*([0][0-9]|[1][012]):([0][0-9]|[1-5][0-9])\s*$/,
    military  : /^\s*([01][0-9]|[2][123]):([0][0-9]|[1-5][0-9])\s*$/,
    currency  : /^\s*\-{0,1}\$\d{1,3}(\,\d{3})*\.\d{2}\s*$/,
    email     : /^(\s*[^@ ]+[@][^@ ]+\.[^@ ]+\s*,?)+$/
};

function Validation(F) {
this.form = F;
this.pattern = pattern; 
} // ctor

function Match(f,p) {
var d        = 0;
var o        = 0;
var type     = '';
var checked  = '';
var matched  = [];
var select   = false;
var selected = false;
var field_name = '';

if (typeof(this.form[f])=='undefined') {
   return false;
} // if


if (typeof(this.form[f][0]) == 'object') {
   if ((typeof(this.form[f].type) != 'undefined') && this.form[f].type.match(/select/)) {
      this.fields = [ this.form[f] ];
   } else {
      this.fields = this.form[f];
   } // if else
} else {
   this.fields = [ this.form[f] ];
} // if else


for (d=0; d<this.fields.length; d++) {
    type    = '';
    checked = '';
    for (property in this.fields[d]) {
	if (property=='type') {
	    type = this.fields[d][property];
	} // if
	if (property=='checked') {
	    checked = this.fields[d][property];
	} // if
    } // for

    if ((this.fields.length==1) && (typeof(this.form[f].type) != 'undefined') && this.form[f].type.match(/select/)) {
	type = this.form[f].type;
    } // if
    if ((type=='checkbox') && (checked)){
        if (this.fields[d].value.match(p)) {
            matched[matched.length++] = this.fields[d].value;
        } else {
            continue;
        } // if else
    } // if 
    
    if ((type=='radio') && (checked)) {
        if (this.fields[d].value.match(p)) {
            matched[matched.length++] = this.fields[d].value;
        } else {
            continue;
        } // if else
    } // if 
  
    if (type.match(/select/)) {
        selected = false;
        select = (this.fields.length==1) ? this.form[f] : this.fields[d];
        for (o=0; o<select.options.length; o++) {
            if (select.options[o].value.match(p) && select.options[o].selected) {
                matched[matched.length++] = select.options[o].value;
                selected = true;
            } // if
        } // for
        if (selected) {
            continue;
        } // if
    } // if 
    
    if (type=='text') {
        if (this.fields[d].value.match(p)) {
            matched[matched.length++] = this.fields[d].value;
        } else {
            continue;
        } // if else
    } // if 
    
    if (type=='textarea') {
        if (this.fields[d].value.match(p)) {
            matched[matched.length++] = this.fields[d].value;
        } else {
            continue;
        } // if else
    } // if 
    
} // for

if (matched.length) {
    return matched;
} else {
    return false;
} // if else
} // method
Validation.prototype.match = Match;
function GetField(name) {
var f;
for (f in this.form) {
   if (f == name) {
      return this.form[f];
   } // if
} // for
} // fn
Validation.prototype.get_field = GetField;
function GetFieldMatch(regex) {
var f;
var list = [];
var found = [];
for (f in this.form) {
   if ( (typeof(found[f])=="undefined") && (typeof(f)=="string")  && (f.match(regex)) ) {
      list[list.length] = f;
      found[f] = true;
   } // if
} // for
return list;
} // fn 
Validation.prototype.get_field_match = GetFieldMatch; 
function DateCompare(start_date, end_date) {
var start = start_date.split(/[-\.\/]/);
var end   = end_date.split(/[-\.\/]/);
start_date = '' + start[2]+start[0]+start[1];
end_date   = '' + end[2]+end[0]+end[1];
return (start_date > end_date);
} // fn 
Validation.prototype.datecompare = DateCompare;
