// url_encode version 1.0 
function url_encode(str){ 
	var hex_chars = "0123456789ABCDEF"; 
    	var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
    	var n, strCode, hex1, hex2, strEncode = ""; 

    	for(n = 0; n < str.length; n++) { 
       	if (noEncode.test(str.charAt(n))) { 
            		strEncode += str.charAt(n); 
		} else {
            		strCode = str.charCodeAt(n); 
            		hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
            		hex2 = hex_chars.charAt(strCode % 16); 
            		strEncode += "%" + (hex1 + hex2); 
			//alert("str.charAt(n): "+str.charAt(n)+" strCode: "+strCode+" strEncode: "+strEncode);
        	}
    	}
    	return strEncode; 
}

// url_decode version 1.0 
function url_decode(str) { 
	var n, strCode, strDecode = ""; 

    	for (n = 0; n < str.length; n++) { 
       	if (str.charAt(n) == "%") { 
            		strCode = str.charAt(n + 1) + str.charAt(n + 2); 
			strDecode += String.fromCharCode(parseInt(strCode, 16)); 
			n += 2; 
        	} else {
            		strDecode += str.charAt(n); 
        	}
    	}
    	return strDecode; 
}
function criaxmlhttp(){
	try { 	//IE 
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); //Nova versão do ActiveX
	} catch (e) { 
		try { 
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //Antiga versão do ActiveX
		} catch (E) { 
			xmlhttp = false; 
		} 
	} 
	//FireFox
	if  (!xmlhttp && typeof  XMLHttpRequest != 'undefined' ) { 
		try  { 
			xmlhttp = new  XMLHttpRequest(); 
		} catch  (e) { 
			xmlhttp = false ; 
		} 
	} 

	return xmlhttp;
}

var loading = '<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">';
    loading =  loading + '<tr>';
    loading =  loading + '<td align="left" valign="middle"><div align="center"><img src="imagens/loading.gif" width="320" height="320" /></div></td>';
    loading =  loading + '</tr>';
    loading =  loading + '</table>';

function ajaxDo(url,form,campos,div,exec) 
{ 
//alert('ajaxDo');
xmlhttp = criaxmlhttp();

	var z = "";
	//if(!form || form!='')
	//{
		for(var i=0;i < campos.length;i++)
		{
	
			if ((document.forms[form].elements[campos[i]].type == 'text') || (document.forms[form].elements[campos[i]].type == 'textarea') || (document.forms[form].elements[campos[i]].type == 'hidden'))
			{
				z = z+campos[i]+"="+url_encode(document.forms[form].elements[campos[i]].value)+"&";
			}
			else
			{
				if((document.forms[form].elements[campos[i]].type == 'select-one') || (document.forms[form].elements[campos[i]].type == 'select-multiple'))
				{
					z = z+campos[i]+"="+document.getElementById(""+campos[i]+"").options[document.getElementById(""+campos[i]+"").selectedIndex].value+"&";
				}
				else//é radio button.
				{
					for(var j=0;j < document.forms[form].elements[campos[i]].length;j++)//array dos radios buttons
					{
						if (document.forms[form].elements[campos[i]][j].checked)
						{
								z = z+campos[i]+"="+url_encode(document.forms[form].elements[campos[i]][j].value)+"&";
						} //3 if
					}
				} // 2 if
			} // 1 if
		}
	//}
	document.getElementById(div).innerHTML = loading;
   	xmlhttp.open("POST",url, true); 
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			//alert(xmlhttp.responseText);
			if(xmlhttp.status == 200)
			{
				if(exec) //o retorno deve ser um javasript válido
				{
					document.getElementById(div).innerHTML = '';
					eval(xmlhttp.responseText);
					//document.location.url='#';
				}
				else
				{
					document.getElementById(div).innerHTML =  xmlhttp.responseText;
					//document.location.url='#';
				}
			}
			else
			{
				document.getElementById(div).innerHTML = "Error: returned status code " + xmlhttp.status + " " + xmlhttp.statusText;
				//document.location.url='#'
			}
		} 
	}

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(z);
}
