/**
 * This function builds a query string with all inputs from the form object it receives as parameter
 */ 
function buildPostString(formObj){
	var postString="";
	if (formObj != null) {
		for(i=0; i<formObj.elements.length; i++) {
    		var element = formObj.elements[i];
    		if (element.type!="undefined") {
        		if ((element.type == "hidden" || element.type == "text" || element.type == "textarea" || element.type == "select-one")
            		|| ((element.type == "checkbox" || element.type == "radio") && (element.checked))) {
            			postString +="&"+element.name+"="+element.value;
        		}
    		}
		}
	}
	return postString;
}

/**
  Creates an HTTP request object for retreiving files.
  @return HTTP request object.
 */
 getHTTP=function() {
     var obj;
     try{ //to get the mozilla httprequest object
         obj = new XMLHttpRequest();
     }catch(e){
         try{ //to get MS HTTP request object
             obj=new ActiveXObject("Msxml2.XMLHTTP.4.0");
         }catch(e){
             try{ //to get MS HTTP request object
                 obj=new ActiveXObject("Msxml2.XMLHTTP");
             }catch(e){
                 try{// to get the old MS HTTP request object
                     obj = new ActiveXObject("microsoft.XMLHTTP"); 
                 }catch(e){
                     throw "Unable to get an HTTP request object.";
                 }
             }    
         }
     }
     return obj;
 }
 

AjaxVilt = {
	syncPost : function(destination,data){
     	var xmlhttp = getHTTP();
      	xmlhttp.open("POST", destination, false);
      	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      	xmlhttp.setRequestHeader("Content-length", data.length);
      	xmlhttp.setRequestHeader("Connection", "close");
      	xmlhttp.send(data);
     	return xmlhttp.responseText;
	},
	
	asyncPost : function(destination, data, handler){
   		var xmlhttp = getHTTP();
      	xmlhttp.open("POST", destination, true);
      	xmlhttp.onreadystatechange=function() {
      		if (xmlhttp.readyState==4) {
      			try {
      				handler(xmlhttp.responseText);
      			} catch(e) {
      				alert(xmlhttp.responseText);
      			}
      		}
      	}
      	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      	xmlhttp.setRequestHeader("Content-length", data.length);
      	xmlhttp.setRequestHeader("Connection", "close");
      	xmlhttp.send(data);
	},
	
	syncGet : function(destination){
      var xmlhttp = getHTTP();
      xmlhttp.open("GET", destination, false);
      xmlhttp.send("");
      return xmlhttp.responseText;
	},
	
	asyncGet : function(destination, handler){
   	var xmlhttp = getHTTP();
      xmlhttp.open("GET", destination, true);
      xmlhttp.onreadystatechange=function() {
      	if (xmlhttp.readyState==4) {
      		try{
      			handler(xmlhttp.responseText);
      		}catch(e){
      			alert(xmlhttp.responseText);
      		}
      	}
      }
      xmlhttp.send("");
	}
}
