﻿/// <module>
///     utility.js
/// </module>
/// <summary>
///     Javascript utility functions for the E-License web application.
/// </summary>
/// <copyright>
///     Copyright © 2011.  Michigan Department of Natural Resources.
///     All Rights Reserved.
///
///     The contents of this file are subject to the Michigan RSS License
///     (the 'License'); you may not use this file except in compliance
///     with the License.
///
///     THE SOFTWARE IS PROVIDED 'AS-IS' AND WITHOUT WARRANTY OF ANY KIND,
///     EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
///     MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
///
///     UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT (INCLUDING
///     NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE INITIAL DEVELOPER, ANY OTHER
///     CONTRIBUTOR, OR ANY DEVELOPER OF COVERED CODE, OR ANY SUPPLIER OF ANY OF
///     SUCH PARTIES, BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY INDIRECT,
///     SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING,
///     WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER
///     FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES,
///     EVEN IF SUCH PARTY SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF SUCH
///     DAMAGES. THIS LIMITATION OF LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH
///     OR PERSONAL INJURY RESULTING FROM SUCH PARTY’S NEGLIGENCE TO THE EXTENT
///     APPLICABLE LAW PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO NOT ALLOW
///     THE EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO
///     THAT EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU.
///     Call Hewlett-Packard Development Company for more information.
///
///     Notices of copyright and/or attribution that appear at the beginning
///     of any file included in this distribution must remain intact.
///
///     See the License for the specific language governing rights and limitations
///     under the License.
/// </copyright>
/// <history>
///     <change date="10/18/2010" author="James Kircoff, MaoShen Taur, Paul Olszewski" release="1">
///         First revision.
///     </change>
///     <change date="12/05/2010" author="Greg Flynn" release="CC 31">
///         Added expiration_alert.
///     </change>
///     <change date="12/08/2010" author="MaoShen Taur" release="1">
///         Updated clear_form() function for EnterCustomerForReprint page.
///     </change>
///     <change date="12/12/2010" author="MaoShen Taur" release="1">
///         Updated clear_form() function per DisputeBearPreferencePointsOrElkChances WPR comments.
///     </change>
///     <change date="01/04/2011" author="Paul Olszewski" release="CC 50">
///         Added checkHipSurveyDeclined() function to reset the user's selections when declining the survey.
///     </change>
///     <change date="01/11/2011" author="MaoShen Taur" release="1">
///         Scroll the Welcome page back to the top after the do not certify alert is acknowledged.
///     </change>
///     <change date="01/11/2011" author="Greg Flynn" release="1  CC 998 CR 70728">
///         Added dnrIdNotification.
///     </change>
///     <change date="01/21/2011" author="James Kircoff" release="1 CR 70872">
///         Modified confirmDeleteCartItem.
///     </change>
///     <change date="01/22/2011" author="Greg Flynn" release="1  CC 998 CR 70919">
///         Fixed expiration date reading issue.
///     </change>
///     <change date="01/24/2011" author="Greg Flynn" release="1  CC 998 CR 70999/70928">
///         Updated clearHuntAreaChoice logic to work around format table in user control.
///         Added additional return in SAH message.
///     </change>
///     <change date="01/25/2011" author="James Kircoff" release="CR 71076">
///         Modified PromptSAH to display the SAH dialog every time Checkout is clicked.
///     </change>
///     <change date="01/26/2011" author="Paul Olszewski" release="1  CC 998 CR 70857">
///         Removed createDeletedItemMessage() as it is no longer being called.
///     </change>
///     <change date="01/26/2011" author="Paul Olszewski" release="1 CC998 CR71113">
///         Added effectiveDateChanged() method to notify customer when effective date is invalid.
///     </change>
///     <change date="01/27/2011" author="Greg Flynn" release="1 CC998 CR 71048">
///         Updated clearHuntAreaChoice logic to work in browsers other than IE (changed from
///         document to ownerDocument).
///     </change>
///     <change date="01/29/2011" author="James Kircoff" release="1 CR 70790">
///         Modified dnrIdNotification to only display popup window when "DNRE Sportcard OR DNRE ID",
///         "MI Driver License OR 13 Character State of MI ID", and "11 Character State of MI ID" are all blank.
///     </change>
///     <change date="02/03/2011" author="Greg Flynn" release="1  CC 998 CR 71226/71290">
///         Removed all EnterCustomerForReprint logic from clear_form.  The aspx will still call this method
///         but only to display the required message.
///     </change>
///     <change date="02/08/2011" author="MaoShen Taur" release="1 CR 71077">
///         Removed all DisputeBearPreferencePointsOrElkChances logic from clear_form.  The aspx will still call this method
///         but only to display the required message.
///     </change>
///     <change date="02/08/2011" author="Greg Flynn" release="1  CC 998 CR 71373">
///         Removed effectiveDateChanged, allowing validation.js to handle the error messaging.
///     </change>
///     <change date="02/09/2011" author="MaoShen Taur" release="1 CR 71077">
///         Removed all CheckBearPreferencePointsOrElkChances logic from clear_form.  The aspx will still call this method
///         but only to display the required message.
///     </change>
///     <change date="02/11/2011" author="MaoShen Taur" release="1 CR 71416">
///         Added function newWindow to allow a page to be opened in a new window with RedirectResponse.
///     </change>
///     <change date="04/07/2011" author="Mike Terranova" release="11.2 CR 72033">
///         Updated "DNRE" in displayed text to "DNR".
///     </change>
///     <change date="04/28/2011" author="Mike Terranova" release="11.2 CR 72033">
///         Updated "DNR" to read from resources provided by Site.master
///     </change>
/// </history>

//10/18/10 JK - Provides popup functionality for printable items.
function elpopup(url) {
    window.open(url, "Print", "toolbar=yes,location=no,resizable=1,directories=no,menubar=yes,scrollbars=yes,width=690,height=550");
}

//10/19/10 JK - Provides popup alert on the Welcome view to those who click on the "I Do Not Certify" button.
function doNotCertifyAlert() {
    alert("Unable to Certify at this time?\nThank you for visiting E-License.");
    scroll(0, 0);
    return true;
}

//11/27/10 MT - Provides field values reset and alert on the CheckBearPreferencePointsOrElkChances view to those who click on the "Clear Form" button.
//12/13/10 MT - Updated the element names for ddlMonth, ddlDay, and txtYear to include the user control name.
function clear_form() {
    return alert("The entry fields have been reset to their default values.\nPlease re-enter the information.");
}

//11/27/10 MT - Provides popup alert on the DisplayBearPreferencePointsOrElkChances view for Bear Preference Points to those who click on the "Agree" button.
function agree_points_alert() {
    return alert("Thank you for using E-License to check your Bear Preference Points.");
}

//11/27/10 MT - Provides popup alert on the DisplayBearPreferencePointsOrElkChances view for Elk Chances to those who click on the "Agree" button.
function agree_chances_alert() {
    return alert("Thank you for using E-License to check your Elk Chances.");
}

//11/30/10 JK - Stores short description of item that is to be deleted in DisplayShoppingCart.
var shortDescription;

//11/30/10 JK - Prompts for confirmation of item deletion from the the shopping cart in DisplayShoppingCart.
function confirmDeleteCartItem(visibleIndex) {
    var confirmResults;

    shortDescription = gvShoppingCart.cpShortDescription[visibleIndex - gvShoppingCart.visibleStartIndex];

    confirmResults = confirm("Are you sure you want to remove the "
            + shortDescription
            + " item from your shopping cart?  If you are sure, click on the OK button.  "
            + "If not, click on the CANCEL button.");

    if (confirmResults) {
        if (gvShoppingCart.cpShortDescription.length <= 1) {
            eval("btnCheckout").SetEnabled(false);
        }

        hdnShoppingCartId = document.getElementById("ctl00_MainContent_hdnShoppingCartId");
        hdnShoppingCartId.value = gvShoppingCart.cpShoppingCartId[visibleIndex - gvShoppingCart.visibleStartIndex];

        return true;
    }
    else {
        return false;
    }
}

//11/30/10 JK - Prompt for order cancelation on DisplayShoppingCart.
function confirmCancelOrder(s, e) {
    e.processOnServer = confirm("Are you sure you want to cancel your order?\n"
            + "If you are sure, click on the OK button.  If not, click on the CANCEL button.");
}

//11/30/10 JK - Prompt for Sportsman Against Hunger donation on DisplayShoppingCart.
function promptSAH(s, e) {
    var confirmResults;

    confirmResults = confirm("Would you like to make a charitable contribution to Sportsmen Against Hunger?\n"
        + "This pays for processing and distribution of donated wild game to help feed local families in need.\n\n"
        + "If you would like to make a contribution click on OK, then click on the Keep Shopping button to "
        + "return to the Available E-License Items page.  If you do not want to make a contribution, "
        + "click CANCEL to complete your order.");

    if (confirmResults) {
        e.processOnServer = false
    }
    else {
        e.processOnServer = true;
    }
}

//12/01/10 GMF - Provides popup alert on the DisplayAvailableItems view explaining that the selected item is within the
// expiration window.
function expiration_alert(s, e) {
    var stringLength = s.name.length;

    // Make sure that the length of the incoming field it long enough as to not cause problems.
    if (stringLength > 34) {
        var expirationDays;
        var licenseYear;
        // Retrieve the required fields from the grid, via the row number embedded in the cell's system name.  Use
        // two vars since we won't know if the second digit is actually numeric.
        var firstNumber = s.name.substring(32, 33);
        var secondNumber = s.name.substring(33, 34);

        // If the second character is Not a Number, then just use the first for the index.  Otherwise, combine
        // the two to get the correct index.
        if (!isNaN(secondNumber)) {
            firstNumber = firstNumber + secondNumber;
        }

        expirationDays = dgCatalog.cpEndingSeasonDate[firstNumber];
        licenseYear = dgCatalog.cpLicenseYear[firstNumber];
    }
    else {
        // If the length of the incoming field is not long enough, skip the alert message.
        expirationDays = -1;
    }


    // If expirationDays is set to -1, bypass the alert screen.  Otherwise, display the days until expiration and the license
    // year that will next be available.
    if (expirationDays == "-1") {
        return;
    }
    else {
        return alert("This license will expire in " + expirationDays + " day(s).  " + licenseYear + " licenses will be on sale March 1, "
         + licenseYear + ".");
    }
}    

//12/05/10 PO - Prompt for item cancellation on AdditionalQuestions.
function confirmCancelItem(s, e) {
    e.processOnServer = confirm("Are you sure you want to cancel the purchase of this item?\n"
            + "Click the 'OK' button and you will be returned to the Available E-License Items page.  Click the 'Cancel' button and you will remain on this page.");
}

//12/11/10 PO - Clear the selected hunt area on HuntAreaQuestions.
function clearHuntAreaChoice(btnClearChoice) {
    var ddlHuntAreas = btnClearChoice.ownerDocument.getElementById(btnClearChoice.id.substring(0, 60) + "ddlHuntAreas");

    ddlHuntAreas[0].selected = true;

    return false; // disable postback
}

//01/04/11 PO - Reset the selected HIP survey questions
function checkHipSurveyDeclined(s, e) {
    if (rdoHipSurveyDecline.GetChecked()) {
        var harvestChoices = document.getElementById(panMigratoryBirdSurvey.name).getElementsByTagName("select");
        for (var choice = 0; choice < harvestChoices.length; choice++) {
            harvestChoices[choice].selectedIndex = 0;
        }
        rdoSnipeSurvey.SetSelectedIndex(1);
    }
}

//01/11/11 GMF - Notify customers of the limited items available to a DNR ID holder.
function dnrIdNotification(s, e) {
    var dnreSportcardOrId = document.getElementById("ctl00_MainContent_roundPanelExistingCustomer_userControlCustomerID_txtSportcardDNRE_I");
    var miDriverLicenseOrThirteenCharStateOfMiId = document.getElementById("ctl00_MainContent_roundPanelExistingCustomer_userControlCustomerID_txtDriverLicenseMichiganID_I");
    var elevenCharStateOfMiId = document.getElementById("ctl00_MainContent_roundPanelExistingCustomer_userControlCustomerID_txtMichiganID_I");
    
    if ((dnreSportcardOrId != null) && (miDriverLicenseOrThirteenCharStateOfMiId != null) && (elevenCharStateOfMiId != null)) {
        if ((dnreSportcardOrId.value == "") && (miDriverLicenseOrThirteenCharStateOfMiId.value == "") && (elevenCharStateOfMiId.value == "")) {
            var departmentName = document.getElementById("DepartmentResource").value;
            e.processOnServer = confirm("Are you aware that you will not be able to buy hunting and fishing licenses when you use a " + departmentName + " ID for identification?\n\n"
                + "If the selection of 'Obtain " + departmentName + " ID' is correct, click on the OK button.\n\n"
                + "If not, click on the Cancel button and either select 'Buy Sportcard' or enter an identification number.");
        }
    }
    
}

//02/11/11 MST - This function can be used with RedirectResponse to open page in a new window.
function newWindow() {
    document.forms[0].target = "_blank";
}
