// [GLOBAL]
// end [GLOBAL]

function newXMLHttpRequest() {
   //Create a boolean variable to check for a valid IE instance.
   var xmlhttp = false;
   //Check if we are using IE.
   try {
      //If the javascript version is greater than 5.
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      //If not, then use the older active x object.
      try {
         //If we are using IE.
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
         //Else we are using a non-IE browser, create a JavaScript instance of the object.
         if ( typeof XMLHttpRequest != 'undefined') {
            xmlhttp = new XMLHttpRequest();
         }else{
            alert("Impossível criar objeto XMLHttpRequest");
            return null;
         }
      }
   }
   return xmlhttp;
}

function setSelect(obj,src){
   var xmlhttp = newXMLHttpRequest();
   xmlhttp.open("GET",src);
   xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	 var ar = eval(xmlhttp.responseText);
//	 alert(xmlhttp.responseText);
         obj.options.length=0;
	 for (i=0;i<ar.length;i++){
            obj.options[i] = new Option(ar[i][0],ar[i][1],ar[i][2],ar[i][3]);
	 }
      }
   }
   xmlhttp.send(null);
}

function sendRequest(xmlhttp,message){
   if ( xmlhttp == null) { return null; }
   xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
         onFormResponse(xmlhttp.responseText);
        // alert(xmlhttp.responseText);
      }
   }
   xmlhttp.send(message);
}

//===== OBJECT VERSIONS =====

//===== Request ===== {

function Request(url,message,action){
   this.xmlhttp = newXMLHttpRequest();
   this.onResponse = action;
   this.response= null;
   this.docType = "text";
   this.method = "get";
   this.send = send;
   this.url = url

   if ( ! this.xmlhttp) { return false; }

   function send(){
      var onResponse = this.onResponse;
      var xmlhttp = this.xmlhttp;
      var method = this.method;
      var type = this.docType;
       this.xmlhttp.onreadystatechange = function() {
         	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	        		if (type=="text"){
				//alert(xmlhttp.responseText);
									var resultado = xmlhttp.responseText;
									resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
									resultado = unescape(resultado); // Resolve o problema dos acentos
              		onResponse(resultado);
	       			}else{
									if (type=="xml"){
						 //alert(xmlhttp.responseXML);
						 					var resultado = xmlhttp.responseXML;
											resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
											resultado = unescape(resultado); // Resolve o problema dos acentos
											onResponse(resultado);
									}
							}
        	}
      }

      if (this.method == 'post' || this.method == 'POST') {
         try {
            this.xmlhttp.open("POST",this.url);
            this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=ISO-8859-1");
         } catch (e) {
            alert("Exception: " + e.message);
	    return null;
         }
         this.xmlhttp.send(message);
      }else{
         try {
            this.xmlhttp.open("GET",this.url+'?'+message);
         } catch (e) {
            alert("Exception: " + e.message);
	    return null;
         }
         this.xmlhttp.send(null);
      }
   }
}

//===== }

//===== AjaxForm ====
function AjaxForm(FormObject,JSaction) {
   this.frm = FormObject;
   this.xmlhttp = newXMLHttpRequest();
   this.getData = getData; // Method: get data of the form in a URI enconde string
   this.send = send; // Methosd: send the data of the form
   this.onReadyStateChange = JSaction; //Method: the action to be doneon the data arrival

   if (this.xmlhttp == null) return null;
   
}

function send(){
   var xmlhttp = this.xmlhttp;
   var onReadyStateChange = this.onReadyStateChange;
   if (this.frm.method == 'post' || this.frm.method == 'POST') {
      try {
         this.xmlhttp.open("POST",this.frm.action);
         this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
      } catch (e) {
         alert("Exception: " + e.message);
	 return null;
      }
      this.xmlhttp.onreadystatechange = function() {
         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						var resultado = xmlhttp.responseText;
						resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
						resultado = unescape(resultado); // Resolve o problema dos acentos
						onReadyStateChange(resultado);
         }
      }
      //alert(this.getData());
      this.xmlhttp.send(this.getData());
   }else{
      try {
         this.xmlhttp.open("GET",this.frm.action + '?'+ this.getData);
      } catch (e) {
         alert("Exception: " + e.message);
	 return null;
      }
      this.xmlhttp.onreadystatechange = function() {
         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						var resultado = xmlhttp.responseText;
						resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
						resultado = unescape(resultado); // Resolve o problema dos acentos
						onReadyStateChange(resultado);
         }
      }
      this.xmlhttp.send(null);
   }
}

function getData(){
    var msg = '';
   for (i=0; i<this.frm.elements.length; i++) {
      // We don't let the browser to try to send fieldset elements
      if ( this.frm.elements[i].type == 'fieldset') { continue; }; // for IE
      // nether of undefined types
      if ( this.frm.elements[i].type == undefined ) { continue; }; // for FireFox
      if ( this.frm.elements[i].type == 'radio' && this.frm.elements[i].checked == false ) { continue; };
      if ( this.frm.elements[i].type == 'checkbox' && this.frm.elements[i].checked == false ) { continue; };
      if ( this.frm.elements[i].disabled == true) { continue; };
      if ( this.frm.elements[i].type == 'select-multiple' ) {
         for (j=0; j<this.frm.elements[i].options.length; j++) {
	    if ( this.frm.elements[i].options[j].selected == false ) {
	       continue;
	    }else{
	       msg += this.frm.elements[i].name + "=" + encodeURIComponent(this.frm.elements[i].options[j].value) + "&";
	    }
	 }
	 continue;
      }
      msg += this.frm.elements[i].name + '=' + encodeURIComponent(this.frm.elements[i].value) + "&";
   }
   return msg;
}
