﻿/*
Array搜索功能及取代
从第几组阵列开始找
var a=new Array('aa1321321','b4323424b','cc345345','dd4654','dd4654','b4323424b');
document.getElementById('t1').value=a.toString();
document.getElementById('t2').value=a.indexOf('cc345345'); 模糊查询 1开始
document.getElementById('t3').value=a.lastIndexOf('dd4654');
document.getElementById('t4').value=a.replace(/\d/g,'*');
document.getElementById('t5').value=a.search('54');			//找出数组内符合的有几笔
*/
Array.prototype.indexOf=function(substr,start,matchoption){
	var ta,rt,d='\0';
	
	if(start!=null)
	{   //typeof(ta) ta.toString()
	    //typeof(ta[s]) ta.join('|')
		ta=this.slice(start);	//Copy string
		rt=start;
	}else{
		ta=this;
		rt=0;
	}
	ta.objtostr();
	var str=d+ta.join(d)+d,t
	if(matchoption==null){
		t=str.indexOf(substr);
	}else{
		t=str.indexOf(d+substr+d);
	}
	if(t==-1)return -1;
	rt+=str.slice(0,t+1).replace(/[^\0]/g,'').length;
	return rt;
}

Array.prototype.lastIndexOf=function(substr,start,matchoption){
	var ta,rt,d='\0';
	if(start!=null){
		ta=this.slice(start);
		rt=start;
	}else{
		ta=this;
		rt=0;
	}
	ta.objtostr();
	ta=ta.reverse();var str=d+ta.join(d)+d,t
	ta=ta.reverse();
	if(matchoption==null){
		t=str.indexOf(substr);
	//	alert(str.slice(t).replace(/[^\0]/g,'').length);
	}else{
		t=str.indexOf(d+substr+d);
	}
	if(t==-1)return -1;
	rt+=str.slice(t).replace(/[^\0]/g,'').length;
	if(matchoption!=null) rt-=1;
	return rt;
}

Array.prototype.replace=function(reg,rpby){
	var ta=this.slice(0),d='\0';
	ta.objtostr();
	var str=ta.join(d);str=str.replace(reg,rpby);
	return str.split(d);
}

Array.prototype.search=function(reg,start,matchoption){
	var j=0,i=0;
	if(start!=null){
		ta=this.slice(start);
		rt=start;
	}else{
		ta=this;
		rt=0;
	}
	ta.objtostr();
	while(ta.indexOf(reg,i,matchoption)>-1){
//		alert('i='+i);
//		alert('ta.indexOf(reg,i,matchoption)='+ta.indexOf(reg,i,matchoption));
//		alert('reg='+reg);
//		alert('ta='+ta);
		i=ta.indexOf(reg,i,matchoption);
		j++
	}
	return j;
}
Array.prototype.objtostr=function(){
    for(s in this)
    {
        if(typeof(this[s])!='function')
        {
            if(typeof(this[s])=='object'){
                //tempstr+=objtostr(this[s]);
                this[s]=objtostr(this[s]);
            }else if(typeof(this[s])=='string')
            {
                break;
            }
        }
    }
}
Function.prototype.andThen=function(g) {
  var f=this;
  return function() {
    f();g();
  }
}
Array.prototype.del=function(index) {
  var temparray=[];
  //this.cmds.length temparray.length
  if(this.length-1<index){
        return this;
   }else{
        for(var i=0;i<=this.length-1;i++)
        {
            if(i!=index){
                temparray.push(this[i]);
            }
        }
        return temparray;
   }
}
Array.prototype.distinct=function(){
	var temp=[];
	for(i=0;i<=this.length-1;i++)
	{
		if(this[i]!=''){
			if(temp.search(this[i],0,1)==0)
			{
				if(arguments.length>0){
					if(temp.length<=arguments[0])
						temp.push(this[i])
					else
						break;
				}else{
					temp.push(this[i]);
				}
			}
		}
	}
	return temp;
}
function objtostr(obj)
{
    if(objtostr.arguments.length>=2)
        splitstr=objtostr.arguments[1]
    else
        splitstr=',';
    var tempstr;
    tempstr='';
    for(o in obj)
    {
        if(typeof(obj[o])=='object'){
            tempstr.length>0 ? tempstr+=splitstr+objtostr(obj[o]):tempstr+=objtostr(obj[o]);
        }else if(typeof(obj[o])=='string')
        {
            tempstr.length>0 ? tempstr+=splitstr+o+'='+obj[o]:tempstr+=o+'='+obj[o];
        }
    }
    return tempstr;
}
