﻿//requests a page and calls responsePage() automatically when recieved response, giving over the containerId and the followupFunction
//parameters: QueryString-ParameterString
//followupFunction: the function called, after the content has been set in responsePage
function requestPage(url, parameters, containerId, followupFunction) {
    lastRequestedUrl = url;
    if (lastRequestedUrl.indexOf("?") > -1)
        lastRequestedUrl = lastRequestedUrl.substr(0, lastRequestedUrl.indexOf("?"));

    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }
    if (!http_request) {
        alert('Cannot create XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = function() {
        responsePage(containerId, followupFunction);
    }
    http_request.open('GET', url + parameters, true);
    http_request.send(null);
}

//will be called from requestPage
function responsePage(containerId, followupFunction) {
    responseContainer = document.getElementById(containerId);
    if (responseContainer != null) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                var text = http_request.responseText;

                
                
                //we need to remove the form around the content (if not, IE is not happy)
                var firstIndex = text.indexOf("<form");
                if (firstIndex > -1) {
                    firstIndex += text.substr(firstIndex).indexOf(">") + 1;
                    var lastIndex = text.indexOf("</form>") - 1;
                    text = text.substr(firstIndex, lastIndex - firstIndex);
                }
                responseContainer.innerHTML = text;                
                var inlineScript  = null;
                if (text.match("<\s*script.*?>(.*?)<\/\s*?script[^>\w]*?>") != null)
                    inlineScript = text.match("<\s*script.*?>(.*?)<\/\s*?script[^>\w]*?>")[1];                                            
                eval(inlineScript);
                if (followupFunction)
                    followupFunction();
            }
        }
    }
}

function createElement(tag, id, x, y, a, b, bgColor, borderWidth, borderColor, borderStyle)
{   
    var element = document.createElement(tag);
    element.style.position = 'absolute';
    element.style.left = x + 'px';
    element.style.top = y + 'px';
    element.style.width = a + 'px';
    element.style.height = b + 'px';
    element.id = id;
    if (borderColor && borderStyle && borderWidth && borderWidth.toString().indexOf('px') > -1)
        element.style.border = borderStyle + ' ' + borderColor + " " + borderWidth.toString();
    if (borderColor && borderStyle && borderWidth && borderWidth.toString().indexOf('px') == -1)
        element.style.border = borderStyle + ' ' + borderColor + " " + borderWidth.toString() + 'px';
    if (bgColor != null && bgColor != '')
        element.style.backgroundColor = bgColor;
    return element;
}
function createElement(tag, id, cssClass) {
    var element = document.createElement(tag);
    element.id = id;
    element.setAttribute('class', cssClass);
    return element;
}
function copyElement(tag, id, element)
{
    if (!element.style)
        return createElement(tag, id, 0, 0, 0, 0);
        
    var x = element.style.left != null && element.style.left != '' ? parseInt(element.style.left.replace('px', '')) : 0;
    var y = element.style.top != null && element.style.top != '' ? parseInt(element.style.top.replace('px', '')) : 0;
    var a = element.style.width != null && element.style.width != '' ? parseInt(element.style.width.replace('px', '')) : 0;
    var b = element.style.height != null && element.style.height != '' ? parseInt(element.style.height.replace('px', '')) : 0;
    
    if (element.style.border != '') {
        var borderAtts = element.style.border.split(' ');
        return createElement(tag, id, x, y, a, b, element.style.backgroundColor, borderAtts[2], borderAtts[1], borderAtts[0]);
    }
    else
        return createElement(tag, id, x, y, a, b, element.style.backgroundColor);
}

function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}

function getStyle(el,styleProp)
{
	if (el.currentStyle) {
	    if (styleProp.indexOf('-') > 0) {
	        var styleWords = styleProp.split('-');
	        var styleString = styleWords[0]; 
	        for (var i = 1; i < styleWords.length; i++) {
	            var testwd = styleWords[i];
                var firstLetter = testwd.substr(0,1);
                var rest = testwd.substr(1, testwd.length -1)
                styleString += firstLetter.toUpperCase() + rest   
	        }
	    }
		var y = el.currentStyle[styleString];
    }
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);

	return y;
}

function redirectPage(url)
{
    window.location.href = url;
}

function encodeHtml(html) {
    var encodedHtml = html.replace('<', '&lt;');
    encodedHtml += encodedHtml.replace('>', '&rt;');
    return encodedHtml;
}
function decodeHtml(html) {
    return unescape(html);
}

//function encodeHtml (source, display, tabs) {
//    var i, s, ch, peek, line, result,
//		next, endline, push,
//		spaces;

//    // Stash the next character and advance the pointer
//    next = function() {
//        peek = source.charAt(i);
//        i += 1;
//    };

//    // Start a new "line" of output, to be joined later by <br />
//    endline = function() {
//        line = line.join('');
//        if (display) {
//            // If a line starts or ends with a space, it evaporates in html
//            // unless it's an nbsp.
//            line = line.replace(/(^ )|( $)/g, '&nbsp;');
//        }
//        result.push(line);
//        line = [];
//    };

//    // Push a character or its entity onto the current line
//    push = function() {
//        if (ch < ' ' || ch > '~') {
//            line.push('&#' + ch.charCodeAt(0) + ';');
//        } else {
//            line.push(ch);
//        }
//    };

//    // Use only integer part of tabs, and default to 4
//    tabs = (tabs >= 0) ? Math.floor(tabs) : 4;

//    result = [];
//    line = [];

//    i = 0;
//    next();
//    while (i <= source.length) { // less than or equal, because i is always one ahead
//        ch = peek;
//        next();

//        // HTML special chars.
//        switch (ch) {
//            case '<':
//                line.push('&lt;');
//                break;
//            case '>':
//                line.push('&gt;');
//                break;
//            case '&':
//                line.push('&amp;');
//                break;
//            case '"':
//                line.push('&quot;');
//                break;
//            case "'":
//                line.push('&#39;');
//                break;
//            default:
//                // If the output is intended for display,
//                // then end lines on newlines, and replace tabs with spaces.
//                if (display) {
//                    switch (ch) {
//                        case '\r':
//                            // If this \r is the beginning of a \r\n, skip over the \n part.
//                            if (peek === '\n') {
//                                next();
//                            }
//                            endline();
//                            break;
//                        case '\n':
//                            endline();
//                            break;
//                        case '\t':
//                            // expand tabs
//                            spaces = tabs - (line.length % tabs);
//                            for (s = 0; s < spaces; s += 1) {
//                                line.push(' ');
//                            }
//                            break;
//                        default:
//                            // All other characters can be dealt with generically.
//                            push();
//                    }
//                } else {
//                    // If the output is not for display,
//                    // then none of the characters need special treatment.
//                    push();
//                }
//        }
//    }
//    endline();

//    // If you can't beat 'em, join 'em.
//    result = result.join('<br />');

//    if (display) {
//        // Break up contiguous blocks of spaces with non-breaking spaces
//        result = result.replace(/ {2}/g, ' &nbsp;');
//    }

//    // tada!
//    return result;
//}

function getCursorPosition(e) {
    e = e || window.event;
    var cursor = { x: 0, y: 0 };
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX +
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY +
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

function getPosition(obj) {
    var pos = { x: 0, y: 0 };

    do {
        pos.x += obj.offsetLeft;
        pos.y += obj.offsetTop;
    } while (obj = obj.offsetParent);

    return pos;
}

function getDimension(obj) {
    var dim = { width: 0, height: 0 };
    dim.width = obj.offsetWidth;
    dim.height = obj.offsetHeight;
    return dim;
}
