// **** FORM VALIDATOR v2.45 **** //
// ** By: Matthew Boik (Sharp Agency)
// **     31/08/2010
// **     Last Update: 16/11/2010

var IE = /*@cc_on!@*/false;

var Validator = {
  RunValidate: function(el, action, options) {
    if(!options) { options = {}; }
    var visibleonly = options.visibleonly == "true" ? true : false;

    if(el && action && el.nodeName == "FORM" && Validation.Text(action)) {
      var valid = true;
      for(var i=0;i<el.elements.length;i++) {
        e = el.elements[i];
        if(IE) { Element.extend(e); } // ** IE6 Fix ** //

        if(visibleonly && this._checkVisible(e) || !visibleonly) {
          if(!this.ValidateItem(e)) {
            valid = false;
          }
        }
      }

      if(valid) {
        var msg = "Are you sure the information provided is correct?";
        if(options.submitmsg) {
          msg = options.submitmsg;
        }

        if(confirm(msg)) {
          el.action = action;
          el.submit();

          // MAKE SURE THE SUBMIT BTN HAS A NAME (NOT SUBMIT)
        }
      }
      else if(this._errors.length > 0) {
        alert(this._errors);
        this._errors = "";
      }
      else {
        var msg = "Some fields have been entered incorrectly, please try again.";
        if(options.errormsg) {
          msg = options.errormsg;
        }
        alert(msg);
      }
    }
    else { alert("Incorrect use of Validator Class"); }
  },

  ValidateItem: function(e, type) {
    var valid = true;
    this._removeInvalid(e);

    var required = e.hasClassName("required");
    e.removeClassName("required");

    if(type && !e.hasClassName(type)) {
      if(!this._tryValidate(e, type)) {
        valid = false;
      }
    }

    if(required || e.value.length > 0) {
      var classes = this.getClassList(e);
      for(var j=0;j<classes.length;j++) {
        class_name = classes[j];
        switch(e.type) {
          default:
            if(!this._tryValidate(e, class_name)) {
              valid = false;
            }
          break;
        }
      }
    }
    if(required) {
      e.addClassName("required");
    }

    return valid;
  },

  //  ** Private ** //
  _tryValidate: function(e, type) {
    var valid = true;
    type = type.replace(/^validate/, "");
    type = type.replace(/^v/, "");
    try {
      if(isFunction(eval("Validation."+type)) === true) {
        var result = eval("Validation."+type+"(e.value, e);");
        if(!result) {
          this._setInvalid(e);
          valid = false;
        }
      }
    }
    catch(x) { }
    return valid;
  },
  _setInvalid: function(e) { this._removeInvalid(e); e.addClassName("invalid"); if(!IE && e.title && e.title.length > 0) { e.title = "Invalid - "+e.title; } },
  _removeInvalid: function(e) { e.removeClassName("invalid"); if(!IE) { e.title = e.title.replace("Invalid - ", ""); } },
  _errors: "",
  _checkVisible: function(e) { return e.visible() && e.getHeight() > 0 && e.getWidth() > 0 ? true : false; },


  // ** Multi-browser classlist function ** //
  getClassList: function(e) {
    var list = new Array();
    if(e.classList) { list = e.classList; }
    else if(e.getAttribute("class")) { list = e.getAttribute("class").split(" "); }
    else { list = e.className.split(" "); }
    return list;
  }
}

// ** Validations ** //
var Validation = {
  Postcode: function(v) {
    return /^[0-9]{4}$/.test(trim(v)) ? true : false;
  },
  Phone: function(v) {
    v = trim(v).replace(/[\(\)\-\s]/g, "");
    return /^([0-9]{8}|[0-9]{10}|\+[0-9]{8,20})$/.test(trim(v)) ? true : false;
  },
  Text: function(v) {
    return trim(v).length > 0 ? true : false;
  },
  Email: function(v) {
    return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(trim(v)) ? true : false;
  },
  NoSpecial: function(v) {
    return /^[a-zA-Z0-9\ ]*$/.test(trim(v)) ? true : false;
  },
  Number: function(v) {
    return /^[0-9\s]+$/.test(trim(v)) ? true : false;
  },
  Float: function(v) {
    return /^[0-9]*(\.[0-9]+)?$/.test(trim(v)) ? true : false;
  },
  Gtz: function(v) {
    return (this.Float(v) && v > 0) ? true : false;
  },
  NotDefault: function(v, e) {
    return e.value == e.defaultValue ? false : true;
  },
  CVV: function(v) {
    return /^[0-9]{2,4}$/.test(trim(v)) ? true : false;
  },
  CC: function(v) {
    v = trim(v).replace(/[\s]/g, "");
    return /^[0-9]{14,20}$/.test(trim(v)) ? true : false;
  },
  ImageUpload: function(v) {
    if(/\.(jpg|gif)$/i.test(trim(v))) {
      return true;
    }
    else {
      Validator._errors += (Validator._errors,length > 0 ? "\n" : "") + "Invalid image, only JPG or GIF files allowed";
    }
  }
}

function trim(v) {
  return v.replace(/(^(\s)+|(\s)+$)/g, "");
}
function clearDefault(e) {
  if(e.value == e.defaultValue) { e.value = ""; }
  return true;
}
function setDefault(e) {
  e.value = trim(e.value).length > 0 ? e.value : e.defaultValue;
}
function isFunction(x) {
  var valid = false;
  switch(typeof x) {
    case "function":
      valid = true;
    break;
    case "object":
      if(("function" !== typeof x.toString ) && ( "function" !== typeof x.valueOf )) {
        valid = (x + "").match(/function/) !== null;
      }
      else { valid = Object.prototype.toString.call(x) === "[object Function]"; }
    break;
    default:
      valid = false;
    break;
  }
  return valid;
}

/**** Add new site specific function
Validation.<FUNCTION NAME> = function(v,e) { // v == value && e == element
  <FUNCTION CONTENTS>
};
****/

/**** Run nominated validation on anything
valid = Validator.ValidateItem(<ELEMENT>, <FUNCTION NAME (NOT REQUIRED)>)
****/


