// 将传入任何类型的参数转换成字符串
function toStr(S)
{
  if (S == undefined || typeof(S) == "undefined" || S == null)
    return "";
    
  if (typeof(S) != "string")
    return new String(S);

  return S;
}

// 为字符串增加前导的"0"，参数Length为加0后需要达到的总长度
function AddLeadingZero(S, Length)
{
  S = toStr(S);

  if (S.length < Length)
    return RepeatString("0", Length - S.length) + S;
  else
    return S;
}

//截取字符串两端的空格
function trim(str)
{
var i, iStart, iEnd;

  iStart = -1;
  for (i = 0; i < str.length; i++)
  {
    if (str.charAt(i) != ' ')
    {
      iStart = i;
      break;
    }
  }

  if (iStart == -1) return '';

  iEnd = str.length;
  for (i = str.length - 1; i >=0; i--)
  {
    if (str.charAt(i) != ' ')
    {
      iEnd = i;
      break;
    }
  }
  
  return str.substring(iStart, iEnd + 1)
}

// 重复产生字符串
function RepeatString(S, Count)
{
  var i, Result;

  S = toStr(S);
  
  if (S == "")
    return "";

  Result = "";
  for (i = 0; i < Count; i++)
    Result += S;
  return Result;  
} 

// 把日期D增加或减去若干月Value
function AddMonth(D, Value)
{
  var year, month, day;
  
  year = D.getFullYear();
  month = D.getMonth();
  day = D.getDate();
  
  year = year + parseInt((month + Value) / 12);
  month = (month + Value) % 12;
  
  return new Date(year, month, day);  
}

// 把日期D增加或减去若干天Value
function AddDay(D, Value)
{
  return new Date(D.getTime() + Value * 24 * 3600 * 1000);  
}

// Date类型格式化为YYYY-MM-DD HH:NN:SS的字符串
function FormatDateTime(D)
{
  var D = new Date(D);
  return D.getFullYear()
    + "-" + AddLeadingZero(D.getMonth() + 1, 2)
    + "-" + AddLeadingZero(D.getDate(), 2)
    + " " + AddLeadingZero(D.getHours(), 2)
    + ":" + AddLeadingZero(D.getMinutes(), 2)
    + ":" + AddLeadingZero(D.getSeconds(), 2);
}


// Date类型格式化为YYYY-MM-DD的字符串
function FormatDate(D)
{
  var D = new Date(D);
  return D.getFullYear()
    + "-" + AddLeadingZero(D.getMonth() + 1, 2)
    + "-" + AddLeadingZero(D.getDate(), 2);
}

// Date类型格式化为YYYYMMDD的字符串
function FormatDate2(D)
{
  var D = new Date(D);
  return D.getFullYear()
    + AddLeadingZero(D.getMonth() + 1, 2)
    + AddLeadingZero(D.getDate(), 2);
}

// 字符串替换，JavaScript的replace方法需要使用/g才能全部替换，否则只会替换第一个 
function StringReplace(S, Old, New)
{
  var index, Result;
  
  S = toStr(S);
  Old = toStr(Old);
  New = toStr(New);
  
  if (S == "" || Old == "") 
    return S;

  Result = "";
  index = S.indexOf(Old);
  while (index != -1)
  {
    Result += S.substr(0, index) + New;
    S = S.substring(index + Old.length, S.length);
    index = S.indexOf(Old);
  }
  if (S != "")
    Result += S;
      
  return Result;
} 

// 替换字符串中的特殊字符为URL表达形式，使之安全
function FilterURL(S)
{
  var Result;
  
  S = toStr(S);

  if (S == "")
    return S;
  
  Result = S;
  Result = StringReplace(Result, "<", "%3c");
  Result = StringReplace(Result, ">", "%3e");
  Result = StringReplace(Result, " ", "%20");
  Result = StringReplace(Result, "#", "%23");
  Result = StringReplace(Result, "+", "%2B");
  
  return Result;
}

function isSmallWindow() {
  return document.body.offsetWidth <= 1024;
}

