//author:amao www.flyy.info
var xmlHttp;
function createXMLHttpRequest(){
	if(window.ActiveXObject){
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(window.XMLHttpRequest){
		xmlHttp=new XMLHttpRequest();
	}
}
function startRequest(args, container) {
	createXMLHttpRequest();
	xmlHttp.onreadystatechange =  function() {
		if (xmlHttp.readyState==4 && xmlHttp.status==200) {
			if (container) {
				document.getElementById(container).innerHTML=xmlHttp.responseText;
				document.getElementById(container).style.visibility='visible';
			}
		}
	}
//alert(args);
	xmlHttp.open("GET",args,true);
	xmlHttp.send(null);
}

//@desc    transform the elements of a form object and their values into request string( such as "action=1&name=surfchen")
//@param   form_obj          the form object
//@usage   formToRequestString(document.form1)
//@notice  this function can not be used to upload a file.if there is a file input element,the func will take it as a text input.
//         as I know,because of the security,in most of the browsers,we can not upload a file via xmlhttp.
//         a solution is iframe.
//@author  SurfChen <surfchen@gmail.com>
//@url     http://www.surfchen.org/
//@license http://www.gnu.org/licenses/lgpl.html LGPL
function formToRequestString(form_obj) {
    var query_string='';
    var and='';
    for (var i=0;i<form_obj.length ;i++ )
    {
        e=form_obj[i];
    
    
        if (e.name) {
            if (e.type=='select-one') {
                element_value=e.options[e.selectedIndex].value;
            } else if (e.type=='select-multiple') {
                for (var n=0;n<e.length;n++) {
                    var op=e.options[n];
                    if (op.selected) {
                        query_string+=and+e.name+'='+encodeURIComponent(op.value);
                        and="&"
                    }
                }
                continue;
            } else if (e.type=='checkbox' || e.type=='radio') {
                if (e.checked==false) {   
                    continue;   
                }   
                element_value=e.value;
            } else if (typeof e.value != 'undefined') {
                element_value=e.value;
            } else {
                continue;
            }
            query_string+=and+e.name+'='+encodeURIComponent(element_value);
            and="&"
        }

    }
    return query_string;
}
function ajaxFormSubmit(form_obj,container) {
	var url = form_obj.getAttributeNode("action").value;
	var request = formToRequestString(form_obj);

	urls=url.split("?");
	if (urls[1]=='' || typeof urls[1]=='undefined')
		url = urls[0]+"?"+request;
	else
		url=urls[0]+"?"+urls[1]+"&"+request;

	startRequest(url, container);
}
