/***************************
 * 1.功能:
 *  (1) 用来判断数据类型的一些基本函数
 *  (2) 数据格式转换函数
 *  (3) 校验合法性
 * 2.文件:common.js
 * 3.开发:David
 * 4.日期:2003/2/11
 * 5.修改:
 * 6.函数列表及其功能简述
 *
 ********************************************/


/***************************
 * 功能:
 * (1) 用来判断数据类型的一些基本函数
 ***************************/
//判断是否为空。
function isNull(pvalue)
 {
   var RegularExp=/^\s*$/;
   if(RegularExp.test(pvalue))
   {
     return true;
   }
   return false;
 }

//判断已输入值是否为整数 如果为空值也返回true
 function isInt(pvalue)
 {
   var RegularExp = /^(\-)?(\d)*$/;
   if(!RegularExp.test(pvalue))
   {
     return false;
   }
   return true;
 }

//判断已输入值是否为浮点数 如果为空值也返回true
 function isFloat(pvalue)
 {
   if(pvalue==""){ return true;}
   var separator="."; //小数点
   var dateArray;
   dateArray= pvalue.split(separator);

   if(dateArray.length!=2)
   {
     return false;
   }
   else
   {
     if(!isInt(dateArray[0]))
     {
       return false;
     }
     if(!isInt(dateArray[1]))
     {
       return false;
     }
   }
   return true;
 }

//判断已输入值是数值（包括整数和浮点数） 如果为空值也返回true
 function isDigital(inputData) {
   if(isFloat(pvalue)){return true;}
   if(isInt(pvalue)){return true;}
   return false;
 }

//判断已输入值否为数字字符
 function isNum(pvalue)
 {
	var exp = new RegExp("^[0-9]*$");
	return exp.test(pvalue);
 }


//判断已输入值否为字符、数字字符、中文
 function isValuableChar(pvalue)
 {

	var exp = new RegExp("^[A-Za-z0-9\u4E00-\u9FA5\uF900-\uFA2D_?？]*$");
	return exp.test(pvalue); 
	
}

 /***************************
  *
  * 功能:
  *  (2) 数据格式转换函数
  *
  ***************************/

//将原字符串中指定的值用指定的值替换
 function replaceStr(Str,toMoveStr,toReplaceStr)
 {
   var returnStr="";
   var sourceStr=Str.toString();
   while (sourceStr.length>0 && sourceStr.indexOf(toMoveStr)!=-1)
   {
     var curPlace=sourceStr.indexOf(toMoveStr);
     returnStr+=sourceStr.substring(0,curPlace)+toReplaceStr;
     sourceStr=sourceStr.substring(curPlace+toMoveStr.length,sourceStr.length);
   }
   returnStr+=sourceStr;
   return returnStr;
 }

//将原字符串中影响HTML的字符替换成合理的格式
 function formatData(inputData)
 {
   var strData="";
   strData=replaceStr(inputData,"&","&amp;");
   strData=replaceStr(strData,"\"","&quot;");
   strData=replaceStr(strData,"'","&apos;");
   strData=replaceStr(strData,"<","&lt;");
   strData=replaceStr(strData,">","&gt;");
   strData=replaceStr(strData,"\n","&#13;");
   return strData;
 }



 /**
*校验字符串是否为日期型
*返回值：
*如果为空，定义校验通过，           返回true
*如果字串为日期型，校验通过，       返回true
*如果日期不合法，                   返回false    参考提示信息：输入域的时间不合法！（yyyy-MM-dd）
*/
function checkIsValidDate(str)
{
    //如果为空，则通过校验
    if(str == "")
        return true;
    var pattern = /^((\d{4})|(\d{2}))-(\d{1,2})-(\d{1,2})$/g;
    if(!pattern.test(str))
        return false;
    var arrDate = str.split("-");
    if(parseInt(arrDate[0],10) < 100)
        arrDate[0] = 2000 + parseInt(arrDate[0],10) + "";
    var date =  new Date(arrDate[0],(parseInt(arrDate[1],10) -1)+"",arrDate[2]);
    if(date.getYear() == arrDate[0]
       && date.getMonth() == (parseInt(arrDate[1],10) -1)+""
       && date.getDate() == arrDate[2])
        return true;
    else
        return false;
}//~~~
/**
*校验两个日期的先后
*返回值：
*如果其中有一个日期为空，校验通过,          返回true
*如果起始日期早于等于终止日期，校验通过，   返回true
*如果起始日期晚于终止日期，                 返回false    参考提示信息： 起始日期不能晚于结束日期。
*/
function checkDateEarlier(strStart,strEnd)
{
    if(checkIsValidDate(strStart) == false || checkIsValidDate(strEnd) == false)
        return false;
    //如果有一个输入为空，则通过检验
    if (( strStart == "" ) || ( strEnd == "" ))
        return true;
    var arr1 = strStart.split("-");
    var arr2 = strEnd.split("-");
    var date1 = new Date(arr1[0],parseInt(arr1[1].replace(/^0/,""),10) - 1,arr1[2]);
    var date2 = new Date(arr2[0],parseInt(arr2[1].replace(/^0/,""),10) - 1,arr2[2]);
    if(arr1[1].length == 1)
        arr1[1] = "0" + arr1[1];
    if(arr1[2].length == 1)
        arr1[2] = "0" + arr1[2];
    if(arr2[1].length == 1)
        arr2[1] = "0" + arr2[1];
    if(arr2[2].length == 1)
        arr2[2]="0" + arr2[2];
    var d1 = arr1[0] + arr1[1] + arr1[2];
    var d2 = arr2[0] + arr2[1] + arr2[2];
    if(parseInt(d1,10) > parseInt(d2,10))
       return false;
    else
       return true;
}//~~~

