/* Ajax Construction Kit  */
/* Validation Toolkit Functions  */

var Validator = new Class.create({

    validateNonEmpty: function(inputControl, helpControl) {
        // See if the input value contains any text
        return this.validateRegEx(/.+/,
            inputControl.value, helpControl,
            "必须填写.");
    },
    
    validateNumber: function(inputControl, helpControl) {
        // First see if the input value contains data
        if (!this.validateNonEmpty(inputControl, helpControl))
            return false;
    
        // Then see if the input value is a number
        return this.validateRegEx(/^[-]?\d*\.?\d*$/,
            inputControl.value, helpControl,
            "只接受数字.");
    },
    
    validateEmail: function(inputControl, helpControl) {
        // First see if the input value contains data
        if (!this.validateNonEmpty(inputControl, helpControl))
            return false;
    
        // Then see if the input value is an email address
        return this.validateRegEx(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
            inputControl.value, helpControl,
            "请填写有效的电子邮箱.");
    },
    
    validateDate: function(inputControl, helpControl) {
        // First see if the input value contains data
        if (!this.validateNonEmpty(inputControl, helpControl))
            return false;
    
        // Then see if the input value is a date
        return this.validateRegEx(/(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d/,
            inputControl.value, helpControl,
            "Please enter a date (for example, 01/14/1975).");
    },

    validateCounter2: function(inputControl, helpControl) {
        if (!inputControl.value) {
            return true; // ignore next check
		} else {
	        return this.validateRegEx(/[^9]/,
    	        inputControl.value, helpControl,
        	    "您所选择的柜台和日期预约已满,请选择其他日期或柜台。");
		}
    },

    validateCheckBox: function(inputControl, helpControl) {
        // See if the checkbox is checked
        if (!inputControl.checked) {
	        return this.validateRegEx(/^y$/, 1, helpControl, "<br />请确认阁下已阅读并接受服务条款及已经年满16周岁。 ");
        } else {
	        return this.validateRegEx(/^1$/, 1, helpControl, "");
        }
    },
    
    
    validateRegEx: function(regex, input, helpControl, helpMessage) {
      if (!regex.test(input)) {
        if (helpControl != null)
          helpControl.innerHTML = helpMessage;
          return false;
      }
      else {
        if (helpControl != null)
          helpControl.innerHTML = "";
          return true;
      }
    }
});
