// Title: Tigra Form Validator
// URL: http://www.softcomplex.com/products/tigra_form_validator/
// Version: 1.0
// Date: 06/24/2004 (mm/dd/yyyy)
// Note: Permission given to use this script in ANY kind of applications if
//    header lines are left unchanged.
//
// the field name is the form field name.
// 'l':  the name of the field for the user message (required),
// 'r':  set to 1 if field is required,
// 'f':  type of validation,
// 'fv': type of field validation by function,
// 't':  the label field id (required)
// 'm':  another field that this field must match,
// 'mn': min number of characters,
// 'mx': max number of chatracters

// www.softidiom.com version history
// v1.03 13/01/2008
//  Added new functions for Gascony Experience.
// v1.02 18/08/2004
//  Renamed date -> dd-mm-yyyy, time -> hh:mm:ss for consistency with new formats.
// v1.01 12/08/2004
//  Added support for: mm-dd-yyyy, IP address, US state, hh:mm.

// regular expressions or function to validate the format
var re_dt = /^(\d{1,2})\-(\d{1,2})\-(\d{4})$/,
re_hhmm = /^(\d{1,2})\:(\d{1,2})$/,
re_hhmmss = /^(\d{1,2})\:(\d{1,2})\:(\d{1,2})$/,
a_formats = {
  'alpha'   : /^[a-zA-Z\.\-]*$/,
  'alphanum': /^\w+$/,
  'unsigned': /^\d+$/,
  'integer' : /^[\+\-]?\d*$/,
  'real'    : /^[\+\-]?\d*\.?\d*$/,
  'email'   : /^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/,
  'phone'   : /^[\d\.\s\-]+$/,
  'xor'           : function (field, fields) {
    // check that at least one of a pair of fields is present.
    var fieldA = fields[field.a];
    var fieldB = fields[field.b];
    field.aa = fieldA.l;
    field.bb = fieldB.l;
    if (!fieldA.v && !fieldB.v) { return (field.cc) ? 11: 6; }
    if (fieldA.v == 0 && fieldB.v == 0) { return (field.cc) ? 11: 6; }
    return 0;
  },
  'DD,MM,YYYY'    : function (field, fields) {
    // check that a date from 3 fields is valid.
    var fieldA = fields[field.a];
    var fieldB = fields[field.b];
    var fieldC = fields[field.c];
    var months = {Jan:1,Feb:2,Mar:3,Apr:4,May:5,Jun:6,Jul:7,Aug:8,Sep:9,Oct:10,Nov:11,Dec:12};
    var daysInMonth = ge_getDaysInMonth(months[fieldB.v], fieldC.v*1);
    if (fieldA.v > daysInMonth) {
      field.vx = fieldA.v+"/"+fieldB.v+"/"+fieldC.v;
      return 7;
    }
    return 0;
  },
  'date_check_1'    : function (field, fields) {
    // check that the first date is before the second date.
    var fieldA1 = fields[field.a1];
    var fieldB1 = fields[field.b1];
    var fieldC1 = fields[field.c1];
    var fieldA2 = fields[field.a2];
    var fieldB2 = fields[field.b2];
    var fieldC2 = fields[field.c2];
    field.vx1 = fieldA1.v+"/"+fieldB1.v+"/"+fieldC1.v;
    field.vx2 = fieldA2.v+"/"+fieldB2.v+"/"+fieldC2.v;
    if (fieldC1.v > fieldC2.v) { return 8; }
    if (fieldC1.v < fieldC2.v) { return 0; }
    // years are the same.
    var months = {Jan:1,Feb:2,Mar:3,Apr:4,May:5,Jun:6,Jul:7,Aug:8,Sep:9,Oct:10,Nov:11,Dec:12};
    if (months[fieldB1.v] > months[fieldB2.v]) { return 8; }
    if (months[fieldB1.v] < months[fieldB2.v]) { return 0; }
    // months are the same.
    if (fieldA1.v > fieldA2.v) { return 8; }
    if (fieldA1.v == fieldA2.v) { return 8; }
    return 0;
  },
  'not_equal'     : function (field, fields) {
    // check that the value is not the one specified.
    if (field.v == field.a) { return 9; }
    return 0;
  },
  'validateParticipants'  : function (field, fields) {
    // check that there is at least one break participant.
    for (var j = 1; j <= 6; j++) {
      var participantField = document.getElementById("form_breakbookingrequest_adults_at_price_"+j);
      var value = (participantField != null) ? participantField.options[participantField.selectedIndex].value: 0;
      if (value != 0) { return 0; }
      participantField = document.getElementById("form_breakbookingrequest_children_at_price_"+j);
      value = (participantField != null) ? participantField.options[participantField.selectedIndex].value: 0;
      if (value != 0) { return 0; }
    }
    return 10;
  },
  'dd-mm-yyyy'    : function (s_date) {
    // check format
    if (!re_dt.test(s_date))
      return false;
    // check allowed ranges 
    if (RegExp.$1 > 31 || RegExp.$2 > 12)
      return false;
    // check number of day in month
    var dt_test = new Date(RegExp.$3, Number(RegExp.$2-1), RegExp.$1);
    if (dt_test.getMonth() != Number(RegExp.$2-1))
      return false;
    return true;
  },
  'mm-dd-yyyy' : function (s_date) { 
    // check format
    if (!re_dt.test(s_date))
      return false;
    // check allowed ranges 
    if (RegExp.$1 > 12 || RegExp.$2 > 31)
      return false;
    // check number of day in month
    var dt_test = new Date(RegExp.$3, Number(RegExp.$1-1), RegExp.$2);
    if (dt_test.getMonth() != Number(RegExp.$1-1))
      return false;
    return true;
  },
  'IP address'   : /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,
  'US state' : /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/,
  'hh:mm'    : function validate_hhmm(s_time) {
    // check format
    if (!re_hhmm.test(s_time))
      return false;
    // check allowed ranges 
    if (RegExp.$1 > 23 || RegExp.$2 > 59 )
      return false;
    return true;
  },
  'hh:mm:ss'    : function validate_hhmmss(s_time) {
    // check format
    if (!re_hhmmss.test(s_time))
      return false;
    // check allowed ranges 
    if (RegExp.$1 > 23 || RegExp.$2 > 59 || RegExp.$3 > 59)
      return false;
    return true;
  }
},
a_messages = [
  'No form name passed to validator construction routine',
  'No array of "%form%" form fields passed to validator construction routine',
  'Form "%form%" can not be found in this document',
  'Incomplete "%n%" form field descriptor entry. "l" attribute is missing',
  'Can not find form field "%n%" in the form "%form%"',
  'Can not find label tag (id="%t%")',
  'Can not verify match. Field "%m%" was not found',
  'you must fill in the %l% field',
  'the value for %l% must be %mn% characters or more',
  'the value for %l% must be no longer than %mx% characters',
  '"%v%" is not a valid value for the %l% field',
  '"%l%" must match "%ml%"',
  'you must fill in at least one of the %aa% or the %bb% fields',
  '%vx% is not a valid date for your %ab%',
  'the check out date (%vx2%) must be after the check in date (%vx1%)',
  'you must change the value of the %l% field',
  'you must select at least one adult or child',
  '%cc%'
]

// validator construction routine
function validator(s_form, a_fields, o_cfg) {
  this.f_error = validator_error;
  this.f_alert = o_cfg && o_cfg.alert
    ? function(s_msg) { alert(s_msg); return false }
    : function() { return false };
    
  // check required parameters
  //  www.softidiom.com 12 Aug 2004: Removed check so that form index 0 can be passed
  //if (!s_form)  
  //  return this.f_alert(this.f_error(0));
  this.s_form = s_form;
  
  if (!a_fields || typeof(a_fields) != 'object')
    return this.f_alert(this.f_error(1));
  this.a_fields = a_fields;

  this.a_2disable = o_cfg && o_cfg['to_disable'] && typeof(o_cfg['to_disable']) == 'object'
    ? o_cfg['to_disable']
    : [];
    
  this.exec = validator_exec;
}

// validator execution method
function validator_exec() {
  var o_form = document.forms[this.s_form];
  if (!o_form)  
    return this.f_alert(this.f_error(2));
    
  b_dom = document.body && document.body.innerHTML;
  
  // check integrity of the form fields description structure
  for (var n_key in this.a_fields) {
    // check input description entry
    this.a_fields[n_key]['n'] = n_key;
    if (!this.a_fields[n_key]['l'])
      return this.f_alert(this.f_error(3, this.a_fields[n_key]));
    get_element
    o_input = o_form.elements[n_key];
    if (!o_input)
      return this.f_alert(this.f_error(4, this.a_fields[n_key]));
    this.a_fields[n_key].o_input = o_input;
  }

  // reset labels highlight
  if (b_dom)
    for (var n_key in this.a_fields) 
      if (this.a_fields[n_key]['t']) {
        var e_labeltag = get_element(this.a_fields[n_key]['t']);
        if (!e_labeltag)
          return this.f_alert(this.f_error(5, this.a_fields[n_key]));
        this.a_fields[n_key].o_tag = e_labeltag;
        
        // normal state parameters assigned here
        e_labeltag.style.backgroundColor = 'white';
      }

  // collect values depending on the type of the input
  for (var n_key in this.a_fields) {
    this.a_fields[n_key]['v'] = null;
    o_input = this.a_fields[n_key].o_input;
    if (o_input.type == 'checkbox') // checkbox
      this.a_fields[n_key]['v'] = o_input.checked;
    else if (o_input.value) // text, password, hidden
      this.a_fields[n_key]['v'] = o_input.value;
    else if (o_input.options) // select
      this.a_fields[n_key]['v'] = o_input.options[o_input.selectedIndex].value;
    else if (o_input.length > 0) // radiobuton
      for (var n_index = 0; n_index < o_input.length; n_index++)
        if (o_input[n_index].checked) {
          this.a_fields[n_key]['v'] = o_input[n_index].value;
          break;
        }
  }
  
  // check for errors
  var n_errors_count = 0,
    n_another, o_format_check;
  for (var n_key in this.a_fields) {
    o_format_check = this.a_fields[n_key]['f'] && a_formats[this.a_fields[n_key]['f']]
      ? a_formats[this.a_fields[n_key]['f']]
      : null;

    // reset previous error if any
    this.a_fields[n_key].n_error = null;

    // check reqired fields
    if (this.a_fields[n_key]['r'] && !this.a_fields[n_key]['v']) {
      this.a_fields[n_key].n_error = 1;
      n_errors_count++;
    }
    // check length
    else if (this.a_fields[n_key]['mn'] && String(this.a_fields[n_key]['v']).length < this.a_fields[n_key]['mn']) {
      this.a_fields[n_key].n_error = 2;
      n_errors_count++;
    }
    else if (this.a_fields[n_key]['mx'] && String(this.a_fields[n_key]['v']).length > this.a_fields[n_key]['mx']) {
      this.a_fields[n_key].n_error = 3;
      n_errors_count++;
    }
    // check format
    else if (this.a_fields[n_key]['v'] && this.a_fields[n_key]['f'] && (
      (typeof(o_format_check) == 'function'
      && !o_format_check(this.a_fields[n_key]['v']))
      || (typeof(o_format_check) != 'function'
      && !o_format_check.test(this.a_fields[n_key]['v'])))
      ) {
      this.a_fields[n_key].n_error = 4;
      n_errors_count++;
    }
    // check match  
    else if (this.a_fields[n_key]['m']) {
      for (var n_key2 in this.a_fields)
        if (n_key2 == this.a_fields[n_key]['m']) {
          n_another = n_key2;
          break;
        }
      if (n_another == null)
        return this.f_alert(this.f_error(6, this.a_fields[n_key]));
      if (this.a_fields[n_another]['v'] != this.a_fields[n_key]['v']) {
        this.a_fields[n_key]['ml'] = this.a_fields[n_another]['l'];
        this.a_fields[n_key].n_error = 5;
        n_errors_count++;
      }
    }
    // check field validate function
    if (this.a_fields[n_key]['fv']) {
      var formatFunction = a_formats[this.a_fields[n_key]['fv']];
      if (typeof(formatFunction) == 'function') {
        var result = formatFunction(this.a_fields[n_key], this.a_fields);
        if (result > 0) {
          this.a_fields[n_key].n_error = result;
          n_errors_count++;
        }
      }
    }
    
  }

  // collect error messages and highlight captions for errorneous fields
  var s_alert_message = '',
    e_first_error;

  if (n_errors_count) {
    for (var n_key in this.a_fields) {
      var n_error_type = this.a_fields[n_key].n_error,
        s_message = '';
        
      if (n_error_type)
        s_message = this.f_error(n_error_type + 6, this.a_fields[n_key]);

      if (s_message) {
        if (!e_first_error)
          e_first_error = o_form.elements[n_key];
        s_alert_message += s_message + "\n";
        // highlighted state parameters assigned here
        if (b_dom && this.a_fields[n_key].o_tag)
          this.a_fields[n_key].o_tag.style.backgroundColor = '#FFCECE';
      }
    }
    alert(s_alert_message);
    // set focus to first errorneous field
    if (e_first_error.focus)
      e_first_error.focus();
    // cancel form submission if errors detected
    return false;
  }
  
  for (n_key in this.a_2disable)
    if (o_form.elements[this.a_2disable[n_key]])
      o_form.elements[this.a_2disable[n_key]].disabled = true;
}

// return the number of days in a month.
function ge_getDaysInMonth(month, year)  {
  var days;
  if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)  days=31;
  else if (month==4 || month==6 || month==9 || month==11) days=30;
  else if (month==2)  {
    if (ge_isLeapYear(year)) days=29;
    else days=28;
  }
  return (days);
}

// return true if this is a leap year.
function ge_isLeapYear (year) {
  if (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0))  return (true);
  else return (false);
}

function validator_error(n_index) {
  var s_ = a_messages[n_index], n_i = 1, s_key;
  for (; n_i < arguments.length; n_i ++)
    for (s_key in arguments[n_i])
      s_ = s_.replace('%' + s_key + '%', arguments[n_i][s_key]);
  s_ = s_.replace('%form%', this.s_form);
  return s_
}

function get_element (s_id) {
  return document.getElementById(s_id);
}
