﻿

function SetUniqueRadioButton(nameregex, current, cid, hf) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
    document.getElementById(hf).value = cid;
}


/* menu.js */
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

function mcancelclosetime() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

// open hidden layer

function mopen(sender, id) {
    // cancel close timer
    mcancelclosetime();

    // close old layer
    if (ddmenuitem) {
        ddmenuitem.style.visibility = 'hidden';
    }

    // get new layer and show it
    ddmenuitem = document.getElementById(id);

    var position = $(sender).position();
    $(ddmenuitem).css('left', position.left);
    $(ddmenuitem).css('top', position.top + $(sender).height());
    $(ddmenuitem).css('visibility', 'visible');            
}
// close showed layer

function mclose() {    
    if (ddmenuitem) {        
        ddmenuitem.style.visibility = 'hidden';
    }
}


// cancel close timer
// close layer when click-out
//document.onclick = mclose;

/* dropdown.js */

function at_show_aux(parent, child) {
    var p = document.getElementById(parent);
    var c = document.getElementById(child);

    var height = c.offsetHeight;

    var maxheight = c.max_height;

    if (maxheight > 0 && height > maxheight) {
        c.style.height = maxheight + 'px';
        c.style.overflowX = "hidden";
        c.style.overflowY = "scroll";
        c.style.width = (c.offsetWidth + 10) + 'px';
    }

    var top = (c.at_position == "y") ? p.offsetHeight + 2 : 0;
    var left = (c.at_position == "x") ? p.offsetWidth + 2 : 0;

    for (; p; p = p.offsetParent) {
        top += p.offsetTop;
        left += p.offsetLeft;
    }

    c.style.position = "absolute";
    c.style.top = top + 'px';
    c.style.left = left + 'px';
    c.style.visibility = "visible";

}

// ***** at_show *****

function at_show() {
    var p = document.getElementById(this.at_parent);
    var c = document.getElementById(this.at_child);

    at_show_aux(p.id, c.id);
    clearTimeout(c.at_timeout);
}

// ***** at_hide *****

function at_hide() {
    var c = document.getElementById(this.at_child);    
    c.at_timeout = setTimeout("document.getElementById('" + c.id + "').style.visibility = 'hidden'", 333);
}

// ***** at_click *****

function at_click() {
    var p = document.getElementById(this.at_parent);
    var c = document.getElementById(this.at_child);

    if (c.style.visibility != "visible") {
        at_show_aux(p.id, c.id);
    } else {
        c.style.visibility = "hidden";
    }
    return false;
}

// ***** at_attach *****
// PARAMETERS:
// parent   - id of the parent html element
// child    - id of the child  html element that should be droped down
// showtype - "click" = drop down child html element on mouse click
//            "hover" = drop down child html element on mouse over
// position - "x" = display the child html element to the right
//            "y" = display the child html element below
// cursor   - omit to use default cursor or specify CSS cursor name

function at_attach(parent, child, showtype, position, cursor, maxheight) {
    var p = document.getElementById(parent);
    var c = document.getElementById(child);

    c.max_height = maxheight;
    p.at_parent = p.id;
    c.at_parent = p.id;
    p.at_child = c.id;
    c.at_child = c.id;
    p.at_position = position;
    c.at_position = position;

    c.style.position = "absolute";
    c.style.visibility = "hidden";

    if (cursor !== undefined) {
        p.style.cursor = cursor;
    }

    switch (showtype) {
        case "click":
            p.onclick = at_show;

            c.onmouseover = at_show;
            c.onmouseout = at_hide;
            break;
        case "hover":
            p.onmouseover = at_show;
            p.onmouseout = at_hide;
            c.onmouseover = at_show;
            c.onmouseout = at_hide;
            break;
    }
}

function confirmDifferentCount(newCount, url) {
    var answer = confirm('The list now contains ' + newCount + ' items. Do you want to continue ?')
    if (answer) {
        window.location = url;
    }
}

jQuery.fn.hint = function (blurClass) {
    if (!blurClass) {
        blurClass = 'blur';
    }

    return this.each(function () {
        // get jQuery version of 'this'
        var $input = jQuery(this),

        // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

        function remove() {
            if ($input.val() === title && $input.hasClass(blurClass)) {
                $input.val('').removeClass(blurClass);
            }
        }

        // only apply logic if the element has the attribute
        if (title) {
            // on blur, set value to title attr if text is blank
            $input.blur(function () {
                if (this.value === '') {
                    $input.val(title).addClass(blurClass);
                }
            }).focus(remove).blur(); // now change all inputs to title

            // clear the pre-defined text when form is submitted
            $form.submit(remove);
            $win.unload(remove); // handles Firefox's autocomplete
        }
    });
};

