﻿/// <summary>
/// 擴充型別定義及實作
/// </summary>
/// <remarks>[RayChien] 2009/10/29 Created</remarks>


/// <summary>
/// 去除字串前後空白
/// </summary>
/// <example>
/// var str_tmp = "  test ";
/// var str_out = str_tmp.trim();
/// 輸出結果: str_out = "test"
/// </example>
/// <remarks>[RayChien] 2009/10/29 Created</remarks>
if (!String.prototype.trim)
{
    String.prototype.trim=function()
    {
        return this.replace(/(^\s*)|(\s*$)/g, "");
    }
}

/// <summary>
/// 去除字串左邊空白
/// </summary>
/// <example>
/// var str_tmp = "  test ";
/// var str_out = str_tmp.ltrim();
/// 輸出結果: str_out = "test "
/// </example>
/// <remarks>[RayChien] 2009/10/29 Created</remarks>
if (!String.prototype.ltrim)
{
    String.prototype.ltrim=function()
    {
        return this.replace(/(^\s*)/g,"");   
    }   
}

/// <summary>
/// 去除字串右邊空白
/// </summary>
/// <example>
/// var str_tmp = "  test ";
/// var str_out = str_tmp.rtrim();
/// 輸出結果: str_out = "  test"
/// </example>
/// <remarks>[RayChien] 2009/10/29 Created</remarks>
if (!String.prototype.rtrim)
{
    String.prototype.rtrim=function()
    {
        return this.replace(/(\s*$)/g,"");
    }
}

/// <summary>
/// 從左邊擷取指定長度的字串
/// </summary>
/// <param name="p_int_number">要擷取的數目</param>
/// <example>
/// var str_tmp = "test";
/// var str_out = str_tmp.left(2);
/// 輸出結果: str_out = "te"
/// </example>
/// <remarks>[RayChien] 2009/10/29 Created</remarks>
if (!String.prototype.left)
{
    String.prototype.left = function(p_int_number)
    {
        return this.slice(0,p_int_number);
    }
}

/// <summary>
/// 從右邊擷取指定長度的字串
/// </summary>
/// <param name="p_int_number">要擷取的數目</param>
/// <example>
/// var str_tmp = "test";
/// var str_out = str_tmp.right(2);
/// 輸出結果: str_out = "st"
/// </example>
/// <remarks>[RayChien] 2009/10/29 Created</remarks>
if (!String.prototype.right)
{
    String.prototype.right = function(p_int_number)
    {
        return this.slice(this.length-p_int_number);
    }
}

/// <summary>
/// 只保留字串中的數字
/// </summary>
/// <example>
/// var str_tmp = "test2123456";
/// var str_out = str_tmp.getNum();
/// 輸出結果: str_out = "2123456"
/// </example>
/// <remarks>[RayChien] 2009/10/29 Created</remarks>
if (!String.prototype.getNum)
{
    String.prototype.getNum = function()
    {
        return this.replace(/\D*/g,"");
    }
}

/// <summary>
/// Array 的 indexOf 字串搜尋
/// </summary>
/// <param name="p_var_elt">要比對的資料</param>
/// <param name="p_int_number">option: 從哪個位置開始尋找，預設是0</param>
/// <example>
/// var str_arr = new Array();
/// str_arr[0] = "raychien";
/// str_arr[1] = "tutor";
/// str_arr[2] = 123;
/// 輸出結果: str_arr.indexOf("tutor");     // 1
/// 輸出結果: str_arr.indexOf("tutor", 2);  // 從第二個位置開始找，因為"tutor"在陣列的第1個位置 所以回傳-1
/// 輸出結果: str_arr.indexOf(123);         // 2
/// </example>
/// <returns>find: 被搜尋字串的位置  not find: -1</returns>
/// <remarks>[RayChien] 2009/10/29 Created</remarks>
if (!Array.prototype.indexOf)
{
    Array.prototype.indexOf = function(p_var_elt)
    {
        var int_len = this.length;
        var int_from = Number(arguments[1]) || 0;
        int_from = (int_from < 0) ? Math.ceil(int_from) : Math.floor(int_from);

        if (int_from < 0)
        {
            int_from += int_len;
        }

        for (; int_from < int_len; int_from++)
        {
            if (int_from in this &
                this[int_from] === p_var_elt)
            {
                return int_from;
            }
        }
        return -1;
    };
}