/**
 * @fileoverview Plugin for southprojects Grid to provide validation methods for cells
 * depending on their data types.
 *
 * <pre>
 * Copyright (c) 2006 by SouthProjects
 * http://www.southprojects.com
 * Santiago, Chile,
 * Version 1.2 Dec 2006.
 * All rights reserved.
 * </pre>
 */

/* $Id: spgrid-validate.js 4672 2006-10-10 13:09:04Z shacka $ */

/**
 * Validates cell of integer data type.
 *
 * @param {object} objCell Cell object
 * @return True if valid
 * @type boolean
 */
southprojects.Grid.prototype.validateInteger = function(objCell) {
  // Infinity is valid
  if (objCell && (objCell.v == Infinity || objCell.v == -Infinity)) {
    return true;
  }
  // Validate original value
  return /^\-?\d+$/.test(objCell.o);
};

/**
 * Validates cell of float data type.
 *
 * @param {object} objCell Cell object
 * @return True if valid
 * @type boolean
 */
southprojects.Grid.prototype.validateFloat = function(objCell) {
  // Infinity is valid
  if (objCell && (objCell.v == Infinity || objCell.v == -Infinity)) {
    return true;
  }
  // Validate original value
  return /^\-?\d*\.?\d+$/.test(objCell.o);
};

/**
 * Validates cell of date data type.
 *
 * @param {object} objCell Cell object
 * @return True if valid
 * @type boolean
 */
southprojects.Grid.prototype.validateDate = function(objCell) {
  // Validate original value
  return !isNaN(Date.parse(objCell.o));
};

/**
 * Validates cell of time data type.
 *
 * @param {object} objCell Cell object
 * @return True if valid
 * @type boolean
 */
southprojects.Grid.prototype.validateTime = function(objCell) {
  // Validate original value
  var arrMatch =
   objCell.o.match(/(\d{1,2})\D+(\d{1,2})(\D+(\d{1,2}))?\W*(AM|PM)?/i);
  if (!arrMatch) {
    // Try without separator
    arrMatch = objCell.o.match(/(\d{2})(\d{2})((\d{2}))?\W*(AM|PM)?/i);
  }
  if (arrMatch && arrMatch[1] && arrMatch[2]) {
    return true;
  }
  return false;
};

/**
 * Validates cell of timestamp data type.
 *
 * @param {object} objCell Cell object
 * @return True if valid
 * @type boolean
 */
southprojects.Grid.prototype.validateTimestamp = function(objCell) {
  // Validate original value
  return /^\d+$/.test(objCell.o);
};

/**
 * Validates cell of boolean data type.
 *
 * @param {object} objCell Cell object
 * @return True if valid
 * @type boolean
 */
southprojects.Grid.prototype.validateBoolean = function(objCell) {
  // Validate original value
  switch (objCell.o.toUpperCase()) {
    case '0':
    case '1':
    case 'F':
    case 'T':
    case 'FALSE':
    case 'TRUE':
    case 'N':
    case 'Y':
    case 'NO':
    case 'YES':
      return true;
  }
  if (southprojects.Grid.booleanValues) {
    switch (objCell.o.toUpperCase()) {
      case southprojects.Grid.booleanValues[0].toUpperCase():
      case southprojects.Grid.booleanValues[1].toUpperCase():
        return true;
    }
  }
  return false;
};

/**
 * Validates cell of booleanTF data type.
 *
 * @param {object} objCell Cell object
 * @return True if valid
 * @type boolean
 */
southprojects.Grid.prototype.validateBooleanTF = function(objCell) {
  // Validate original value
  switch (objCell.o.toUpperCase()) {
    case '0':
    case '1':
    case 'F':
    case 'T':
    case 'FALSE':
    case 'TRUE':
    case 'N':
    case 'Y':
    case 'NO':
    case 'YES':
      return true;
  }
  if (southprojects.Grid.booleanTFValues) {
    switch (objCell.o.toUpperCase()) {
      case southprojects.Grid.booleanTFValues[0].toUpperCase():
      case southprojects.Grid.booleanTFValues[1].toUpperCase():
        return true;
    }
  }
  return false;
};

/**
 * Associative array to access validation method by data type name.
 * @private
 */
southprojects.Grid.validateByType = {
  'int': 'validateInteger',
  'integer': 'validateInteger',
  'float': 'validateFloat',
  'date': 'validateDate',
  'time': 'validateTime',
  'timestamp': 'validateTimestamp',
  'timestampMMDDYYYY': 'validateTimestamp',
  'timestampDDMMYYYY': 'validateTimestamp',
  'timestampYYYYMMDD': 'validateTimestamp',
  'boolean': 'validateBoolean',
  'booleanTF': 'validateBooleanTF'
};

/**
 * Returns name of validation method corresponding to data type.
 *
 * @param {string} strType Data type
 * @return Validation method name or undefined if not found
 * @type string
 */
southprojects.Grid.prototype.getValidateByType = function(strType) {
  return southprojects.Grid.validateByType[strType];
};

/**
 * Creates validation method for custom data type.
 *
 * @param {function} funcConvert Function that validates cell value
 * @param {string} strTypeName Type name
 */
southprojects.Grid.createValidation = function(funcValidate, strTypeName) {
  // Check arguments
  if (typeof funcValidate != 'function' || typeof strTypeName != 'string' ||
   !strTypeName.length) {
    return;
  }
  // Form method name
  var strFuncName = 'validateCustom' + strTypeName.charAt(0).toUpperCase() +
   strTypeName.substr(1);
  // Add method
  southprojects.Grid.prototype[strFuncName] = funcValidate;
  southprojects.Grid.validateByType[strTypeName] = strFuncName;
};
