/* CODEDATE $Workfile: QuickSearch.js $ ** $Revision: 12 $ ** $Date: 6/06/06 11:28a $  */
/* Copyright (C) - 2003 - Fidelity National Information Solutions */
function QS(varName) { this.init(varName); }
QS.prototype = {
 strings: {
 domIDStr: "MSXML2.DOMDocument",
 domIDStr4: "MSXML2.DOMDocument.4.0"
 },
 init: function(varName)
 {
 this.QuickWizard = new ActiveXObject(this.strings.domIDStr);
 this.QuickWizard.async = false;
 this.counts = new ActiveXObject(this.strings.domIDStr4);
 this.varName = varName;
 this.container = document.getElementById("quickSearch");
 try
 {
 // load QuickSearch wizard
 //if (DebugFromALocalFile)
 //{
 // this.QuickWizard.validateOnParse = false;
 // this.hasWiz = this.QuickWizard.load("QuickWizard.xml");
 //}
 //else
 this.hasWiz = siteObj.getWizard(this.QuickWizard, "Search", null, "QuickSearch", null, true);
 if (!this.hasWiz)
 return;
 if (this.QuickForm = this.QuickWizard.selectSingleNode("//QuickSearch"))
 {
 this.values = []; // this collects the input values
 this.currentDiv = -1; // index of current search div
 this.show();
 }
 }
 catch(e) {}
 },
 show: function()
 {
 // each div node under the QuickSearch node defines the HTML for a search panel
 this.divs = this.QuickForm.selectNodes("//div");
 for (var i = 0; i < this.divs.length; i++)
 {
 if (this.divs[i].getAttribute("default"))
 {
 this.currentDiv = i;
 break;
 }
 }
 
 // construct each panel's input elements from the corresponding prompts
 var prompts = this.QuickWizard.selectNodes("//PROMPT");
 for (i = 0; i < prompts.length; i++)
 {
 var promptGroup = this.QuickForm.selectSingleNode("//div[@id='" + prompts[i].getAttribute("QuickType") + "']");
 var node = this.QuickWizard.createNode(1, "input", "");
 node.setAttribute("onkeyup", this.varName + ".InputChanged(this);");
 node.setAttribute("onchange", this.varName + ".InputChanged(this);"); // for copy/paste via mouse context menu
 node.setAttribute("onactivate", this.varName + ".OnActivate(this);");
 node.setAttribute("ondeactivate", this.varName + ".OnDeactivate(this);");
 var uniqID = prompts[i].getAttribute("Field")+ "$" + i;
 node.setAttribute("id", prompts[i].getAttribute("Field")+ "$" + i);
 node.setAttribute("field", prompts[i].getAttribute("Field"));
 node.setAttribute("name", prompts[i].firstChild.text);
 if (prompts[i].selectSingleNode("*/SEARCHHELP"))
 node.setAttribute("title", prompts[i].selectSingleNode("*/SEARCHHELP").text);
 var picklist = null;
 if (picklist = prompts[i].selectSingleNode("*/PICKLIST"))
 node.setAttribute("style", "float:left; WIDTH:" + (this.container.style.pixelWidth - 27) + "px;");
 else
 node.setAttribute("style", "float:left; WIDTH:" + (this.container.style.pixelWidth - 4) + "px;");
 promptGroup.appendChild(node);
 if (picklist)
 {
 if (!this.picklists)
 this.picklists = [];
 this.picklists[uniqID] = picklist;
 node = this.QuickWizard.createNode(1, "img", "");
 node.setAttribute("src", "images/glass.gif");
 node.setAttribute("onclick", this.varName + ".OnLookup(this);");
 node.setAttribute("WIDTH", "17");
 node.setAttribute("HEIGHT", "17");
 node.setAttribute("style", "float:right;");
 promptGroup.appendChild(node);
 }
 }
 
 // add a counts button and text span to each panel
 for (var i = 0; i < this.divs.length; i++)
 {
 node = this.QuickWizard.createNode(1, "button", "");
 node.setAttribute("id", "btnCount" + i);
 node.setAttribute("onclick", this.varName + ".doCount();");
 node.setAttribute("style", "float:left; margin:10px 0px 5px 5px;");
 node.text = "Count";
 this.divs[i].appendChild(node);
 node = this.QuickWizard.createNode(1, "span", "");
 node.setAttribute("id", "spnCount" + i);
 node.setAttribute("style", "float:left; margin:11px 1px 1px 5px;");
 node.text = "0";
 this.divs[i].appendChild(node);
 }
 // add panel switching buttons
 var nodeNav = this.QuickWizard.createNode(1, "span", ""); // not a div, reserved for forms at this level!
 nodeNav.setAttribute("style", "display: inline-block");
 this.QuickForm.insertBefore(nodeNav, this.divs[0]);
 node = this.QuickWizard.createNode(1, "button", "");
 node.setAttribute("id", "btnPrevious");
 node.setAttribute("onclick", this.varName + ".ChangePanel(-1);");
 node.setAttribute("style", "float:left;");
 node.text = "<<";
 nodeNav.appendChild(node);
 node = this.QuickWizard.createNode(1, "div", "");
 node.setAttribute("id", "divLabel");
 node.setAttribute("style", "margin-right: 20px;");
 nodeNav.appendChild(node);
 var txtNode = this.QuickWizard.createNode(3, this.divs[this.currentDiv].value, "");
 node.appendChild(txtNode);
 node = this.QuickWizard.createNode(1, "button", "");
 node.setAttribute("id", "btnNext");
 node.setAttribute("onclick", this.varName + ".ChangePanel(1);");
 node.setAttribute("style", "float:right; margin-top: -14px; margin-bottom:5px");
 node.text = ">>";
 nodeNav.appendChild(node);
 // add a report button node
 node = this.QuickWizard.createNode(1, "button", "");
 node.setAttribute("id", "btnQS");
 node.setAttribute("onclick", this.varName + ".doQuickSearch()");
 node.setAttribute("style", "margin:1px 27px 5px 20px;");
 node.text = "Report";
 this.QuickForm.appendChild(node);
 
 document.write(this.QuickForm.xml); // create HTML in div with the XHTML content
 var nodes = this.QuickForm.selectNodes("//div/input"); // make them all opaque 40%
 for (i = 0; i < nodes.length; i++)
 {
 this.OnDeactivate(document.getElementById(nodes[i].getAttribute("id")));
 }
 btnQS.disabled = true;
 this.ChangePanel(0);
 
 return true;
 },
 // populate a field from a lookup dialog
 OnLookup: function(elem)
 {
 var bBoolean = false;
 var picklist = this.picklists[elem.previousSibling.id];
 
 if (picklist)
 {
 var nodeXML;
 if (nodeXML = picklist.selectSingleNode("//PICKLIST[@Type='XML']"))
 bBoolean = picklist.getAttribute("Maximum") > 1;
 }
 if (bBoolean)
 //window.showModalDialog('rexSrchBoolean.htm', picklist, 'help:yes;status:no;scroll:no;resizable:yes;');
 alert("rexSrchBoolean.htm needs to have non-searchgrid support");
 else
 {
 var divName = this.divs[this.currentDiv].getAttribute("id");
 var value = "";
 // can't check value directly, since it may be empty and holding the field name as a prompt
 this.values[divName] && this.values[divName][elem.previousSibling.id] && (value = this.values[divName][elem.previousSibling.id].trim());
 picklist.setAttribute("current", value);
 window.showModalDialog('rexSrchTableHelp.htm', picklist, 'help:no;status:no;scroll:no;resizable:yes;');
 var newValue = picklist.getAttribute("current");
 if (newValue && (newValue = newValue.trim()).length >= 1)
 {
 elem.previousSibling.value = newValue;
 this.InputChanged(elem.previousSibling);
 }
 }
 },
 
 
 // collect element value, disable button if no values set for the panel that it's in.
 // values are kept in a two dimensional associative array, 1st dimension is div id, second is field name.
 // if bCheckOnly, only set search button accordingly
 InputChanged: function(elem, bCheckOnly)
 {
 // alloc values cache if not present
 var divName = this.divs[this.currentDiv].getAttribute("id");
 if (!this.values[divName])
 {
 if (bCheckOnly)
 {
 btnQS.disabled = true;
 return;
 }
 this.values[divName] = new Object();
 }
 if (!bCheckOnly)
 {
 this.values[divName][elem.id] = elem.value.trim();
 }
 // check to see if all values in the current search type are empty, disable search button if so
 var nElements = 0;
 for (idx in this.values[divName])
 {
 nElements++;
 if (this.values[divName][idx].length == 0)
 nElements--;
 }
 btnQS.disabled = (nElements == 0);
 },
 
 OnActivate: function(elem)
 {
 if (elem.value.trim() == elem.name)
 elem.value = "";
 elem.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (elem.name == elem.value ? "50)" : "100)");
 },
 OnDeactivate: function(elem)
 {
 if (elem.value.trim().length == 0)
 elem.value = elem.name;
 elem.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (elem.name == elem.value ? "50)" : "100)");
 },
 
 ChangePanel: function(dir)
 {
 var current = this.currentDiv + dir;
 if (current < 0)
 current = this.divs.length - 1;
 else if (current >= this.divs.length)
 current = 0;
 this.currentDiv = current;
 divLabel.innerText = this.divs[current].getAttribute("label");
 for (var i = 0; i < this.divs.length; i++)
 document.getElementById(this.divs[i].getAttribute("id")).style.display = ((i == current) ? "" : "none");
 try
 {
 document.getElementById(this.divs[current].getAttribute("id")).focus();
 }
 catch(e) {}; // called during init to set initial panel, it's not drawn yet so nothing can be focused
 
 this.InputChanged(null, true);
 },
 
 doCount: function()
 {
 var spnCount = document.getElementById("spnCount" + this.currentDiv);
 var qry = this.buildQuery(this.divs[this.currentDiv]);
 if (qry == "")
 {
 spnCount.innerHTML = "---";
 spnCount.title = "";
 return;
 }
 var countsSearch = siteObj.countsURL(this.QuickWizard.selectSingleNode('//SEARCHURL').text, qry);
 this.counts.async = false;
 var tObj = xaObj.createTransactionObject('Counts', countsSearch, this.counts);
 var errCode = xaObj.begin(tObj);
 
 if (errCode)
 {
 spnCount.innerHTML = "FAILURE";
 spnCount.title = tObj.reason;
 }
 else
 {
 var status = this.counts.selectSingleNode("//STATUS");
 if (status.text != "SUCCESS")
 {
 spnCount.innerHTML = status.text;
 spnCount.title = this.counts.selectSingleNode("//STATUSMESSAGE").text;
 }
 else
 {
 var total = this.counts.selectSingleNode("//TOTAL");
 spnCount.innerHTML = total ? total.text : "---";
 spnCount.title = "SUCCESS";
 }
 }
 },
 
 doQuickSearch: function()
 {
 windowObj.doFunction("QuickSearch", "", this);
 },
 
 // applies Quick Search data to the report wizard
 //
 ApplyQuickSearch: function(wizard, queryBuilder, callingWindow)
 {
 var nodeDiv = this.divs[this.currentDiv];
 var criteria = this.values[nodeDiv.getAttribute("id")];
 var reportSingle = nodeDiv.getAttribute("single");
 var reportMulti = nodeDiv.getAttribute("multi");
 
 var hasCriteria = false;
 for (var idx in criteria)
 {
 if (criteria[idx].trim() != "")
 {
 var fld = idx.substr(0,idx.lastIndexOf('$'));
 var rNode = wizard.selectSingleNode("*/PROMPT[@Field='" + fld + "']");
 // create CURRENT and SEARCHCLAUSE nodes, since these fields are not attached to any grid controls
 var currentNode = null;
 if (rNode && !(currentNode = rNode.selectSingleNode("CURRENT")))
 {
 currentNode = wizard.createNode(1, "CURRENT", "");
 rNode.appendChild(currentNode);
 }
 currentNode.text = criteria[idx].toUpperCase();
 if (rNode && !(currentNode = rNode.selectSingleNode("SEARCHCLAUSE")))
 {
 currentNode = wizard.createNode(1, "SEARCHCLAUSE", "");
 rNode.appendChild(currentNode);
 }
 currentNode.text = fld + "=" + criteria[idx].toUpperCase();
 hasCriteria = true;
 }
 }
 if (hasCriteria)
 {
 // get counts
 var qry = queryBuilder();
 var countsSearch = siteObj.countsURL(wizard.selectSingleNode('//SEARCHURL').text, qry);
 this.counts.async = false;
 var tObj = xaObj.createTransactionObject('Counts', countsSearch, this.counts);
 xaObj.begin(tObj);
 
 var total = this.counts.selectSingleNode("//TOTAL");
 if (total)
 total = total.text;
 if (!total || total == "0")
 {
 callingWindow.close();
 alert("There are no matching records.");
 return;
 }
 // select report according to count, either may be missing
 var selectedReport = reportSingle;
 if (!selectedReport || (total > 1 && reportMulti))
 selectedReport = reportMulti;
 if (!selectedReport)
 selectedReport = "MLSX_AOPALL"; // punt!
 // set selected report in the wizard
 var sNode = wizard.selectSingleNode('.//SEARCHWIZARD');
 if (sNode)
 {
 var rNode = sNode.selectSingleNode('.//SELECTEDREPORT');
 if (!rNode)
 {
 rNode = wizard.createElement('SELECTEDREPORT');
 sNode.appendChild(rNode);
 }
 rNode.text = selectedReport;
 rNode.setAttribute("Resource", wizard.documentElement.getAttribute("Resource"));
 rNode.setAttribute("Level", wizard.documentElement.getAttribute("Level"));
 rNode.setAttribute("DisplayName", "");
 }
 }
 },
 
 buildQuery: function(theDiv)
 {
 var qStr = '';
 var criteria = this.values[theDiv.getAttribute("id")];
 var val;
 for (var idx in criteria)
 {
 if ((val = criteria[idx].trim()) != "")
 {
 var fld = idx.substr(0,idx.lastIndexOf('$'));
 qStr += (fld + '=' + escape(val) + '&').replace(/\+/g, '%2B');
 }
 }
 
 return qStr;
 }
};
