function Cookie(path) {

    this.path = path;

    this.set = function(name, value) {
        document.cookie = name + '=' + escape(value) + '; path=' + this.path;
    }

    this.get = function(name) {
        var cookie = document.cookie.split('; ');
        for (var i = 0; i < cookie.length; i++) {
            var crumb = cookie[i].split('=');
            if (name == crumb[0])
            return unescape(crumb[1]);
        }
        return null;
    }

    this.remove = function(name) {
        document.cookie = name + '=null; expires=Fri, 31 Dec 1999 23:59:59 GMT;';
    }

}

function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function writeCookie(name, value, hours, path)
{
  var expire = "";
  var q = name + "=" + escape(value);
  if (hours != null) {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
    q += expire;
  }
  if(path) {
    q += '; path='+path;
  }
  document.cookie = q;
}

function doShowAllObjectsReg(expr) {
    for (var id in document.all) {
        var str = new String(id);
        if (str.search(expr) == 0) {
            doShowHideObjectEx(id, 1);
        }
    }
}

function doHideAllObjectsReg(expr) {
    for (var id in document.all) {
        var str = new String(id);
        if (str.search(expr) == 0) {
            doShowHideObjectEx(id, 0);
        }
    }
}
function doShowHideAllObjectsReg(expr) {
    for (var id in document.all) {
        var str = new String(id);
        if (str.search(expr) == 0) {
//          alert("found " + id);
            doShowHideObject(id);
        }
    }
}

function getById(id) {
    if (document.getElementById) {
        var object = document.getElementById(id);
    } else if (document.all) {
        var object = document.all[id];
    }
    return object;
}

function doShowHidePanel(id) {
    var object = getById(id);
    if (object) {
        var cookie = new Cookie('/');
        var trigger_img = document.images['trigger_' + id];
        if ('none' == object.style.display) {
            object.style.display = 'block';
            trigger_img.src = '/images/btn_expand.gif';
            cookie.set(id, '1', null);
        } else {
            object.style.display = 'none';
            trigger_img.src = '/images/btn_collapse.gif';
            cookie.set(id, '0', null);
        }
    }
}

function doShowHideObject(id) {
    var object = getById(id);
    if (object)
        if ('none' == object.style.display) {
            object.style.display = 'block';
//          alert("block" + id);
        } else {
            object.style.display = 'none';
//          alert("none" + id);
        }
}

function doShowHideObjectEx(id, show) {
    var object = getById(id);
    if (object)
        if (show) {
            object.style.display = 'block';
        } else {
            object.style.display = 'none';
        }
}

function doShowHideObjectCond(optElem, radioGroup, checkValue, show, defaultHide) {
    if (defaultHide == null)
        defaultHide = true;
    var radio = document.frm.elements[radioGroup];
    for (var i=0; i<radio.length; i++)
        if (radio[i].checked) {
            if (radio[i].value==checkValue)
                doShowHideObjectEx(optElem, show);
            else
                doShowHideObjectEx(optElem, !show);
            return;
        }
    if (defaultHide)
        doShowHideObjectEx(optElem, false);
}

function doShowChildren(id) {
    var object = getById(id);
    doShowChildrenHelper(object);
}

function doShowChildrenHelper(object) {
    if (object) {
        if (object.style && 'none' == object.style.display) {
            object.style.display = 'block';
        }
        for (var i = 0; i < object.children.length; i++) {
            doShowChildrenHelper(object.children[i]);
        }
    }
}

function doHideChildren(id) {
    var object = getById(id);
    doHideChildrenHelper(object);
}

function doHideChildrenHelper(object) {
    if (object) {
        if (object.style && 'block' == object.style.display) {
            object.style.display = 'none';
        }
        for (var i = 0; i < object.children.length; i++) {
            doHideChildrenHelper(object.children[i]);
        }
    }
}

function popUpWindow(url, target, width, height, scrolling) {
    var left, top;
    if (typeof(scrolling)=='undefined')
        scrolling = 1;
    if (typeof(screen)=='undefined') {
        left = top = 100;
    } else {
        left = parseInt((screen.width - width) / 3);
        if (left<0) {
            left = 0;
            width = screen.width;
        }
        top = parseInt((screen.height - height) / 3);
        if (top<0) {
            top = 0;
            height = screen.height;
        }
    }
    //alert("width="+width+",height="+height);
    var wnd;
    if (scrolling)
        wnd = window.open(url, target, "width="+width+",height="+height+",scrollbars,left="+left+",top="+top);
    else
        wnd = window.open(url, target, "width="+width+",height="+height+",left="+left+",top="+top);
    wnd.focus();
    return false;
}

function popUpWindowXY(url, target, left, top, width, height) {
    window.open(url, target, "width="+width+",height="+height+",scrollbars,left="+left+",top="+top).focus();
    return false;
}

function confirmEx(msg, url1, url2) {
    if (confirm(msg)) {
        if (url1 != null)
            location.href = url1;
    } else {
        if (url2 != null)
            location.href = url2;
    }
    return false;
}

function xPush(array, val, rem) {
    //alert('xPush: '+val+'='+rem);
    if (rem === null) rem = false;
    for (var i=array.length; i--;) {
        if (!array[i]) {
            delete array[i];
            continue;
        }
        if (!rem && array[i]==val) 
            return;
        if (rem && array[i]==val)
            delete array[i];
    }
    if (!rem)
        array[array.length] = val;
}
function count(array) {
    var cnt = 0;
    for (var i=array.length; i--;) {
        if (array[i]) ++cnt;
    }
    return cnt;
}

function select4compare(id, action) {
    var cookiename = 'aupairs4compare';
    var cnt = 0;
    var value = '';
    if (action=='clear') {
        var checks = document.getElementsByName('compare_aupair');
        for (var i=checks.length; i--;)
            checks[i].checked = false;
    } else {
        value = readCookie(cookiename);
        if (!value) value = '';
        var haveid = value.indexOf(id) != -1;
        var ids = (value.split(/\s+/));
        if (action=='update') {
            var el = getById('compare_aupair_'+id);
            if (el) { 
                el.checked = haveid;
            }
        } else if (action=='set') {
            //var remove = !event.srcElement.checked;
            if (id && typeof id=='object') ids = id;
            if (id) xPush(ids, id, haveid);
        } else if (action=='get') {
            return haveid;
        }
        cnt = count(ids);
        value = ids.join(' ');
    }
    //alert('Cookie: '+cookiename+'='+value);
    if (action=='set' || action=='clear' || action=='updatecount') {
        writeCookie(cookiename, value, 7*24, '/');
        //writeCookie(cookiename, value, 7*24);
        // updating total count
        var el = getById('comparecount');
        if (el)
            el.value = cnt;
    }
}

// el itself is also "parent" here
function getFirstParentByTagName(el, tagname) {
    tagname = String(tagname).toUpperCase();
    while (el && el.tagName.toUpperCase()!=tagname) 
        el = el.parentElement;
    if (!el) return null;
    return el;
}

function showHideElement(el, show) {
    if (!(typeof el == 'object'))
        el = document.getElementById(el);
    if (!el) return null;
    if (show===undefined) 
        show = !!(el.style.display == 'none');
    el.style.display = (show ? 'block' : 'none');
    return el.style.display;
}
function showElement(el) {
    return showHideElement(el, true);
}
function hideElement(el) {
    //alert(el.innerText);
    return showHideElement(el, false);
}
function removeElement(el) {
    //alert(el.innerText);
    el.removeNode(el);
}

function walkColumn(td, action, params) {
    td = getFirstParentByTagName(td, 'TD');
    if (!td) return;
    var tr = getFirstParentByTagName(td, 'TR');
    if (!tr) return;
    var table = getFirstParentByTagName(tr, 'TABLE');
    if (!table) return;
    var index=td.cellIndex;
    var trs = table.rows;
    for (var i=0; i<trs.length; ++i) {
        var curtd = trs[i].cells[index];
        var ret = action(curtd, params, i, index);
        if (ret=='stop') break;
    }
}

function walkRow(td, action, params) {
    var tr = getFirstParentByTagName(td, 'TR');
    if (!tr) return;
    var tds = tr.cells;
    for (var i=0; i<tds.length; ++i) {
        var ret = action(tds[i], params, tr.rowIndex, i);
        if (ret=='stop') break;
    }
}

function hideColumn(td) {
    return walkColumn(td, hideElement);
}

function removeColumn(td) {
    return walkColumn(td, removeElement);
}

function shiftTd(td, dir, i, j) {
    if (!td || !td.swapNode) return false;
    if (dir==1) {
        if (td.nextSibling)
            td.swapNode(td.nextSibling);
    } else if (dir==-1) {
        if (td.previousSibling)
            td.swapNode(td.previousSibling);
    } else {
        alert('shiftTd: Unsupported dir!');
        return false;
    }
    return true;
}

function shiftColumn(td, dir) {
    return walkColumn(td, shiftTd, dir);
}


