// JavaScript Document /* BIBLIOTECA AJAX '/************************************************************* '* Empresa: CTIS Tecnologia S/A '* Autor: Cristian Medina '* Data de Criação: 11/05/2009 '* Data da Última Alteração: 28/08/2009 '* '* Revisões '* '* Data: '* Autor: '* Descrição: '****************************************************************/ function getxmlHttpRequest() { var obj = null; if (window.XMLHttpRequest){ obj = new XMLHttpRequest(); if(obj.overrideMimeType){ obj.overrideMimeType("text/xml"); } } else if (window.ActiveXObject){ try{ obj = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){ try{ obj = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){} } } return obj; } function executaAjax(url, metodo, parametros, callback) { xmlHttp = getxmlHttpRequest(); if ((xmlHttp != null) || (xmlHttp != undefined)){ xmlHttp.onreadystatechange = function (){ if (xmlHttp.readyState == 4){ if (xmlHttp.status == 200){ callback(xmlHttp); } else { alert('Ocorreu um erro.'); } } } xmlHttp.open(metodo, url, true); if (metodo=='POST'){ xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlHttp.setRequestHeader("Content-length", parametros.length); } xmlHttp.send(parametros); } } function executaAjaxForm(url, metodo, parametros, callback, form) { xmlHttp = getxmlHttpRequest(); if ((xmlHttp != null) || (xmlHttp != undefined)) { xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { callback(xmlHttp, form); } else { alert('Ocorreu um erro.'); } } } xmlHttp.open(metodo, url, true); if (metodo == 'POST') { xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlHttp.setRequestHeader("Content-length", parametros.length); // xmlHttp.setRequestHeader("Connection", "close"); } xmlHttp.send(parametros); } } function serializeForm(form) { // Return value var retVal = ''; // Getting ALL elements inside of form element var els = form.getElementsByTagName('*'); // Looping through all elements inside of form and checking to see if they're "form elements" for (var idx = 0; idx < els.length; idx++) { var el = els[idx]; // According to the HTTP/HTML specs we shouldn't serialize disabled controls // Notice also that according to the HTTP/HTML standards we should also serialize the // name/value pair meaning that the name attribute are being used as the ID of the control // Though for Ra controls the name attribute will have the same value as the ID attribute if (!el.disabled && el.name && el.name.length > 0) { switch (el.tagName.toLowerCase()) { case 'input': switch (el.type) { // Note we SKIP Buttons and Submits since there are no reasons as to why we // should submit those anyway case 'checkbox': case 'radio': if (el.checked) { if (retVal.length > 0) { retVal += '&'; } retVal += el.name + '=' + escape(el.value); } break; case 'hidden': case 'password': case 'text': if (retVal.length > 0) { retVal += '&'; } retVal += el.name + '=' + escape(el.value); break; } break; case 'select': case 'textarea': if (retVal.length > 0) { retVal += '&'; } retVal += el.name + '=' + escape(el.value); break; } } } return retVal; } /* standard small functions */ function $m(quem) { return document.getElementById(quem) } function remove(quem) { quem.parentNode.removeChild(quem); } function addEvent(obj, evType, fn) { // elcio.com.br/crossbrowser if (obj.addEventListener) obj.addEventListener(evType, fn, true) if (obj.attachEvent) obj.attachEvent("on" + evType, fn) } function removeEvent(obj, type, fn) { if (obj.detachEvent) { obj.detachEvent('on' + type, fn); } else { obj.removeEventListener(type, fn, false); } } function ajaxUpload(form, url_action, callback) { //testing if 'form' is a html object or a id string form = typeof (form) == "string" ? $m(form) : form; var erro = ""; if (form == null || typeof (form) == "undefined") { erro += "The form of 1st parameter does not exists.\n"; } else if (form.nodeName.toLowerCase() != "form") { erro += "The form of 1st parameter its not a form.\n"; } if (erro.length > 0) { alert("Error in call ajaxUpload:\n" + erro); return; } //Criando o Iframe var iframe = document.createElement("iframe"); iframe.setAttribute("id", "ajax-temp"); iframe.setAttribute("name", "ajax-temp"); iframe.setAttribute("width", "0"); iframe.setAttribute("height", "0"); iframe.setAttribute("border", "0"); iframe.setAttribute("style", "width: 0; height: 0; border: none;"); //Adiciona ao documento form.parentNode.appendChild(iframe); window.frames['ajax-temp'].name = "ajax-temp"; //ie sucks //add Eventos var carregou = function() { removeEvent($m('ajax-temp'), "load", carregou); var cross = "javascript: "; cross += "window.parent." + callback + "(document.body.innerHTML); void(0); "; $m('ajax-temp').src = cross; //deleta o iframe setTimeout(function() { remove($m('ajax-temp')) }, 250); } addEvent($m('ajax-temp'), "load", carregou) //propriedades do form form.setAttribute("target", "ajax-temp"); form.setAttribute("action", url_action); form.setAttribute("method", "post"); form.setAttribute("enctype", "multipart/form-data"); form.setAttribute("encoding", "multipart/form-data"); //submit form.submit(); }