SetupNamespaces("EZLandlordForms");

EZLandlordForms.DocumentBuilding = {
    Wizard: function(_userID, _documentID) { 
        var private = {
            userID: _userID, 
            documentID: _documentID, 
            curtainElem: null, 
            dialogElem: null, 
            stepNumberText: null, 
            contentArea: null, 
            positionInterval: null, 
            loadingImage: new Image(), 
            documentsCompleted: false, 
            
            DisplayCurtainAndDialog: function() { 
                var dropbox = Get("ElementDropBox");
                var pageHeight = ((document.height) ? document.height : document.body.offsetHeight) + 20;
                var pageWidth = ((document.width) ? document.width : document.body.offsetWidth);
                
                if (document.documentElement.clientHeight > pageHeight) 
                    pageHeight = document.documentElement.clientHeight;
                
                private.curtainElem = Elem("div");
                private.curtainElem.style.display = "block";
                private.curtainElem.style.position = "absolute";
                private.curtainElem.style.top = "0px";
                private.curtainElem.style.left = "0px";
                private.curtainElem.style.backgroundColor = "#000000";
	            private.curtainElem.style.opacity = 0.8;
	            private.curtainElem.style.filter  = "alpha(opacity=80)";
                private.curtainElem.style.height = Math.ceil(pageHeight) + "px";
                private.curtainElem.style.width = Math.ceil(pageWidth) + "px";
                private.curtainElem.style.margin = "0px auto";
                private.curtainElem.style.zIndex = 175;
	            private.curtainElem.appendChild(document.createTextNode(" "));
	            
	            private.dialogElem = Elem("div");
	            private.dialogElem.className = "Dialog";
	            private.dialogElem.style.width = "740px";
	            private.dialogElem.style.height = "540px";
	            private.dialogElem.style.zIndex = 176;
	            private.dialogElem.style.overflow = "hidden";
	            
	            var dialogHeader = Elem("h1");
	            private.stepNumberText = Elem("span");
	            var closeButton = Elem("a");
	            var closeButtonImg = Elem("img");
	            
	            closeButton.href = "#";
	            closeButton.style.display = "block";
	            closeButton.style.margin = "0 -4px 0 0";
	            closeButton.style.position = "relative";
                if (typeof(closeButton.style.cssFloat) != "undefined") closeButton.style.cssFloat = "right";
                else closeButton.style.styleFloat = "right";
                closeButton.onclick = function() { 
                        if (private.documentsCompleted || confirm("Are you sure you want to exit this wizard? Your document will not be created.")) 
                            private.HideCurtainAndDialog(); 
                            
                        return false;
                    };
                    
	            closeButtonImg.src = "/images/layout2009/curtain_close.gif";
	            closeButtonImg.width = 26;
	            closeButtonImg.height = 26;
	            closeButtonImg.alt = "Close";
                closeButton.appendChild(closeButtonImg);
                dialogHeader.appendChild(closeButton);
	            
	            dialogHeader.style.lineHeight = "29px";
	            dialogHeader.style.margin = "-10px -20px 10px -20px";
	            dialogHeader.style.padding = "5px 10px";
	            dialogHeader.style.background = "transparent url(/images/layout2009/forms/builder-wizard/bg_bar_39px_grey.gif) repeat-x scroll center top";
	            dialogHeader.style.borderBottom = "1px solid #E5E5E5";
	            dialogHeader.appendChild(Text("Document Builder Wizard"));
	            
	            private.stepNumberText.style.marginLeft = "10px";
	            private.stepNumberText.style.fontFamily = "Arial, Helvetica, Sans-Serif";
	            private.stepNumberText.style.fontWeight = "normal";
	            private.stepNumberText.style.color = "#999999";
	            private.stepNumberText.style.fontSize = "11px";
	            private.stepNumberText.style.lineHeight = "14px";
	            dialogHeader.appendChild(private.stepNumberText);
	            
	            private.dialogElem.appendChild(dialogHeader);
	            
	            private.contentArea = Elem("div");
	            private.contentArea.style.position = "relative";
	            private.dialogElem.appendChild(private.contentArea);
	            
	            private.PositionDialog();
	            private.positionInterval = setInterval(function() { private.PositionDialog(); }, 500);
	            
	            dropbox.appendChild(private.curtainElem);
	            dropbox.appendChild(private.dialogElem);
                
                dropbox = null;
            }, 
            
            HideCurtainAndDialog: function() { 
                clearInterval(private.positionInterval);
            
                var dropbox = Get("ElementDropBox");
                dropbox.removeChild(private.curtainElem);
                dropbox.removeChild(private.dialogElem);
                dropbox = null;
            }, 
            
            PositionDialog: function() { 
	            private.dialogElem.style.top = Pliner.Util.Display.FindScreenCenteredY(600) + "px";
	            private.dialogElem.style.left = Pliner.Util.Display.FindScreenCenteredX(800) + "px";
            }, 
            
            ShowLoadingImage: function(msg) { 
                ClearElem(private.contentArea);
                
                if (!msg) msg = "Loading";
                
                var loadingMessage = Elem("div");
                loadingMessage.style.position = "absolute";
                loadingMessage.style.zIndex = 180;
                loadingMessage.style.top = "200px";
                loadingMessage.style.left = "310px";
                loadingMessage.style.padding = "0 0 0 40px";
                loadingMessage.style.fontSize = "18px";
                loadingMessage.style.lineHeight = "32px";
                loadingMessage.style.color = "#171553";
                loadingMessage.style.background = "transparent url(" + private.loadingImage.src + ") no-repeat scroll center left";
                loadingMessage.appendChild(Text(msg + "&hellip;", true));
                
                private.contentArea.appendChild(loadingMessage);
            }, 
            
            ShowErrorMessage: function() { 
                ClearElem(private.contentArea);
                
                var errorMessage = Elem("div");
                errorMessage.style.margin = "200px auto 0 auto";
                errorMessage.style.fontSize = "18px";
                errorMessage.style.lineHeight = "32px";
                errorMessage.style.color = "#B81818";
                errorMessage.style.textAlign = "center";
                errorMessage.appendChild(Text("Error Contacting The Server"));
                
                var errorLink = Elem("a");
                errorLink.style.display = "block";
                errorLink.style.margin = "10px auto 0 auto";
                errorLink.style.fontSize = "12px";
                errorLink.style.textAlign = "center";
                errorLink.href = "#";
                errorLink.onclick = function() { private.HideCurtainAndDialog(); public.StartWizard(); return false; };
                errorLink.appendChild(Text("Try Again"));
                
                private.contentArea.appendChild(errorMessage);
                private.contentArea.appendChild(errorLink);
            }, 
            
            AnimateSlideIn: function(content, currPos, endPos, percent) { 
                currPos = currPos - ((currPos - endPos) * percent);
                
                if (currPos < endPos - 1) {
                    content.style.marginTop = currPos + "px";
                    setTimeout(function() { private.AnimateSlideIn(content, currPos, endPos, percent + 0.025) }, 100);
                }
                else {
                    content.style.marginTop = endPos + "px";
                }
            }, 
            
            PropertyTenantSelectionStepLoaded: function(data) { 
                ClearElem(private.contentArea);
                
                var checkboxes = [];
                var dataLines = data.split("\n");
                var documentName = dataLines[0];
                var propertySelectOnly = dataLines[1].toLowerCase() == "true";
                var instructions = dataLines[2];
                
                var stepContainer = Elem("div");
                stepContainer.style.height = "500px";
                stepContainer.style.overflow = "hidden";
                
                var stepContent = Elem("div");
                stepContent.style.height = "500px";
                stepContent.style.marginTop = "-500px";
                stepContent.style.visibility = "hidden";
                
                // add the header and instructions
                var documentNameHeader = Elem("h2");
                documentNameHeader.appendChild(Text(documentName));
                
                stepContent.appendChild(documentNameHeader);
                stepContent.innerHTML += instructions;
                
                var selectionList = Elem("div");
                
                // add the list of properties and tenants
                for (var d=3; d < dataLines.length; d++) {
                    if (!dataLines[d] || dataLines[d].length <= 0)
                        continue;
                
                    var documentFields = dataLines[d].split("[split]");
                    var leaseID = documentFields[0];
                    var propertyID = documentFields[1];
                    var propertyName = documentFields[2];
                    var tenantsNames = documentFields[3];
                    
                    var selectionItem = Elem("div");
                    selectionItem.style.background = "transparent url(/images/layout2009/forms/builder-wizard/bg_bar_39px_grey.gif) repeat-x scroll center bottom";
                    selectionItem.style.border = "1px solid #E5E5E5";
                    if (d != 3) selectionItem.style.borderTopWidth = 0;
                    selectionItem.style.padding = "6px 10px";
                    selectionItem.onclick = function() { 
                            var checkedItems = 0;
                            for (var c=0; c < checkboxes.length; c++)
                                checkedItems += checkboxes[c].checked ? 1 : 0;
                            continueButton.style.background = checkedItems > 0 ? 
                                "#2353B5 url(/images/layout2009/btn_blue_bg.jpg) repeat-x scroll center top" : 
                                "#CCCCCC none repeat-x scroll center top";
                            continueValidatorInstr.style.display = checkedItems > 0 ? "none" : "inline";
                        };
                    
                    var checkbox = Elem("input");
                    checkbox.id = "InputSelectItem" + (propertySelectOnly ? propertyID : leaseID);
                    checkbox.type = "checkbox";
                    checkbox.style.verticalAlign = "middle";
                    checkbox.style.marginRight = "4px";
                    checkbox.value = propertySelectOnly ? propertyID : leaseID;
                    
                    var label = Elem("label");
                    label.htmlFor = "InputSelectItem" + (propertySelectOnly ? propertyID : leaseID);
                    label.appendChild(Text(propertyName.length > 0 ? propertyName : "(no property selected)"));
                    if (!propertySelectOnly)
                        label.appendChild(Text(tenantsNames.length > 0 ? " with tenants " + tenantsNames : " with (no tenants selected)"));
                    
                    checkboxes.push(checkbox);
                    selectionItem.appendChild(checkbox);
                    selectionItem.appendChild(label);
                    
                    selectionList.appendChild(selectionItem);
                }
                
                // create the 'add another property/tenant' links that allow you to create a property/lease in a popup and refresh this 
                // list when the popup completes the addition of the new item
                var addAnotherItem = Elem("div");
                addAnotherItem.style.background = "transparent url(/images/layout2009/forms/builder-wizard/bg_bar_39px_grey.gif) repeat-x scroll center bottom";
                addAnotherItem.style.border = "1px solid #E5E5E5";
                addAnotherItem.style.borderTopWidth = 0;
                addAnotherItem.style.padding = "6px 10px";
                
                var addAnotherLink = Elem("a");
                addAnotherLink.href = "#";
                addAnotherLink.onclick = function() { 
                        if (propertySelectOnly) {
                            window.open("/members/manageproperties.aspx?propertyID=NEW&pop=1", "CreateProperty", "width=740,height=480,resizable=no,scrollbars=yes,menubar=no,status=no");
                            RefreshToPropertyStep = function() { public.LoadPropertyTenantSelectionStep(); };
                        }
                        else {
                            window.open("/wizards/leases/basic.aspx?isNew=true&leaseID=NEW&createdoc=" + private.documentID + "&pop=1", "CreateBasicLease", "width=740,height=480,resizable=no,scrollbars=yes,menubar=no,status=no");
                            LeasesListUpdated = function() { public.LoadPropertyTenantSelectionStep(); };
                        }
                        
                        return false; 
                    };
                addAnotherLink.appendChild(Text("Click here to add a " + (propertySelectOnly ? "property" : "lease")));
                addAnotherItem.appendChild(addAnotherLink);
                
                // if there are results, display them
                if (checkboxes.length > 0) { 
                    var listHeader = Elem("div");
                    listHeader.style.border = "1px solid #D5D7E3";
                    listHeader.style.backgroundColor = "#F4F7FF";
                    listHeader.style.color = "#3366CC";
                    listHeader.style.fontSize = "14px";
                    listHeader.style.fontWeight = "bold";
                    listHeader.style.padding = "8px 10px";
                    listHeader.style.margin = "10px 0 1px 0";
                    
                    var selectAllNone = Elem("div");
                    selectAllNone.style.fontSize = "11px";
                    selectAllNone.style.position = "relative";
                    if (typeof(selectAllNone.style.cssFloat) != "undefined") selectAllNone.style.cssFloat = "right";
                    else selectAllNone.style.styleFloat = "right";
                    var selectAllLink = Elem("a");
                    selectAllLink.href = "#";
                    selectAllLink.onclick = function() { 
                            for (var c=0; c < checkboxes.length; c++) checkboxes[c].checked = true;
                            return false;
                        };
                    selectAllLink.appendChild(Text("Select All"));
                    var selectNoneLink = Elem("a");
                    selectNoneLink.href = "#";
                    selectNoneLink.onclick = function() { 
                            for (var c=0; c < checkboxes.length; c++) checkboxes[c].checked = false;
                            return false;
                        };
                    selectNoneLink.appendChild(Text("Select None"));
                    selectAllNone.appendChild(selectAllLink);
                    selectAllNone.appendChild(Text(" | "));
                    selectAllNone.appendChild(selectNoneLink);
                    
                    listHeader.appendChild(selectAllNone);
                    listHeader.appendChild(Text("Customize This Document For:"));
                    stepContent.appendChild(listHeader);
                    
                    selectionList.appendChild(addAnotherItem);
                    
                    stepContent.appendChild(selectionList);
                    
                    // add the 'continue' button
                    var continueButtonRow = Elem("div");
                    continueButtonRow.style.margin = "10px 0 0 0";
                    continueButtonRow.style.textAlign = "right";
                    var continueButton = Elem("input");
                    continueButton.className = "Button";
                    continueButton.type = "button";
                    continueButton.value = "Continue";
                    if (checkboxes.length != 1) continueButton.style.background = "#CCCCCC none repeat-x scroll center top";
                    continueButton.onclick = function() { 
                            var leaseOrPropertyIDs = [];
                            for (var c=0; c < checkboxes.length; c++)
                                if (checkboxes[c].checked) 
                                    leaseOrPropertyIDs.push(parseInt(checkboxes[c].value));
                            
                            if (leaseOrPropertyIDs.length <= 0)
                                alert("You did not select any " + (propertySelectOnly ? "properties" : "leases") + 
                                    ".  You must select at least one " + (propertySelectOnly ? "property" : "lease") + 
                                    " to create a document for.");
                            else
                                public.LoadDocumentSetupStep(documentName, propertySelectOnly, leaseOrPropertyIDs);
                            return false;
                        };
                    var continueValidatorInstr = Elem("span");
                    continueValidatorInstr.style.color = "#999999";
                    continueValidatorInstr.style.paddingRight = "10px";
                    continueValidatorInstr.appendChild(Text("Please select a " + (propertySelectOnly ? "property" : "lease") + " to continue."));
                    continueValidatorInstr.style.display = checkboxes.length != 1 ? "inline" : "none";
                    continueButtonRow.appendChild(continueValidatorInstr);
                    continueButtonRow.appendChild(continueButton);
                    stepContent.appendChild(continueButtonRow);
                }
                
                // if there are no results, display a message indicating that there are no results
                else {
                    var nothingMsg = Elem("div");
                    nothingMsg.style.fontWeight = "bold";
                    nothingMsg.style.color = "#B81818";
                    nothingMsg.style.margin = "10px 0";
                    nothingMsg.style.fontSize = "14px";
                    nothingMsg.appendChild(Text("You do not have any " + (propertySelectOnly ? "properties" : "leases") + " on your account. "));
                    nothingMsg.appendChild(addAnotherLink);
                    stepContent.appendChild(nothingMsg);
                }
                
                // add the content to the dialog, and setup scrolling if needed
                stepContainer.appendChild(stepContent);
                private.contentArea.appendChild(stepContainer);
                
                ClearElem(private.stepNumberText);
                private.stepNumberText.appendChild(Text("Step 1 of 3 - Select Rental Units"));
                
                setTimeout(function() {
                        if (checkboxes.length == 1) // only one lease? auto-select it
                            checkboxes[0].checked = true;
                        
                        if (selectionList.offsetHeight > 340) { 
                            selectionList.style.position = "relative";
                            selectionList.style.height = "340px";
                            selectionList.style.overflow = "auto";
                        }
                        
                        stepContent.style.visibility = "visible";
                        setTimeout(function() { private.AnimateSlideIn(stepContent, -500, 0, 0.4); }, 100);
                    }, 1);
            }, 
            
            DocumentSetupStepLoaded: function(documentName, propertySelectOnly, data) { 
                ClearElem(private.contentArea);
                
                var dataLines = data.split("\n");
                var instructions = dataLines[0];
                var fieldsData = dataLines[1].split("[break]");
                var autoFillData = dataLines[2].split("[break]");
                var autoFillEditors = [];
                var customInputFields = [];
                
                var stepContainer = Elem("div");
                stepContainer.style.height = "500px";
                stepContainer.style.overflow = "hidden";
                
                var stepContent = Elem("div");
                stepContent.style.height = "500px";
                stepContent.style.marginTop = "-500px";
                stepContent.style.visibility = "hidden";
                
                // display the document name and the instructions for this step
                var documentNameHeader = Elem("h2");
                documentNameHeader.appendChild(Text(documentName));
                
                stepContent.appendChild(documentNameHeader);
                stepContent.innerHTML += instructions;
                
                // display the documents list header
                var listHeader = Elem("div");
                listHeader.style.border = "1px solid #D5D7E3";
                listHeader.style.backgroundColor = "#F4F7FF";
                listHeader.style.color = "#3366CC";
                listHeader.style.fontSize = "14px";
                listHeader.style.fontWeight = "bold";
                listHeader.style.padding = "8px 10px";
                listHeader.style.margin = "10px 0 1px 0";
                listHeader.appendChild(Text("Your Requested Documents:"));
                stepContent.appendChild(listHeader);
                
                var documentsList = Elem("div");
                
                // display each document with it's auto-fill information and custom fields for the user to configure
                for (var a=0; a < autoFillData.length; a++) {
                    if (!autoFillData[a] || autoFillData[a].length <= 0)
                        continue;
                        
                    var autoFillFields = autoFillData[a].split("[split]");
                    var leaseID = autoFillFields[0];
                    var propertyID = autoFillFields[1];
                    var builtID = autoFillFields[2];
                    
                    var autoFillEditor = new AutoFillEditor("DivAutoFillSender_" + builtID, "DivAutoFillRecipient_" + builtID, 
                        private.userID, builtID, leaseID, propertyID);
                    autoFillEditor.SenderName = autoFillFields[3];
                    autoFillEditor.SenderCompany = autoFillFields[4];
                    autoFillEditor.SenderAddressLine1 = autoFillFields[5];
                    autoFillEditor.SenderAddressLine2 = autoFillFields[6];
                    autoFillEditor.SenderCity = autoFillFields[7];
                    autoFillEditor.SenderState = autoFillFields[8];
                    autoFillEditor.SenderZipCode = autoFillFields[9];
                    autoFillEditor.SenderPhoneNumber = autoFillFields[10];
                    autoFillEditor.SenderFaxNumber = autoFillFields[11];
                    autoFillEditor.SenderEmail = autoFillFields[12];
                    autoFillEditor.RecipientName = autoFillFields[13];
                    autoFillEditor.RecipientAddressLine1 = autoFillFields[14];
                    autoFillEditor.RecipientAddressLine2 = autoFillFields[15];
                    autoFillEditor.RecipientCity = autoFillFields[16];
                    autoFillEditor.RecipientState = autoFillFields[17];
                    autoFillEditor.RecipientZipCode = autoFillFields[18];
                    
                    var selectionItem = Elem("div");
                    selectionItem.style.background = "transparent url(/images/layout2009/forms/builder-wizard/bg_bar_39px_grey.gif) repeat-x scroll center bottom";
                    selectionItem.style.border = "1px solid #E5E5E5";
                    if (a != 0) selectionItem.style.borderTopWidth = 0;
                    selectionItem.style.padding = "6px 10px";
                    
                    var documentTitle = Elem("div");
                    documentTitle.style.fontWeight = "bold";
                    documentTitle.style.fontSize = "12px";
                    var documentTitleIcon = Elem("img");
                    documentTitleIcon.src = "/images/create_icon.gif";
                    documentTitleIcon.width = 16;
                    documentTitleIcon.height = 16;
                    documentTitleIcon.alt = "Auto-Fill Document";
                    documentTitleIcon.align = "absmiddle";
                    var documentTitleFor = Elem("span");
                    documentTitleFor.style.fontWeight = "normal";
                    documentTitleFor.appendChild(Text(" for:"));
                    documentTitle.appendChild(documentTitleIcon);
                    documentTitle.appendChild(Text(" " + documentName));
                    documentTitle.appendChild(documentTitleFor);
                    selectionItem.appendChild(documentTitle);
                    
                    var col1 = Elem("div");
                    var col2 = Elem("div");
                    var col3 = Elem("div");
                    
                    var setupCol = function(col) { // so we can use this for multiple cols
                            col.style.marginLeft = "10px";
                            col.style.position = "relative";
                            if (typeof(col.style.cssFloat) != "undefined") col.style.cssFloat = "right";
                            else col.style.styleFloat = "right";
                        }
                        
                    setupCol(col2);
                    setupCol(col3);
                    col1.style.width = "175px";
                    col2.style.width = "175px";
                    col3.style.width = "320px";
                    
                    var autoFillSenderHeader = Elem("div");
                    autoFillSenderHeader.style.fontWeight = "bold";
                    autoFillSenderHeader.style.color = "#3366CC";
                    autoFillSenderHeader.style.margin = "10px 0 0 0";
                    autoFillSenderHeader.appendChild(Text("Your Address:"));
                    var autoFillSender = Elem("div");
                    autoFillSender.id = "DivAutoFillSender_" + builtID;
                    var autoFillRecipientHeader = Elem("div");
                    autoFillRecipientHeader.style.fontWeight = "bold";
                    autoFillRecipientHeader.style.color = "#3366CC";
                    autoFillRecipientHeader.style.margin = "10px 0 0 0";
                    autoFillRecipientHeader.appendChild(Text("Recipient's Address:"));
                    var autoFillRecipient = Elem("div");
                    autoFillRecipient.id = "DivAutoFillRecipient_" + builtID;
                    
                    var customFields = Elem("div");
                    var customFieldsHeader = Elem("div");
                    customFieldsHeader.style.fontWeight = "bold";
                    customFieldsHeader.style.color = "#3366CC";
                    customFieldsHeader.style.margin = "10px 0 0 0";
                    customFieldsHeader.appendChild(Text("Additional Information:"));
                    customFields.appendChild(customFieldsHeader);
                    var numCustomFields = 0;
                    for (var f=0; f < fieldsData.length; f++) { 
                        if (!fieldsData[f] || fieldsData[f].length <= 0)
                            continue;
                        
                        var fieldName = fieldsData[f].split("[split]")[0];
                        var fieldPrompt = fieldsData[f].split("[split]")[1];
                        
                        var fieldContainer = Elem("div");
                        fieldContainer.style.textAlign = "right";
                        fieldContainer.style.clear = "both";
                        var fieldInput = Elem("input");
                        fieldInput.id = "InputCustomPrompt_" + builtID + "_" + fieldName;
                        fieldInput.type = "text";
                        fieldInput.style.width = "165px";
                        fieldInput.value = "(please specify)";
                        fieldInput.style.color = "#969696";
                        fieldInput.onfocus = function(input) { return function() { ClearDefaultText(input, "(please specify)"); }; }(fieldInput);
                        fieldInput.onblur = function(input) { return function() { SetupDefaultText(input, "(please specify)"); }; }(fieldInput);
                        var fieldLabel = Elem("div");
                        fieldLabel.style.width = "145px";
                        fieldLabel.style.textAlign = "left";
                        if (typeof(fieldLabel.style.cssFloat) != "undefined") fieldLabel.style.cssFloat = "left";
                        else fieldLabel.style.styleFloat = "left";
                        fieldLabel.innerHTML += fieldPrompt + ":&nbsp;";
                        fieldContainer.appendChild(fieldLabel);
                        fieldContainer.appendChild(fieldInput);
                        customFields.appendChild(fieldContainer);
                        
                        numCustomFields++;
                        customInputFields.push(fieldInput);
                        
                        if (a == 0 && f == 0) // first field on page? auto-focus it
                            setTimeout(function(fi) { return function() { try { fi.focus(); } catch(e) {  } }; }(fieldInput), 750);
                    }
                    
                    if (numCustomFields > 0) 
                        col3.appendChild(customFields);
                    else 
                        col3.appendChild(Elem("br"));
                    
                    col1.appendChild(autoFillSenderHeader);
                    col1.appendChild(autoFillSender);
                    col2.appendChild(autoFillRecipientHeader);
                    col2.appendChild(autoFillRecipient);
                    
                    selectionItem.appendChild(col3);
                    selectionItem.appendChild(col2);
                    selectionItem.appendChild(col1);
                    
                    documentsList.appendChild(selectionItem);
                    autoFillEditors.push(autoFillEditor);
                }
                
                stepContent.appendChild(documentsList);
                
                // display the document date creation input
                var currentDate = new Date();
                var creationDateContainer = Elem("div");
                creationDateContainer.style.border = "1px solid #D5D7E3";
                creationDateContainer.style.backgroundColor = "#F4F7FF";
                creationDateContainer.style.padding = "8px 10px";
                creationDateContainer.style.margin = "1px 0 10px 0";
                var creationDateLabel = Elem("span");
                creationDateLabel.style.color = "#3366CC";
                creationDateLabel.style.fontWeight = "bold";
                creationDateLabel.appendChild(Text("Document Date:&nbsp; ", true));
                creationDateContainer.appendChild(creationDateLabel);
                creationDateContainer.appendChild(Text("This date will appear on the document(s), indicating the date the document was signed and mailed. "));
                var creationDateInputs = Elem("div");
                var creationDateInput = Elem("input");
                creationDateInput.id = "InputCreationDate";
                creationDateInput.value = (currentDate.getMonth() + 1) + "/" + currentDate.getDate() + "/" + currentDate.getFullYear();
                creationDateInput.style.width = "125px";
                creationDateInputs.style.marginLeft = "95px";
                creationDateInputs.appendChild(Text("When will this document be mailed? "));
                creationDateInputs.appendChild(creationDateInput);
                creationDateContainer.appendChild(creationDateInputs);
                stepContent.appendChild(creationDateContainer);
                
                // display the continue and back buttons. The continue button validates this step.
                var continueButtonRow = Elem("div");
                continueButtonRow.style.margin = "10px 0 0 0";
                continueButtonRow.style.textAlign = "right";
                var goBackLink = Elem("a");
                goBackLink.style.display = "block";
                goBackLink.style.position = "relative";
                if (typeof(goBackLink.style.cssFloat) != "undefined") goBackLink.style.cssFloat = "left";
                else goBackLink.style.styleFloat = "left";
                goBackLink.href = "#";
                goBackLink.onclick = function() { public.LoadPropertyTenantSelectionStep(); return false; };
                goBackLink.appendChild(Text("&laquo; Go Back", true));
                var continueButton = Elem("input");
                continueButton.className = "Button";
                continueButton.type = "button";
                continueButton.value = "Create Documents";
                continueButton.onclick = function() {
                        var customFieldBlank = false;
                        for (var c=0; c < customInputFields.length; c++) { 
                            if (customInputFields[c].value == "" || customInputFields[c].value == "(please specify)") { 
                                customFieldBlank = true;
                                break;
                            }
                        }
                        
                        if (customFieldBlank && !confirm("You have left one or more important fields blank on this page.  " + 
                            "\n\nIf you do not fill in the informational fields listed with each " + ((propertySelectOnly) ? "property" : "lease") + 
                            ", the created document may be incomplete.  \n\nClick 'Cancel' to continue working on this page, " + 
                            "or click 'OK' to create the documents."))
                            return false;
                        
                        private.SubmitDocumentsToBuilder(documentName, propertySelectOnly, fieldsData, autoFillEditors);
                        return false;
                    };
                continueButtonRow.appendChild(goBackLink);
                continueButtonRow.appendChild(continueButton);
                stepContent.appendChild(continueButtonRow);
                
                stepContainer.appendChild(stepContent);
                private.contentArea.appendChild(stepContainer);
                
                // show this step, and make the documents list scrollable if it is too big
                ClearElem(private.stepNumberText);
                private.stepNumberText.appendChild(Text("Step 2 of 3 - Customize Your Documents"));
                
                setTimeout(function() {
                        for (var a=0; a < autoFillEditors.length; a++) { 
                            autoFillEditors[a].DisplaySender();
                            autoFillEditors[a].DisplayRecipient();
                        }
                
                        if (documentsList.offsetHeight > 300) { 
                            documentsList.style.position = "relative";
                            documentsList.style.height = "300px";
                            documentsList.style.overflow = "auto";
                        }
                        stepContent.style.visibility = "visible";
                        setTimeout(function() { private.AnimateSlideIn(stepContent, -500, 0, 0.4); }, 100);
                    }, 1);
            }, 
            
            SubmitDocumentsToBuilder: function(documentName, propertySelectOnly, fieldsData, autoFillEditors) {
                var data = "";
                
                for (var a=0; a < autoFillEditors.length; a++) { 
                    var customFieldsString = "";
                
                    data += autoFillEditors[a].BuiltID + "[split]";
                    data += autoFillEditors[a].LeaseID + "[split]";
                    data += autoFillEditors[a].PropertyID + "[split]";
                    
                    for (var f=0; f < fieldsData.length; f++) { 
                        if (!fieldsData[f] || fieldsData[f].length <= 0)
                            continue;
                        
                        var fieldName = fieldsData[f].split("[split]")[0];
                        var fieldValue = Get("InputCustomPrompt_" + autoFillEditors[a].BuiltID + "_" + fieldName).value;
                        
                        if (fieldValue == "(please specify)") fieldValue = "";
                        
                        customFieldsString += "[[" + fieldName + "]]" + fieldValue;
                    }
                    
                    data += customFieldsString + "[break]";
                }
                
                var msg = new Pliner.Util.Ajax.Message("/wizards/documents/ajax/submitdocstobuild.aspx", 0);
                msg.AddPostDataKey("document", private.documentID);
                msg.AddPostDataKey("creationdate", Get("InputCreationDate").value);
                msg.AddPostDataKey("data", data);
                msg.AddOnCompletedEvent(function(msg) { private.ShowLoadingImage("Creating"); private.CheckDocumentProgress(documentName, propertySelectOnly, autoFillEditors); });
                msg.AddOnErrorEvent(function(msg) { private.ShowErrorMessage(); });
                
                private.ShowLoadingImage("Submitting");
                ajaxConnection.AddToQueue(msg);
            }, 
            
            CheckDocumentProgress: function(documentName, propertySelectOnly, autoFillEditors) { 
                var msg = new Pliner.Util.Ajax.Message("/documentprogressbar.aspx", 0);
                msg.AddOnCompletedEvent(function(msg) { private.DocumentProgressChecked(documentName, propertySelectOnly, autoFillEditors, msg.responseText); });
                msg.AddOnErrorEvent(function(msg) { private.CheckDocumentProgress(documentName, propertySelectOnly, autoFillEditors); });
                ajaxConnection.AddToQueue(msg);
            }, 
            
            DocumentProgressChecked: function(documentName, propertySelectOnly, autoFillEditors, progressData) { 
                progressDataLines = progressData.split("\n");
                
                if (progressDataLines.length <= 1 || !progressDataLines[1] || progressDataLines[1].length <= 0)
                    private.LoadDownloadLinks(documentName, autoFillEditors);
                else
                    setTimeout(function() { private.CheckDocumentProgress(documentName, propertySelectOnly, autoFillEditors); }, 1000);
            }, 
            
            LoadDownloadLinks: function(documentName, autoFillEditors) { 
                private.ShowLoadingImage("Locating");
                
                var msg = new Pliner.Util.Ajax.Message("/wizards/documents/ajax/loadprintinginfo.aspx", 0);
                
                msg.AddPostDataKey("documentName", documentName);
                msg.AddPostDataKey("documentsCount", autoFillEditors.length);
                for (var a=0; a < autoFillEditors.length; a++)
                    msg.AddPostDataKey("builtDocument" + a, autoFillEditors[a].BuiltID);
                    
                msg.AddOnCompletedEvent(function(msg) { private.DownloadLinksLoaded(documentName, msg.responseText); });
                msg.AddOnErrorEvent(function(msg) { private.ShowErrorMessage(); });
                ajaxConnection.AddToQueue(msg);
            }, 
            
            DownloadLinksLoaded: function(documentName, data) { 
                private.documentsCompleted = true;
                ClearElem(private.contentArea);
                
                var stepContainer = Elem("div");
                stepContainer.style.height = "500px";
                stepContainer.style.overflow = "hidden";
                
                var stepContent = Elem("div");
                stepContent.style.height = "500px";
                stepContent.style.marginTop = "-500px";
                stepContent.style.visibility = "hidden";
                
                var documentNameHeader = Elem("h2");
                documentNameHeader.appendChild(Text(documentName));
                stepContent.appendChild(documentNameHeader);
                
                var instructions = Elem("div");
                instructions.style.marginBottom = "10px";
                instructions.appendChild(Text("Your documents are ready.  You can download them below."));
                stepContent.appendChild(instructions);
                
                var printInfo = Elem("div");
                printInfo.style.position = "relative";
                printInfo.style.height = "440px";
                printInfo.style.overflow = "auto";
                printInfo.innerHTML = data;
                stepContent.appendChild(printInfo);
                
                stepContainer.appendChild(stepContent);
                private.contentArea.appendChild(stepContainer);
                
                // show this step, and make the print list scrollable if it is too long
                ClearElem(private.stepNumberText);
                private.stepNumberText.appendChild(Text("Step 3 of 3 - Download Your Documents"));
                
                setTimeout(function() {
                        stepContent.style.visibility = "visible";
                        setTimeout(function() { private.AnimateSlideIn(stepContent, -500, 0, 0.4); }, 100);
                    }, 1);
            }
        };
        
        var public = {
            StartWizard: function() { 
                private.DisplayCurtainAndDialog();
                public.LoadPropertyTenantSelectionStep();
            }, 
            
            LoadPropertyTenantSelectionStep: function() { 
                private.ShowLoadingImage();
                
                var msg = new Pliner.Util.Ajax.Message("/wizards/documents/ajax/loadpropertiestenants.aspx", 0);
                msg.AddPostDataKey("document", private.documentID);
                msg.AddOnCompletedEvent(function(msg) { private.PropertyTenantSelectionStepLoaded(msg.responseText); });
                msg.AddOnErrorEvent(function(msg) { private.ShowErrorMessage(); });
                ajaxConnection.AddToQueue(msg);
            }, 
            
            LoadDocumentSetupStep: function(documentName, propertySelectOnly, leaseOrPropertyIDs) { 
                private.ShowLoadingImage();
                
                var leaseOrPropertyIDList = "";
                for (var id=0; id < leaseOrPropertyIDs.length; id++) {
                    if (leaseOrPropertyIDList.length > 0)
                        leaseOrPropertyIDList += ",";
                    leaseOrPropertyIDList += leaseOrPropertyIDs[id];
                }
                
                var msg = new Pliner.Util.Ajax.Message("/wizards/documents/ajax/loaddoccustomization.aspx", 0);
                msg.AddPostDataKey("document", private.documentID);
                if (propertySelectOnly)
                    msg.AddPostDataKey("properties", leaseOrPropertyIDList);
                else
                    msg.AddPostDataKey("leases", leaseOrPropertyIDList);
                msg.AddOnCompletedEvent(function(msg) { private.DocumentSetupStepLoaded(documentName, propertySelectOnly, msg.responseText); });
                msg.AddOnErrorEvent(function(msg) { private.ShowErrorMessage(); });
                ajaxConnection.AddToQueue(msg);
            }
        };
        
        private.loadingImage.src = "/images/ajax-loader.gif";
        
        return public;
    }
};

// used for global callbacks for popups that trigger updates
var LeasesListUpdated, RefreshToPropertyStep;