﻿$(document).ready(function() {



    //RETRO DATE FUNCTIONS

    //hide retro date control
    $("[id *= 'retrodiv']").hide();


    $("[id *= 'retrord']").change(function() {
        if ($(this).val() == 'retrord1') {
            $("[id *= 'retrodiv']").show('slow');
        } else {
            $("[id *= 'retrodiv']").hide('slow');

        }
    });

    //END OF RETRO FUNCTIONS



    //END OF INSURANCE LINES DISPLAY FUNCTIONS


    //PERCENTBOX FUNCTIONS

    $("[id *= 'PercentBox']").live('keyup', function() {
        var myMin = $("#" + this.id.replace('PercentBox', 'MinTag')).val();
        var myMax = $("#" + this.id.replace('PercentBox', 'MaxTag')).val();
        var myPrefix = $("#" + this.id.replace('PercentBox', 'PrefixTag')).val();
        var mySuffix = $("#" + this.id.replace('PercentBox', 'SuffixTag')).val();
        AddMask(this, myMin, myMax, myPrefix, mySuffix);

    });

    function AddMask(myInput, myMin, myMax, myPrefix, mySuffix) {
        var myval = myInput.value.replace(myPrefix, '');
        myval = myval.replace(mySuffix, '');


        if (myval != '') {
            if (parseFloat(myval) > parseFloat(myMax)) {
                myval = myMax;
            }

            if (parseFloat(myval) < parseFloat(myMin)) {
                myval = myMin;
            }

            myval = myval + 'A';
            myval = myval.replace('A', '');
        };

        myval = FormatNumber(myval);

        myInput.value = myPrefix + myval + mySuffix;
        
        if (myInput.value == myPrefix + mySuffix) {
            myInput.value = '';
        }

        var pos = 0;

        if (myPrefix == '') {
            pos = myInput.value.length - mySuffix.length;
        } else {
            pos = myInput.value.length;
        }

        if ($(myInput).get(0).setSelectionRange) {
            $(myInput).get(0).setSelectionRange(pos, pos);
        } else if ($(myInput).get(0).createTextRange) {
            var range = $(myInput).get(0).createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        }

    };



    //Code to format a number with commas
    function FormatClean(num) {
        var sVal = '';
        var nVal = num.length;
        var sChar = '';

        try {
            for (c = 0; c < nVal; c++) {
                sChar = num.charAt(c);
                nChar = sChar.charCodeAt(0);
                if ((nChar >= 48) && (nChar <= 57)) { sVal += num.charAt(c); }
            }
        }
        catch (exception) { }
        return sVal;
    }

    // use dec_sep for internationalization
    function Decimals(x, dec_sep) {
        var tmp = new String();
        tmp = x;
        if (tmp.indexOf(dec_sep) > -1) {
            return tmp.length - tmp.indexOf(dec_sep) - 1;
        }
        else {
            return 0;
        }
    }



    function FormatNumber(num) {
        var minus = '';
        var comma = '';
        var dec = '';
        var preDecimal = '';
        var postDecimal = '';
        var decimalPlaces = 0;

        try {



            decimalPlaces = Decimals(num, ".");
            comma = ",";
            dec = ".";

            if (decimalPlaces < 1) { dec = ''; }
            if (num.lastIndexOf("-") == 0) { minus = '-'; }

            preDecimal = FormatClean(num);
            // Set to "" if the number is 0
            if (num == "" || num.match(/^s+$/)) { return ""; }

            // preDecimal doesn't contain a number at all.
            // Return formatted zero representation.

            if (preDecimal.length < 1) {
                return minus + FormatEmptyNumber(dec, decimalPlaces);
            }

            // preDecimal is 0 or a series of 0's.
            // Return formatted zero representation.

            if (parseInt(preDecimal) < 1) {
                return minus + FormatEmptyNumber(dec, decimalPlaces);
            }

            // predecimal has no numbers to the left.
            // Return formatted zero representation.

            if (preDecimal.length == decimalPlaces) {
                return minus + '0' + dec + preDecimal;
            }

            // predecimal has fewer characters than the
            // specified number of decimal places.
            // Return formatted leading zero representation.

            if (preDecimal.length < decimalPlaces) {
                if (decimalPlaces == 2) {
                    return minus + FormatEmptyNumber(dec, decimalPlaces - 1) + preDecimal;
                }
                return minus + FormatEmptyNumber(dec, decimalPlaces - 2) + preDecimal;
            }

            // predecimal contains enough characters to
            // qualify to need decimal points rendered.
            // Parse out the pre and post decimal values
            // for future formatting.

            if (preDecimal.length > decimalPlaces) {
                postDecimal = dec + preDecimal.substring(preDecimal.length - decimalPlaces,
                                               preDecimal.length);
                preDecimal = preDecimal.substring(0, preDecimal.length - decimalPlaces);
            }

            // Place comma oriented delimiter every 3 characters
            // against the numeric represenation of the "left" side
            // of the decimal representation.  When finished, return
            // both the left side comma formatted value together with
            // the right side decimal formatted value.

            var regex = new RegExp('(-?[0-9]+)([0-9]{3})');

            while (regex.test(preDecimal)) {
                preDecimal = preDecimal.replace(regex, '$1' + comma + '$2');
            }

        }
        catch (exception) { }
        return minus + preDecimal + postDecimal;
    }

    function FormatEmptyNumber(decimalDelimiter, decimalPlaces) {
        var preDecimal = '0';
        var postDecimal = '';

        for (i = 0; i < decimalPlaces; i++) {
            if (i == 0) { postDecimal += decimalDelimiter; }
            postDecimal += '0';
        }
        return preDecimal + postDecimal;
    }


    //END OF PERCENTBOX FUNCTIONS

    //LIMITS AND EXCESS DISPLAY FUNCTIONS


    $("[id *= 'ExcessUpdatePanel']").each(function() {
        var myID = $(this).attr('ID');
        var newID = myID.replace('Excess', 'Limit');
        var newID = newID.replace('UpdatePanel', '');
        if ($("[id *= '" + newID + "']").val() == '0') {
            $(this).hide();
        }

    });


    $("[id *= 'drpLimit']").live('change', function() {
        showHideExcess($(this));

    });

    function showHideExcess(myControl) {
        var myID = myControl.attr('ID');
        var newID = myID.replace('Limit', 'Excess');
        var newID = newID.replace('_drpLimit', 'UpdatePanel');

        if ($("[id *= '" + myID + "']").val() == '0') {
            $("[id *= '" + newID + "']").hide();
        } else {
            $("[id *= '" + newID + "']").show();
        }
    };

    //END OF LIMITS AND EXCESS DISPLAY FUNCTIONS




});
