﻿// THIS FILE LIVES IN CIDS, CLEARCLAIM AND IMACC.NET
// PLEASE KEEP ALL COPIES IDENTICAL
// Note that not all jQuery selectors are applicable in all contexts.
// This is intentional in favor of maintaining one common file.

//The following function fires on jQuery's document ready event...
$(function() {
    $('input:text:visible:first').focus();
    $('[id*=ddlMall]').focus();
    $('#divConfirmWaterMitigation').hide();
    $('#trConfirmWaterMitigation').hide();

    // Initialize checkbox validation messages.
    $('[id*=ChkInvalid]').hide();
    initCheckedFlags();
    validateDamageCheckboxes(false);

    // Use id* selector for compat with ASP.Net controls.
    if ($('[id*=txtDateOfLoss]').datepicker) {
        $('[id*=txtDateOfLoss]').datepicker({
            inline: true
        });
    }

    // Prepend blank selection for mall drop down list box
    $('[id*=ddlMall]').prepend('<option selected="selected" value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>');
    if ($('[id*=ddlMall]').length > 0) {
        $('[id*=ddlState]').html('<option selected="selected" value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>');
        disablePropertyControls();
    }
});    

// globals
window.checkboxesValidWater = false;
window.checkboxesWaterMsg = 'Invalid Type of Damage selections.'
window.checkboxesValidFire = false;
window.checkboxesFireDamageMsg = 'Invalid Type of Damage selections.'
window.checkboxesValidReconstruction = false;
window.checkboxesReconstructionMsg = 'Invalid Type of Damage selections.'



function initCheckedFlags() {
    if ($('[id*=chkWater]').is(':checked')) { window.waterDamage = true; };
    if ($('[id*=chkWind]').is(':checked')) { window.windDamage = true; };
    if ($('[id*=chkFire]').is(':checked')) { window.fireDamage = true; };
    if ($('[id*=chkContents]').is(':checked')) { window.contents = true; };
    if ($('[id*=chkReconstruction]').is(':checked')) { window.buildingReconstruction = true; };
    if ($('[id*=chkDryClean]').is(':checked')) { window.dryCleaning = true; };
    if ($('[id*=chkHazmat]').is(':checked')) { window.hazmat = true; };
    if ($('[id*=chkTemp]').is(':checked')) { window.temp = true; };
    if ($('[id*=chkMold]').is(':checked')) { window.mold = true; };
    if ($('[id*=chkTrees]').is(':checked')) { window.trees = true; };
    if ($('[id*=chkCO]').is(':checked')) { window.CO = true; };
}

function validateDamageCheckboxes(confirmWater) {

    // validate water
    if (window.waterDamage &&
            (window.buildingReconstruction ||
             window.fireDamage)) {
        window.checkboxesValidWater = false;
    } else {
        window.checkboxesValidWater = true;
    }

    // validate fire
    if (window.fireDamage &&
            (window.waterDamage ||
             window.windDamage ||
             window.buildingReconstruction ||
             window.hazmat ||
             window.temp ||
             window.mold ||
             window.trees ||
             window.co)) {
        window.checkboxesValidFire = false;
    } else {
        window.checkboxesValidFire = true;
    }
    
    // validate reconstruction
    if (window.buildingReconstruction &&
            (window.trees)) {
        window.checkboxesValidReconstruction = false;
    } else {
        window.checkboxesValidReconstruction = true;
    }

    displayCheckBoxValidators();

    if (confirmWater != false) {
        confirmNeed();
    }

}

function displayCheckBoxValidators() {
    if (window.checkboxesValidWater) {
        $('[id*=ChkInvalidWater]').hide();
    } else {
        if (window.fireDamage) { $('#chkFireChkInvalidWater').show(); } else { $('#chkFireChkInvalidWater').hide(); }
    if (window.buildingReconstruction) { $('#chkReconstructionChkInvalidWater').show(); } else { $('#chkReconstructionChkInvalidWater').hide(); }    
    }

    if (window.checkboxesValidReconstruction) {
        $('[id*=ChkInvalidReconstruction]').hide();
    } else {
    if (window.trees) { $('#chkTreesChkInvalidReconstruction').show(); } else { $('#chkTreesChkInvalidReconstruction').hide(); }
    }

    if (window.checkboxesValidFire) {
            $('[id*=ChkInvalidFire]').hide();
    } else {
        if (window.waterDamage) { $('#chkWaterChkInvalidFire').show(); } else { $('#chkWaterChkInvalidFire').hide(); }
        if (window.windDamage) { $('#chkWindChkInvalidFire').show(); } else { $('#chkWindChkInvalidFire').hide(); }
        // if (window.contents) { $('#chkContentsChkInvalidFire').show(); } else { $('#chkContentsChkInvalidFire').hide(); }
        if (window.buildingReconstruction) { $('#chkReconstructionChkInvalidFire').show(); } else { $('#chkReconstructionChkInvalidFire').hide(); }
        if (window.hazmat) { $('#chkHazmatChkInvalidFire').show(); } else { $('#chkHazmatChkInvalidFire').hide(); }
        if (window.temp) { $('#chkTempChkInvalidFire').show(); } else { $('#chkTempChkInvalidFire').hide(); }
        if (window.mold) { $('#chkMoldChkInvalidFire').show(); } else { $('#chkMoldChkInvalidFire').hide(); }
        if (window.trees) { $('#chkTreesChkInvalidFire').show(); } else { $('#chkTreesChkInvalidFire').hide(); }
        if (window.co) { $('#chkCOChkInvalidFire').show(); } else { $('#chkCOChkInvalidFire').hide(); }
    }
}

function confirmNeed() {
    var dtNow = new Date();
    var dtLoss = new Date(window.dateOfLoss);
    if ($('#chkWaterChkInvalidFire').is(":visible") == false && window.waterDamage == true && !isDate(window.dateOfLoss)) {
        if (window.confirmed != true) {
        displayConfirmNeedWidget();
            // Pop-up the confirm msg only once.
            window.confirmed = true
        }
        //$('#simplemodal-container').css('width', 'auto');
    } else if (window.waterDamage == true && isDate(window.dateOfLoss) && dtNow - dtLoss >= 3 * 24 * 60 * 60 * 1000) {
        if (window.confirmed != true) {
        displayConfirmNeedWidget();
            // Pop-up the confirm msg only once.
            window.confirmed = true
        }
        //$('#simplemodal-container').css('width', 'auto');
    } else {
        hideConfirmNeedWidget()
    }
}

function hideConfirmNeedWidget() {
    // Confirm need widget can be either modal pop-up or custom tool tip.
    if ($('#divConfirmWaterMitigation').length > 0) {
        // SimpleModal is loaded. Use it.
        $('#divConfirmWaterMitigation').hide();
    } else {
        // TODO: display/hide custom tool tip
        // TODO: $('#chkWater').tooltip(); 
    }
}

function displayConfirmNeedWidget() {
    // Confirm need widget can be either modal pop-up or custom tool tip.
    if ($('#divConfirmWaterMitigation').length > 0) {
        // SimpleModal is loaded. Use it.
        $('#divConfirmWaterMitigation').modal();
    } else {
        // TODO: display custom tool tip
        // TODO: $('#chkWater').tooltip(); 
    }
}


function isDate(value) {
    return (!isNaN(new Date(value).getYear()));
}

function validate(doConfirm) {
    var n = $('input:checked').length;
    if (n == 0) {
        alert('Please select type of damage.');
        return false;
    } else if (!window.checkboxesValidWater) {
        alert(window.checkboxesWaterMsg);
        return false;
    } else if (!window.checkboxesValidReconstruction) {
        alert(window.checkboxesReconstructionMsg);
        return false;
    } else if (!window.checkboxesValidFire) {
        alert(window.checkboxesFireDamageMsg);
        return false;
    } else if (doConfirm && doConfirm == true) {
        return confirm("Are you sure you are ready to submit this claim?")
    } else {
        return true;
    }
}

function formatDt(el) {
    // Don't call this function if you are using the date picker.
    var dt = new Date(el.value);
    el.value = dt.format('mm/dd/yyyy');
}

function getSpecialPropertyInfo(id) {

    $("body").css("cursor", "progress");
    // Disable controls while ajax request is pending.
    // if ($("[id*=ddlAdjuster]").attr("disabled") == false) { $("[id*=ddlAdjuster]").attr("disabled", "disabled"); }

    var url = 'CustomHandlers/Handler.ashx?action=GetSpecialPropertyInfo&ID=' + id;
    var xhr = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xhr = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xhr.open('GET', url, true);
    // self.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xhr.onreadystatechange = function() {
        if (self.xhr.readyState == 4) {
            // process response
            var json
            if (self.xhr.responseText.length) {
                json = eval('(' + self.xhr.responseText + ')');
            }
            setControls(json);

            // Reenable controls.
            // $("[id*=ddlAdjuster]").removeAttr("disabled");
            $("body").css("cursor", "auto");
        }
    }
    self.xhr.send(null);
}

//Clear the contents of ddlAdjuster and add the adjusters of the currently selected insurance company
function setControls(json) {
    var o = json[0];
    var txtFName = $("[id*=txtCustomerFirstName]");
    var txtLName = $("[id*=txtCustomerLastName]");
    var txtAddr = $("[id*=txtStreetAddress]");
    var txtCity = $("[id*=txtCity]");
    var ddlState = $("[id*=ddlState]");
    var txtZip = $("[id*=txtZip]");
    var txtPh1 = $("[id*=CustPhone1]");
    var txtPh2 = $("[id*=CustPhone2]");
    var txtPh3 = $("[id*=CustPhone3]");

    txtFName.val(o.gmfName);
    txtLName.val(o.gmlName);
    txtAddr.val(o.address);
    txtCity.val(o.city);
    ddlState.html('');
    ddlState.append('<option selected="selected" value="">' + o.state + '</option>');
    txtZip.val(o.zip);
    txtPh1.val(o.phone.substring(0,3));
    txtPh2.val(o.phone.substring(3, 6));
    txtPh3.val(o.phone.substring(6, 10));

    disablePropertyControls();

    $("[id*=AltPhone1]").focus();
    
    return;
}

function disablePropertyControls() {
    var txtFName = $("[id*=txtCustomerFirstName]");
    var txtLName = $("[id*=txtCustomerLastName]");
    var txtAddr = $("[id*=txtStreetAddress]");
    var txtCity = $("[id*=txtCity]");
    var ddlState = $("[id*=ddlState]");
    var txtZip = $("[id*=txtZip]");
    var txtPh1 = $("[id*=CustPhone1]");
    var txtPh2 = $("[id*=CustPhone2]");
    var txtPh3 = $("[id*=CustPhone3]");
    
    txtFName.attr('readonly', 'readonly');
    txtFName.css('background-color', '#DCDCDC');
    
    txtLName.attr('readonly', 'readonly');
    txtLName.css('background-color', '#DCDCDC');

    txtAddr.attr('readonly', 'readonly');
    txtAddr.css('background-color', '#DCDCDC');
    
    txtCity.attr('readonly', 'readonly');
    txtCity.css('background-color', '#DCDCDC');

    ddlState.attr("disabled", "disabled");
    ddlState.css('background-color', '#DCDCDC');
    ddlState.css('color', '#000');

    txtZip.attr('readonly', 'readonly');
    txtZip.css('background-color', '#DCDCDC');
    
    txtPh1.attr('readonly', 'readonly');
    txtPh1.css('background-color', '#DCDCDC');

    txtPh2.attr('readonly', 'readonly');
    txtPh2.css('background-color', '#DCDCDC');

    txtPh3.attr('readonly', 'readonly');
    txtPh3.css('background-color', '#DCDCDC');
}

