/* * ajax - Kaérea Asynchronous JavaScript And XML * Marcio Ghiraldelli / André Cupini * 20/04/2006 */ function getE(el) { return document.getElementById(el); } // Tenta criar objeto xmlHTTP try { xmlhttp = new XMLHttpRequest(); } catch(e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { xmlhttp = false; } } } // Pilha de conexões pilha=[]; ipilha=0; function asHTML(idObj, url) { ajaxSendRequest(idObj, url, null, null); } function asForm(idObj, url, form) { ajaxSendRequest(idObj, url, form, null); } function asXML(callback, url, form) { ajaxSendRequest(null, url, form, callback); } function ajaxSendRequest(idObj, url, form, callback){ if (getE('divLoading')) { getE('divLoading').style.display = 'block'; } var delimiter; if (url.indexOf("?") > 0) { delimiter = "&"; } else { delimiter = "?"; } url = url + delimiter + "ajaxRand=" + Math.floor(Math.random()*9999); pilha[pilha.length] = [idObj, url, form, callback]; if ( (ipilha + 1) == pilha.length ) { ajaxRun(); } } function ajaxRun(){ var idObj = pilha[ipilha][0]; var url = pilha[ipilha][1]; var form = pilha[ipilha][2]; var callback = pilha[ipilha][3]; var method = "GET"; if (form != null) { method = "POST"; } xmlhttp.open(method, url, true); var formData = ""; var boundary; if (form != null) { boundary = "---------------------------"+ Math.floor(Math.random()*99999999999999); formData = generateMultiPart(form, boundary); xmlhttp.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary); } xmlhttp.send(formData); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4){ if (getE(idObj)) { retornoHTML = unescape(xmlhttp.responseText.replace(/\+/g, " ")); getE(idObj).innerHTML = retornoHTML; } if (callback) { if (! xmlhttp.responseXML || ! xmlhttp.responseXML.documentElement) { alert(xmlhttp.responseText); } else { eval(callback + "(xmlhttp.responseXML)"); } } ipilha++ if (ipilha < pilha.length) { setTimeout('ajaxRun()', 20); } else { if (getE('divLoading')) { getE('divLoading').style.display = 'none' } } } } } function generateMultiPart(frm, boundary) { var formData = ""; var cDisposition = 'Content-Disposition: form-data; name="'; for(i = 0; i < frm.elements.length; i++){ var e = frm.elements; if (e[i].name) { if (e[i].type == "text" || e[i].type == "textarea" || e[i].type == "button" || e[i].type == "hidden") { formData += '--' + boundary + '\r\n' + cDisposition + e[i].name +'"\r\n\r\n' + e[i].value + "\r\n"; } else if (e[i].type == "select-one") { formData += '--' + boundary + '\r\n' + cDisposition + e[i].name +'"\r\n\r\n' + e[i].options[e[i].selectedIndex].value + "\r\n"; } else if (e[i].type == "radio") { if (e[i].checked == true) { formData += '--' + boundary + '\r\n' + cDisposition + e[i].name +'"\r\n\r\n' + e[i].value + "\r\n"; } } else if (e[i].type == "select-multiple") { for (j = 0; j < e[i].length; j++) { if (e[i][j].selected) { formData += '--' + boundary + '\r\n' + cDisposition + e[i].name +'"\r\n\r\n' + e[i][j].value + "\r\n"; } } } else if (e[i].type == "checkbox") { if (e[i].checked) { formData += '--' + boundary + '\r\n' + cDisposition + e[i].name +'"\r\n\r\n' + e[i].value + "\r\n"; } } } } formData += "--" + boundary + "--\r\n"; return formData; }