/* Unit de funções JavaScript para processamento de requisições AJAX Copyright © 2007-2008 Server Informática Autor - Fábio Manfredini */ function GetParams(obj) { var i = 0; var getstr = ''; var objChild = obj.childNodes[i]; if (obj) { while (objChild) { getstr += GetParams(objChild); i++; objChild = obj.childNodes[i]; } // if (obj.tagName == "DIV") { // alert(obj.innerHTML); // for (var i=0,c;c=obj.childnodes[i];i++) alert(c.tagName) // } if (obj.tagName) { if (obj.tagName.toUpperCase() == "SELECT") { /* if (obj.selectedIndex > -1) */ for (var i = 0; i < obj.options.length; i++) if (obj.options[i].selected) getstr += obj.name + "=" + encodeURIComponent(obj.options[i].value) + "&"; } if (obj.tagName.toUpperCase() == "TEXTAREA") { getstr += obj.name + "=" + encodeURIComponent(obj.value) + "&"; } if (obj.tagName.toUpperCase() == "INPUT") { if (obj.type == "text") { getstr += obj.name + "=" + encodeURIComponent(obj.value) + "&"; } if (obj.type == "hidden") { getstr += obj.name + "=" + encodeURIComponent(obj.value) + "&"; } if (obj.type == "password") { getstr += obj.name + "=" + encodeURIComponent(obj.value) + "&"; } if (obj.type.toUpperCase() == "CHECKBOX") { if (obj.checked) { getstr += obj.name + "=S&"; } else { getstr += obj.name + "=N&"; } } if (obj.type.toUpperCase() == "RADIO") { if (obj.checked) { getstr += obj.name + "=" + encodeURIComponent(obj.value) + "&"; } } } } } return getstr; } function MakeParamsURL(URL, Parameters, obj) { var strResult = ""; var strFormFields; if (typeof obj == "undefined") obj = document.body; if (obj == null) obj = document.body; strFormFields = GetParams(obj); strResult = URL; if (Parameters != "") { strResult += "?" + Parameters; } if (strFormFields != "") { if (Parameters != "") strResult += "&" else strResult += "?"; strResult += strFormFields; } return strResult; } /* Execução uma requisição Ajax no servidor e o insere o HTML retornado no DIV informado */ function Ajax(divID, URL, Parameters, obj, Carregando, OnGetResponse, UseObj, async, CallBack, ResponseToOnGetParameter) { if (window.XMLHttpRequest) { var HTTPReq = new XMLHttpRequest(); } else { var HTTPReq = new ActiveXObject("Microsoft.XMLHTTP"); } el = document.getElementById(divID); obj = document.getElementById(obj); //var URLParams = MakeParamsURL(URL,Parameters, obj); if (UseObj){ var URLParams = MakeParamsURL(URL,Parameters, obj); } else { var URLParams = URL + "?" + Parameters; } var SessionID = getParameterByName('SessionID'); if (SessionID !== ''){ URLParams = URLParams +'&SessionID='+SessionID; } $.ajax({url:URLParams, dataType : 'text', success: function(response) { if (el) { if (el.tagName) { if (response.substr(0,4) == 'URL:') { window.location = response.substr(4); } else { div = document.getElementById(divID); if (div == '' || div == null || div ==undefined){ return; } if (el.tagName.toUpperCase() == "DIV") { document.getElementById(divID).innerHTML = response; } else { document.getElementById(divID).value = decodeURIComponent(response); } } } } $('.bshop-filtro label + input[type=checkbox]').prev().css('display','inline'); $('.bshop-filtro label + input[type=checkbox]').css('vertical-align','middle'); if ($.mobile !== undefined) { $.mobile.loading("hide"); } if (OnGetResponse !== '' && OnGetResponse !== null && OnGetResponse !==undefined){ if (ResponseToOnGetParameter){ OnGetResponse(response); } else { OnGetResponse(); } } } }).done(function(response) { if (CallBack !== '' && CallBack !== null && CallBack !==undefined){ CallBack(response); } }); if (Carregando == 1) { /* if (el) { if (el.tagName) { if (el.tagName.toUpperCase() == "DIV") { document.getElementById(divID).innerHTML = "Carregando..." + document.getElementById(divID).innerHTML; } } }*/ if ($.mobile !== undefined) { $.mobile.loading( "show", { text: "Buscando dados...", textVisible: true, theme: "b", html: "" }); } else { if (el) { if (el.tagName) { if (el.tagName.toUpperCase() == "DIV") { document.getElementById(divID).innerHTML = "Carregando..." + document.getElementById(divID).innerHTML; } } } } } } function AjaxNewWindow(URL, Parameters, obj) { var URLParams = MakeParamsURL(URL,Parameters, obj); window.open(URLParams); } function AjaxRedirect(URL, Parameters) { var URLParams = MakeParamsURL(URL,Parameters, null); window.location = URLParams; } function setHTMLElement(Field, Value) { document.getElementById(Field).value = Value; } function AjaxSwitch(divID, URL, Parameters, obj, Carregando, CallBack) { if (document.getElementById(divID).innerHTML == "") { Ajax(divID,URL,Parameters, obj, Carregando, null, true, true, CallBack, true); } else { document.getElementById(divID).innerHTML = ""; } } function setVisibility(id, value){ el = document.getElementById(id); if (el !== null) { el.style.visibility = value; } } function getSelectedOption(id){ return $( "select#"+id+" option:selected" ).val() } function setModoLayout(ReportName, Preset){ var str = ''; var DoSetModoLayout = function(s){ if (s == '' || s == undefined || s==null){ return; } var obj = JSON.parse(s); if (!obj.PresetFound){ return; } elReportMode = document.getElementById('ReportMode'); elLayout = document.getElementById('Layout'); if (elLayout == null || elLayout == undefined){ Ajax('divLayouts','/bshop/GetReportLayouts','Report=' + ReportName,null,1,function(){setModoLayout(ReportName, getSelectedOption('Preset'))},true,true,null,false); } if (elReportMode !== null && elReportMode !== undefined){ var refresh = obj.Modo != elReportMode.value; if (refresh){ elReportMode.value = obj.Modo; elReportMode.onchange(); } } if (elLayout !== null && elLayout !== undefined && obj.Layout != ''){ elLayout.value = obj.Layout; } else { if (elLayout !== null){ elLayout.SelectedIndex = 0; } } //if (elReportMode == null || elReportMode == undefined){ //document.getElementById('BtnAtualiza').click(); document.getElementById('BtnRefreshFilter').click(); //} } Ajax('', '/bshop/getModoLayoutFromPreset', 'Preset=' + Preset + '&ReportName=' + ReportName + "&A=1", null, 0, DoSetModoLayout, false, true, null, true); } function getParameterByName(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, "\\$&"); var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); }