var ajaxCounter = 0;
//var debugAjax = false;

function Ajax(url,callbackFunction,xml,waitingFunction,doneFunction,debug){
 this.url = url;
 this.xml = xml;
 this.setHTTP = setHTTP;
 this.get = get;
 this.post = post;
 
 if(debugAjax && !this.debug)
  this.debug = true;
 else
  this.debug =  (typeof(debug)=='undefined')?false:debug;
   
 this.privateError = '';
 this.urlError = '';
 this.callbackFunction = (typeof(callbackFunction)=='undefined')?'void':callbackFunction;
 this.waitingFunction = waitingFunction;
 this.doneFunction = doneFunction;
 ajaxCounter +=1;}

function setHTTP(){
	
 this.http_request = false;
 if (window.XMLHttpRequest) { // Mozilla, Safari,... 
       this.http_request = new XMLHttpRequest(); 
       if (this.http_request.overrideMimeType)
           this.http_request.overrideMimeType('text/xml'); 
   } else if (window.ActiveXObject) { // IE 
       try { 
           this.http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
       } catch (e) { 
           try { 
               this.http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
           } catch (e) {} 
       } 
   } 
	
 //alert(this.http_request); 
 if (!this.http_request) { 
  alert('Seu browser não oferece suporte para esta aplicação'); 
  return false;}
 
 pXml = this.xml;
 pDebug = this.debug;
 pCallback = this.callbackFunction;
 pWait = this.waitingFunction;
 pDone = this.doneFunction;
 pUrlError = this.url;
 xmlHttp = this.http_request;

 this.http_request.onreadystatechange = function(){
		   eval(pWait)
	       if (xmlHttp.readyState == 4) 
		    { 
			   if (xmlHttp.status == 200) 
				{	
				
				 eval(pDone);
				 
				 var response = '';
				 var tmpCallBack = pCallback;
				 
				 if(pXml)
				  response = xmlHttp.responseXML; 
				 else 
				  response = xmlHttp.responseText;
				  
				 if(pDebug)
				   getError(response,pUrlError); 

				 eval(tmpCallBack + '(response)'); 
				 
				} 
			   	else
				 if(pDebug){
				  if(pXml)
				   getError(xmlHttp.responseXML,pUrlError); 
				  else 
				   getError(xmlHttp.responseText,pUrlError);}
				 else
				  alert('Ocorreu um erro ao tentar fazer a requisição no servidor.\nPor favor, contate o suporte técnico');
				}
       		}
}

function getError(error,urlError){
 debugAjax = new Ajax('erro.html');
 debugAjax.callbackFunction='showError';
 debugAjax.privateError = error;
 debugAjax.debug = false;
 debugAjax.urlError = urlError;
 debugAjax.get();
}

function showError(layoutErro){
 var errorWindow = layoutErro.replace('<title error>',debugAjax.urlError);
 errorWindow = errorWindow.replace('<erro>',debugAjax.privateError);
 errorWindow = errorWindow.replace('closeErrorWindow','closeErrorWindow('+ajaxCounter+')');
 
 errorWindow = errorWindow.replace(/IDerrorWindow/gi,'IDerrorWindow' + ajaxCounter);
 errorWindow = errorWindow.replace(/IDerrorTitle/gi,'IDerrorTitle' + ajaxCounter);
 errorWindow = errorWindow.replace(/IDerrorContent/gi,'IDerrorContent' + ajaxCounter);
  
 errorWindow = errorWindow.replace(';','top:'+parseInt(document.body.scrollHeight+2)+'px');
 errorWindow = errorWindow.replace(';',';height:300px;width:'+parseInt(document.body.scrollWidth-35)+'px');

 document.body.innerHTML += '<div>'+ errorWindow +'</div>';
}

function closeErrorWindow(windowNumber){
 document.getElementById('IDerrorWindow'+windowNumber).style.display = 'none';	
}
	
function get(){
   this.setHTTP();
   this.http_request.open('GET', this.url, true); 
   this.http_request.send(null);}
   
function post(frm){
	this.setHTTP();
	this.http_request.open('POST', this.url, true);
	this.http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
    this.http_request.send(formParameters(frm));}

function formParameters(frm){
 var formdata = '';
 with(frm){
  for (var i=0;i<length;i++)
   {
	 with(elements[i])
	  {
	   if(type!= 'image')
	    {
	     switch(type)
	   	 {
			case 'radio':if(checked)
						  formdata += name +'='+value.replace(/ /gi,'%20')+'&';
						 break;
			case 'select-multiple': if(selectedIndex==-1){formdata += name +'=&';}
								   for (var s=0;s<length;s++){
							   		if(options[s].selected)
						    		formdata += name +'='+options[s].value.replace(/ /gi,'%20')+'&';}
								   break;
			default		:formdata += name +'='+value.replace(/ /gi,'%20')+'&';
		 } // End Switch
		}// End If
	  } // End With 
   }// Next
 }// End With
 if(formdata!='' || formdata!='&')
  formdata = formdata.substr(0,formdata.length-1);
return formdata;
}
