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

/* $Id: spgrid-convert.js 4811 2006-10-18 21:48:26Z alex $ */

/**
 * Converts cell to string data type.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertString = function(objCell) {
  if (!(objCell instanceof Object)) {
    objCell = {};
  }
  // Convert to string
  objCell.v += '';
  // Remove multiple whitespaces
  objCell.v = objCell.v.replace(/\s+/g, ' ');
  // Remove leading and trailing space
  objCell.v = objCell.v.replace(/^\s/, '').replace(/\s$/, '');
  objCell.c = objCell.o = objCell.v;
  // Remove HTML tags
  objCell.c = objCell.c.replace(/<[^>]*>/g, '');
  return objCell;
};

/**
 * Converts cell to insensitive string data type.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertInsensitiveString = function(objCell) {
  objCell = this.convertString(objCell);
  objCell.c = objCell.c.toUpperCase();
  return objCell;
};

/**
 * Converts cell to integer data type.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertInteger = function(objCell) {
  if (objCell && (objCell.v == Infinity || objCell.v == -Infinity)) {
    return objCell;
  }
  objCell = this.convertString(objCell);
  // Get only numbers and decimal
  objCell.c = objCell.c.replace(/[^0-9\.\-]/g, '');
  // Ignore any chars after decimal, no rounding
  objCell.c = objCell.c.replace(/\..*/g, '');
  // Convert to integer
  objCell.c = parseInt(objCell.c);
  if (isNaN(objCell.c)) {
    objCell.c = 0;
  }
  return objCell;
};

/**
 * Converts cell containing number in German format to integer data type.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertIntegerGerman = function(objCell) {
  if (objCell && (objCell.v == Infinity || objCell.v == -Infinity)) {
    return objCell;
  }
  objCell = this.convertString(objCell);
  // Get only numbers and decimal
  objCell.c = objCell.c.replace(/[^0-9,\-]/g, '');
  // Ignore any chars after decimal, no rounding
  objCell.c = objCell.c.replace(/,.*/g, '');
  // Convert to integer
  objCell.c = parseInt(objCell.c);
  if (isNaN(objCell.c)) {
    objCell.c = 0;
  }
  return objCell;
};

/**
 * Converts cell to float data type.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertFloat = function(objCell) {
  if (objCell && (objCell.v == Infinity || objCell.v == -Infinity)) {
    return objCell;
  }
  objCell = this.convertString(objCell);
  // Get only numbers and decimal
  objCell.c = objCell.c.replace(/[^0-9\.\-]/g, '');
  // Convert to float
  objCell.c = parseFloat(objCell.c);
  if (isNaN(objCell.c)) {
    objCell.c = 0;
  }
  return objCell;
};

/**
 * Converts cell containing number in German format to float data type.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertFloatGerman = function(objCell) {
  if (objCell && (objCell.v == Infinity || objCell.v == -Infinity)) {
    return objCell;
  }
  objCell = this.convertString(objCell);
  // Get only numbers and decimal
  objCell.c = objCell.c.replace(/[^0-9,\-]/g, '');
  objCell.c = objCell.c.replace(/,/g, '.');
  // Convert to float
  objCell.c = parseFloat(objCell.c);
  if (isNaN(objCell.c)) {
    objCell.c = 0;
  }
  return objCell;
};

/**
 * Converts cell to date data type.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertDate = function(objCell) {
  objCell = this.convertString(objCell);
  objCell.c = Date.parse(objCell.c);
  return objCell;
};

/**
 * Converts cell to time data type.
 *
 * <pre>
 * Following time formats are recognized:
 * 1) HH:MM:SS
 * 2) HH:MM:SS AM/PM
 * 3) HH:MM
 * 4) HH:MM AM/PM
 * 5) HHMMSS
 * 6) HHMMSS AM/PM
 * 7) HHMM
 * 8) HHMM AM/PM
 *
 * Any type of separator can be used.
 *
 * Examples: 1.40AM, 05-06-02, 3:8:1, 0205[PM], etc.
 * </pre>
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertTime = function(objCell) {
  objCell = this.convertString(objCell);
  // Parse value
  var arrMatch =
   objCell.c.match(/(\d{1,2})\D+(\d{1,2})(\D+(\d{1,2}))?\W*(AM|PM|A|P)?/i);
  if (!arrMatch) {
    // Try without separator
    arrMatch = objCell.c.match(/(\d{2})(\d{2})((\d{2}))?\W*(AM|PM|A|P)?/i);
  }
  // Get compare value
  if (arrMatch && arrMatch[1] && arrMatch[2]) {
    // Get hour
    var hour = arrMatch[1] * 1;
    // Correct hour
    if (arrMatch[5]) {
      var strAmPm = arrMatch[5].toUpperCase();
      if (strAmPm == 'PM' || strAmPm == 'P') {
        if (hour < 12) {
          hour += 12;
        }
      } else { // AM
        if (hour == 12) {
          hour = 0;
        }
      }
    }
    if (hour < 10) {
      hour = '0' + hour;
    }
    // Get minute
    var minute = arrMatch[2] * 1;
    // Correct minute
    if (minute < 10) {
      minute = '0' + minute;
    }
    // Get second
    var second = 0;
    if (arrMatch[4]) {
      second = arrMatch[4] * 1;
    }
    // Correct second
    if (second < 10) {
      second = '0' + second;
    }
    // Get time
    objCell.c = hour + ':' + minute + ':' + second;
  }
  return objCell;
};

/**
 * Converts cell to UNIX timestamp data type.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertTimestampLocale = function(objCell) {
  objCell = this.convertString(objCell);
  objCell.v = (new Date(parseInt(objCell.v) * 1000)).toLocaleString();
  return objCell;
};

/**
 * Converts cell to UNIX timestamp data type. Value displayed in MM/DD/YYYY
 * format.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertTimestampMMDDYYYY = function(objCell) {
  objCell = this.convertString(objCell);
  var objDate = new Date(parseInt(objCell.v) * 1000);
  var strMonth = objDate.getMonth() + 1;
  if (strMonth < 10) {
    strMonth = '0' + strMonth;
  }
  var strDay = objDate.getDate();
  if (strDay < 10) {
    strDay = '0' + strDay;
  }
  var strYear = objDate.getYear();
  if (strYear < 1900) {
    strYear += 1900;
  }
  objCell.v = strMonth + '/' + strDay + '/' + strYear;
  return objCell;
};

/**
 * Converts cell to UNIX timestamp data type. Value displayed in DD/MM/YYYY
 * format.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertTimestampDDMMYYYY = function(objCell) {
  objCell = this.convertString(objCell);
  var objDate = new Date(parseInt(objCell.v) * 1000);
  var strMonth = objDate.getMonth() + 1;
  if (strMonth < 10) {
    strMonth = '0' + strMonth;
  }
  var strDay = objDate.getDate();
  if (strDay < 10) {
    strDay = '0' + strDay;
  }
  var strYear = objDate.getYear();
  if (strYear < 1900) {
    strYear += 1900;
  }
  objCell.v = strDay + '/' + strMonth + '/' + strYear;
  return objCell;
};

/**
 * Converts cell to UNIX timestamp data type. Value displayed in YYYY/MM/DD
 * format.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertTimestampYYYYMMDD = function(objCell) {
  objCell = this.convertString(objCell);
  var objDate = new Date(parseInt(objCell.v) * 1000);
  var strMonth = objDate.getMonth() + 1;
  if (strMonth < 10) {
    strMonth = '0' + strMonth;
  }
  var strDay = objDate.getDate();
  if (strDay < 10) {
    strDay = '0' + strDay;
  }
  var strYear = objDate.getYear();
  if (strYear < 1900) {
    strYear += 1900;
  }
  objCell.v = strYear + '/' + strMonth + '/' + strDay;
  return objCell;
};

/**
 * Converts cell to boolean data type.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertBoolean = function(objCell) {
  objCell = this.convertString(objCell);
  switch (objCell.c.toUpperCase()) {
    case '0':
    case 'F':
    case 'FALSE':
    case 'N':
    case 'NO':
    case southprojects.Grid.booleanValues[0].toUpperCase():
      objCell.c = 0;
      break;
    default:
      objCell.c = 1;
  }
  objCell.v = southprojects.Grid.booleanValues[objCell.c];
  return objCell;
};

/**
 * Display values for boolean data type. Since this is static variable,
 * external application is able to substitute it with other values,
 * e.g. ['Non', 'Oui'].
 * @private
 */
southprojects.Grid.booleanValues = ['No', 'Yes'];

/**
 * Sets display values for boolean data type.
 *
 * @param {string} strNo Replacement for "No"
 * @param {string} strYes Replacement for "Yes"
 */
southprojects.Grid.prototype.setBooleanValues = function(strNo, strYes) {
  if (typeof strNo == 'string') {
    southprojects.Grid.booleanValues[0] = strNo;
  }
  if (typeof strYes == 'string') {
    southprojects.Grid.booleanValues[1] = strYes;
  }
};

/**
 * Converts cell to boolean data type.
 *
 * @param {object} objCell Cell object
 * @return Converted cell object
 * @type object
 */
southprojects.Grid.prototype.convertBooleanTF = function(objCell) {
  objCell = this.convertString(objCell);
  switch (objCell.c.toUpperCase()) {
    case '0':
    case 'F':
    case 'FALSE':
    case 'N':
    case 'NO':
    case southprojects.Grid.booleanTFValues[0].toUpperCase():
      objCell.c = 0;
      break;
    default:
      objCell.c = 1;
  }
  objCell.v = southprojects.Grid.booleanTFValues[objCell.c];
  return objCell;
};

/**
 * Display values for booleanTF data type.
 * @private
 */
southprojects.Grid.booleanTFValues = ['False', 'True'];

/**
 * Sets display values for booleanTF data type.
 *
 * @param {string} strFalse Replacement for "False"
 * @param {string} strTrue Replacement for "True"
 */
southprojects.Grid.prototype.setBooleanTFValues = function(strFalse, strTrue) {
  if (typeof strFalse == 'string') {
    southprojects.Grid.booleanTFValues[0] = strFalse;
  }
  if (typeof strTrue == 'string') {
    southprojects.Grid.booleanTFValues[1] = strTrue;
  }
};

/**
 * Associative array to access conversion method by data type name.
 * @private
 */
southprojects.Grid.convertByType = {
  'string': 'convertString',
  'istring': 'convertInsensitiveString',
  'int': 'convertInteger',
  'intGerman': 'convertIntegerGerman',
  'integer': 'convertInteger',
  'integerGerman': 'convertIntegerGerman',
  'float': 'convertFloat',
  'floatGerman': 'convertFloatGerman',
  'date': 'convertDate',
  'time': 'convertTime',
  'timestamp': 'convertTimestampLocale',
  'timestampMMDDYYYY': 'convertTimestampMMDDYYYY',
  'timestampDDMMYYYY': 'convertTimestampDDMMYYYY',
  'timestampYYYYMMDD': 'convertTimestampYYYYMMDD',
  'boolean': 'convertBoolean',
  'booleanTF': 'convertBooleanTF'
};

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

/**
 * Associative array to get class name by data type.
 * @private
 */
southprojects.Grid.classByType = {
  'string': 'spgridTypeString',
  'istring': 'spgridTypeStringInsensitive',
  'int': 'spgridTypeInt',
  'intGerman': 'spgridTypeIntGerman',
  'integer': 'spgridTypeInt',
  'integerGerman': 'spgridTypeIntGerman',
  'float': 'spgridTypeFloat',
  'floatGerman': 'spgridTypeFloatGerman',
  'date': 'spgridTypeDate',
  'time': 'spgridTypeTime',
  'timestamp': 'spgridTypeTimestampLocale',
  'timestampMMDDYYYY': 'spgridTypeTimestampMMDDYYYY',
  'timestampDDMMYYYY': 'spgridTypeTimestampDDMMYYYY',
  'timestampYYYYMMDD': 'spgridTypeTimestampYYYYMMDD',
  'boolean': 'spgridTypeBoolean',
  'booleanTF': 'spgridTypeBooleanTF'
};

/**
 * Returns class name corresponding to data type.
 *
 * @param {string} strType Data type
 * @return Class name or undefined if not found
 * @type string
 */
southprojects.Grid.prototype.getClassByType = function(strType) {
  return southprojects.Grid.classByType[strType];
};

/**
 * Associative array to get data type by class name.
 * @private
 */
southprojects.Grid.typeByClass = {
  'spgridTypeString': 'string',
  'spgridTypeStringInsensitive': 'istring',
  'spgridTypeInt': 'int',
  'spgridTypeIntGerman': 'intGerman',
  'spgridTypeFloat': 'float',
  'spgridTypeFloatGerman': 'floatGerman',
  'spgridTypeDate': 'date',
  'spgridTypeTime': 'time',
  'spgridTypeTimestampLocale': 'timestamp',
  'spgridTypeTimestampMMDDYYYY': 'timestampMMDDYYYY',
  'spgridTypeTimestampDDMMYYYY': 'timestampDDMMYYYY',
  'spgridTypeTimestampYYYYMMDD': 'timestampYYYYMMDD',
  'spgridTypeBoolean': 'boolean',
  'spgridTypeBooleanTF': 'booleanTF'
};

/**
 * Returns data type corresponding to class name. Default is "string".
 *
 * @param {string} strClass className attribute value
 * @return Data type
 * @type string
 */
southprojects.Grid.prototype.getTypeByClass = function(strClass) {
  // className may contain several classes
  var arrClasses = strClass.split(/\s+/);
  // Search first applicable class
  for (var iClass = 0; iClass < arrClasses.length; iClass++) {
    var strType = southprojects.Grid.typeByClass[arrClasses[iClass]];
    if (typeof strType != 'undefined') {
      return strType;
    }
  }
  // Default is "string"
  return 'string';
};

/**
 * Creates custom data type.
 *
 * @param {function} funcConvert Function that converts cell value
 * @param {string} strTypeName Type name
 * @param {string} strTypeClass Optional. Class name
 */
southprojects.Grid.createType = function(funcConvert, strTypeName, strTypeClass) {
  // Check arguments
  if (typeof funcConvert != 'function' || typeof strTypeName != 'string' ||
   !strTypeName.length) {
    return;
  }
  // Form method name
  var strFuncName = 'convertCustom' + strTypeName.charAt(0).toUpperCase() +
   strTypeName.substr(1);
  // Add method
  southprojects.Grid.prototype[strFuncName] = funcConvert;
  // Add type
  southprojects.Grid.convertByType[strTypeName] = strFuncName;
  // Add class
  if (typeof strTypeClass == 'string' && strTypeClass.length) {
    southprojects.Grid.classByType[strTypeName] = strTypeClass;
    southprojects.Grid.typeByClass[strTypeClass] = strTypeName;
  }
};
