/// ajaxy and other common stuff

// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
function getAnchorPosition(anchorname){var useWindow=false;var coordinates=new Object();var x=0,y=0;var use_gebi=false, use_css=false, use_layers=false;if(document.getElementById){use_gebi=true;}else if(document.all){use_css=true;}else if(document.layers){use_layers=true;}if(use_gebi && document.all){x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);}else if(use_gebi){var o=document.getElementById(anchorname);x=AnchorPosition_getPageOffsetLeft(o);y=AnchorPosition_getPageOffsetTop(o);}else if(use_css){x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);}else if(use_layers){var found=0;for(var i=0;i<document.anchors.length;i++){if(document.anchors[i].name==anchorname){found=1;break;}}if(found==0){coordinates.x=0;coordinates.y=0;return coordinates;}x=document.anchors[i].x;y=document.anchors[i].y;}else{coordinates.x=0;coordinates.y=0;return coordinates;}coordinates.x=x;coordinates.y=y;return coordinates;}
function getAnchorWindowPosition(anchorname){var coordinates=getAnchorPosition(anchorname);var x=0;var y=0;if(document.getElementById){if(isNaN(window.screenX)){x=coordinates.x-document.body.scrollLeft+window.screenLeft;y=coordinates.y-document.body.scrollTop+window.screenTop;}else{x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;}}else if(document.all){x=coordinates.x-document.body.scrollLeft+window.screenLeft;y=coordinates.y-document.body.scrollTop+window.screenTop;}else if(document.layers){x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;}coordinates.x=x;coordinates.y=y;return coordinates;}
function AnchorPosition_getPageOffsetLeft(el){var ol=el.offsetLeft;while((el=el.offsetParent) != null){ol += el.offsetLeft;}return ol;}
function AnchorPosition_getWindowOffsetLeft(el){return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;}
function AnchorPosition_getPageOffsetTop(el){var ot=el.offsetTop;while((el=el.offsetParent) != null){ot += el.offsetTop;}return ot;}
function AnchorPosition_getWindowOffsetTop(el){return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;}

// end Matt's stuff.

var isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);

// make sure innerText works!
if (! isIE) {
  HTMLElement.prototype.__defineGetter__("innerText", 
              function () { return(this.textContent); });
  HTMLElement.prototype.__defineSetter__("innerText", 
              function (txt) { this.textContent = txt; });
}

function createrequest(url,meth,send,func)
{
    var request;
    if(window.XMLHttpRequest)
    {
        request = new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    document.getElementById('ajaxrunning').style.display="inline";
    
    
    request.onreadystatechange = function()
    {
        // process the response
        if(request.readyState == 4)
        {
            document.getElementById('ajaxrunning').style.display="none";
            if(request.status==200)
            {
                // received OK
                func(request);
            }
            else
            {
                alert('error : '+request.status);
            }
        }
    }
    
    request.open(meth,url,true);
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.send(send);
}

// get the value of a node in an XML doc, used in Ajax stuff.

function getNodeValue(obj,tag)
{
    if(obj.getElementsByTagName(tag)[0].firstChild)
    {
        return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
    }
    else
    {
        return "";
    }
}


// handle a standard OK/not OK response

function handlesimplereply(req)
{
    // handle the reply
                      
    var x = req.responseXML.getElementsByTagName('reply');
    if(x)
    {
        x = x[0];
        
        if(x && x.getElementsByTagName('error'))
        {
            if(!parseInt(getNodeValue(x,'error')))
            {
                alert("Done!");
                return true;
            }
            else
            {
                alert(getNodeValue(x,'status'));
                return false;
            }
        }
    }
    alert("an error occurred")
    return false
    
}

// handle a standard OK/not OK response

function handlesimplereplynonotify(req)
{
    // handle the reply
                      
    var x = req.responseXML.getElementsByTagName('reply');
    if(x)
    {
        x = x[0];
        if(x && x.getElementsByTagName('error'))
        {
            if(!parseInt(getNodeValue(x,'error')))
            {
                // OK
                return true;
            }
            else
            {
                alert(getNodeValue(x,'status'));
                return false;
            }
        }
    }
    alert("error: invalid response from server")
    return false
}

// remove the contents of (usually) a div or span. Or anything.

function removecontents(d)
{
    while(d.firstChild)
    {
        d.removeChild(d.firstChild);
    }
}

// conceal a div called 'closedfoo' and reveal a div called 'openfoo'

function showForm(f)
{
    document.getElementById('open'+f).style.display = 'block';
    document.getElementById('open'+f).elements[0].focus();
    document.getElementById('closed'+f).style.display = 'none';
}

// reveal a div called 'closedfoo' and conceal a div called 'openfoo'

function hideForm(f)
{
    document.getElementById('open'+f).style.display = 'none';
    document.getElementById('closed'+f).style.display = 'block';
}

// empty an element and replace the contents with some text

function replacediv(id,t)
{
    if(t.length<1)
      t="(field is empty)";
    var d = document.getElementById(id);
    removecontents(d);
    d.appendChild(document.createTextNode(t));
}
    
function dump(obj)
{
    var temp="";
    for (x in obj)
    temp += x + ": " + obj[x] + "\n";
    alert (temp);    
}

// general confirmation functions

function myconfirm()
{
    return confirm("Are you sure?");
}

function areyousure(func)
{
    if(myconfirm())
    {
        func();
    }
}

// Hide a text element, copy its text out into the text element of a form, make that form visible.

function opentextform(idtext,idform)
{
    var t = document.getElementById(idtext);
    var txt = t.innerText; // get text contents
    t.style.display="none"; // hide text
    
    
    
    t = document.getElementById(idform);// get form
    
    var list = t.getElementsByTagName('input');
    
    // find the text input
    for(var i=0;i<list.length;i++)
    {
        var n = list[i];
        if(n.type == 'text')
        {
            // put the text in
            n.value = txt;
        }
    }
    
    t.style.display='inline'; // show form
    t.elements[0].focus();
}

// show the text and hide the form

function closetextform(idtext,idform)
{
    var t = document.getElementById(idtext);
    if(t)
    {
        t.style.display='inline'; // show text
    
        t = document.getElementById(idform);
        t.style.display='none'; // hide form
    }
}

