/*
 函数名称：trim
 函数功能: 去除字符串头部和尾部的空格
 传入参数：字符串变量
 传出结果：处理后的子串
*/
function trim(str){
  return str.replace(/(^\s*)|(\s*$)/g, "");
}


/*  函数功能：判断传入参数是否为yyyy-mm-dd或
     yyyy/mm/dd格式的正确日期
     2001/01/2和2001-3-04也是允许的格式
     如果是，则返回一个对应的日期对象
     如果否，则返回false
*/
function isDate(strDate){
  var regYear = /\d{4}[-/]/g;	//year pattern
  var regMonth;
  var regDay =  /\d{1,2}/g;;
  var chrSeperator;
  var arr,str;
  if ((arr = regYear.exec(strDate)) == null)
    return false;
  var intYearlen = arr.lastIndex - arr.index - 1;
  if (arr.index != 0 || (intYearlen != 4 && intYearlen != 2))
    return false;
  str = arr[0];
  chrSeperator = str.charAt(str.length - 1);  // get the seperator ('-' or '/')
  intYear = parseInt(str.substr(0, str.length - 1));	// get the year
  if (intYear < 1900 || intYear > 2099)  //Error Year
    return false;

  strDate = strDate.substr(arr.lastIndex);
  if (chrSeperator == "-")
    regMonth = /\d{1,2}[-]/g;
  else
    regMonth = /\d{1,2}[/]/g;
  if ((arr = regMonth.exec(strDate)) == null)
    return false;
  if (arr.index != 0)
    return false;
  str = arr[0];
  if (str.charAt(0) == '0') {
    intMonth = parseInt(str.substr(1, str.length - 2)); // get the month
  } else {
    intMonth = parseInt(str.substr(0, str.length - 1)); // get the month
  }
  if (intMonth < 1 || intMonth > 12) //Error Month
    return false;

  strDate = strDate.substr(arr.lastIndex);

  if ((arr = regDay.exec(strDate)) == null)
    return false;
  if (arr.index != 0 || arr.lastIndex != strDate.length)
    return false;
  str = arr[0];
  if (str.charAt(0) == '0') {
    intDay = parseInt(str.substr(1, str.length - 1)); // get the day
  } else {
    intDay = parseInt(str); // get the day
  }
  if (intDay < 1 || intDay > 31)  //Error Day
    return false;

  datDate = new Date(intYear, intMonth - 1, intDay); //Test the Date
  if (isNaN(datDate))  //Error Date Format
    return false;
  if (datDate.getMonth() != intMonth - 1 || datDate.getDate() != intDay)  //invalid date such as '1999/02/29' and '1999/04/31'
    return false;
  return datDate;  //Return the Date in parsed format
}


function isMail(str) {
  var a=str.indexOf("@")+1;
  var p=str.indexOf(".")+1;
  if(str.indexOf("'") > 0)
    return false;
  if(str.indexOf('"') > 0)
    return false;
  if (a<2)
    return false;
  if (p<1)
    return false;
  if (p<a+2)
    return false;
  if (str.length==p)
    return false;
  return true;
}

function isFloat(str) {
  var count = 0;
  for(var i=0;i<str.length;i++){
    if(str.charAt(i)=="."){
	count++;
      }
  }
  if(count>1){
   return false;
  }
  var ch=str.charAt(0);
  if( ch == "." ) return false;
  for (var i=0; i < str.length; i++)
             {	ch=str.charAt(i);
  if ((ch != ".") && (ch != "0") && (ch != "1") && (ch != "2") && (ch != "3") && (ch != "4") && (ch != "5") && (ch != "6") && (ch != "7") && (ch != "8") && (ch != "9"))
    return false;
  }
  return true;
}

function isInt(str) {
  for (var i=0; i < str.length; i++)
             {	var ch=str.charAt(i);
    if ((ch != "0") && (ch != "1") && (ch != "2") && (ch != "3") && (ch != "4") && (ch != "5") && (ch != "6") && (ch != "7") && (ch != "8") && (ch != "9"))
    return false;
  }
  return true;
}


/*
 函数名称：checkString()
 函数功能: 不能包含’、”、<、>、:、;等特殊字符;
  合法字符：32（空格）、38 (&) 、40~57（(),等字符与数字）、65~90（大写字符）、95（下划线）、97~122（小写字符）、>127（汉字）。
 传入参数：字符串变量
 传出结果：是否合法
*/
function checkString(str){
  var strChar = str;
  var isValid = true;
  for (var i = 0; i < str.length; i++){
    if ( (str.charCodeAt(i) == 32)|| (str.charCodeAt(i) == 38) || ((str.charCodeAt(i) >= 40) && (str.charCodeAt(i) <= 57)) ||
	 ((str.charCodeAt(i) >= 65) && (str.charCodeAt(i) <= 90)) || (str.charCodeAt(i) == 95) ||
	((str.charCodeAt(i) >= 97) && (str.charCodeAt(i) <= 122)) || (str.charCodeAt(i) > 127) ) {
      // do nothing
    } else {
      isValid = false;
      break;
    }
  }
  return isValid;
}
/*
 函数名称：checkEnString()
 函数功能: 不能包含’、”、<、>、:、;等特殊字符;以及中文
  合法字符：38 (&) 、40~57（(),等字符与数字）、65~90（大写字符）、95（下划线）、97~122（小写字符）。
 传入参数：字符串变量
 传出结果：是否合法
*/

function checkEnString(str){
  var strChar = str;
  var isValid = true;
  for (var i = 0; i < str.length; i++){
    if ( (str.charCodeAt(i) == 38) || ((str.charCodeAt(i) >= 40) && (str.charCodeAt(i) <= 57)) ||
	 ((str.charCodeAt(i) >= 65) && (str.charCodeAt(i) <= 90)) || (str.charCodeAt(i) == 95) ||
	((str.charCodeAt(i) >= 97) && (str.charCodeAt(i) <= 122))  ) {
      // do nothing
    } else {
      isValid = false;
      break;
    }
  }
  return isValid;
}
/*
 函数名称：format
 函数功能: 格式化 金钱类型的小数 #0.00
 传入参数：浮点数
 传出结果：处理后的串
*/
function formatStr(strNumber){
  strNumber=strNumber*1000+5;
  strNumber=parseInt(strNumber/10)/100;
  if(strNumber==parseInt(strNumber)){
    strNumber=strNumber+".00";
  }
  else if(strNumber*10==parseInt(strNumber*10)){
    strNumber=strNumber+"0";
  }
  return strNumber;
}
/*
校验字符串中是否含有非法的"和'字符
*/
function checkQuote(str){
  if(str==null){
    return false;
  }
  var partern=/"|'/g;
  var s = str.match(partern);
  if(s!=null){
   return true;
  }
  else{
    return false;
  }
}


/* 取得字符串的字节长度 */

function strlen(str)
{var i;
var len;
len = 0;
for (i=0;i<str.length;i++)
{
if (str.charCodeAt(i)>255) len+=2; else len++;
}
return len;
}

