﻿function toggleVisible(name, visible)
{
    document.getElementById(name).style.display = visible;
}


function popup(url, title, width, height) {

    if(!width){
        width = 400;
    }
    if(!height){
        height = 500;
    }
    if(!title){
        title = '';
    }
    
    var left   = (screen.width  - width)/2;
	var top    = (screen.height - height)/2;
    window.open(url, title, 'height=' + height + ',width=' + width + ',top=' + top + ',left=' + left + ',resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,,status=yes');
}

function popupModal(url, title, width, height) {

    if(!width){
        width = 400;
    }
    if(!height){
        height = 500;
    }
    if(!title){
        title = '';
    }
    return window.showModalDialog(url,title,'dialogWidth:' + width + 'px;dialogHeight:' + height + 'px;center;help:no;Yes;resizable:yes;status:no;scroll:yes');
}

function closePopupRefresh(){
    if(window.opener != null){
        window.opener.location = window.opener.location;
    }
    window.close();
}

function closePopup(){
    window.close();
}

function clearContents(field, fieldValue)
{
    if(!fieldValue){
        fieldValue = "dd-MMM-yyyy";
    }
    if(field.value == fieldValue)
        field.value = "";
}

function createCbRange($all_boxes, $button, action) {

    var range_class = "cb_highlight"

    if ($all_boxes.length == 0 || $button.length == 0) return;

    var shift = false;
    var range_start, range_end;

    function $rangeBoxes() {
        if (!(shift && range_start && range_end)) return $();

        var a = $all_boxes.index(range_start);
        var z = $all_boxes.index(range_end);

        if (a == z) return $(range_start);

        return $all_boxes.slice(Math.min(a, z), Math.max(a, z) + 1);
    }

    function hlRange() {
        var $hlBoxes = $rangeBoxes();
        if ($hlBoxes.length == 1) $hlBoxes = $();

        $all_boxes.not($hlBoxes.addClass(range_class)).removeClass(range_class);
    }

    function setButtonEnabled() {
        $button.attr("disabled", !$all_boxes.is(":checked"));
    }

    function confirmClick() {
        var checkedCount = $all_boxes.filter(":checked").length;
        if (checkedCount == 0) {
            return false;
        } else {
            return confirm('Are you sure you wish to ' + action + ' the selected Quote Request' + (checkedCount > 1 ? 's' : '') + '?');
        }
    }

    $all_boxes.mouseenter(function () {
        range_end = this; hlRange();

    }).mouseleave(function () {
        range_end = null; hlRange();

    }).click(function () {
        if (shift && range_start) {
            range_end = this;
            $rangeBoxes().not(range_end).attr("checked", range_end.checked);
        } else {
            range_start = this;
        }
        setButtonEnabled();
    });

    $(document).on("keyup", function (e) {
        if (e.which == 16) { shift = false; hlRange(); }
    }).on("keydown", function (e) {
        if (e.which == 16) { shift = true; hlRange(); }
    });

    $button.click(confirmClick);

    setButtonEnabled();
}
