﻿

        window.onload = function() {
            if (document.getElementById("frame") != null) {
                if (document.getElementById("frame").offsetHeight) {
                    document.getElementById("borderRight").style.height = document.getElementById("frame").offsetHeight - 5;
                    document.getElementById("borderLeft").style.height = document.getElementById("frame").offsetHeight - 5;
                    document.getElementById("bottomBox").style.height = document.getElementById("mainArea").offsetHeight - 570;
                } else if (document.getElementById("frame").style.pixelHeight) {

                    document.getElementById("borderRight").style.height = document.getElementById("frame").style.pixelHeight;
                    document.getElementById("borderLeft").style.height = document.getElementById("frame").style.pixelHeight;
                    document.getElementById("bottomBox").style.height = document.getElementById("mainArea").offsetHeight - 570;
                }
            }

        };



        function hideAll() {
            document.getElementById("subItems1").style.display = "none";
            document.getElementById("subItems2").style.display = "none";
//            document.getElementById("subItems3").style.display = "none";
            document.getElementById("subItems4").style.display = "none";
        }

        function Imgonfocus(img) {
            img = new Image();
            img.src = "Images/Play2.png";
        }

        function showObj(itemId) {
            if (document.getElementById(itemId).style.display == "none") {
                hideAll();
                document.getElementById(itemId).style.display = "inline";
            }
            else {
                //document.getElementById(itemId).style.display = "none";
            }

        }

        function hiLite(imgDocID, imgObjName) {
            var img;
            //document.images[imgDocID].src = eval(imgObjName + ".src");
            img = document.getElementById(imgDocID);
            img.src = eval(imgObjName + ".src");
        }

       // function ChangeCusor() {
           // document.body.style.cursor = "hand";
        //}

        //function ChangeCusorAuto() {
           // document.body.style.cursor = "auto";
       // }

        function CurrencyFormatted(amount) {

            if (isNaN(i)) {
                return amount;
            } else {
                var i = parseFloat(amount);

                var minus = '';
                if (i < 0) { minus = '-'; }
                i = Math.abs(i);
                i = parseInt((i + .005) * 100);
                i = i / 100;
                s = new String(i);
                if (s.indexOf('.') < 0) { s += '.00'; }
                if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
                s = minus + s;
                return s;
            }
        }

        
//        function formatCurrency(num) {
//            num = num.toString().replace(/\$|\,/g, '');
//            if (isNaN(num)) {
//                return num;
//            } else {
//                sign = (num == (num = Math.abs(num)));
//                num = Math.floor(num * 100 + 0.50000000001);
//                cents = num % 100;
//                num = Math.floor(num / 100).toString();
//                if (cents < 10)
//                    cents = "0" + cents;
//                for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
//                    num = num.substring(0, num.length - (4 * i + 3)) + ',' +
//        num.substring(num.length - (4 * i + 3));
//                return (((sign) ? '' : '-') + num);
//            }
//            //return (((sign)?'':'-') + '$' + num + '.' + cents);
//        }

        function formatCurrency(Id) {
            var frmvalidator = new Validator("aspnetForm");
            frmvalidator.addValidation(Id, "num", "Please provide a valid numeric value.");

            num = $('#' + Id).val();
            if (num == null) {
                return 0;
                
            }
            num = num.toString().replace(/\$|\,/g, '');
            if (isNaN(num)) {
                return num;
            } else {
                sign = (num == (num = Math.abs(num)));
                num = Math.floor(num * 100 + 0.50000000001);
                cents = num % 100;
                num = Math.floor(num / 100).toString();
                if (cents < 10)
                    cents = "0" + cents;
                for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
                    num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
                return (((sign) ? '' : '-') + num);
            }
            //return (((sign)?'':'-') + '$' + num + '.' + cents);
        }


        function getfocus(id) {
            document.getElementById(id).focus()
        }

        function selText(txt) {
            document.getElementById(txt).select()
        }

        function EnterToTab(event) {
            if (event.keyCode == 13)
                event.keyCode = 9
                return event.keyCode;
        }


        function addbookmark() {
            bookmarkurl = "http://www.bicalculator.com/"
            bookmarktitle = "LMI Group BIcalculator Website."
            if (document.all)
                window.external.AddFavorite(bookmarkurl, bookmarktitle)
        }

        function SetFocusToButton(btnId) {
            if (event.keyCode == 13) {
                document.getElementById(btnId).focus();
            }
        }

        function OneTextToAnother(fId, sId, tId) {
            fId = '#' +fId;
            sId = '#' + sId;
            tId = '#' + tId;
             window.setTimeout(function(){
                $(sId).val($(fId).val());
                $(tId).val($(fId).val());
            }, 0);
            
        }

        function MakeQuestionConfirmVisible(id, ansId, isChecked, cur) {
            var strId = '#' + 'divExplanation' + ansId;

            var reg = /_accContent._hidden?/;
            var serverid = id.substr(id.search(reg) + 13, (id.length - id.search(reg) - 12));

            $(strId).dialog({ autoOpen: true });

            $(strId).dialog({ buttons: [
            {
                text: "Yes",
                click: function () {
                    MakeQuestionVisible(serverid, cur); 
                    $(this).dialog("close"); }
            },
            {
                text: "No",
                click: function () {
                    if (isChecked)

                        $('#' + id).attr('checked', false);
                    else
                        $('#' + id).attr('checked', true);
                         
                         $(this).dialog("close");
                }
            }
            ],
                title:"Warning"
            });
        }

        function MakeQuestionVisible(id, cur) {
            var checkbox = $('.ckBoxClass label[for$=' + id + ']').parent();
            var _id = 'txtIsVisible' + id,
                title = $('.ckBoxClass label[for$=' + id + ']').text();

            if ($('#c_' + _id).length > 0) {
                $('#c_' + _id).remove();
            } else {
                $.template('quesTmpl', '<div id="${container_id}" class="selected_item_unit"><div class="delete" onclick="DeleteAdditionalItemZA(this)"></div><input id="${id}" type="text" value="${title}" class="title tipsy-explanation" rel="${rel}" readonly="readonly" /> ${cur} <input id="${content_id}" class="selected_item_content" type="text" onblur="javascript:this.value=formatCurrency(this.id);" /></div>');
                $.tmpl('quesTmpl', { 'id': _id, 'title': title, 'container_id': 'c_' + _id, 'content_id': 'ctnt_' + _id, 'rel': checkbox.attr('rel'), 'cur': cur })
                 .appendTo('#selected_list');
 		$('#ctnt_' + _id).focus();
            }
            return false;

            /*var compliexId = 'tr[id$=' + id + ']';
            var txtIsVisible = '#' + 'txtIsVisible' + id;
            if ($(compliexId).hasClass('hidden')) {
                $(compliexId).removeClass('hidden').addClass('Visible');
                $(txtIsVisible).val('true');  
            } else {
                $(compliexId).removeClass('Visible').addClass('hidden');
                $(txtIsVisible).val('false');

            }*/
        }

        function MakeQuestionVisibleNonZA(id, cur) {
            var checkbox = $('.ckBoxClass label[for$=' + id + ']').parent();
            var _id = 'txtIsVisible' + id,
                title = $('.ckBoxClass label[for$=' + id + ']').text();

            if ($('#c_' + _id).length > 0) {
                $('#c_' + _id).remove();
            } else {
                $.template('quesTmpl', '<div id="${container_id}" class="selected_item_unit"><input id="${id}" type="text" value="${title}" class="title tipsy-explanation" rel="${rel}" /> ${cur} <input id="${content_id}" class="selected_item_content" type="text" onblur="javascript:this.value=formatCurrency(this.id);" /></div>');
                $.tmpl('quesTmpl', { 'id': _id, 'title': title, 'container_id': 'c_' + _id, 'content_id': 'ctnt_' + _id, 'rel': checkbox.attr('rel'), 'cur': cur })
                 .appendTo('#selected_list');
            }
            return false;
        }

        function AddAdditionalItem(limit, cur, tooltip) {
            var count = $('.selected_item_unit').length;
            if (count >= limit) {
                alert("Additional items at maximum");
                return false;
            }
            $.template('quesTmpl', '<div class="selected_item_unit"><div class="delete" onclick="DeleteAdditionalItem(this)"></div><input type="text" class="title tipsy-explanation" title="${tooltip}" /> ${cur} <input id="${id}" class="selected_item_content" type="text" onblur="javascript:this.value=formatCurrency(this.id);" /></div>');
            $.tmpl('quesTmpl', { 'id': 'itm_' + count, 'cur': cur, 'tooltip': tooltip }).appendTo('#selected_list');
            return false;
        }

        function DeleteAdditionalItem(self) {
            $(self).parent().remove();
        }

        function DeleteAdditionalItemZA(self) {
            var res = confirm('Are you sure you want to remove this item?'),
                titleElement = $(self).siblings('input[type="text"]');
            
            if (!res) return;

            if (titleElement) {
                var rel = titleElement.attr('rel');
                var chkBoxElement = $('span.ckBoxClass[rel="' + rel + '"] input:checkbox')[0];
                if (chkBoxElement) {
                    chkBoxElement.checked = false;
                }
            }
            $(self).parent().remove();
        }

        function extractValues() {
            var items = $('.selected_item_unit'),
                regex = /\b(txtIsVisiblehidden)(\d+)\b/,
                results = [],
                rels = [];

            if ($('#hid_rels').val()) {
                rels = $('#hid_rels').val().split(',');
            }

            for (var i = items.length; --i >= 0; ) {
                var data = {},
                    titleElement = $(items[i]).children('.title'),
                    contentElement = $(items[i]).children('.selected_item_content'),
                    isValid = true;

                data.visible = true;
                data.rel = titleElement.attr('rel') || rels[i];
                data.title = titleElement.val();
                //data.seq = titleElement.attr('id').match(regex).splice(-1)[0]; // Get the number from id
                data.val = contentElement.val().replace(/,/g, '');

                if (!data.val || isNaN(data.val) || data.val == 0 || !data.title) {
                    alert('Please enter value for all selected items.');
                    return false;
                } else {
                    results.push(data);
                }
            }
            $('#hidden_dynamic_data').val(JSON.stringify(results));
            // alert(JSON.stringify(results));
            return true;
        }

        function AddSupplierPanel(id,nid,pnlId,spId,isBoolOnly) {

            id = '#' + id;
            nid='#'+nid;
            pnlId = '#' + pnlId;
            spId = '#' + spId;


            if (isBoolOnly == 'false') {
                if ($(id).val() == 'true') {
                    $(pnlId).removeClass('hidden').addClass('Visible');
                } else {
                    $(pnlId).removeClass('Visible').addClass('hidden');
                }
            }

            if ($(spId).val() == 'true') {
                $(spId).val('false');
             } else {
                $(spId).val('true');
             }

             $(nid).attr('checked', false);       
            
        }

        function Validate100Percent(id1, lbId) {
            id1 = '#' + id1;
            id1 = id1.substr(0, id1.length - 1);
            var id2 =  id1+'2';
            var id3 = id1+'3';
            var id4 = id1+'4';
            var id5 = id1 + '5';
            id1 = id1 + '1';
            lbId = '#' + lbId;

            var total = parseInt($(id1).val()) + parseInt($(id2).val()) + parseInt($(id3).val()) + parseInt($(id4).val()) + parseInt($(id5).val());

            if (total > 100) {
                $(lbId).removeClass('hidden').addClass('Visible');
            } else {
                $(lbId).removeClass('Visible').addClass('hidden');
            }
        }

        function SetValue() {
            var txtMessageFrom = document.getElementById("txtMessageFrom");
            var txtMessageTo = document.getElementById("txtMessageTo");

            // var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

            if ((reg.test(txtMessageFrom.value) == true) & (reg.test(txtMessageTo.value) == true)) {
                window.open("CCSave.aspx?txtMessageFrom=" + txtMessageFrom.value + "&txtMessageTo=" + txtMessageTo.value);
                document.aspnetForm.submit();
            } else {

                if (reg.test(txtMessageFrom.value) == false) {
                    alert("Please enter a valid email address for 'From' field, eg. example@example.com");
                }

                if (reg.test(txtMessageTo.value) == false) {
                    alert("Please enter a valid email address for 'To' field, eg. example@example.com");
                }
            }
        }

        function openEmail(url) {

//                    mail_str ="mailto:?subject=LMIGroup Business Interruption Cover Calculator";
//            	    mail_str += "&body= %0APlease " + escape("<a href="+url+">click here</a>");
//                    var str1 = "to access a sum insured calculator to assist you accurately calculate your business interruption insurance.%0A";
//                    var str2 = "%0AOnce you have completed the simple process, please email the results page back to me.%0A%0A%0ARegards";
//                    mail_str += str1+str2;
            //            	    location.href = mail_str;


            var mail_str = "mailto:?subject=LMIGroup Business Interruption Cover Calculator";
            mail_str += "&body= %0ABelow is a link to a sum insured calculator to assist you accurately calculate your business interruption insurance.%0A";
            var str2 = "%0A%0AOnce you have completed the simple process, please email the results page back to me.%0A%0A%0ARegards";

            mail_str = mail_str + escape(url) + str2;
            location.href = mail_str;
        }





