$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});


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);
}

