
function setPlaceholder(f, q, ph) {
    var f = document.getElementById(f);
    if (!f || !f[q]) {
        return;
    }
    var q = f[q];
    q.onfocus = function() {
        if (q.value == ph) {
            q.value = '';
            q.className = q.className.replace(/\bplaceholder\b/g, '');
        }
    }
    q.onblur = function() {
        if (q.value == '') {
            q.className += ' placeholder';
            q.value = ph;
        }
    }
    f.onsubmit = function() {
        if (q.value == ph) {
            q.value = '';
        }
    }
    q.value = '';
    q.onblur();
}

function checkAllTrigger(name, self)
{
    var ch_list = document.getElementsByName(name);
    var ch_cnt = 0;
    var all_ch = false;
    if (ch_list && ch_list.length > 0) {
        if (self.id == 'checkAllTrigger_'+name) {
            for (var i = 0, j = ch_list.length; i < j; i++) {
                ch_cnt += ch_list[i].checked;
                ch_list[i].checked = self.checked;
            }
            all_ch = self.checked;
        } else {
            for (var i = 0, j = ch_list.length; i < j; i++) {
                ch_cnt += ch_list[i].checked;
            }
            var trigger = document.getElementById('checkAllTrigger_'+name);
            if (trigger) {
                trigger.checked = (ch_cnt == ch_list.length);
            }
            all_ch = trigger.checked;
        }
    }
    return [all_ch, ch_cnt];
}

function setFocus(id)
{
    var el = document.getElementById(id);

    if (el) {
        el.focus();
    } else {
        el = document.getElementsByName(id);
        if (el.length > 0) {
            el[0].focus();            
        }
    }
}


function redirect(url/*, new_win*/)
{
    var new_win = arguments[1] || 0;
    if ( ! new_win) {
        window.location.href = url;
    } else {
        window.open(url);
    }
}


function popup(url, w, h)
{
    var x = parseInt(screen.availWidth / 2 - w / 2);
    var y = parseInt(screen.availHeight / 2 - h / 2) - 50;
    
    win = window.open(url, '', 'toolbar=no,location=no,directories=no,'+
        'status=yes,menubar=no,scrollbars=no,resizable=no,copyhistory=no,'+
        'width='+w+',height='+h+',top='+y+',left='+x);

    win.focus();

    return win;
}


function getKeyNum(ev)
{
    var keynum;
    var is_ctl = arguments[1] || 0;

    if(window.event) {     // IE
        keynum = window.event.keyCode;
    } else if(ev.which) {     // Netscape/Firefox/Opera
        keynum = ev.which;
    }

    if (isNaN(keynum) || (!is_ctl && keynum < 32)) {
        return -1;
    }

    return keynum;
}


function maxLength(field, ev)
{
    var maxChars = field.getAttribute ? 
      parseInt(field.getAttribute('maxlength')) : '';

    var keynum = getKeyNum(ev);

    if (keynum == -1) {
        return true;
    }

    if(field.value.length >= maxChars) {
        return false;
    }
}  


function setValue(id, value)
{
    var el = document.getElementById(id);

    if (el) {
        el.value = value;
    } else {
        el = document.getElementsByName(id);
        if (el.length > 0) {
            el[0].value = value;
        }
    }
}


/*
 * http://www.mredkj.com/tutorials/tutorial005.html
 * ================================================
 * (slightly modified)
 *
 */
function insertOptionBefore(list, text, value)
{
    if (typeof(list) == 'string') {
        list = document.getElementById(list);
    }

    if (list.selectedIndex >= 0) {
        var optNew = document.createElement('option');
        optNew.text = text;
        optNew.value = value;
        var optOld = list.options[list.selectedIndex];  
        try {
            list.add(optNew, optOld); // standards compliant; doesn't work in IE
        }
        catch(ex) {
            list.add(optNew, list.selectedIndex); // IE only
        }
    }
}


function removeOptionSelected(list)
{
    if (typeof(list) == 'string') {
        list = document.getElementById(list);
    }
    var i;
    for (i = list.length - 1; i>=0; i--) {
        if (list.options[i].selected) {
            list.remove(i);
        }
    }
}


function appendOptionLast(list, text, value)
{
    if (typeof(list) == 'string') {
        list = document.getElementById(list);
    }
    var optNew = document.createElement('option');
    optNew.text = text;
    optNew.value = value;

    try {
        list.add(optNew, null); // standards compliant; doesn't work in IE
    }
    catch(ex) {
        list.add(optNew); // IE only
    }
}


function removeOptionLast(list)
{
    if (typeof(list) == 'string') {
        list = document.getElementById(list);
    }
    if (list.length > 0)
    {
        list.remove(list.length - 1);
    }
}


function removeAllOptions(list) 
{ 
    if (typeof(list) == 'string') {
        list = document.getElementById(list);
    }
    list.length = 0;    
}


function selectAllOptions(list) 
{ 
    if (typeof(list) == 'string') {
        list = document.getElementById(list);
    }
    var i;
    for (i = 0; i < list.length; i++) {
        list[i].selected = true;
    }
}

/*
 * http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
 * ========================================================================================
 */
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

/*
 * http://dklab.ru/chicken/nablas/38.html
 * ======================================
 */
function Dump(d,l) {
	if (l == null) l = 1;
	var s = '';
	if (typeof(d) == "object") {
		s += typeof(d) + " {\n";
		for (var k in d) {
			for (var i = 0; i < l; i++) s += "  ";
			s += k + ": " + Dump(d[k],l + 1);
		}
		for (var i = 0; i < l - 1; i++) s += "  ";
		s += "}\n"
	} else {
		s += "" + d + "\n";
	}
	return s;
}

/*
 * from JS Calendar
 * ======================================
 */
function hideShowCovered(el) {		 

	function getAbsolutePos(el) {
		var r = { x: el.offsetLeft, y: el.offsetTop };
		if (el.offsetParent) {
			var tmp = getAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		return r;
	};

	function getStyleProp(obj, style){
		var value = obj.style[style];
		if (!value) {
			if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C
				value = document.defaultView.getComputedStyle(obj, "").getPropertyValue(style);
			} else if (obj.currentStyle) { // IE
				value = obj.currentStyle[style];
			} else {
				value = obj.style[style];
			}
		}
		return value;
	};

	var tags = new Array("applet", "iframe", "select");

	var p = getAbsolutePos(el);
	var EX1 = p.x;
	var EX2 = el.offsetWidth + EX1;
	var EY1 = p.y;
	var EY2 = el.offsetHeight + EY1;

	for (var k = tags.length; k > 0; ) {
		var ar = document.getElementsByTagName(tags[--k]);
		var cc = null;

		for (var i = ar.length; i > 0;) {
			cc = ar[--i];

			p = getAbsolutePos(cc);
			var CX1 = p.x;
			var CX2 = cc.offsetWidth + CX1;
			var CY1 = p.y;
			var CY2 = cc.offsetHeight + CY1;

			if ((el.style.display == 'none') || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
				if (!cc.__msh_save_visibility) {
					cc.__msh_save_visibility = getStyleProp(cc, "visibility");
				}
				cc.style.visibility = cc.__msh_save_visibility;
			} else {
				if (!cc.__msh_save_visibility) {
					cc.__msh_save_visibility = getStyleProp(cc, "visibility");
				}
				cc.style.visibility = "hidden";
			}
		}
	}
}
