/* Add some custom validations */

Validation.addAllThese([
	['validate-all-selection', 'Please make a selection', function(v,elm){
				var p = elm.parentNode;
				var options = p.getElementsByTagName('SELECT');
				return $A(options).all(function(elm) {
					return elm.options ? elm.selectedIndex > 0 : !Validation.get('IsEmpty').test(v);
				});
			}],
			
	['validate-all-required', 'These are required fields.', function(v, elm) {
				var p      = elm.parentNode;
				var inputs = p.getElementsByTagName('INPUT');
				return $A(inputs).all(function(elm) {
				  return !Validation.get('IsEmpty').test($F(elm));
				});
			}],
  
	['validate-two-part-height', 'Please enter a valid height.', function (v,elm) {
        /* This expects mm dd yyyy as the order of select tags */
				var p     = elm.parentNode;
				var parts = p.getElementsByTagName('SELECT');
				return !Validation.get('IsEmpty').test($F(parts[0])) && !/^\s$/.test($F(parts[0])) && !Validation.get('IsEmpty').test($F(parts[1])) && !/^\s$/.test($F(parts[1]));
			}],
			
	['validate-three-part-dob', 'Please enter a valid date of birth.', function (v,elm) {
        /* This expects mm dd yyyy as the order of select tags */
				var p     = elm.parentNode;
				var parts = p.getElementsByTagName('SELECT');
        if (parts.length == 0) {
				  parts = p.getElementsByTagName('INPUT');
        }
				return /^(0?[1-9])|(1[012])$/.test($F(parts[0])) && /^(0?[1-9])|([12][0-9])|(3[01])$/.test($F(parts[1])) && /^[0-9]{4}$/.test($F(parts[2]));
			}],

  ['validate-state', 'Please enter a valid state abbreviation.', {
        pattern: /^[a-zA-Z]{2}$/,
        include: ['required']
			}],
			
  ['validate-zip', 'Please enter a valid zip code.', {
        pattern: /^\d{5}$/,
        include: ['required']
			}],
			
  ['validate-full-name', 'Please enter a first and last name.', {
        pattern: /\w+\s+\w+/,
        include: ['required']
			}],

  ['validate-mandatory-email', 'Please enter a valid email.', {
        include: ['validate-email', 'required']
			}],

  // Phone patterns checked (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
  ['validate-phone', 'Please enter a valid phone number.', {
        pattern: /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
			}],

  ['validate-mandatory-phone', 'Please enter a valid phone number.', {
        include: ['validate-phone', 'required']
			}],
			
  ['reformat-validate-mandatory-phone', 'Please enter a valid phone number.', function (v, elm) {
        v = new FormHelper().reformatPhone(elm);
        return Validation.get('required').test(v) && Validation.get('validate-phone').test(v);
			}],
			
	['validate-checked', 'Please check the checkbox.', {
        pattern: /^[1yY]$/,
        include: ['required']
			}],
	//validation hack, dont try this at home:)
	['validate-pre-existing-conditions', ' ', function(elm) {        
		    var is_valid  = true;
		    is_valid      = Form.getElements($("pre_existing_list")).any(function(e){ return (e.checked); });
		    if(!is_valid){
		      $("pre_existing_conditions_error").addClassName("title-with-errors");
		    }else{
		      $("pre_existing_conditions_error").removeClassName("title-with-errors");
		    }
		    return is_valid;
			}]
]);

/* Create the FormHelper Class */
var FormHelper = Class.create();
FormHelper.prototype = {
  initialize : function() {},
  
  validateHeight : function() {
    if (Validation.get('validate-selection').test($F('insured1_height_inches'), $('insured1_height_inches'))) {
      return Validation.validate('insured1_height_inches');
    }
  },
  
  validateDateOfBirth : function() {
    if (Validation.get('validate-selection').test($F('dob1_yyyy_on'), $('dob1_yyyy_on'))) {
      return Validation.validate('dob1_yyyy_on');
    }
  },
  
  validateCityStateZip : function() {
    if (Validation.get('required').test($F('address1_zip'), $('address1_zip'))) {
      return Validation.validate('address1_zip');
    }
  },
  
  reformatPhone : function(id) {
    var phone          = $F(id);
    var clean_phone    = phone.gsub(/\D/, '');
    var phone_area     = clean_phone.substr(0,3);
    var phone_exchange = clean_phone.substr(3,3);
    var phone_station  = clean_phone.substr(6,4);
    
    var phone_formatted = "";
    if (phone_area.length > 0) phone_formatted = "(" + phone_area;
    if (phone_exchange.length > 0) phone_formatted += ") " + phone_exchange;
    if (phone_station.length > 0) phone_formatted += "-" + phone_station;
    
    $(id).value = phone_formatted;
    
    return phone_formatted;
  }
}

var formhelper = new FormHelper();