﻿// This script is included in the master page of SearchResults and ExpandedResults pages for Printing the results.

var addressInputURL = '../Shared/CoverLetterAddressUI.aspx';
var createPdfURL = '../Shared/CreatePDF.aspx?Page=';
var pageName = '';

function addressEntry() {
    // This funtion is invoked when 'Cover Letter' option is selected after clickimg Print button in Serach Results page. 
    // This brings up a modal window showing fields for entering Address information, which is used to create a cover letter in the generated PDF.
    var retArray = new Array(5);
    //The following two variables are for constructing the query string with the Address fields
    var addrInfo = ['Name', 'Address1', 'Address2', 'City', 'State', 'Zip'];
    var addrQryString = '&MakeCoverLetter=True';
    retArray = window.showModalDialog(addressInputURL, '', 'dialogWidth:625px;dialogHeight:350px;');
    // If CoverLetterAddressUI form gets Closed or Cancelled, retArray would be Null or Undefined; CreatePDF page needs to be invoked only if retArray has contents.
    if (retArray != null || retArray != undefined) {
        for (i = 0; i < retArray.length; i++) {
            addrQryString += '&' + addrInfo[i] + '=' + retArray[i];
        }
        // Sending request to CreatePDF page to bring up Search Results page with a cover letter showing address information entered earlier through the modal window UI.
        window.open(createPdfURL + pageName + addrQryString);
    }
}

function createPDF(callingPageName) {
    pageName = callingPageName;
    // Variable securityMode is defined and get assigned a value in the aspx page.
    if (securityMode == 'Operations') {
        var retVal = confirm('          - Click OK if you want a Cover Letter \n\n          - Click Cancel if you do not want a Cover Letter\n\n');
        if (retVal == true) {
            addressEntry();
        }
        else {
            window.open(createPdfURL + pageName);
        }       
    }
    else {
        window.open(createPdfURL + pageName);
    }
}
