//  http://developer.apple.com/internet/webcontent/xmlhttpreq.html
// http://www.jibbering.com/2002/4/httprequest.html
 
Ajax = function(){

	return{

	getResponseHeaders : function(uri, callback, header){
	     xmlhttp.open("HEAD", uri, true);
             xmlhttp.onreadystatechange=function() {
                 if (xmlhttp.readyState==4) {
  		    if (xmlhttp.status==404){
                        callback.failure(xmlhttp.getAllResponseHeaders());
		    }
		    else{
		        if(header){
                           callback.success(xmlhttp.getAllResponseHeaders());
		        }
			else{
			   callback.success(xmlhttp.getResponseHeader(header));
			}
	            }
                 }
             }
              xmlhttp.send(null);
	},

        // Code for this method from YUI connection object setForm() method
	getFormData: function(formId){

                var oForm, oElement, oName, oValue, oDisabled,  hasSubmit = false, data = [], item = 0, i,len,j,jlen,opt;

		if(typeof formId == 'string'){
			oForm = (document.getElementById(formId) || document.forms[formId]);
		}
		else if(typeof formId == 'object'){
			// Treat argument as an HTML form object.
			oForm = formId;
		}

		// Iterate over the form elements collection to construct the
		// label-value pairs.
		for (i=0,len=oForm.elements.length; i<len; ++i){
			oElement  = oForm.elements[i];
			oDisabled = oElement.disabled;
		         oName  = oElement.name;

			// Do not submit fields that are disabled or
			// do not have a name attribute value.
			if(!oDisabled && oName)
			{
                             oName  = encodeURIComponent(oName)+'=';
                             oValue = encodeURIComponent(oElement.value);
		             switch(oElement.type)
			     {
                                 // Safari, Opera, FF all default opt.value from .text if
                                 // value attribute not specified in markup
			         case 'select-one':
                                     if (oElement.selectedIndex > -1) {
                                         opt = oElement.options[oElement.selectedIndex];
                                         data[item++] = oName + encodeURIComponent((opt.attributes.value && opt.attributes.value.specified) ? opt.value : opt.text);
                                      }
                                 break;
				 case 'select-multiple':
                                     if (oElement.selectedIndex > -1) {
                                         for(j=oElement.selectedIndex, jlen=oElement.options.length; j<jlen; ++j){
                                             opt = oElement.options[j];
                                             if (opt.selected) {
                                                  data[item++] = oName + encodeURIComponent((opt.attributes.value && opt.attributes.value.specified) ? opt.value : opt.text);
                                             }
                                         }
                                     }
				break;
				case 'radio':
				case 'checkbox':
				    if(oElement.checked){
                                       data[item++] = oName + oValue;
				    }
				break;
				case 'file':
				// stub case as XMLHttpRequest will only send the file path as a string.
				case undefined:
				// stub case for fieldset element which returns undefined.
				case 'reset':
				// stub case for input type reset button.
				case 'button':
				// stub case for input type button elements.
				break;
				default:
                        		data[item++] = oName + oValue;
				}
			}
		}


		var sFormData = data.join('&');

		return sFormData;

	},



	/*
	0	The request is not initialized
	1	The request has been set up
	2	The request has been sent
	3	The request is in process
	4	The request is complete
	var callback =
        {
	     success: fnsucess,
             failure: fnfailure,
	     reqsent: fnsent,
	     inprocess: fninprocess,
	     reqsetup: fnsetup,
	     reqnotinit: fnnotinit
        };
	*/
	connect :  function(method, url, callback, params, async)
	{
		var xmlhttp;

		if (window.XMLHttpRequest)
  		{
  			// code for IE7+, Firefox, Chrome, Opera, Safari
	  		xmlhttp=new XMLHttpRequest();
  		}
		else
	  	{
  			// code for IE6, IE5
 			 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	 	 }

		xmlhttp.onreadystatechange=function()
		{

			// alert(xmlhttp.readyState);

			if(xmlhttp.readyState==0)
	  		{
  				callback.reqnotinit(xmlhttp);
  			}

			if(xmlhttp.readyState==1)
  			{
  				callback.reqsetup(xmlhttp);
	  		}

			if(xmlhttp.readyState==2)
  			{
  				callback.reqsent(xmlhttp);
	  		}

			if(xmlhttp.readyState==3)
  			{
  				callback.reqinprocess(xmlhttp);
	  		}

			if(xmlhttp.readyState==4)
  			{
  				if (xmlhttp.status==404){
					callback.failure(xmlhttp);
				}
				else{
				   callback.success(xmlhttp);
				}
	  		}
		}

	        if(!params){
		   var params = '';
	        }

	        if(!async){
		   var async = true;
	        }

		if(method=='GET'){
                        if(params!=''){
   			   url+='?'+params;
                        }
			xmlhttp.open('GET',url, async);
			xmlhttp.send(null);
		}
		else{
			xmlhttp.open('POST',url, async);
        		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        		xmlhttp.setRequestHeader("Content-length", params.length);
        		xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.send(params);
		}
	
	
	}


	};

}();


