ValidationMgr = Class.create();
ValidationMgr.prototype = {
  initialize : function() {
    this.errors      = 0;
    this.fields      = new Array();
    this.field_names = new Array();
    return;
  },
  add : function(field_name, type, options) {
    var error_msg;
    var error_regex;
    
    error_msg   = eval('Validation.'+type+'.msg');
    error_regex = eval('Validation.'+type+'.regex');
    //disables js validation
    error_msg   = '';
    error_regex = new RegExp('^.+');
    var params    = $H({
                      field_name   : field_name,                        // field to check
                      required     : 0,                                 // duh
                      error_id     : field_name+'Error',                // element id that will get error message
                      error_msg    : error_msg,                         // regex error message
                      error_regex  : error_regex,                       // regex run on field
                      req_msg      : '',                                // required error message
                      match_id     : false,                             // false or field id of field to match
                      match_msg    : 'Field must match',                // message if two fields don't match, should never use default
                      border_color : 'FF0000',                          // color of error border
                      border_width : '1',                               // width of error border
                      callback     : null                               // callback
                    });
    var options   = (options) ? options : $H({});
    var newParams = params.merge(options);
    this.fields[field_name] = newParams;
    this.field_names.push(field_name);
    
  },
  handleExtendedErrors : function(myJSON) {
    var params,field_name,error_msg,error_id,border_color,border_width;
    for (var i =0;i<this.field_names.length;++i) {
      error_msg = eval('myJSON.'+this.field_names[i]);
      if (error_msg) {
        params = this.fields[this.field_names[i]];
        if (params) {
          error_id     = params.get('error_id');

          field_name   = params.get('field_name');
          border_color = params.get('border_color');
          border_width = params.get('border_width');
          callback     = params.get('callback');
          if ($(error_id)) {
            if ($(field_name) && $(field_name).type != 'hidden') $(field_name).setStyle({border:'solid '+border_width+'px #'+border_color});
            $(error_id).update(error_msg);
            $(error_id).show();
            
            if(typeof callback == 'function') {
              callback($(error_id));
            }
            
          }
        }
      }
    }
  },
  validate : function() {
    var validForm = true;
    var params,required,field_name,error_id,match_id,good,border_color,border_width;
    for (var i =0;i<this.field_names.length;++i) {
      params       = this.fields[this.field_names[i]];
      required     = params.get('required');
      field_name   = params.get('field_name');
      error_id     = params.get('error_id');
      match_id     = params.get('match_id');
      border_color = params.get('border_color');
      border_width = params.get('border_width');
      callback     = params.get('callback');
      good         = true;
      if (required == 1 || (required == 0 && $(field_name) && $F(field_name) != '')) {
        if (required == 1 && $(field_name) && $F(field_name) == '') {
          good = false;
          if ($(error_id)) $(error_id).update(params.get('req_msg'));
        }
        
        
        if (good && required == 1 && $(field_name)) {
          if ($(field_name).type == 'checkbox') {
            if (!$(field_name).checked) {
              good = false;
            }
          } else {
            if ($(field_name) && $F(field_name) != '' && !$F(field_name).match(params.get('error_regex'))) {
              good = false;
            }
          }
          if (good == false) {
            if ($(error_id)) $(error_id).update(params.get('error_msg'));
          }
        }
        
        if (good && match_id && $(field_name) && $F(field_name) != $F(match_id)) {
          good = false;
          if ($(error_id)) {
            $(error_id).update(params.get('match_msg'));
          }
        }
        
      } else {
        good = true;
      }
      if (good) {
        if ($(error_id)) {
          if ($(field_name) && $(field_name).type != 'hidden') $(field_name).setStyle({border:'solid 1px #CFCFCF'});
          $(error_id).update('');
          $(error_id).hide();
        }
      } else {
        validForm = false;
        if ($(field_name) && $(field_name).type != 'hidden') $(field_name).setStyle({border:'solid '+border_width+'px #'+border_color});
        if ($(error_id)) $(error_id).show();
        
        if(typeof callback == 'function') {
          callback($(field_name));
        }
        
      }
    };
    
    return validForm;
  }
  
}


Validation = {
  Address : {
    regex : new RegExp('^[\\w\\d\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\xFF#, \\:;\' \\. -]+$'),
    msg   : 'Only letters, numbers, apostrophes, periods, pound signs, commas, and dashes are allowed.'
  },
  City : {
    regex : new RegExp('^[\\w\\d\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\xFF \' \. -]+$'),
    msg   : 'Only letters, apostrophes, periods, and dashes are allowed.'
  },
  Email : {
    regex : new RegExp('[\\w\\d\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\xFF_\\.\\+-]+@[\\w\\d\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\xFF_\\.-]+\\.[\\w\\d\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\xFF_-]+$'),
    msg   : 'You must enter a valid e-mail.'
  },
  Generic : {
    regex : new RegExp('^[^\<\>]{2,}$'),
    msg   : 'At least two characters, except < and > may be used.'
  },
  Int : {
    regex : new RegExp('^[0-9]+$'),
    msg   : 'Only numbers are allowed.'
  },
  Decimal : {
    regex : new RegExp('^[0-9]+(\\.\\d+)?$'),
    msg   : 'Must be a valid decimal value.'
  },
  CardNumber : {
    regex : new RegExp('^\\d{12,16}$'),
    msg   : 'Must be a card number between 12 and 16 digits without any spaces.'
  },
  Phone : {
    regex : new RegExp('^[\\D+\\d+ \\(\\)\\#-]+$'),
    msg   : 'Only letters, numbers, plus signs, pound signs, parenthesis and dashes are allowed.'
  },
  InternationalPhone : {
    regex : new RegExp('^[\\d+\\D+ \\(\\)\\#-]+$'),
    msg   : 'Only letters, numbers, plus signs, pound signs, parenthesis and dashes are allowed.'
  },
  USPhone : {
    regex : new RegExp('^[\\d]{3,3}\\-[\\d]{3,3}\\-[\\d]{4,4}$'),
    msg   : 'Must be in 555-555-5555 format'
  },
  InternationalZip : {
    regex : new RegExp('^[\\w\\d\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\xFF -]+$'),
    msg   : 'Must be in a valid zip code format.'
  },
  USZip : {
    regex : new RegExp('^[0-9]{5,5}(\-[0-9]{4,4})?$'),
    msg   : 'Must be in a valid zip code format.'
  },
  Name : {
    regex : new RegExp('^[A-Za-z\\u00C0-\\u00FF\\u0100-\\u0259\\u0386\\u0388-\\u04E9\\u05D0-\\u06D3\\u1E80-\\u200F\'�. -]{2,255}$'),
    msg   : 'At least two letters, spaces, dashes, periods and aposrophes are allowed.'
  },
  Password : {
    regex : new RegExp('^[^\<\> ]{6,20}$'),
    msg   : 'Passwords must be 6-20 characters, without spaces or < >.'
  },
  Province : {
    regex : new RegExp('^[\\w\\d\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\xFF ]+$'),
    msg   : 'Only letters are allowed.'
  },
  Username : {
    regex : new RegExp('^[\\D\\d]{5,}$'),
    msg   : 'At least 5 letters and/or numbers without spaces must be used.'
  },
  WebSite : {
    regex : new RegExp('^(http(s)?:\/\/|www.)[^<>]*$'),
    msg   : 'Must begin with http://, https://, or www.'
  }
}

function stripHTML(string, removeNewLine, removeBR) {
  var remove   = (removeNewLine) ? removeNewLine : false;
  var removeBR = (removeBR)      ? removeBR      : 'yes';
  
  if (remove) {
    string   = string.replace(/\n/gi, "");
  }
  
  if (removeBR == 'yes') {
    string = string.replace(/<br>/gi, "");
    string = string.replace(/<br \/>/gi, "");
  }
  
  //	string = string.replace(/<span.*openURL\(event, ?'(.+[!']*)'.*[^<]\<\/span\>/gi, "$1");
  string = string.replace(/\<span class="spanLink" onclick="openURL\(event, '([^']+)'[^\<]+\<\/span\>/gi, "$1");
  string = string.replace(/<\/span>/gi, "\n");
  string = string.replace(/<span(.)*[^\<]*>/gi, "");
  string = string.replace(/<a[^\>\<]* href="([^\"]+)"[^\>\<]*>([^\<\>]+)<\/a>/gi, "$1");
  string = string.replace(/<a[^\>\<]* href=([^ ]+)[^\>\<]*>([^\<\>]+)<\/a>/gi, "$1");


  //remove any other a tags
  string = string.replace(/<a[^\>\<]*>/gi, "");
  
  //remove closing for a tags
  string = string.replace(/\<\/a\>/gi,'');
  
  string = string.replace(/\<((?!span|b|ol|ul|li|i|em(?!bed)|a|p|div|br|unb|uni))/gi,'&lt;$1');
  string = string.replace(/\&lt;\/(span|b|ol|ul|li|i|em(?!bed)|a|p|div|br|unb|uni)/gi,'</$1');
  
  return string;
}