/* CODEDATE $Workfile: rxRptSharedFunc.js $ ** $Revision: 35 $ ** $Date: 10/09/07 12:46p $  */
/* Copyright (C) - 2003 - Fidelity National Information Solutions */
 
function GetMultipleReports(ReportNames)
{
 var str = "";
 var pnodes;
 xmlMultiReportData = new ActiveXObject(domIDStr); // All reports
 xmlReportData = new ActiveXObject(domIDStr); // Displayed report
 var tempXML = new ActiveXObject(domIDStr); // temp structure
 var node = tempXML.createNode(1, "REPORTS", "");
 root = xmlMultiReportData.selectSingleNode("./REPORTS");
 if (!root)
 {
 node = xmlMultiReportData.appendChild(node);
 root = xmlMultiReportData.selectSingleNode("./REPORTS");
 }
 sNames = parent.SearchWizards.selectNodes("//SEARCHWIZARD/@Name");
 rNames = ReportNames.split("|");
 var i = 0;
 pnodes = parent.SearchWizards.selectNodes("//SEARCHWIZARD[@Name='" + sNames[i].text + "']/PROMPT");
 str = reportBuildSearch(pnodes, sNames[i].text);
 str += _retFlds;
 if (hostSortNode)
 str += "&srtflds=" + hostSortNode.text; 
 rstateCount = 0;
 str = searchStr + str;
 tempXMLReportData.async = true;
 tranObj = _xaObj.createTransactionObject("Request report data", str, tempXMLReportData);
 tranObj.onreadystatechange = ReportsChangedAndDelay();
 tranObj.ondataavailable = ReportsChangedAndDelay();
 _xaObj.begin(tranObj);
 tempXML = null;
}
function BuildMultiReportDataForProspecting()
{
 xmlMultiReportData = new ActiveXObject(domIDStr); // All reports
 var tempXML = new ActiveXObject(domIDStr); // temp structure
 var rptsNode = tempXML.createNode(1, "REPORTS", "");
 var rptNode = tempXML.createNode(1, "REPORT", "");
 var prefNode = _siteObj.XMLDocument.selectSingleNode("//PREFERENCE[@Name='FIELDNAMES'&& @Resource='" + currentResource + "'&&@Level='MLS']");
 var multiReportDataRoot, reportNodeMulti, reportName;
 
 if (prefNode)
 {
 var LN_Node = prefNode.selectSingleNode("//FIELD[@Name='LISTNUMBER']");
 if (LN_Node)
 { 
 var namesNode = xmlReportData.selectSingleNode("//NAMES");
 if (namesNode)
 {
 listNoShortNameNode = namesNode.selectSingleNode("//N[@L='" + LN_Node.text + "']");
 if (listNoShortNameNode)
 var listNoName = listNoShortNameNode.getAttribute("S"); 
 }
 else
 var listNoName = LN_Node.text;
 }
 else
 {
 alert("Error: missing or incorrect setup of the FIELDNAMES Preference structure for the '" + currentResource + "' resource.");
 return false;
 }
 }
 else
 {
 alert("Error: missing or incorrect setup of the List Number Name Preference."); 
 return false;
 }
 multiReportDataRoot = xmlMultiReportData.selectSingleNode("./REPORTS");
 if (!multiReportDataRoot)
 {
 node = xmlMultiReportData.appendChild(rptsNode);
 multiReportDataRoot = xmlMultiReportData.selectSingleNode("./REPORTS");
 }
 for (i=0; i<rNames.length; i++)
 {
 reportNodeMulti = multiReportDataRoot.appendChild(rptNode.cloneNode(true));
 reportName = rNames[i];
 reportNodeMulti.setAttribute("Name", reportName);
 listNumbers = MRProspectSearchIndex[i].split(",");
 for(j=0; j<listNumbers.length; j++)
 {
 recNode = xmlReportData.selectSingleNode("//RECORD[" + listNoName + "='" + listNumbers[j] + "']");
 if (recNode) 
 reportNodeMulti.appendChild(recNode.cloneNode(true));
 }
 }
 if (namesNode)
 multiReportDataRoot.appendChild(namesNode.cloneNode(true)); 
}
function reportBuildSearch(pnodes, wizName)
{
 // BuildSearch is contained inside of XMLSEARCH.HML
 // It builds and returns the search criteria contianed in a the search wizard.
 // Added the parameter pnodes
 var str = "";
 var s = '';
 var sNode = null;
 var i;
 var bMultiReps = false;
 if (!wizName)
 {
 for (i=0; i < pnodes.length; i++)
 {
 sNode = pnodes[i].selectSingleNode('SEARCHCLAUSE');
 if (sNode)
 {
 s = sNode.text;
 if (s.length > 0)
 {
 str += escape(s) + '&';
 }
 }
 //str = str.replace(/\+/g,'%2B');
 }
 str = str.replace(/\+/g,'%2B');
 }
 if (parent.nodeWindowUserData){
 if (wizName && parent.SearchWizards)
 {
 searchClauseNode = parent.SearchWizards.selectSingleNode(".//SEARCHWIZARD[@Name='" + wizName + "']/SEARCHCLAUSE");
 bMultiReps = true;
 }
 else
 searchClauseNode = parent.nodeWindowUserData.selectSingleNode('.//SEARCHWIZARD/SEARCHCLAUSE');
 if (searchClauseNode)
 {
 str = searchClauseNode.text;
 try
 {
 if (xmlMultiReportData && xmlReportData.selectSingleNode('//@Name'))
 {
 if (!bMultiReps) // 2nd and up reports were getting the XMLRECNO from the first report, so basically if parent.SearchWizards exists, then use that.
 snode = getOneSWFromMany('/REPORTSELECTIONS', false);
 else
 snode = parent.SearchWizards.selectSingleNode('/SEARCHWIZARDS/SEARCHWIZARD[@Name="' + wizName + '"]/REPORTSELECTIONS');
 }
 else
 {
 if (parent.SearchWizards && wizName)
 {
 snode = parent.SearchWizards.selectSingleNode('/SEARCHWIZARDS/SEARCHWIZARD[@Name="' + wizName + '"]/REPORTSELECTIONS');
 } 
 else
 {
 snode = parent.xmlSearchWizardNode.selectSingleNode("//REPORTSELECTIONS");
 }
 }
 }
 catch(e)
 {
 snode = parent.xmlSearchWizardNode.selectSingleNode("//REPORTSELECTIONS");
 }
 if (snode)
 {
 // SELECTALLCHECKBOX TODO redo subselections in the report checkbox onclick
 // handler to address Select All checkbox implementation, need new logic to handle NOT XMLRECNO depending upon
 // the number of selections in ratio to total listings
 // If the user sub selects records, goes to the new report, deselects all
 // of the records in the new report and then chooses a new report,
 // we need to display the report with the original subselections
 // If they choose back to search or reload, then we clear the old selections
 var searchClause = snode.getAttribute("SEARCHCLAUSE");
 var oldSelections = snode.getAttribute("OldSelections");
 if (searchClause == "" && (oldSelections != "" && oldSelections != null))
 {
 snode.setAttribute("SEARCHCLAUSE", oldSelections);
 searchClause = oldSelections;
 }
 else
 snode.setAttribute("OldSelections", searchClause);
 
 str += escape(searchClause) + '&';
 }
 }
 else
 str = null;
 }
 lastQuery = str;
 return str;
}
function reportLaunchWindow(wName, component, qualifier, content, url,windowStyle, sFeatures)
{
 var wNode;
 var tNode;
 
 windowValues.load(hostCity + "WINDOWS");
 pObj = windowValues.XMLDocument;
 fNode = windowValues.XMLDocument.selectSingleNode("//WINDOWS");
 wNode = fNode.selectSingleNode("WINDOW[@ID='" + wName + "']");
 dt = new Date();
 if (!wNode)
 {
 wNode = pObj.createNode(1, "WINDOW", "");
 wNode.setAttribute("ID", wName);
 wNode.setAttribute("StartTime", (new Date()).getTime().toString());
 wNode.setAttribute("Component", component);
 wNode.setAttribute("Qualifier", qualifier);
 wNode.setAttribute("InvokedBy", window.name); // We invoked ourselves
 fNode.appendChild(wNode); // Add our window to the list of windows
 }
 wNode.setAttribute("LastUpdated", dt.getTime().toString());
 oldNode = wNode.selectSingleNode("//" + content.nodeName);
 if (oldNode)
 {
 pNode = oldNode.parentNode;
 pNode.removeChild(oldNode);
 }
 newNode = content.cloneNode(true);
 wNode.appendChild(newNode);
 windowValues.save(hostCity + "WINDOWS");
 if (sFeatures == null)
 sFeatures = "width=800,height=600,menubar=yes";
 if (windowStyle == "Modal")
 nw = window.showModalDialog(url , "", sFeatures);
 else 
 nw = window.open(url, wName, sFeatures);
 return nw;
}
function getPhotoCount(mask)
{
 var count = 0;
 if (mask<0)
 {
 count = 1;
 mask = parseInt(mask,"10") + 32768;
 }
 for (i=0; i<25; i++){ // was i<15 wjs 10/9/07
 if ((Math.pow(2,i) & mask) > 0)
 count += 1;
 }
 if (mask == 99999)
 count = 1; //for the 16th photo only
 return count;
}
function getOneSWFromMany(pXPath, Nodes)
{
 // pXPath is the child node of the searchwizard
 // Nodes is true is selectNodes, false for selectSingleNode
 // pXPath needs to have the correct syntax ie: "/REPORTSELECTIONS" not "REPORTSELECTIONS"
 var oneSWNode;
 var strPMRName = xmlReportData.selectSingleNode('//@Name');
 if (strPMRName)
 {
 strPMRName = unescape(strPMRName.text);
 if (!reportTypeNode)
 {
 reportTypeNode = parent.xmlReportWizardNode.selectSingleNode("//@Type");
 reportTypeNode = reportTypeNode ? reportTypeNode.text.toLowerCase() : "";
 }
 else if (typeof reportTypeNode == "object")
 reportTypeNode = reportTypeNode.text.toLowerCase();
 if (reportTypeNode == "revprospect")
 strPMRName = strPMRName.slice(strPMRName.indexOf('# ') + 2, strPMRName.lastIndexOf(' -'));
 else
 strPMRName = strPMRName.slice(strPMRName.indexOf(': ') + 2, strPMRName.lastIndexOf(' for'));
 // the Name attribute is unescaped in a few wizards, so we try escaped first and if we get null then we try without escaping.
 // if we get null after that then we legitamately got no xml, and we can safely return an expected null(ie first checkbox clicked).
 var fXPath = '/SEARCHWIZARDS/SEARCHWIZARD[@Name="' + escape(strPMRName) + '"]' + pXPath;
 if (Nodes)
 {
 if (parent.SearchWizards)
 {
 oneSWNode = parent.SearchWizards.selectNodes(fXPath);
 if (!oneSWNode)
 {
 var fXPath = '/SEARCHWIZARDS/SEARCHWIZARD[@Name="'+strPMRName+'"]' + pXPath;
 oneSWNode = parent.SearchWizards.selectNodes(fXPath);
 }
 }
 }
 else
 {
 if (parent.SearchWizards)
 oneSWNode = parent.SearchWizards.selectSingleNode(fXPath);
 else
 oneSWNode = null;
 if (!oneSWNode && typeof(parent.SearchWizards) != "undefined")
 {
 var fXPath = '/SEARCHWIZARDS/SEARCHWIZARD[@Name="' + strPMRName + '"]' + pXPath;
 oneSWNode = parent.SearchWizards.selectSingleNode(fXPath);
 }
 }
 return oneSWNode;
 }
 else
 return null;
}

