
function popup(url, width, height) {
    var windowW = screen.width;
    var windowH = screen.height;
    var popupW = width;
    var popupH = height;
    var w = Math.floor((windowW - popupW)/2);
    var h = Math.floor((windowH - popupH)/2);
    day = new Date();
    id = day.getTime();
    window.open(url, id, "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+popupW+",height="+popupH+",left = "+w+",top = "+h);
}

function popupAllFeaturesOn(url, width, height) {
    var windowW = screen.width;
    var windowH = screen.height;
    var popupW = width;
    var popupH = height;
    var w = Math.floor((windowW - popupW)/2);
    var h = Math.floor((windowH - popupH)/2);
    day = new Date();
    id = day.getTime();
    window.open(url, id, "toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width="+popupW+",height="+popupH+",left = "+w+",top = "+h);
}

function overlay(id) {
    var el = document.getElementById("overlay" + id);
    el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
}

function traverseDOM(x, arr) {
    arr.push("<DL><DT>")
    arr.push(x);
    for (var i = 0; i < x.childNodes.length; i++) {
        arr.push("<DD>");
        traverseDOM(x.childNodes[i], arr);
    }
    arr.push("</DL>");
    return arr;
}

function printDOM(x) {
    arr = Array();
    arr = traverseDOM(x, arr);
    for (var i = 0; i < arr.length; i++) {
        document.write(arr[i]);
    }
}

function getSelectHeader(headerTxt, headerWidth) {
    var filler = "&nbsp;";
    var prefixWidth = 2;
    var suffixWidth = headerWidth - headerTxt.length;
    var ret = "";
    
    for (var i = 0; i < prefixWidth; i++) {
        ret += filler;
    }
    
    ret += headerTxt;
    
    for (var i = 0; i < suffixWidth; i++) {
        ret += filler;
    }
    
    return ret;
}

function addHiddenInputToForm(oDocument, oForm, sName, sValue) {
    var hidden1 = oDocument.createElement('input');
    hidden1.setAttribute('type', 'hidden');    
    hidden1.setAttribute('value', sValue);    
    hidden1.setAttribute('name', sName);    
    oForm.appendChild(hidden1);               
}

function removeInputsFromForm(oForm, sNameMatch) {
    for (var i = 0; i < oForm.elements.length; i++) {
        var el = oForm.elements[i];
        if (el.name.indexOf(sNameMatch) != -1) {
            el.parentNode.removeChild(el);
            
            if (i > 0) {
              i--;
            }
        }
    }             
}

function changeInputValues(oDocument, sName, sNewValue) {
    var elements = oDocument.getElementsByName(sName);
    for (i = 0; i < elements.length; i++) {
        elements[i].value = sNewValue;
    }
}

function goToPopup(sUrl) {
    if (sUrl != null && sUrl.length > 0) {
      popupAllFeaturesOn(sUrl, 1024, 768);
    }
}
            
function hasOptions(obj) {
    if (obj != null && obj.options != null) { 
        return true; 
    }

    return false;
}

function resetOptions(oSelect) {
    for (var i = (oSelect.options.length - 1); i >= 0; i--) {
        oSelect.options[i] = null;
    }
}

//* get value between [] in string for example dtos[12].amount as parameter returns 12
//  this is useful function when working with stripes  */
function getIndexOrMapKey(value) {
    var start = 0;
    var end = 0;
    for (i = 0 ; i < value.length; i++) {
        var character = value.charAt(i);
        if (character == '[') {
            start = i;
        } else if (character == ']') {
            end = i;
        } 
    }
    return value.substring(start + 1, end);
}

