/*
* @Author  JLAPPBOX	 www.ii23.cn	LastModified:2015-04-02		
*/
//参数：字符串，查找开始，查找结尾，第N个(默认第1个)，开始字符为空或结尾为空匹配全部
//匹配遵循最近原则，如 ababcabc  截取a和c后得到b 而不是bab
function getposmsg(as_msg,as_begin,as_end,ai_num){
	 if (!as_begin) as_begin='';
	 if (!as_end) as_end = '';
	 if (!ai_num) ai_num=1;
   var i=1;
   var ls_tmp = as_msg;
   if ((as_begin=='')&&(as_end=='')){
     if (ai_num==1) 
         return ls_tmp;
     else return  '';
   }
   while (i<ai_num){
      if (as_begin!=''){
       var  li_pos = ls_tmp.indexOf(as_begin);
       while (li_pos>=0){
           ls_tmp =  ls_tmp.substr(li_pos + as_begin.length);
           li_nextpos =  ls_tmp.indexOf(as_begin);
           li_endpos =  ls_tmp.indexOf(as_end);
           if ((li_nextpos==-1) || (li_nextpos>=li_endpos)) break;
           li_pos = li_nextpos;
         }
         if (li_pos == -1)
            ls_tmp = ls_tmp;
         else
        {
           li_endpos =  ls_tmp.indexOf(as_end);
           if (li_endpos>=0)
              ls_tmp = ls_tmp.substr(li_endpos +1,ls_tmp.length);
           else{
		           if (as_end=='')
		                ls_tmp = ls_tmp 
		           else
		             ls_tmp =  '';
            }
         }
        }else{
           li_endpos =ls_tmp.indexOf(as_end);
           
           if (li_endpos>=0)
              ls_tmp = ls_tmp.substr(li_endpos +1,ls_tmp.length);
           else{
		           if (as_end=='')
		               ls_tmp =  ls_tmp ;
		            else
		              ls_tmp = '';
            }
       }
       i= i + 1;
   }
 
  
    if (as_begin!=''){
       li_pos = ls_tmp.indexOf(as_begin);
       while (li_pos>=0){
           ls_tmp =  ls_tmp.substr(li_pos + as_begin.length);
           li_nextpos =  ls_tmp.indexOf(as_begin);
           li_endpos =  ls_tmp.indexOf(as_end);
           if ((li_nextpos==-1) || (li_nextpos>=li_endpos)) break;
           li_pos = li_nextpos;
         }
       
         if (li_pos == -1)
            return '';
         else
        { 
        	 if (as_end!=''){
        	 
		           li_endpos =  ls_tmp.indexOf(as_end);
		           if (li_endpos>=0)
		              return ls_tmp.substr(0,li_endpos);
		           else{
				          return ''; 
		            }
           }else
           	  return ls_tmp;
         }
        }else{
           li_endpos =ls_tmp.indexOf(as_end);
           if (li_endpos>=0)
                return ls_tmp.substr(0,li_endpos);
           else{
		            return ls_tmp;
            }
       }
}


//字符串替换函数,全部替换
function strReplaceAll(str,oldStr,newStr){
    var regS = new RegExp(oldStr,"gi");
    newStr = str.replace(regS,newStr);
    return newStr;   
}

//trim函数
String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}


/**
 * 时间对象的格式化;  用法: new Date().format("YYYYMMdd")
 */
Date.prototype.format = function(format) {
    /*
     * eg:format="YYYY-MM-dd hh:mm:ss";
     */
    var o = {
        "M+" :this.getMonth() + 1, // month
        "d+" :this.getDate(), // day
        "h+" :this.getHours(), // hour
        "m+" :this.getMinutes(), // minute
        "s+" :this.getSeconds(), // second
        "q+" :Math.floor((this.getMonth() + 3) / 3), // quarter
        "S" :this.getMilliseconds()
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
    }
    for ( var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
                    : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

/**时间戳转日期**/
function getdate(timestamp,formatstr) {
			if (!formatstr){formatstr='yyyy-MM-dd';}
      return (new Date(timestamp*1000)).format(formatstr);
} 


//字符串操作函数
function hex2str(str) {
    var arr = [];
    for (var i = 0; i < str.length; i = i + 2) {
        arr.push("\\x" + str.substr(i, 2))
    }
    arr = arr.join("");
    eval("var temp = '" + arr + "'");
    return temp
}
function str2hex(str){
	var arr=[];
	for(var i = 0; i < str.length; i++){
		var temp = str.charCodeAt(i).toString(16);
		if (temp.length==1) temp = "0" + temp;
    arr.push(temp);
  }
  return arr.join("").toUpperCase(); 	
}


function writeUnsignedInt(str) {
	var maxLength = 8;
	str = parseInt(str);
	var hex = str.toString(16);
	var len = hex.length;
	for (var i = len; i < maxLength; i++) {
		hex = "0" + hex
	}
  return hex.toUpperCase();
}

function writeUTFBytes(str){
	return str2hex(str);
}

function uin2hex(str) {
	var maxLength = 16;
	str = parseInt(str);
	var hex = str.toString(16);
	var len = hex.length;
	for (var i = len; i < maxLength; i++) {
		hex = "0" + hex
	}
	var arr = [];
	for (var j = 0; j < maxLength; j += 2) {
		arr.push("\\x" + hex.substr(j, 2))
	}
	var result = arr.join("");
	eval('result="' + result + '"');
	return result;
}

function readUnsignedInt(str){
	 var tmp = str.substr(0,8);
	 tmp="0x"+ tmp;
	 return parseInt(tmp,16);
}

//add 20140506 json转str
function json2str(obj){
	switch(typeof(obj)){
           case "string": 
                return '"' + obj.replace(/(["\\])/g, '\\$1').replace(new RegExp("\x00","gi"),"") + '"'; 
            case "array":        
                return "[" + obj.map(obj).join(",") + "]";  
            case "object":     
                 if(obj instanceof Array){  
                    var strArr = [];         
                 var len = obj.length;        
               for(var i=0; i<len; i++){      
                        strArr.push(json2str(obj[i]));   
                    } 
                    return "[" + strArr.join(",") + "]";    
                }else if(obj==null){     
                    return "null";   
                }else{       
                   var string = [];    
                    for (var property in obj) string.push(json2str(property) + ":" + json2str(obj[property])); 
                    return "{" + string.join(",") + "}";     
                }      
          case "number":     
              return obj;           
          case false:          
               return obj;        
        }                  
   }
   
//add 20150402 V3.10
String.prototype.format = function(args) {
  var result = this;
  if (arguments.length > 0) {
    if (arguments.length == 1 && typeof(args) == "object") {
      for (var key in args) {
        if (args[key] != undefined) {
          //var reg = new RegExp("({" + i + "})", "g");//这个在索引为数字时会有问题
          var reg = new RegExp("({)" + key + "(})", "g");
          result = result.replace(reg, args[key]);
        }
      }
    } else {
      for (var i = 0; i < arguments.length; i++) {
        if (arguments[i] != undefined) {
          //var reg = new RegExp("({[" + i + "]})", "g");//这个在索引大于9时会有问题
          var reg = new RegExp("({)" + i + "(})", "g");
          result = result.replace(reg, arguments[i]);
        }
      }
    }
  }
  return result;
}