/** * @author Francesco */ function AjaxModule() {} AjaxModule.prototype.convertSettings = []; AjaxModule.prototype.convertSettings[0] = []; AjaxModule.prototype.convertSettings[0]["convert"] = {pattern:/&/g, stringa:"[EC]"}; AjaxModule.prototype.convertSettings[0]["restore"] = {pattern:/\[EC\]/g, stringa:"&"}; AjaxModule.prototype.convertSettings[1] = []; AjaxModule.prototype.convertSettings[1]["convert"] = {pattern:/:/g, stringa:"[DP]"}; AjaxModule.prototype.convertSettings[1]["restore"] = {pattern:/\[DP\]/g, stringa:":"}; AjaxModule.prototype.convertSettings[2] = []; AjaxModule.prototype.convertSettings[2]["convert"] = {pattern:/\+/g, stringa:"[P]"}; AjaxModule.prototype.convertSettings[2]["restore"] = {pattern:/\[P\]/g, stringa:"+"}; AjaxModule.prototype.convertSettings[3] = []; AjaxModule.prototype.convertSettings[3]["convert"] = {pattern:/\*/g, stringa:"[AS]"}; AjaxModule.prototype.convertSettings[3]["restore"] = {pattern:/\[AS\]/g, stringa:"*"}; AjaxModule.prototype.convertSettings[4] = []; AjaxModule.prototype.convertSettings[4]["convert"] = {pattern:/%/g, stringa:"[MD]"}; AjaxModule.prototype.convertSettings[4]["restore"] = {pattern:/\[MD\]/g, stringa:"%"}; AjaxModule.prototype.history = []; AjaxModule.prototype.addHistoryEvent = function(page, pars) { AjaxModule.prototype.history[AjaxModule.prototype.history.length] = pars; if (typeof(window.dhtmlHistory) != 'undefined') dhtmlHistory.add(AjaxModule.prototype.history.length - 1, page); } AjaxModule.prototype.historyListener = function(newLocation, historyData) { if (newLocation != null && newLocation != undefined && parseInt(newLocation) >= 0) AjaxModule.prototype.historyRequest(historyData, AjaxModule.prototype.history[newLocation]); } AjaxModule.prototype.historyRequest = function(sPage, pars) { AjaxModule.prototype.clearParams(); AjaxModule.prototype.xmlhttp.open("POST", sPage, true); AjaxModule.prototype.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); AjaxModule.prototype.xmlhttp.onreadystatechange = AjaxModule.prototype.handleResponse; AjaxModule.prototype.xmlhttp.send(pars); } AjaxModule.prototype.convertString = function(str, key) { if (key != "convert" && key != "restore") throw new Error("Chiave per la conversione stringa non valida!"); var stringa = new String(str); for (var i = 0; i < AjaxModule.prototype.convertSettings.length; i++) { stringa = stringa.replace ( AjaxModule.prototype.convertSettings[i][key].pattern, AjaxModule.prototype.convertSettings[i][key].stringa ); } return stringa; } AjaxModule.prototype.createXMLHttpRequest = function() { if (typeof XMLHttpRequest != "undefined") { return new XMLHttpRequest(); } else if (window.ActiveXObject) { var axoVersions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "Microsoft.XMLHttp"]; for (var i = 0; i < axoVersions.length; i++) { try { var xmlobj = new ActiveXObject(axoVersions[i]); return xmlobj; } catch (e) {} } } throw new Error("Impossibile inizializzare l'oggetto XMLHttp."); } AjaxModule.prototype.loadingSrc = "/immagini/loading2.gif"; AjaxModule.prototype.setLoadingSrc = function(imm) { AjaxModule.prototype.loadingSrc = imm; } AjaxModule.prototype.loadingTarget = document.body; AjaxModule.prototype.setLoadingTarget = function(target) { var err = new String("Target non valido, impossibile gestire correttamente la risposta!"); if (typeof(target) == "object") { try { AjaxModule.prototype.loadingTarget = target; } catch (e) { throw new Error(err); } } else throw new Error(err); } AjaxModule.prototype.loadingGIF = function() { var contentHTML = ''; document.getElementById(AjaxModule.prototype.loadingTarget.id).innerHTML = contentHTML; } AjaxModule.prototype.params = []; AjaxModule.prototype.target = document.body; AjaxModule.prototype.xmlhttp = AjaxModule.prototype.createXMLHttpRequest(); AjaxModule.prototype.clearParams = function() { AjaxModule.prototype.params = []; } AjaxModule.prototype.setTarget = function(target) { var err = new String("Target non valido, impossibile gestire correttamente la risposta!"); if (typeof(target) == "object" || typeof(target) == "function") { try { AjaxModule.prototype.target = target; } catch (e) { throw new Error(err); } } else throw new Error(err); } AjaxModule.prototype.sendRequest = function(sPage, ah) { var pars = new String(); for (var i in AjaxModule.prototype.params) { pars += encodeURIComponent(i) + "="; pars += encodeURIComponent(AjaxModule.prototype.params[i]) + "&"; } AjaxModule.prototype.clearParams(); pars = pars.substr(0, (pars.length - 1)); // AGGIUNGO ALL'HISTORY if (ah != null && ah != undefined && ah === true) AjaxModule.prototype.addHistoryEvent(sPage, pars); AjaxModule.prototype.xmlhttp.send(pars); } AjaxModule.prototype.makeLoading = false; AjaxModule.prototype.makeRequest = function(sPage, mL, ah) { if (mL === true) AjaxModule.prototype.makeLoading = false; else AjaxModule.prototype.makeLoading = true; AjaxModule.prototype.xmlhttp.open("POST", sPage, true); AjaxModule.prototype.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); AjaxModule.prototype.xmlhttp.onreadystatechange = AjaxModule.prototype.handleResponse; AjaxModule.prototype.sendRequest(sPage, ah); } AjaxModule.prototype.handleResponse = function() { if (AjaxModule.prototype.xmlhttp.readyState >= 1 && AjaxModule.prototype.xmlhttp.readyState <= 3) { if (AjaxModule.prototype.makeLoading === true) AjaxModule.prototype.loadingGIF(); } else if (AjaxModule.prototype.xmlhttp.readyState == 4) { if (AjaxModule.prototype.xmlhttp.status == 200) { var response = AjaxModule.prototype.convertString(AjaxModule.prototype.xmlhttp.responseText, "restore"); if (typeof(AjaxModule.prototype.target) == "object") AjaxModule.prototype.target.innerHTML = response; else if (typeof(AjaxModule.prototype.target) == "function") AjaxModule.prototype.target(response); else throw new Error("Target non valido, impossibile gestire correttamente la risposta!"); } else { try { var contentHTML = "Errore durante il trasferimento dati."; contentHTML += "
" + AjaxModule.prototype.xmlhttp.statusText; contentHTML += " (" + AjaxModule.prototype.xmlhttp.status + ")"; document.getElementById(AjaxModule.prototype.target).innerHTML = contentHTML; } catch (e) {} } } } //--- Metodi per la lettura dinamica delle Form HTML AjaxModule.prototype.getSelected = function(opt) { var selected = new Array(); var index = 0; for (var intLoop = 0; intLoop < opt.length; intLoop++) { if ((opt[intLoop].selected) || (opt[intLoop].checked)) { index = selected.length; selected[index] = new Object; selected[index].value = opt[intLoop].value; selected[index].index = intLoop; } } return selected; } AjaxModule.prototype.outputSelected = function(opt, p) { var sel = AjaxModule.prototype.getSelected(opt); var strSel = ""; var cont = 0; for (var item in sel) { cont++; AjaxModule.prototype.params[p + "_" + cont] = sel[item].value; } AjaxModule.prototype.params["n_" + p] = cont; } AjaxModule.prototype.form_read = function(nome_form) { var oForm = document.getElementById(nome_form); for (var i = 0; i < oForm.elements.length; i++) { var oField = oForm.elements[i]; switch (oField.type) { case "checkbox" : case "radio" : if (oField.checked) AjaxModule.prototype.params[oField.id] = "1"; else AjaxModule.prototype.params[oField.id] = "0"; break; case "text" : case "hidden" : case "password" : case "textarea" : case "select-one" : AjaxModule.prototype.params[oField.id] = oField.value; break; case "select-multiple" : AjaxModule.prototype.outputSelected(oField.options, oField.id); break; } } }