﻿function registerSafeLoad(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function insertAfter(newElement, targetElement) {
    var parent = targetElement.parentNode;
    if (parent.lastChild == targetElement) {
        parent.appendChild(newElement);
    } else {
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}

function wait(milliseconds) {
    var now = new Date();
    var exitTime = now.getTime() + milliseconds;

    while(true)
    {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}

function collectionToArray(collection) 
{ 
    var array = []; 
    for(var i=0, len = collection.length; i < len; i++) 
    { 
        array.push(collection[i]); 
    } 
    
    return array; 
}