/*
 * validation.js: Scripts for fields validation in javascript
 *
 * Requirements:
 * JQuery
 */

/*
 * Constructor
 *
 * Receive: error class to be set in case of validation error
 */
function Validation(errorClass) {
    if (!this.checkText(errorClass))
        this.errorClass = error; // default value
    else
        this.errorClass = errorClass;
    this.validateList = new Array();
}

Validation.prototype.TYPE_RADIO = "radio";
Validation.prototype.TYPE_TEXT = "text";
Validation.prototype.TYPE_EMAIL = "email";
Validation.prototype.TYPE_NUMBER = "number";

Validation.prototype.TYPE_SIZE_MIN = "size_min";
Validation.prototype.TYPE_SIZE_MAX = "size_max";

Validation.prototype.PARAM_FIELD_ID = "fieldId";
Validation.prototype.PARAM_ERROR_FIELD_ID = "errorFieldId";
Validation.prototype.PARAM_LABEL_FIELD_ID = "labelFieldId";
Validation.prototype.PARAM_TYPE = "type";
Validation.prototype.PARAM_IS_ENABLED = "isEnabled";
Validation.prototype.PARAM_SIZE = "size";

/*
 * registerField(fieldId, errorFieldId, labelFieldId, type, isEnabled): Register a field to be validated
 *
 * Receive:
 * - fieldId: Field id. Important: use name for the radio type
 * - errorFieldId: 
 * - labelFieldId:
 * - type:
 * - isEnabled:
 */
Validation.prototype.registerFieldSize = function(fieldId, errorFieldId, labelFieldId, type, size, isEnabled) {
    var record = new Array();
      
    record[this.PARAM_FIELD_ID]       = fieldId;
    record[this.PARAM_ERROR_FIELD_ID] = errorFieldId;
    record[this.PARAM_LABEL_FIELD_ID] = labelFieldId;
    record[this.PARAM_SIZE]           = size;
    record[this.PARAM_TYPE]           = type;
    record[this.PARAM_IS_ENABLED]     = isEnabled;
    
    this.validateList[this.validateList.length] = record;
}

/*
 * registerField(fieldId, errorFieldId, labelFieldId, type, isEnabled): Register a field to be validated
 *
 * Receive:
 * - fieldId: Field id. Important: use name for the radio type
 * - errorFieldId: 
 * - labelFieldId:
 * - type:
 * - isEnabled:
 */
Validation.prototype.registerField = function(fieldId, errorFieldId, labelFieldId, type, isEnabled) {
    var record = new Array();
      
    record[this.PARAM_FIELD_ID]       = fieldId;
    record[this.PARAM_ERROR_FIELD_ID] = errorFieldId;
    record[this.PARAM_LABEL_FIELD_ID] = labelFieldId;
    record[this.PARAM_TYPE]           = type;
    record[this.PARAM_IS_ENABLED]     = isEnabled;
    
    this.validateList[this.validateList.length] = record;
}

/*
 * checkRadio(object): Check the radio buttons
 *
 * Receive: The radio button name
 * Return: True if the radio button is filled
 */
Validation.prototype.checkRadio = function(radioName) {
    var radioChecked = $("input[@type=radio][@name="+radioName+"][@checked]").val();
    if (radioChecked == null)
        return false;
    return true;
}

/*
 * checkNumerFormat(email): Check the number format
 *
 * Receive: The numer as String
 * Return: True if is a valid number format
 */
Validation.prototype.checkNumberFormat = function(email) {
	var filter  = /^[0-9]+$/;
	return filter.test(email);
}


/*
 * checkMailFormat(email): Check the email format
 *
 * Receive: The email as String
 * Return: True if is a valid email format
 */
Validation.prototype.checkEmailFormat = function(email) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);
}

/*
 * checkSize(type, value): Check the text fields size
 *
 * Receive: The field
 * Return: True if the text field is within the size limit
 */
Validation.prototype.checkSize = function(field) {
  if(field[this.PARAM_TYPE] == this.TYPE_SIZE_MIN) {
    return $(field[this.PARAM_FIELD_ID]).val().length >= field[this.PARAM_SIZE];
  }
  if(field[this.PARAM_TYPE] == this.TYPE_SIZE_MAX) {
    return $(field[this.PARAM_FIELD_ID]).val().length <= field[this.PARAM_SIZE];
  }
	return false;
}

/*
 * checkText(value): Check the text fields
 *
 * Receive: The text field
 * Return: True if the text field is not empty
 */
Validation.prototype.checkText = function(value) {
	if(value == "" || $.trim(value) == "")
		return false;
	return true;
}

/*
 * Set if the field is enabled or disabled for validation.
 */
Validation.prototype.enableValidation = function(field, isEnabled) {
    this.validateList[field][this.PARAM_IS_ENABLED] = isEnabled;
}

/*
 * Clear all fields error
 */
Validation.prototype.clearErrors = function() {
    var i;

    for (i = 0; i < this.validateList.length; i++) {
        $(this.validateList[i][this.PARAM_ERROR_FIELD_ID]).hide();
        $(this.validateList[i][this.PARAM_LABEL_FIELD_ID]).removeClass(this.errorClass);
    }
} 

/*
 * Validate the fields enabled.
 */
Validation.prototype.validate = function() {
    var i;
    var errorNum = 0;

    this.clearErrors();

    for (i = 0; i < this.validateList.length; i++) {

        var type = this.validateList[i][this.PARAM_TYPE];

        if (this.validateList[i][this.PARAM_IS_ENABLED] &&
            ((type == this.TYPE_RADIO && !this.checkRadio(this.validateList[i][this.PARAM_FIELD_ID])) ||
             (type == this.TYPE_EMAIL && !this.checkEmailFormat($(this.validateList[i][this.PARAM_FIELD_ID]).val())) ||
             (type == this.TYPE_NUMBER && !this.checkNumberFormat($(this.validateList[i][this.PARAM_FIELD_ID]).val())) ||
             ((type == this.TYPE_SIZE_MIN || type == this.TYPE_SIZE_MAX) && !this.checkSize(this.validateList[i])) ||
             (type == this.TYPE_TEXT  && !this.checkText($.trim($(this.validateList[i][this.PARAM_FIELD_ID]).val()))))) {
            errorNum++;
            $(this.validateList[i][this.PARAM_ERROR_FIELD_ID]).show();
            $(this.validateList[i][this.PARAM_LABEL_FIELD_ID]).addClass(this.errorClass);
        }
    }
    return errorNum;
} 

