var REGEX_REQUIRED = /.+/;
var REGEX_EMAIL = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var REGEX_POSTCODE = /^([A-Za-z]{1,2})([0-9]{1,2})\s{0,1}([0-9]{1})([A-Za-z]{2})$/;
var REGEX_ZIP = /^\d{5}([\-]\d{4})?$/;

var VALIDATE_MESSAGE_REQUIRED = "This is a required field";
var VALIDATE_MESSAGE_EMAIL = "Please enter a valid email address";
var VALIDATE_MESSAGE_POSTCODE = "Please enter a valid post code";
var VALIDATE_MESSAGE_ZIP = "Please enter a valid ZIP code";

var validationPopup;
var validationPopupText;

function validateAll() {
  var pass = true;
  var lbl;
  var msg;
  for (var i=0; i < arguments.length; i++ ) {
    if(i == 0) lbl = $elm(arguments[i])
    else if(i == 1) msg = arguments[i]
    else {
      var elm = $elm(arguments[i]);
      var toEval = getEventContent(elm.getAttribute("onfocus"));
      if(toEval != "" && toEval != null && toEval != "null")
      {
        var val = eval(toEval);
        if(!val)
          pass = false;
      }
    }
  }
  if(!pass) lbl.innerHTML = msg;
  else lbl.innerHTML = "";


  return pass;
}

function getEventContent(evt) {
  var toEval = String(evt).replace("this","\"" + elm.id + "\"");
  toEval = replaceAll(toEval,"\r","");
  toEval = replaceAll(toEval,"\n","");
  var re = new RegExp(/function.*?{\s*(.*)\s*}/gim);
  var m = re.exec(toEval);
  if(m != null) toEval = m[1];
  if(toEval.indexOf("return ") == 0) {
    toEval = toEval.substring(7, toEval.length);
  }
  return toEval;
}

function validatePostcode() {
  var v = new Validator(REGEX_POSTCODE, VALIDATE_MESSAGE_POSTCODE);
  return v.validate(arguments);
}

function validateZip() {
  var v = new Validator(REGEX_ZIP, VALIDATE_MESSAGE_ZIP);
  return v.validate(arguments);
}

function validateEmail() {
  var v = new Validator(REGEX_EMAIL, VALIDATE_MESSAGE_EMAIL);
  return v.validate(arguments);
}

function validate() {
  var v = new Validator(REGEX_REQUIRED, VALIDATE_MESSAGE_REQUIRED);
  return v.validate(arguments);
}

function validateCustom(elm,regex,msg,group,blank) {
  var v = new Validator(regex,msg,group,2,blank);
  return v.validate(elm);
}

function replaceAll(text, strA, strB) {
    while ( text.indexOf(strA) != -1) {
        text = text.replace(strA,strB);
    }
    return text;
}

function $elm() {
  elm = arguments[0];
  if(typeof(elm) == "string") {
    elm = document.getElementById(elm);
  }
  return elm;
}

////////////////////////////////////////////////////////////////////////////////////////////

_validationPopups = Array();

function Validator() {
  if(arguments[0])
    this._regex = arguments[0];
  else this._regex = '.+';

  if(arguments[1])
    this._message = arguments[1];
  else this._message = 'Please enter a valid input';

  if(arguments[2])
    this._group = arguments[2];
  else this._group = "global";

  if(arguments[3])
    this._style = arguments[3];
  else this._style = 1;

  if(arguments[4])
    this._blank = arguments[4];
  else this._blank = false;
} 

Validator.prototype._regex;
Validator.prototype._blank;
Validator.prototype._message;
Validator.prototype._style;
Validator.prototype._popup;
Validator.prototype._popupPointer;
Validator.prototype._popupText;
Validator.prototype._group;

Validator.prototype.getRegex = function() {
  return this._regex;
}

Validator.prototype.getBlank = function() {
  return this._blank;
}

Validator.prototype.getMessage = function() {
  return this._message;
}

Validator.prototype.getStyle = function() {
  return this._style;
}

Validator.prototype.setRegex = function() {
  this._regex = arguments[0];
}

Validator.prototype.setBlank = function() {
  this._blank = arguments[0];
}

Validator.prototype.setMessage = function() {
  this._message = arguments[0];
}

Validator.prototype.setStyle = function() {
  this._style = arguments[0];
}

Validator.prototype.validate = function() {
  var pass = true;
  if(this.getRegex() == "" || this.getRegex() == null)
    return true;

  for (var i = 0; i < arguments.length; i++ ) {
    var thisPass = true;
    var elm = $elm(arguments[i]);

    addEvent(elm,"blur",function(){eval(getEventContent(this.getAttribute("onfocus")))},false);
    if(elm.type == "radio")
    {
      var elms = document.getElementsByName(elm.name);
      thisPass = false;
      for(var j = 0; j < elms.length; j++)
      {
        if(elms[j].checked)
          thisPass = true;
      }
      if(this.getBlank())
        thisPass = true;
    }
    else
      thisPass = (this.getBlank() && (elm.value == "" || elm.value == null)) || elm.value.match(this.getRegex());
    if(!thisPass) {
      this.showValidationMessage(elm);
      if(!elm.className.match("validation_error"))
        elm.className = elm.className + " " + "validation_error";
      pass = false;
    }
    else if(elm.className.match("validation_error")) {
      elm.className = elm.className.replace("validation_error","");
      this.hideValidationMessage();
    }
  }
  return pass;
}

Validator.prototype.hideValidationMessage = function() {
  if(_validationPopups[this._group] != null)
    _validationPopups[this._group].hide();
}

Validator.prototype.showValidationMessage = function(elm) {
  if(_validationPopups[this._group] == null)
    _validationPopups[this._group] = new ValidatorPopup();
  _validationPopups[this._group].show(elm,this.getMessage(),this.getStyle());
}

function ValidatorPopup() {
}

ValidatorPopup.prototype._box;
ValidatorPopup.prototype._pointer;
ValidatorPopup.prototype._text;

ValidatorPopup.prototype.show = function(elm,message,style) {
  if(!this._box) {
    this._box = document.createElement("div");
    this._pointer = document.createElement("img");
    this._pointer.setAttribute("src", "/image registry/image web/008249.gif");
    this._text = document.createElement("div");
    this._pointer.className = "validation_popup_pointer validation_popup_pointer_" + style;
    this._box.appendChild(this._pointer);
    this._box.appendChild(this._text);
    this._box.className = "validation_popup validation_popup_" + style;
    document.body.appendChild(this._box);
    this._box.style.display = "none";
  }
  if(this._box.style.display == "none")
  {
    this._box.style.display = "block";
    this._text.innerHTML = message;
    this._box.style.left = (parseInt(getElementPositionX(elm)) + parseInt(elm.offsetWidth)) + "px";
    this._box.style.top = getElementPositionY(elm) + "px";
  }
}

ValidatorPopup.prototype.hide = function() {
  if(this._box)
    this._box.style.display = "none";
}

