/*
ajax
*/
var ajax_get_tiems_statistic=0;
var ajax_post_tiems_statistic=0;
var ajax_get_urls=new String();
var ajax_post_urls=new String();
function ajax()
{
	this.com;
	this.ret;
	this.lastmodified;
	this.etag;
	this.regheader=new Array("User-Agent",
		"Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4",
		"Accept",
		"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
		"Accept-Language",
		"zh-cn",
		"Accept-Encoding",
		"gzip,deflate",
		"Accept-Charset",
		"gb2312,utf-8;q=0.7,*;q=0.7",
		"Keep-Alive",
		"300",
		"Connection",
		"keep-alive");
	this.init=function()
	{
 		var msxmlhttp=new Array(
					'Msxml2.XMLHTTP.5.0',
					'Msxml2.XMLHTTP.4.0',
					'Msxml2.XMLHTTP.3.0',
					'Msxml2.XMLHTTP',
					'Microsoft.XMLHTTP');
		for(i=0;i<msxmlhttp.length;i++)
		{
			try
			{
				this.com=new ActiveXObject(msxmlhttp[i]);
			}
			catch(e)
			{
				this.com=null;
			}
		}
		if(!this.com
			&& typeof XMLHttpRequest != "undefined")
			this.com=new XMLHttpRequest();
		if(!this.com)
		{
			alert("Your Browser doesn't support Ajax technology.");
		}
		this.ret=false;
	}
	this.actStatus=function(text)
	{
		status=text;
	}
	this.actGET=function(url)
	{
		var tobj=this;
		ajax_get_tiems_statistic++;
		ajax_get_urls+=url+'\r\n';
		tobj.ret=false;
		(url.indexOf('?')!=-1) ? url=url+"&randomstring="+Math.random() : url+"?randomstring="+Math.random() ;
		tobj.com.open("GET", url, true);
		tobj.com.setRequestHeader("Host", document.domain);
		reglen=tobj.regheader.length;
		for(regi=0;regi<reglen;regi=regi+2)
			tobj.com.setRequestHeader(tobj.regheader[regi], tobj.regheader[regi+1]);
		if(tobj.lastmodified!=null
			&&tobj.etag!=null
			&&tobj.lastmodified!=""
			&&tobj.etag!="")
		{
			tobj.com.setRequestHeader("If-Modified-Since", tobj.lastmodified);
			tobj.com.setRequestHeader("If-None-Match", tobj.etag);
		}
		tobj.com.onreadystatechange=function()
		{
			tobj.ret=true;
			if(tobj.com.readyState==4)
			{
				tobj.lastmodified=tobj.com.getResponseHeader("Last-Modified");
				tobj.etag=tobj.com.getResponseHeader("ETag");
				/*if(tobj.com.status!=200)
				{
					tobj.ret=false;
				}*/
				tobj.doGetAfter(tobj);
			}
			else
				tobj.doGetOn(tobj);
		}
		tobj.actStatus('loading...');
		tobj.com.send(null);
	}
	this.doGetAfter=function(obj)
	{
		if(obj.afterGET!=null)
			obj.afterGET();
		obj.actStatus('done');
	}
	this.doGetOn=function(obj)
	{
		if(obj.onGET!=null)
			obj.onGET();
		obj.actStatus('done');
	}
	this.doPostAfter=function(obj)
	{
		if(obj.afterPOST!=null)
			obj.afterPOST();
		obj.actStatus('done');
	}
	this.doPostOn=function(obj)
	{
		if(obj.onGET!=null)
			obj.onGET();
		obj.actStatus('done');
	}
	this.actPOST=function(url,data,contenttype)
	{
		var tobj=this;
		ajax_post_tiems_statistic++;
		ajax_post_urls+=url+'\r\n';
		(url.indexOf('?')!=-1) ? url=url+"&randomstring="+Math.random() : url+"?randomstring="+Math.random() ;
		tobj.com.open("POST", url, true);
		tobj.com.setRequestHeader("Host", document.domain);
		reglen=tobj.regheader.length;
		for(regi=0;regi<reglen;regi=regi+2)
			tobj.com.setRequestHeader(tobj.regheader[regi], tobj.regheader[regi+1]);
		if(contenttype==null)
			tobj.com.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		else
			tobj.com.setRequestHeader("Content-Type", contenttype);
		tobj.com.setRequestHeader("Content-Length", data.length);
		tobj.com.onreadystatechange=function()
		{
			tobj.ret=true;
			if(tobj.com.readyState==4)
			{
				/*if(tobj.com.status!=200)
				{
					tobj.ret=false;
				}*/
				tobj.doPostAfter(tobj);
			}
			else
				tobj.doPostOn(tobj);
		}
		tobj.actStatus('loading...');
		tobj.com.send(data);
	}
	this.afterGET;
	this.afterPOST;
	this.onGET;
	this.onPOST;
	this.init();
}

/*
xmlloader
*/
function xmlloader()
{
	this.lastmodified;
	this.data;
	this.xml;
	this.ajax;
	this.ret;
	this.url;
	this.urls;
	this.urli;
	this.ini=function()
	{
		this.ret=false;
		this.data = new Array();
	}
	this.parse=function(tag,type)
	{
		if(this.xml)
		{
			// if tag = 'items'
			try
			{
				this.data = new Array();
				targetnodes = this.xml.getElementsByTagName(tag); // items collection
				if(targetnodes.length>0)
				{
					targetlen = targetnodes.length;
					ic=0;
					for(i=0;i<targetlen;i++)
					{
						currentnode = targetnodes[i]; // items child
						if(currentnode.nodeName!='#text')
						{
							if(type=='a')
							{
								attribs = currentnode.attributes; // attributes of anyone items
								tmplen = attribs.length;
								this.data[ic] = new Array();
								for(j=0;j<tmplen;j++)
								{
									tmpdoc = attribs[j];
									this.data[ic][j] = tmpdoc.nodeValue;
								}
							}
							if(type=='ag')
							{
								childnodes = currentnode.childNodes; // child collection of anyone items
								tmplenchild = childnodes.length;
								if(tmplenchild>0)
								{
									this.data[ic] = new Array();
									jc=0;
									for(j=0;j<tmplenchild;j++)
									{
										currentchild = childnodes[j]; // per child of child collection
										if(currentchild.nodeName!='#text')
										{
											attribs = currentchild.attributes; // attributes of anyone per child
											tmplenattrib = attribs.length;
											if(tmplenattrib>0)
											{
												this.data[ic][jc] = new Array();
												kc=0;
												for(k=0;k<tmplenattrib;k++)
												{
													currentattrib = attribs[k];
													this.data[ic][jc][kc] = currentattrib.nodeValue;
													kc++;
												}
											}
											jc++;
										}
									}
								}
							}
							if(type=='n')
							{
								childnodes = currentnode.childNodes; // per child of child collection
								tmplenchild = childnodes.length;
								this.data[ic] = new Array();
								jc=0;
								for(j=0;j<tmplenchild;j++)
								{
									currentchild = childnodes[j];
									if(currentchild.nodeType==1)
									{
										if(currentchild.childNodes.length==0)
											this.data[ic][jc] = currentchild.nodeValue;//alert(currentchild.data);
										else
											this.data[ic][jc] = currentchild.childNodes[0].nodeValue;//alert(currentchild.data);
										jc++;
									}
								}
							}
							ic++;
						}
					}
				}
			}
			catch(e)
			{
				alert('exception on parse ('+tag+'): '+e);
			}
		}
		return this.data;
	}
	this.load=function(url)
	{
		try
		{
			this.url=url;
			this.ret=false;
			if(this.ajax==null)
			{
				this.ajax=new ajax();
				this.ajax.caller=this;
			}
			this.ajax.afterGET=function()
			{
				if(this.ret)
				{
					this.caller.ret=true;
					this.caller.lastmodified=this.lastmodified;
					this.caller.xml=this.com.responseXML;
				}
				if(this.caller.afterLoad!=null)
					this.caller.afterLoad();
			}
			this.ajax.actGET(this.url,this.lastmodified);
		}
		catch(e)
		{
			alert('exception on load ('+url+'): '+e);
		}
	}
	this.loads=function()
	{
		try
		{
			if(this.ajax==null)
			{
				this.ajax=new ajax();
				this.ajax.caller=this;
			}
			this.ajax.afterGET=function()
			{
				if(this.ret)
				{
					this.caller.ret=true;
					this.caller.xml=this.com.responseXML;
				}
				this.caller.loads();
			}
			if(this.urli<this.urls.length
				&& !this.ret)
			{
				this.url=this.urls[this.urli];
				this.ajax.actGET(this.url);
				this.urli++;
			}
			else
			{
				if(this.afterLoad!=null)
					this.afterLoad();
			}
		}
		catch(e)
		{
			alert('exception on loads ('+urls+'): '+e);
		}
	}
	this.afterLoad;
}
/*

*/
function xmltaskmanager()
{
	this.loaders=new Array();
	this.urls=new Array();
	this.datas=new Array();
	this.tags=new Array();
	this.types=new Array();
	this.steps=new Array(); // 0 - initial, 1 - loaded
	this.states=0; // 0 - ready, 1 - loading, 2- done
	this.index=0;
	this.loader=null;
	this.check=function(url)
	{
		cklen=this.urls.length;
		for(cki=0;cki<len;cki++)
		{
			if(this.urls[cki]==url)
				return this.loaders[cki];
		}
		return null;
	}
	this.load=function()
	{
		if(this.states==2)
			return;
		if(this.urls[this.index]!=null)
		{
			this.states=1;
			tmploader=this.check(this.urls[this.index]);
			if(tmploader==null)
			{
				if(this.loaders[this.index]==null)
				{
					this.loaders[this.index]=new xmlloader();
					this.loaders[this.index].caller=this;
				}
				this.loaders[this.index].afterLoad=function()
				{
					if(this.ret)
						this.caller.datas[this.caller.index]=this.parse(this.caller.tags[this.caller.index], this.caller.types[this.caller.index]);
					else
						this.caller.datas[this.caller.index]=null;
					this.caller.index++;
					this.caller.load();
				}
				this.loaders[this.index].load(this.urls[this.index]);
			}
			else
			{
				this.datas[this.index]=tmploader.parse(this.tags[this.index], this.types[this.index]);
				this.index++;
				this.load();
			}
		}
		else
		{
			this.states=2;
			if(this.afterSet!=null)
				this.afterSet();
		}
	}
	this.start=function()
	{
		if(this.status!=1)
			this.load();
	}
	this.add=function(url,tag,type)
	{
		len=this.urls.length;
		this.urls[len]=url;
		this.tags[len]=tag;
		this.types[len]=type;
		this.steps[len]=0;
	}
}
function form_value_replace(val){
    val = val.replace(/&/, '%26');
    val = val.replace(/=/, '%3D');
    val = val.replace(/ /, '%20');
    val = val.replace(/\n/, '\\n');
    return val;
}
function form_load_item(tobj, iname, ivalue){
    ttag = tobj.tagName;
    if(ttag == 'TEXTAREA')
        tobj.value = ivalue;
    else if(ttag == 'SELECT'){
        tlen = tobj.options.length;
        tval = ','+ivalue+',';
        for(ti=0;ti<tlen;ti++){
            if(tval.indexOf(','+tobj.options[ti].value+',') != -1)
                tobj.options[ti].selected = true;
            else
                tobj.options[ti].selected = false;
        }
    }
    else if(ttag == 'INPUT'){
        ttype = tobj.type;
        tval = ivalue;
        if(ttype == 'text'
            || ttype == 'textfield'
            || ttype == 'password'
            || ttype == 'hidden'
            || ttype == 'button'
            || ttype == 'submit'
            || ttype == 'reset'){
            tobj.value = ivalue;
        }
        else if(ttype == 'radio'
            || ttype == 'checkbox'){
            form_load_groups_item(tobj, iname, ivalue);
        }
    }
    else if(tobj.length > 0){
        tlen = tobj.length;
        if(tlen > 1){
            for(ti=0;ti<tlen;ti++){
                tttype = tobj[ti].type;
                if(tttype == 'radio'
                    || tttype == 'checkbox'){
                    form_load_groups_item(tobj[ti], iname, ivalue);
                }
            }
        }
    }
}
function form_load_groups_item(tobj, iname, ivalue){
    tval = ','+ivalue+',';
    if(tval.indexOf(','+tobj.value+',') != -1)
        tobj.checked = true;
    else
        tobj.checked = false;
}
function form_load_by_list(frm, lists){
	datas = new Array();
	tds = lists.split('&');
	fl_len = tds.length;
	for(fl_i=0;fl_i<fl_len;fl_i++){
		datas[fl_i] = new Array();
		tp = tds[fl_i].indexOf('=');
		if(tp > 0){
			datas[fl_i][0] = tds[fl_i].substring(0, tp);
			datas[fl_i][1] = tds[fl_i].substring(tp+1, tds[fl_i].length);
		}
		else{
			datas[fl_i][0] = '';
			datas[fl_i][1] = '';			
		}
	}
	fm_len = frm.elements.length;
	for(fm_i=0;fm_i<fm_len;fm_i++){
		issetit = false;
		tmp_name = frm.elements[fm_i].name;
		if(tmp_name == null || tmp_name == '')
			continue;
        if(tmp_name.substring(tmp_name.length-2, tmp_name.length) == "[]")
            tmp_name = tmp_name.substring(0, tmp_name.length-2);
		for(fl_i=0;fl_i<fl_len;fl_i++){
			if(datas[fl_i][0] != '' && datas[fl_i][0] == tmp_name){
				issetit = true;
				break;
			}
		}
		if(!issetit){
			datas[fl_len] = new Array();
			datas[fl_len][0] = tmp_name;
			datas[fl_len][1] = '';
			fl_len++;
		}
	}
	form_load(frm, datas);
}
function form_load(frm, datas){
    fl_len = datas.length;
    for(fl_i=0;fl_i<fl_len;fl_i++){
        iline = datas[fl_i];
        iname = iline[0];
        ivalue = iline[1];
        if(iname != ''
        	&& ivalue != null){
	        tobj = eval("frm."+iname);
	        if(tobj != null)
	        	form_load_item(tobj, iname, ivalue);
	        else{
				tobj = document.getElementsByName(iname+"[]");
				if(tobj != null)
					form_load_item(tobj, iname, ivalue);
	        }
        }
    }
}
function form_post(frm){
	var fp_names = new Array();
    var fp_name_list = new String();
    fp_name_list = ',';
    var fp_data_list = new String();
    var fp_tmp_len = frm.elements.length;
    var fp_obj = new Object();
    fp_j = 0;
    for(fp_i=0;fp_i<fp_tmp_len;fp_i++){
        tmp_obj = frm.elements[fp_i];
        tmp_tag = tmp_obj.tagName;
        tmp_type = tmp_obj.type;
        tmp_name = tmp_obj.name;
        if(tmp_name == null || tmp_name == '')
        	continue;
        if(tmp_name.substring(tmp_name.length-2, tmp_name.length) == "[]")
            tmp_name = tmp_name.substring(0, tmp_name.length-2);
        // 
        if(fp_name_list.indexOf(','+tmp_name+',') == -1){
            fp_name_list += tmp_name+',';
            fp_names[fp_j] = tmp_name;
            fp_j++;
            //
        	if(tmp_tag == 'TEXTAREA' || (tmp_tag == 'INPUT' && (tmp_type != 'radio' && tmp_type != 'checkbox'))){
        		eval('fp_obj.'+tmp_name+' = tmp_obj.value');
        	}
        	else if(tmp_tag == 'SELECT'){
                sel_len = tmp_obj.options.length;
                for(sel_i=0;sel_i<sel_len;sel_i++){
                    if(tmp_obj.options[sel_i].selected){
                        if(eval('fp_obj.'+tmp_name) == null)
                            eval('fp_obj.'+tmp_name+' = "'+form_value_replace(tmp_obj.options[sel_i].value)+'"');
                        else
                            eval('fp_obj.'+tmp_name+' = fp_obj.'+tmp_name+'+",'+form_value_replace(tmp_obj.options[sel_i].value)+'"');
                    }        
                }
        	}
        	else if(tmp_tag == 'INPUT'){
        		if(tmp_obj.checked == true){
        			eval('fp_obj.'+tmp_name+' = tmp_obj.value');	
        		}
        	}
        }
        else{
        	if(tmp_tag == 'TEXTAREA' || (tmp_tag == 'INPUT' && (tmp_type != 'radio' && tmp_type != 'checkbox'))){
        		if(eval('fp_obj.'+tmp_name) != null)
        			eval('fp_obj.'+tmp_name+' += \',\'+tmp_obj.value');
        		else
        			eval('fp_obj.'+tmp_name+' = tmp_obj.value');
        	}
        	else if(tmp_tag == 'SELECT'){
                sel_len = tmp_obj.options.length;
                for(sel_i=0;sel_i<sel_len;sel_i++){
                    if(tmp_obj.options[sel_i].selected){
                        if(eval('fp_obj.'+tmp_name) == null)
                            eval('fp_obj.'+tmp_name+' = "'+form_value_replace(tmp_obj.options[sel_i].value)+'"');
                        else
                            eval('fp_obj.'+tmp_name+' = fp_obj.'+tmp_name+'+",'+form_value_replace(tmp_obj.options[sel_i].value)+'"');
                    }        
                }
        	}
        	else if(tmp_tag == 'INPUT'){
        		if(tmp_obj.checked == true){
        			if(eval('fp_obj.'+tmp_name) != null)
        				eval('fp_obj.'+tmp_name+' += \',\'+tmp_obj.value');	
        			else
        				eval('fp_obj.'+tmp_name+' = tmp_obj.value');	
        		}
        	}
        }
    }
    fp_len = fp_names.length;
    for(fp_k=0;fp_k<fp_len;fp_k++){
        tmp_name = fp_names[fp_k];
        if(eval('fp_obj.'+tmp_name) != null)
            fp_data_list += '&'+tmp_name+'='+eval('fp_obj.'+tmp_name);
    }
    //alert(fp_data_list);
    return (fp_data_list);
}