/* Type */
Object.isNull=function(o){return(null==o||undefined==o);}
var __sp=String.prototype;
__sp.endsWith=function(suffix){
	return(this.substr(this.length-suffix.length)==suffix);
}
__sp.startsWith=function(prefix){return(this.substr(0,prefix.length)==prefix);}
__sp.lTrim=__sp.trimStart=function(){return this.replace(/^\s*/,"");}
__sp.rTrim=__sp.trimEnd=function(){return this.replace(/\s*$/,"");}
__sp.trim=function(){return this.trimStart().trimEnd();}
__sp.format=function(){
	var s=this;
	var aRE=__sp.format.aRegExp;
	var iCount=arguments.length;
	for(var i=0;i<iCount;i++){
		if(!aRE[i]){
			aRE[i]=new RegExp("\\{"+i+"\\}","g");}
		s=s.replace(aRE[i],arguments[i]);
	}
	return(s);
}
__sp.format.aRegExp=[];
String.isEmpty=function(string){return Object.isNull(string)||""==string;}



/* Ajax */
//构造函数
function Ajax(){}
//定义原型
Ajax.prototype = {
	err_404		: "Page not found!",
	err_500		: "Application error!",
	
	Execute		: function(url, callback, resultFormat, eventCtl, statusCtl){
		this.eventCtl = eventCtl;
		this.statusCtl = statusCtl;
		
		if( this.eventCtl != undefined ){
			this.eventCtl.disabled = true;
		}
		if( this.statusCtl!= undefined){
			this.statusCtl.innerText = "Processing...";
			this.statusCtl.className = "processInfo";
		}
		this.Open(url,"GET","",callback , "" , resultFormat);
	},
	
	Open : function(url,method,data,onsucc,onerror,resultFormat){
		var req = this.GetRequest();
		this.succCallback = onsucc;
		this.errCallback = onerror ? onerror : this.ShowError;
		this.resultFormat = resultFormat?resultFormat:"text";
		if( req == null){
			this.errCallback("Not Support Ajax!");
			return;
		}
		else{
			this.request = req;
		}
		
		var loader = this;
        this.request.onreadystatechange = function(){loader.StateChange();};		
		req.open(method,url,true);
		if( method == "POST"){
		    req.setRequestHeader("Content-Length",data.length); 
			req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			
		}
		req.send(data);
		req = null;
	},
	//返回所有text,data以数组传入  比如 var pars = new Array();  pars["Page"]=1;
	Post : function(url,data,callback){
	    var pars = this.CreateParmsString(data);
	    this.Open(url,"POST",pars,callback,null,"allText");
	    pars = null;
	},	
	//提交整个表单 formName未表单Id
	postForm : function(formName,callback)
    {
        var obj = document.getElementById(formName);
        var pars = new Array();
        for(var i=0;i<= obj.length-1;i++) 
        {   
             var el = obj.elements[i];
             switch(el.type)
             {
                    case "radio":if(el.checked==true) pars[el.name] = el.id;break;
                    case "checkbox":
                        if(pars[el.name]==null)
                           pars[el.name] = el.checked + ",";
                        else
                           pars[el.name] += el.checked + ",";
                        break;
                    default : pars[el.name] = el.value;break;
             }          
        }  
        this.Post(obj.action,pars,callback);
    },
	StateChange : function(){
		var req = this.request;
		if( req.readyState == 4){
			if( req.status == 200){
			    this.request = null;
				if( this.resultFormat == "xml"){
					this.succCallback(req.responseXML);
				}
				else if(this.resultFormat=="allText")
				    this.succCallback(req.responseText);
				else{
					var resText = req.responseText;
					if( !resText.startsWith("result:") ){
						this.errCallback("System Error!");
					}
					else{
						if( this.statusCtl !=undefined){
							if( resText.substring( 7, 8) == "0"){
								this.statusCtl.className="okinfo";
							}
							else{
								this.statusCtl.className = "errinfo";
							}
							this.statusCtl.innerHTML = resText.substring( resText.indexOf("|") + 1, resText.length);
						}
						this.succCallback(resText.substring( 7, resText.indexOf("|")), resText.substring( resText.indexOf("|") + 1, resText.length));	
					}				
					
					if( this.eventCtl != undefined){
						this.eventCtl.disabled = false;
					}
				}
			}
		}
		else{
		}
		req = null;
		
	},

	GetRequest : function(){
		var req = null;
		if( window.XMLHttpRequest){
			req = new XMLHttpRequest();
			if(req.overrideMimeType){
                 req.overrideMimeType("text/xml");
            }
           // alert("firefox");
		}
		else{
			if(window.ActiveXObject){
				try{
					req = new ActiveXObject("Msxml3.XMLHTTP");
				}
				catch(e){
					try{
						req = new ActiveXObject("Msxml2.XMLHTTP");
					}
					catch(e){
						try{
							req = new ActiveXObject("Microsoft.XMLHTTP");
						}
						catch(e){
						}
					}
				}				
			}
		}
		
		return req;
	},
	
	ShowError : function(err){
		window.alert(err);
	},
	CreateParmsString:function(pars){
        var parms = "";
        var i=0;
        for(var key in pars)
        {
            ++i;
            parms+=key + "=" + pars[key]+ "&" ;
        }
        if(i>0)
            parms = parms.substring(0,parms.length-1);
        return parms;
   }
}
//样式一<span id="txt">测试标题</span><button class="edit" onclick="edit('txt','/T.aspx');" id="xxx">ggg</button>
//样式二<span id="Span1" onclick="edit(this,'/T.aspx');">测试标题</span>
function edit(obj,url,inputClass,buttonClass)
{
   if(typeof(obj)=="string")
      obj = document.getElementById(obj);
   var eobj = event.srcElement.id;
   document.getElementById(eobj).onclick = null;
   var _postTxt = obj.innerHTML;
   obj.innerHTML = "<input type='text' class='" + inputClass + "' value='" + _postTxt + "'/>";
   obj.innerHTML += "<button onclick=\"postEdit(this,'"+url+"','" +_postTxt+ "','"+eobj+"','"+ obj.id +"');\" class='" + buttonClass + "'>提交</button>";
   
}
function postEdit(obj,url,postTxt,eobj,editor)
{
      var frm = obj.parentNode;
      var input = frm.getElementsByTagName("INPUT")[0];
      var pars = new Array();
      pars["input"] = input.value;
      (new Ajax()).Post(url,pars,postEditCallack);
      function postEditCallack(str)
      {
          if(str=="ok")
          {
            frm.innerHTML = pars["input"];
            document.getElementById(eobj).onclick =function(){edit(editor,url);}
          }
          else
            frm.innerHTML=postTxt;
      }
}




/*  JSON    */
function json(){}