//=================================================================================
// 字符串公用操作函数
//=================================================================================

//=================================================================================
// 目录
// 1. 将指定字符串中的指定字符用给定字符替换     strReplace(org, rg, replaceText)
// 2. 将指定字符串进行Base64编码                 strEncode64(input)
// 3. 将进行过Base64编码的字符串进行解码         strDecode64(input)
// 4. 交验居民身份证号的合法性                   strCheckSfzh(hm)
// 5. 交验组织机构代码号的合法性                 strCheckZZJGDM(hm)
// 6. 交验日期字符串是否有效                     strCheckDateAvailability
//=================================================================================


//*********************************************************************************
// 1. 将指定字符串中的指定字符用给定字符替换
//*********************************************************************************
function strReplace(org, rg, replaceText){
    var newstr = "";
	var istrLoop = 0;
    var ssArry = org.split(rg);
    for (istrLoop = 0; istrLoop < ssArry.length; istrLoop++)
    {
        if (newstr != null && newstr != "" && istrLoop > 0)
        {
            newstr = newstr + replaceText;
        }
        newstr = newstr + ssArry[istrLoop];
    }
    return newstr;
}


//*********************************************************************************
// 2. 将指定字符串进行Base64编码
//*********************************************************************************
function strEncode64(input) {
  var keyStr = "ABCDEFGHIJKLMNOP" + "QRSTUVWXYZabcdef" + "ghijklmnopqrstuv" +"wxyz0123456789+/"+"=";
  //input = escape(input);
  var output = "";
  var chr1, chr2, chr3 = "";
  var enc1, enc2, enc3, enc4 = "";
  var i = 0;
  do {
	chr1 = input.charCodeAt(i++);
	chr2 = input.charCodeAt(i++);
	chr3 = input.charCodeAt(i++);
	enc1 = chr1 >> 2;
	enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	enc4 = chr3 & 63;
	if (isNaN(chr2)) {
	  enc3 = enc4 = 64;
	}else if (isNaN(chr3)) {
	  enc4 = 64;
	}
	output = output + 
  	  keyStr.charAt(enc1) + 
	  keyStr.charAt(enc2) + 
	  keyStr.charAt(enc3) + 
	  keyStr.charAt(enc4);
	chr1 = chr2 = chr3 = "";
	enc1 = enc2 = enc3 = enc4 = "";
  } while (i < input.length);
  return output;
}


//*********************************************************************************
// 3. 将进行过Base64编码的字符串进行解码
//*********************************************************************************
function strDecode64(input) {
  var keyStr = "ABCDEFGHIJKLMNOP" + "QRSTUVWXYZabcdef" + "ghijklmnopqrstuv" +"wxyz0123456789+/"+"=";
  var output = "";
  var chr1, chr2, chr3 = "";
  var enc1, enc2, enc3, enc4 = "";
  var i = 0;
  // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  var base64test = /[^A-Za-z0-9\+\/\=]/g;
  if (base64test.exec(input)) {
	 alert("There were invalid base64 characters in the input text.\n" +
		   "Valid base64 characters are A-Z, a-z, 0-9, '+', '/', and '='\n" +
		   "Expect errors in decoding.");
  }
  input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  do {
	 enc1 = keyStr.indexOf(input.charAt(i++));
	 enc2 = keyStr.indexOf(input.charAt(i++));
	 enc3 = keyStr.indexOf(input.charAt(i++));
	 enc4 = keyStr.indexOf(input.charAt(i++));
	 chr1 = (enc1 << 2) | (enc2 >> 4);
	 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	 chr3 = ((enc3 & 3) << 6) | enc4;
	 output = output + String.fromCharCode(chr1);
	 if (enc3 != 64) {
		output = output + String.fromCharCode(chr2);
	 }
	 if (enc4 != 64) {
		output = output + String.fromCharCode(chr3);
	 }
	 chr1 = chr2 = chr3 = "";
	 enc1 = enc2 = enc3 = enc4 = "";
  } while (i < input.length);
  return unescape(output);
}


//*********************************************************************************
// 4. 交验居民身份证号的合法性, 并通过身份证号码取得出生日期和性别
//*********************************************************************************
function strCheckSfzh(hm, Obj){
	Obj.CheckMsg = "";
	Obj.Csrq = "";
	Obj.Xb = "";
	// 1. 交验时间
	var d_csrq=new Date();
	var y,m,d;
	var xbflag;
	if(hm.length != 15 && hm.length != 18){
		Obj.CheckMsg = "居民身份证号码位数不正确!";
		return false;
	}
	if(hm.length == 15)
	{
		var bds = /\d{15}/;
		if(!bds.test(hm))
		{
			Obj.CheckMsg = "居民身份证号码中含有非法字符!";
			return false;
		}
		if (hm.substr(6, 2) * 1 < 6){
			y = "20" + hm.substr(6, 2);
		}else{
			y = "19" + hm.substr(6, 2);
		}
		m = hm.substr(8, 2);
		d = hm.substr(10, 2);
		xbflag = hm.substr(14, 1);
	}
	else if(hm.length==18)
	{
		var bds = /\d{17}\w{1}/;
		if(!bds.test(hm))
		{
			Obj.CheckMsg = "居民身份证号码中含有非法字符!";
			return false;
		}
		y = hm.substr(6, 4);
		m = hm.substr(10, 2);
		d = hm.substr(12, 2);
		xbflag = hm.substr(16, 1);
	}
	// 取得性别
	Obj.Xb = "1";
	if ((xbflag * 1)%2 == 0){
		Obj.Xb = "2";
	}
	// 取得出生日期
	d_csrq=new Date(eval(y), eval(m) - 1, eval(d));
	if( (d_csrq.getMonth()+1)!=eval(m) || d_csrq.getDate()!=eval(d))
	{
		Obj.CheckMsg = "居民身份证号码中出生年月不正确!";
		return false;
	}
	Obj.Csrq = y + '.' + m + '.' + d;

	// 如果居民身份证号为18位，则交验其交验位是否有效
	if (hm.length == 15){
		return true;
	}

	// 交验位
	var checkbit = new Array('0', '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
	var checksum = new Array(0, 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
	var tempnum = 0;

    var i = 0;
    for(i = 1; i <= 17; i++)
	{
		checksum[i] = checksum[i] * ((hm.substr(i - 1, 1)) * 1);
		tempnum = tempnum + checksum[i];
	}
    
	tempnum = tempnum % 11;

    if(checkbit[tempnum + 1] != hm.substr(17, 1)){
		Obj.CheckMsg = "居民身份证号码中校验位不正确!";
		return false;
	}
	
	return true;
	
}


//*********************************************************************************
// 5. 交验组织机构代码号的合法性
//*********************************************************************************
function strCheckZZJGDM(strDm){
	var iLen = strDm.length;
	var onebit = "";
	var onebitasc = 0;
	var tempnum = 0;
	var li_ChkBit = 0;
	var ls_ChkBit = "";
	if(iLen != 9){
		return false;
	}
	
	var checkbit = new Array(3, 7, 9, 10, 5, 8, 4, 2);
	var i = 0;
    for(i = 1; i <= 8; i++)
	{
		onebit = (strDm.substr(i - 1, 1));
		onebitasc = onebit.charCodeAt() - 48;
		if(onebitasc > 9){
			onebitasc = onebitasc - 7;
		}
		tempnum = tempnum + checkbit[i - 1] * onebitasc;
	}
	
	li_ChkBit = 11 - tempnum % 11;
	switch(li_ChkBit){
		case 10:
		  ls_ChkBit = 'X';
		  break;
		case 11:
		  ls_ChkBit = '0';
		  break;
		default:
		  ls_ChkBit = li_ChkBit + "";
	}
	if(ls_ChkBit == strDm.substr(8, 1))
	  return true;
	
	return false;
}


//*********************************************************************************
// 6. 交验日期字符串是否有效
//*********************************************************************************
function strCheckDateAvailability(strDate){
	var sDate;
	var y,m,d;
	var ynum, mnum, dnum;
	if (strDate == null || strDate == "" || strDate.length < 1){
		return false;
	}
	var datelist = new Array(31,29,31,30,31,30,31,31,30,31,30,31);

	// 将日期字符串格式化为纯数字串
	sDate = strDate.replace(/[^0-9]/g,"");
	y = sDate.substr(0, 4);
	m = sDate.substr(4, 2);
	d = sDate.substr(6, 2);

	ynum = eval(y);
	mnum = eval(m);
	dnum = eval(d);

	if (ynum > 2200 || ynum < 1900 || mnum > 12 || mnum < 1 || dnum > 31 || dnum < 1) {
		return false;
	}
	if( dnum > datelist[mnum - 1] ){
		return false;
	}

	if ( mnum == 2 )
    {
        if ( (ynum % 400) == 0 )
        {
            if ( dnum > 29 ){
				return false;
			}
        }
        else if ( (ynum % 4) == 0 && (ynum % 100) != 0 )
        {
            if ( dnum > 29 ){
				return false;
			}
        }
        else
        {
            if ( dnum > 28 ){
				return false;
			}
        }
    }
	return true;




//	var d_date = new Date();	
//	d_date.setYear(y);
//	d_date.setMonth(eval(m)-1);
//	d_date.setDate(eval(d));
//	d_date.setHours(0, 0, 0, 0);
//
//	if( (d_date.getMonth()+1)!=eval(m) || d_date.getDate()!=eval(d))
//	{
//		return false;
//	}
//	return true;
}