String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g,""); }

function Img(src)
{
    this.preload = function()
    {
        var new_img = new Image();
        new_img.src = this.getSrc();
    }
    this.get = function()
    {
        return "<img src=\"" + this.getSrc() + "\" />";
    }
    this.getSrc = function()
    {
        return this.src;
    }

    this.src = src;
    this.preload();
}


/**
 *
 *
 */
function preloadImage(mix)
{
    if(typeof mix == 'string')
    {
        new Image().src = mix;
    }
    else
    {
        for(var i in mix)
        {
            new Image().src = mix[i];
        }
    }
}

/**
 * 获取文件扩展名
 * dellker.com 2007-10-31
 * wkkes.com 2008-9-3 17:44
*/
function getFileext(sFile, bToLower) {
  if(! sFile) {
    return null;
  } else {
    var a = sFile.split('.');
    if(a.length>0) {
      return bToLower === true ? a[a.length-1].toLowerCase() : a[a.length-1];
    } else {
      return null;
    }
  }
}


/**
 * 加入收藏夹方法
 * 兼容 FF & IE
*/
function addFavorite(sURL, sTitle) {
  try {
    window.external.addFavorite(sURL, sTitle);
  } catch(e) {
    try {
      window.sidebar.addPanel(sTitle, sURL, "");
    } catch(e) {
      alert("加入收藏失败，请使用Ctrl+D进行添加");
    }
  }
}

/* 判断是否包含中文
*/
function haveChinese($s)
{
  return escape($s).indexOf('%u') < 0 ? false : true;
}

/* 显示日期
*/
function showDate()
{
  var s = '';
  var d = new Date(); 
  s += ((document.all)?d.getYear():d.getYear()+1900)+'-'+(d.getMonth()+1)+'-'+d.getDate()+'';
  s += ' ' + d.getHours() + ':' + (d.getMinutes()*1 > 9 ? d.getMinutes() : '0'+d.getMinutes());
  s += '<span style="background-color:lightyellow;border:solid 1px #EFEFEF;padding:2px;margin-left:3px;margin-right:3px;">';
  switch(d.getDay()) {
   case 0:
    s += '  <font color="#FF0000">Sun';
    break;
   case 1:
    s += '  <font color="#555555">Mon';
    break;
   case 2:
    s += '  <font color="#555555">Tue';
    break;
   case 3:
    s += '  <font color="#555555">Wed';
    break;
   case 4:
    s += '  <font color="#555555">Thu';
    break;
   case 5:
    s += '  <font color="#555555">Fri';
    break;
   case 6:
    s += '  <font color="#FF0000">Sat';
    break;
  }
  s += '</font></span>';
  //s += ':' + d.getSeconds();
  document.write(s);
  //setTimeout('showDate()', 1000);
}








/**
 * Cookie Javascript 操作函数
 *
 * 获取Cookie val = _cookie(id);
 *
 * 更改Cookie _cookie(id, val, [expir_sec]);
 * expir_sec 为失效时间，单位为秒，可选参数，默认为 60小时
 */
function _cookie()
{
	var _ = _cookie.arguments;
    if(_.length == 1)
    {
        var re = new RegExp(" \\b"+_[0]+"=([^;]*)","ig");
        return re.exec(document.cookie) ? unescape(RegExp.$1) : null;
    }
	else if(_.length == 2 || _.length == 3)
    {
        if(_.length==3)
        {
            var expires = _[2] * 1000;
        }
        else
        {
            var expires = ( +new Date + 60 * 60 * 60 * 1000 );
        }
	    document.cookie = _[0] + "=" + escape(_[1]) + ";expires="
            + (new Date(expires)).toGMTString();
    }
}




/**
 * php.js segment
 */
function http_build_query( formdata, numeric_prefix, arg_separator ) {
    var key, use_val, use_key, i = 0, j=0, tmp_arr = [];

    if (!arg_separator) {
        arg_separator = '&';
    }

    for (key in formdata) {
        use_val = urlencode(formdata[key].toString());
        use_key = urlencode(key);

        if (numeric_prefix && !isNaN(key)) {
            use_key = numeric_prefix + j;
            j++;
        }
        tmp_arr[i++] = use_key + '=' + use_val;
    }

    return tmp_arr.join(arg_separator);
}

function urlencode( str ) {
    var histogram = {}, tmp_arr = [];
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}
