/*********************************************************************************
 * StringBuffer uses an array to efficiently build a string
 *********************************************************************************/
function StringBuffer() {
	this.buffer = [];
}
StringBuffer.prototype = new Object();
StringBuffer.prototype.a = function (x) {
	this.buffer[this.buffer.length] = x;
}

StringBuffer.prototype.toString = function () {
	return this.buffer.join("");
}

StringBuffer.prototype.length = function () {
	return this.buffer.length;
}

StringBuffer.prototype.init = function () {
	this.buffer.length = 0;
}
StringBuffer.prototype.deleteString = function (x) {
	for(var i = 0; i < this.buffer.length;i++){
		if(this.buffer[i] == x){
			delete this.buffer[i];
			break;
		}
	}
}
String.prototype.IsEndWith = function(v) {
	return (this.substring(this.length-v.length) == v)
}

//onkeypress="return event.keyCode>=48&amp;&amp;event.keyCode<=57||event.keyCode==48"

String.prototype.isNumRE = /[0-9]+(\.[0-9])*|\.[0-9]+/;
String.prototype.isNumber = function() {
	var m = this.match(this.isNumRE);
	return (m && this == m[0]);
	//return (this.isNumRE.test(this));
}
String.prototype.isTitleChar = function() {
	var m = this;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789-_";
	for (var i=0; i < m.length; i++) {
		var letter = m.charAt(i).toLowerCase();
		c = m.charCodeAt(i);
		if (validchars.indexOf(letter) == -1&&c <= 255){
			alert('包含非法字符:'+letter);
			return false;
		}
	}
	return true;
}
String.prototype.isInteger = function() {//是否是整数
	if(this.indexOf('\.')>0){
		return false;
	}
	var m = this.match(this.isNumRE);
	return (m && this == m[0]);
}
String.prototype.isReal = function() {//是否是实数
	var vs = this.split('\.');
	if(vs.length > 2)
		return false;
	var b = true;
	for(var i=0;i<vs.length;i++){
		var v = vs[i];
		if(!v.isNumber()){
			b = false;
			break;
		}
	}
	return b;
}
String.prototype.isMail = function() {//是否是邮件地址
	if (this.trim() == ""){
		return false;
	}
	
	var a=this.indexOf("@")+1;
  var p=this.indexOf(".")+1;
  if(this.indexOf("'") > 0)
		return false;
	if(this.indexOf('"') > 0)
		return false;
  if (a<2)
     return false;
  if (p<1)
     return false;
  if (p<a+2)
     return false;
  if (this.length==p)
     return false;
  return true;
}
String.prototype.isPostCode = function() {//检查邮编的合法性
	if(this.length!=6)
		return false;
	return this.isInteger();
}
String.prototype.isValidIpAddr = function() {//判断ip地址是否合法 
	if(this.trim() == ""){
     return false;
  }
  var ss = this.split(".");
  if(ss.length != 4)
   {
      return false;
   }
   var i=0;
   for(i=0;i<ss.length;i++)
   {
      if ( !ss[i].isInteger() || parseInt(ss[i]) < 0 || parseInt(ss[i])>255)
      {
        return false;
      }
   }
  return true;
}
String.prototype.isValidIDCard  = function() {//判断身份证号码是否合法
	if ((this == "")||(!(this.isInteger())&&(this.length == 15))||
	         ((this.length != 15)&&(this.length != 18))){
	    alert("身份证号码应是15或18位数字,请输入正确!");
		  return false;
	}else if (this.length == 18){
		//对18位身份证的判断，18位允许最后一位为X，不过必须根据特定的算法
		var Wi = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
	 	var Ai = new Array('1','0','X','9','8','7','6','5','4','3','2');
	 	if (this.charAt(17) == 'x'){
		  this = this.replace("x","X"); 
		}
		var checkDigit = this.charAt(17);  
		var cardNoSum = 0;
		for (var i=0; i<this.length-1; i++){
			cardNoSum = cardNoSum + this.charAt(i)*Wi[i];
	  }
	 var seq = cardNoSum%11;
	 var getCheckDigit = Ai[seq]; 
	 if (checkDigit != getCheckDigit){
	  alert("您的身份证号码输入有误!");
	  return false;
	 }     
	 return true;
	}else{
		// var newID = this.IDCard15To18();
		// return newID.isValidIDCard();
		return true;
	}
}
String.prototype.IDCard15To18 = function(centry) {//身份证号码从15到18 
	if(this==null)
		return "";
	if(this.length!=15)
		return this;
	if(centry==null)
		centry="19";
	var check = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
	var  input= new Array(15);
  var  inputNew= new Array(18);
	for(var i=0; i<input.length; i++){ 
		input[i]=this.substring(i, i+1); 
  } 
  for(var i=0; i<6; i++){
  	inputNew[i]=input[i];
  }
  for(var i=0; i<2; i++){
  	 inputNew[i+6] = centry.substring(i, i+1);
  }
  for(var i=6; i<input.length; i++){
  	 inputNew[i+2]=input[i];
  }
  var total=0;
  for(var i=0; i<inputNew.length-1; i++){ 
  	total+=inputNew[i]*parseInt(check[i]);
  }
  var lastNumber="0";
  total = total%11;
	switch (total){
   case 0:lastNumber="1";break;
   case 1:lastNumber="0";break;
   case 2:lastNumber="X";break;
   case 3:lastNumber="9";break;
   case 4:lastNumber="8";break;
   case 5:lastNumber="7";break;
   case 6:lastNumber="6";break;
   case 7:lastNumber="5";break;
   case 8:lastNumber="4";break;
   case 9:lastNumber="3";break;
   default:lastNumber="2";
  }
	inputNew[17] = lastNumber;

  var newID = "";
  for(var i =0; i < inputNew.length;i++){
  	newID = newID + "" + inputNew[i];
  }
	return newID;
}

String.prototype.invalidDNChars = /[\\\n\r\",+=<>#;&\/]/g;
String.prototype.containsInvalidDNChars = function() {
	var res = this.match(this.invalidDNChars);
	var c = null;
	
	if (!res) res = [];
	for (var i=0; i < this.length; i++) 
	{
		c = this.charCodeAt(i);
		if (c > 255) 
		{				
		   c = "&#" + c.toString(16) + ";" ;
		   res.join(c);
		}
	}
	return (res ? res.join('') : null);
}
// String.replaceAll( filter, text ) -> String
//		Replaces all filter matches with text
// Inputs:
//		filter - regular expression or string match
//		text - replacement text

String.prototype.replaceAll = function(filter,text) {
	if ( !filter ) return this;
	
	//filter = filter.source ? new RegExp( filter.source, filter.global ? "gi" : "g" ) : filter;
	if (filter.source)
	{
		var s = filter.toString();
		if ((s.charAt(s.length - 1) == 'i') || (s.charAt(s.length - 2) == 'i'))
		{
			filter = new RegExp(filter.source, "gi");
		}
		else
		{
			filter = new RegExp(filter.source, "g");
		}
	}
	else {
		var exp = /([\/\\\.\*\+\?\|\(\)\[\]\{\}'])/g;
		newcrit = filter.replace(exp, "\\$1");
		filter = new RegExp(newcrit, "g")
	}
	return this.replace( filter, text );
}
//String.prototype.invalidChars = /[\?\(\)\/{}\[\]<>\\]/g;
String.prototype.invalidChars = /[\%\&\?\(\)\/{}\[\]<>\\]/g;
String.prototype.containsInvalidChars = function() {
	var res = this.match(this.invalidChars);
	return (res ? res.join('') : null);
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,""); 
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
String.prototype.trim = function() { 
	return this.replace(/^\s+|\s+$/g,"") 
}
String.prototype.trim2 = function() { 
	return this.replace(/\s+/g,"%20") 
}
var URLUTF8Encoder=
{

	hex:[
		"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
		"%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
		"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
		"%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
		"%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
		"%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
		"%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
		"%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
		"%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
		"%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
		"%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
		"%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
		"%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
		"%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
		"%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
		"%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
		"%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
		"%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
		"%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
		"%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
		"%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
		"%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
		"%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
		"%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
		"%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
		"%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
		"%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
		"%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
		"%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
		"%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
		"%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
		"%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
	],

	/**
	 * Encode a string to the "x-www-form-urlencoded" form, enhanced
	 * with the UTF-8-in-URL proposal. This is what happens:
	 *
	 * <ul>
	 * <li><p>The ASCII characters 'a' through 'z', 'A' through 'Z',
	 *        and '0' through '9' remain the same.
	 *
	 * <li><p>The unreserved characters - _ . ! ~ * ' ( ) remain the same.
	 *
	 * <li><p>The space character ' ' is converted into a plus sign '+'.
	 *
	 * <li><p>All other ASCII characters are converted into the
	 *        3-character string "%xy", where xy is
	 *        the two-digit this.hexadecimal representation of the character
	 *        code
	 *
	 * <li><p>All non-ASCII characters are encoded in two steps: first
	 *        to a sequence of 2 or 3 bytes, using the UTF-8 algorithm;
	 *        secondly each of these bytes is encoded as "%xx".
	 * </ul>
	 *
	 * @param s The string to be encoded
	 * @return The encoded string
	 */
	encode:function(s)
	{
		var sbuf = new StringBuffer();
		var len = s.length;
		for(var i = 0; i < len; i++)
		{
			var ch = s.charAt(i);
			if('A' <= ch && ch <= 'Z')
			{ // 'A'..'Z'
				sbuf.a(ch);
			}
			else if('a' <= ch && ch <= 'z')
			{ // 'a'..'z'
				sbuf.a(ch);
			}
			else if('0' <= ch && ch <= '9')
			{ // '0'..'9'
				sbuf.a(ch);
			}
			else if(ch == ' ')
			{ // space
				sbuf.a('+');
			}
			else if(ch == '-' || ch == '_' // unreserved
					|| ch == '.' || ch == '!'
					|| ch == '~' || ch == '*'
					|| ch == '\'' || ch == '('
					|| ch == ')')
			{
				sbuf.a(ch);
			}
			else
			{
				ch = s.charCodeAt(i);
				if(ch <= 0x007f)
				{ // other ASCII
					sbuf.a(this.hex[ch]);
				}
				else if(ch <= 0x07FF)
				{ // non-ASCII <= 0x7FF
					sbuf.a(this.hex[0xc0 | (ch >> 6)]);
					sbuf.a(this.hex[0x80 | (ch & 0x3F)]);
				}
				else
				{ // 0x7FF < ch <= 0xFFFF
					sbuf.a(this.hex[0xe0 | (ch >> 12)]);
					sbuf.a(this.hex[0x80 | ((ch >> 6) & 0x3F)]);
					sbuf.a(this.hex[0x80 | (ch & 0x3F)]);
				}
			}
		}
		return sbuf.toString();
	}
}


function generateSlipView(buf,obj){
		if(obj==null)
			return '';
		var slipid = obj.slipid;
		var s_detail = obj.s_detail;
		var bet_name = obj.bet_name;
		var mult = obj.mult;
		var bet_date = obj.bet_date;
		var price = obj.price;
		var memberid = obj.memberid;
		var memberName = memberid;
		try{
			memberName = obj.memberName;
		}catch(e){}
		if(memberName==null)
			memberName = memberid;

		buf.a('<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#C0C0C0" width="99%" bgcolor="#FFFFFF">');
		buf.a('<tr><td width="100%">');
		buf.a('<table cellSpacing="1" cellPadding="0" width="100%" border="0" style="border-collapse: collapse" bordercolor="#111111">');
		buf.a('<tr bgColor="#ffffff">');
		buf.a('<td width="236" bgcolor="#FEDFF6" colspan="2" height="20">');
		buf.a('<font color="#FF8400">&nbsp;投注单号：</font>'+slipid);
		buf.a('&nbsp;<font color="#FF8400">会员：</font>'+memberName);
		buf.a('</td>');
		buf.a('</tr>');
		buf.a('<tr bgColor="#ffffff">');
		buf.a('<td width="70%" bgcolor="#FEDFF6">');
		buf.a('&nbsp;游戏及过关方式 ：'+bet_name);
		buf.a('</td>');
		buf.a('<td width="30%" height="20">&nbsp;投注倍数：&nbsp;'+mult+'</td>');
		buf.a('</tr>');
		buf.a('<tr bgColor="#ffffff">');
		buf.a('<td colspan="2" width="237" height="2" background="images/xian.gif">');
		buf.a('<img border="0" src="images/xian.gif" width="1" height="1">');
		buf.a('</td>');
		buf.a('</tr>');

		buf.a('<tr bgColor="#ffffff">');
		buf.a('<td width="237" bgcolor="#FEDFF6" colspan="2" height="20">');
		buf.a('&nbsp;投注场次 ：');
		buf.a('</td>');
		buf.a('</tr>');
		buf.a('<tr bgColor="#ffffff">');
		buf.a('<td width="100%" colspan="2">');//场次
			buf.a('<table border="0" cellpadding="0" cellspacing="1" style="border-collapse: collapse" bordercolor="#111111" width="100%">');
			buf.a('<tr>');
			buf.a('  <td width="9%" bgcolor="#AA002F" align="center" height="18">');
			buf.a('  <font color="#FFFFFF">场次</font></td>');
			buf.a('  <td width="43%" bgcolor="#AA002F" align="center" height="18">');
			buf.a('  <font color="#FFFFFF">主队vs客队</font></td>');
			buf.a('  <td width="27%" bgcolor="#AA002F" align="center" height="18">');
			buf.a('  <font color="#FFFFFF">投注结果</font></td>');
			buf.a('</tr>');
			parseSlipDetail(buf,s_detail);
			buf.a('</table>');
		buf.a('</td>');
		buf.a('</tr>');
		buf.a('<tr bgColor="#ffffff">');
		buf.a('<td colspan="2" width="237" height="2" background="images/xian.gif">');
		buf.a('<img border="0" src="images/xian.gif" width="1" height="1">');
		buf.a('</td>');
		buf.a('</tr>');
		buf.a('</table>');
		buf.a('</td></tr>');
		
		buf.a('<tr>');
		buf.a('<td width="100%">');
		buf.a('<table border="0" cellpadding="2" cellspacing="0" style="border-collapse: collapse" width="100%"  bgcolor="#FFF0F8" height="70">');
		buf.a('<tr>');
		buf.a('<td width="80%">');
		buf.a('<p style="line-height: 150%">');
		buf.a('<font color="#FF8400">投注时间：</font>'+bet_date+'<font color="#FF8400"><br>');
		buf.a('投 注 额：</font><font color="#FF0000">'+price+'</font>元<font color="#FF8400"><br>');
		buf.a('</td>');
		buf.a('<td width="20%">');
		
		buf.a('<p align="center"><img border="0" src="jsp/member/images/zhang.gif">');
		buf.a('</td>');
		buf.a('</tr>');
		buf.a('</table>');
		buf.a('</td>');
		buf.a('</tr>');
		buf.a('</table>');
		buf.a('<br/>');
	}
	function parseSlipDetail(buf,detail){
		if(detail!=null){
			var ss = detail.split('/');
			if(ss.length > 0){
				for(var i = 0;i < ss.length;i++){
					var race = '';
					var name = '';
					var result = '';
					var er = ss[i];
					//var ers = er.split(':');
					var splitIndex = er.indexOf(":");
					if(splitIndex > -1){
						var name = er.substring(0,splitIndex);
						result = er.substring(splitIndex+1,er.length);
						var names = name.split(')');
						if(names.length==2){
							name = names[1];
							race = names[0];
						}
					}
					var bgColor = '';
					if(i%2==0){
						bgColor = 'bgcolor="#FFF0F8"';
					}
					buf.a('<tr>');
					buf.a('  <td width="9%" '+bgColor+' align="center" height="18">');
					buf.a(race+'</td>');
					buf.a('  <td width="43%"  '+bgColor+' height="18">');
					buf.a(name+'</td>');
					buf.a('  <td width="27%"  '+bgColor+'   align="center" height="18">');
					buf.a(result+'</td>');
					buf.a('</tr>');
				}
			}
		}
	}
	