function SSObject(pName,pParams)
{
this.name=pName;
this.params=pParams;
}
function Trans(pTrans,pBID,pOW,pC)
{
this.trans=pTrans;
this.bufferId=pBID;
this.overWrite=pOW;
this.condition=pC;
}
function Win(pUrl, pName, pFeats)
{
this.url=pUrl;
this.name=pName;
this.feats=pFeats;
this.open=false;
}
function Page(pName,pDiv,pUserOnly)
{
this.name = pName; this.container = pDiv; this.loaded = false; this.working = false; this.packages = []; this.packagesLoaded = false; this.data = []; this.dataLoaded = false; this.structure = ""; this.structureLoaded= false; this.structureMounted= false; this.userOnly = pUserOnly; this.params = []; this.ssObjects = [];
this.ssTrans = [];
}
Page.prototype.load=function(pParams)
{
this.loaded=(this.packagesLoaded&&this.structureLoaded&&this.dataLoaded);
var str="\nPage: "+this.name
+"\nthis.packagesLoaded: "+this.packagesLoaded;
if (!this.packagesLoaded) {
str+="[<br>";
for (var i=0;i<c.packages.length;i++) {
if (!c.packages[i].loaded) {
str+='<br>'+_arrToStr(c.packages[i],"this.packages["+i+"]","html");
}
}
str+="<br>]<br>";
ctrl.loadPackages(this);
}
str+="\nthis.structureLoaded: "+this.structureLoaded
+"\nthis.dataLoaded: "+this.dataLoaded
+"\nthis.loaded: "+this.loaded;
if(this.loaded)
{
if(this.data!=[])
{
ctrl.addToBuffer(this.data);
this.data=[];
}
var hasPackages=(this.packages && this.packages!="" && this.packages.length>0);
if (hasPackages) {
if(typeof this.params=="object" && this.params.join){
try{
window[this.name+"_setState"]('open',this.params);
}catch(e){
ctrl.loadPackages(this);
}
}else if(typeof this.params=="string"){
try{
eval(this.name+"_setState('open',['"+this.params.replace(/,/g,"','")+"']);");
}catch(e){
error("string"
+"\nthis.name: "+this.name
+"\neval: "+this.name+"_setState('open',['"+this.params.replace(/,/g,"','")+"']);"
+"\ne: "+_arrToStr([e])
,"Page.prototype.load");
}
}else{
try{
eval(this.name+"_setState('open',['"+this.params+"']);");
}catch(e){
error("else"
+"\nthis.name: "+this.name
+"\neval: "+this.name+"_setState('open',['"+this.params+"']);"
+"\ne: "+_arrToStr([e])
,"Page.prototype.load");
}
}
} else {
control(this.name, "response", ["ok"]);
}
this.params=[];
}
}
Page.prototype.setPackagesLoaded=function(pLoaded)
{
var i;
this.packagesLoaded=pLoaded;
this.load();
}
Page.prototype.setStructureLoaded=function(pLoaded)
{
this.structureLoaded=pLoaded;
this.load();
}
function Batch(i,t,tc,p) {
this.id=i;
this.transaction=t;
this.transCondition=tc; 
this.priority=p;
this.loaded=false;
this.firstTransCondition=this.transCondition;
}
function Controller()
{
var folder="";
var topAnchor="";
var currPage=-1;
this.messages={
carregando: "Carregando...",
carregandoTit: "Carregando<br>",
montaPagina: "Monta página...",
pelaPrimeriaVez: "•&nbsp;A página pela primeira vez<br>",
oConteudo: "•&nbsp;O conteúdo da página<br>"
};
this.pages=new Object(); this.batches=[]; this.windows=new Object(); this.urls=new Object(); var hideNewPageWhileLoading=true;
var minimumPageHeight=300;
var userOnlyRedirect="";
var useNewRequestObj = false;
var dataScript=null; 
var batchInited=false; 
var batchesLoaded=false;
var firstBatchFrequence=5000; 
var batchFrequence=1500; 
var batchWorking=false; 
var batchHanged=0; 
var batchHangedAfterTime=25000; 
var log=new Log();
var activateLog=false; 
var events={ 
onBatchesLoaded:[], 
onBatchLoaded:[], 
onBatchFractionLoaded:[]
};
this.getCurrPage=function() {
return currPage;
}
this.getSerializedServerObject=function(pObj) {
if (useNewRequestObj) {
return strObj=getRequestObject2(pObj);
} else {
return strObj=getRequestObject(pObj);
}
}
this.setMessages=function(pMsg) {
for (var prop in pMsg) {
this.messages[prop]=pMsg[prop];
}
}
this.setUseNewRequestObj=function(pBool) {
useNewRequestObj = pBool;
}
this.setHideNewPageWhileLoading=function(bHide) {
hideNewPageWhileLoading=bHide;
}
this.setMinimumPageHeight=function(h) {
minimumPageHeight=h;
}
this.setActivateLog=function(bActivate) {
activateLog=bActivate;
}
this.setBatchFrequence=function(bf,fbf) {
this.batchFrequence=bf;
this.firstBatchFrequence=(isUndefined(fbf) ? bf : fbf);
}
this.setTopAnchor=function(pTopAnchor)
{
this.topAnchor=pTopAnchor;
}
this.setUserOnlyRedirect=function(pPage)
{
this.userOnlyRedirect=pPage;
}
this.getPageset=function(pPageName)
{
var prop;
for(prop in this.pages)
if(prop==pPageName)return this.pages[prop];
return -1;
}
this.isWorking=function(){
for(var prop in this.pages)
if(this.pages[prop].working){
alert("working: "+prop);
return true;
}
return false;
}
this.addToBuffer2=function(pData)
{
for(var i=0;i<pData.length;i++){
Buffer.add(pData[i].type,pData[i].data,pData[i].collection,pData[i].objClass,pData[i].isCollection);
}
}
this.addToBuffer=function(pData)
{
for(var i=0;i<pData.length;i++){
Buffer.add(pData[i].type,pData[i].dataString,pData[i].collection,pData[i].objClass,pData[i].isCollection);
}
}
this.displayDummy=function(show) {
var div=document.getElementById("divDummy"); 
if(div)
div.style.display=(show ? "block" : "none");
}
this.initPage=function(pParams)
{
if(currPage.userOnly 
&& (!user.isUser||user.isUser==0)){ 
pParams[2]=[pParams[0],pParams[1],pParams[2]];
pParams[1]=[-1];
pParams[0]=this.userOnlyRedirect;
this.openPage(pParams);
return;
}
document.getElementById("iLoader").style.display = "none";
var div;
var newPage=(currPage.name!=pParams[0]);
use_form=false;
if (newPage) { 
this.setWaitMsg(this.messages.montaPagina); 
currPage=this.getPageset(pParams[0]);
for(prop in this.pages){
div=document.getElementById("div"+prop);
if(new String(div)!="null"&&currPage!=prop)
div.style.display="none";
}
setTimeout('ctrl.displayDummy(false);'
+'div=document.getElementById("div'+pParams[0]+'");'
+'if(new String(div)!="null")'
+ 'div.style.display="block";',1);
}else{ 
this.displayDummy(false);
div=document.getElementById("div"+pParams[0]);
if(new String(div)!="null")
div.style.display="block";
}
var data=iLoader.data;
if(currPage.packagesLoaded){ 
this.addToBuffer(data);
}else{
currPage.data=data;
}
if(pParams[1]==1 && currPage!=-1){ 
div=document.getElementById(currPage.container);
if(new String(div)!="null")
div.innerHTML=window.frames["iLoader"].document.getElementById("structure").innerHTML;
currPage.setStructureLoaded(true);
currPage.structureMounted=false;
}
if(new String(pParams[2])!="undefined"){ 
currPage.params=pParams[2];
}
if(currPage!=-1){
var el=document.getElementById(this.topAnchor);
if(new String(el)!="null"){
el.scrollIntoView();
}
currPage.dataLoaded=true;
currPage.load();
}
};
this.initASPPage=function () {
this.displayDummy(false);
for(prop in this.pages){
div=document.getElementById("div"+prop);
if(new String(div)!="null"&&currPage!=prop)
div.style.display="none";
}
var el=document.getElementById(this.topAnchor);
if(new String(el)!="null"){
el.scrollIntoView();
}
document.getElementById("iLoader").style.display = "inline";
this.resizeILoader(self.frames["iLoader"].document);
currPage="static";
}
this.resizeILoader = function(doc){
document.getElementById("iLoader").style.height = (minimumPageHeight>doc.body.scrollHeight ? minimumPageHeight : doc.body.scrollHeight)+"px";
}
this.openPage=function(pParams){
if(new String(pParams)=="undefined"){ 
return;
}
this.setWaitMsg(this.messages.carregando); 
var msg=this.messages.carregandoTit;
var i;
document.getElementById("iLoader").style.display = "none";
if(this.hideNewPageWhileLoading
|| currPage.name!=pParams[0]){ 
for(prop in this.pages) 
document.getElementById("div"+prop).style.display="none";
this.displayDummy(true);
var el=document.getElementById(this.topAnchor);
if(new String(el)!="null"){
el.scrollIntoView();
}
}
currPage=this.getPageset(pParams[0]);
var strPage=pParams[0]; 
if(currPage.userOnly 
&& (!user.isUser||user.isUser==0)){ 
pParams[2]=[pParams[0],pParams[1],pParams[2]];
pParams[1]=[-1];
pParams[0]=this.userOnlyRedirect;
this.openPage(pParams);
return;
}
if(!currPage.packagesLoaded){ 
this.loadPackages(currPage);
}
if(!currPage.structureLoaded){ 
msg+=this.messages.pelaPrimeriaVez;
var strStruct="0"; 
}else 
var strStruct="1"; 
if (useNewRequestObj) {
var strObj=getRequestObject2(pParams[1]);
} else {
var strObj=getRequestObject(pParams[1]);
}
var strTrans=getTransactions();
if (strTrans!="") {
msg+=this.messages.oConteudo;
}
currPage.dataLoaded=false;
if(new String(pParams[2])!="undefined"){
if(pParams[2].length && typeof pParams[2]=="object")
currPage.params=pParams[2];
else if(typeof pParams[2]=="string")
currPage.params=[pParams[2].replace(/,/g,"','")];
else 
currPage.params=[pParams[2]];
var strParams=currPage.params.join("','"); 
}
var str="";
str="Response.asp?page="+strPage+"&structLoaded="+strStruct;
str+="&Trans="+strTrans;
str+="&params="+strParams;
if(!use_form && strObj.length<1500) 
{
str+="&Objects="+strObj;
self.frames['iLoader'].location=this.folder+str;
}else{ 
self.frames['iLoader'].iLoaderForm.action=str;
self.frames['iLoader'].iLoaderForm.elements("Objects").value=strObj;
self.frames['iLoader'].iLoaderForm.submit();
}
this.setWaitMsg(msg); 
}
var getRequestObject=function(pObjects){
var strObj=""; 
if(0<currPage.ssObjects.length)
{
for(i=0;i<currPage.ssObjects.length;i++){ 
if((pObjects.length==currPage.ssObjects.length)
&&(pObjects.length!=0)
&&(typeof pObjects=="object") 
&& (pObjects[0]!=-1) && (pObjects[0]!="")) { 
if(new String(pObjects[i]).indexOf("[[")==-1 
|| !isNaN(pObjects[i])){
pObjects[i]="[['"+pObjects.join("','")+"']]"; 
}
currPage.ssObjects[i].params=pObjects[i]; 
} else if(pObjects.length!=0) { 
if((new String(pObjects).indexOf("[[")==-1) 
&& (typeof pObjects=="object")) { 
pObjects="[['"+pObjects.join("','")+"']]"; 
} else if ((new String(pObjects).indexOf("[[")==-1) 
&& (typeof pObjects=="string")) { 
pObjects="[['"+pObjects.replace(/,/g,"\',\'")+"']]"; 
} else if ((new String(pObjects).indexOf("[[")==-1) 
|| (!isNaN(pObjects))) { 
pObjects="[['"+pObjects+"']]"; 
}
currPage.ssObjects[i].params=pObjects;
}else{
pObjects="[[]]"; 
currPage.ssObjects[i].params=pObjects;
}
strObj+="["+currPage.ssObjects[i].name+","+currPage.ssObjects[i].params+"],";
}
if(strObj.charAt(strObj.length-1)==",")
strObj=strObj.slice(0,strObj.length-1);
}
return strObj;
}
var getRequestObject2=function(pObjects){
var str="";
if (typeof pObjects=="object" && typeof pObjects.length == "undefined") {
pObjects=[pObjects];
}
if(0<pObjects.length){
for (var i=0;i<pObjects.length;i++) {
if (typeof pObjects[i]!="object") {
error("Item "+i+"; "+pObjects[i]+" is not an object."
+"\nctrl.currPage.name: "+currPage.name
,"Controller.prototype.getRequestObject");
}
if (typeof pObjects[i]=="object" && typeof pObjects[i].length == "undefined") {
pObjects[i] = [pObjects[i]];
}
constr=Object.getConstructorName(pObjects[i][0]);
str+="[\""+constr+"\","+Parser.getSerializedObjectLiteral(pObjects[i])+"]";
if (i<pObjects.length-1) {
str+=",";
}
}
}
return str;
}
var getTransactions=function(){
var strTrans=""; 
if(0<currPage.ssTrans.length){
for(i=0;i<currPage.ssTrans.length;i++){
if(eval(currPage.ssTrans[i].condition) 
&& (Buffer.getRefByCollection(currPage.ssTrans[i].bufferId)==-1
|| currPage.ssTrans[i].overWrite)) 
strTrans+=currPage.ssTrans[i].trans+","; 
}
if(strTrans.charAt(strTrans.length-1)==",")
strTrans=strTrans.slice(0,strTrans.length-1);
}
return strTrans;
}
this.showServerResponse=function(){
if(document.getElementById('iLoader').style.display=='block'){
document.getElementById('iLoader').style.display='none';
}else{
document.getElementById('iLoader').style.display='block';
}
window.resizeBy(0,1);
window.resizeBy(0,-1);
}
this.showPrinter=function(){
if(document.getElementById('iPrinter').style.display=='block'){
document.getElementById('iPrinter').style.display='none';
}else{
document.getElementById('iPrinter').style.display='block';
}
window.resizeBy(0,1);
window.resizeBy(0,-1);
}
this.showLog=function(pCont){
if (pCont.style.display=='block') {
pCont.style.display='none';
}else {
log.sortBy('time',false);
log.printLog(pCont,true);
pCont.style.display='block';
}
}
this.addToLog=function(pLink,pPage,pDebug){
if (activateLog) {
log.addItem(pLink,pPage,pDebug);
}
}
this.addEventListener=function(pEvent,pHandler){
return events[pEvent].push(pHandler);
}
this.removeEventListener=function(pEvent,pIndex){
return events[pEvent].splice(pIndex,1);
}
this.loadData=function(pTrans,pObjects){
this.addToLog("loadData","controller","dataScript: "+dataScript+", this.batchesLoaded: "+this.batchesLoaded+", this.batchWorking: "+this.batchWorking);
if (dataScript) {
return;
}
str="Data.asp?Trans="+pTrans;
str+="&Objects="+getRequestObject2(pObjects);
dataScript = document.createElement('script');
if (false) {
iLoader.location=this.folder+str;
}else {
dataScript.src = this.folder+str;
dataScript.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(dataScript);
this.addToLog("loadData","batch url",this.folder+str);
}
}
this.initData=function(pData){
this.addToLog("initData","controller","pData: "+pData+", this.batchesLoaded: "+this.batchesLoaded);
this.addToBuffer(pData);
if (this.batchesLoaded){
var data=Buffer.getRefByCollection("Ingredients")
}
setTimeout('ctrl.dataLoaded()',50);
return;
}
this.dataLoaded=function(){
this.addToLog("dataLoaded","controller","this.batchWorking: "+this.batchWorking+", this.batchesLoaded: "+this.batchesLoaded);
this.removeDataScript();
if (!this.batchesLoaded) {
this.startBatch();
}
return;
}
this.removeDataScript=function() {
if (dataScript) {
document.getElementsByTagName('head')[0].removeChild(dataScript);
dataScript = null;
}
}
var killBatchHanged=function () {
if (batchHanged) {
clearTimeout(batchHanged);
batchHanged=0;
}
};
this.setBatchLoaded=function(pId,pIsIndex){
var listeners=events.onBatchLoaded;
function fireEvent(pCtrl,pId,pIndex) {
if (listeners.length>0) {
var i=listeners.length-1;
do {
if (typeof(listeners[i]) == "function") {
listeners[i](pId,pIndex);
}
} while (0>i--);
}
}
if (pIsIndex) {
this.batches[pId].loaded=true;
fireEvent(this,this.batches[pId].id,pId);
return true;
}
for (var i=0;i<this.batches.length;i++) {
if (this.batches[i].id==pId) {
this.batches[i].loaded=true;
fireEvent(this,pId,i);
return true;
}
}
return false;
}
this.getBatchIndex=function(pId){
var i=0;
if (0<this.batches.length)
do {
if (this.batches[i].id==pId)
return i;
} while (this.batches.length>++i);
return -1;
}
this.sortBatches=function(){
this.batches.propSort("priority");
}
this.unloadBatch=function(id) {
for (var i=0;i<this.batches.length;i++) {
if (this.batches[i].id==id) {
Buffer.removeCollection(id);
this.batches[i].loaded=false;
this.batches[i].transCondition=this.batches[i].firstTransCondition;
this.batchesLoaded=false;
}
}
}
this.startBatch=function(){
this.addToLog("startBatch","controller","this.batchWorking: "+this.batchWorking+", dataScript: "+dataScript);
if (this.batchWorking || dataScript!=null || this.isWorking()) {
if (this.isWorking()) {
clearTimeout(batchQueue);
batchQueue=setTimeout("ctrl.startBatch();",200);
}
return;
}
this.batchWorking=true;
if (!this.batchesLoaded) {
var i=-1;
do {
i++;
if (!this.batches[i].loaded) {
if (!this.batchInited) {
setTimeout("ctrl.loadData(ctrl.batches["+i+"].transaction,ctrl.batches["+i+"].transCondition);",this.firstBatchFrequence);
this.batchInited=true;
}else{
setTimeout("ctrl.loadData(ctrl.batches["+i+"].transaction,ctrl.batches["+i+"].transCondition);",this.batchFrequence);
}
break;
}
if (this.batches[i].loaded && this.batches.length>i+1) {
this.batchesLoaded=true;
}
} while (this.batches[i].loaded && this.batches.length>i+1);
}
}
this.batchFractionLoaded=function(pParams){
killBatchHanged();
this.batchWorking=false;
var bufferId="collection";
var data="dataString";
var ind=this.getBatchIndex(pParams.data[0][bufferId]);
this.addToLog("batchFractionLoaded","controller","this.batchWorking: "+this.batchWorking+", pParams.data[0].collection: "+pParams.data[0].collection);
this.batches[ind].transCondition=pParams.transCondition;
this.initData(pParams.data);
for (var i=0;i<events.onBatchFractionLoaded.length;i++) {
events.onBatchFractionLoaded[i](pParams.data[0].dataString); 
}
if (this.batches[ind].transCondition.top > pParams.data[0][data].length) {
this.setBatchLoaded(ind,true);
this.batches[ind].loaded=true;
if (ind == this.batches.length-1) {
batchesLoaded=true;
for (var i=0;i<events.onBatchesLoaded.length;i++) { 
events.onBatchesLoaded[i]();
}
}
}
try {
} catch (e) {
error("Something went wrong when trying to read the batch collection"
+"\n- Does this batch exist? - "+pParams.data[0][bufferId]
,"Controller.prototype.batchFractionLoaded")
}
}
this.release=function(){
}
this.getUrl=function(pUrl,pId)
{
var i,url=new String(pUrl);
if(url.indexOf(".")==-1) url=this.urls[url];
if(new String(pId)=="undefined")
pId="";
if(typeof pId=="object") {
for(i=0;i<pId.length;i++)
if(isNaN(pId[i])) pId[i]="'"+pId[i]+"'";
pId=pId.join(",");
}
pId=new String(pId);
if(pId.indexOf("[[")==-1) pId="[["+pId+"]]";
return url.replace(/<>/, pId); }
this.openWin=function(pWin, pId)
{
var h,w,f=this.windows[pWin].feats.split(",");
for (var i=0;i<f.length;i++) {
f[i]=f[i].split("=");
if (f[i][0]=="height") {
h=f[i][1];
}else if (f[i][0]=="width") {
w=f[i][1];
}
}
var t=Screen.getCentralizedTop(h);
var l=Screen.getCentralizedTop(w);
var url=this.getUrl(this.windows[pWin].url,pId);
this.open=true;
self.open(url,this.windows[pWin].name,this.windows[pWin].feats+",left="+l+",top="+t);
}
this.closeWins=function()
{
}
this.setPSPackages=function(pPageset,pLoaded)
{
try {
pPageset.setPackagesLoaded(pLoaded);
} catch (e) {
error("pPageset: "+pPageset
,"ctrl.setPSPackages");
}
}
this.loadPackages=function(pPageset)
{
if(pPageset.packages && (pPageset.packages!="")&&(pPageset.packages.length>0))
{
c.loadPackages(pPageset,pPageset.packages);
}else{
this.setPSPackages(pPageset, true);
}
}
this.loadPrinter=function(pParams){
var url="wPrinter.asp?Objects="+pParams[0]+"&Trans="+pParams[1];
open(url);
}
this.setPrint=function(pStr)
{
this.doPrint(pStr);
}
this.doPrint=function(pStr)
{
self.frames("iPrinter").printHTML(pStr);
}
}
Controller.prototype=new Page();
