// DelArte copyright 2005 - 2010
// Library for Art-o-Matic

org = geturlparam("or");
norgid = org;


function noenter() {
    return !(window.event && window.event.keyCode == 13);
}

function oldclone(myObj) {
    if(typeof(myObj) != 'object') return myObj;
    if(myObj == null) return myObj;

    var myNewObj = new Object();

    for(var i in myObj)
        myNewObj[i] = oldclone(myObj[i]);

    return myNewObj;
}

// this is a better clone
function clone(obj){

    if(obj == null || typeof(obj) != 'object')
        return obj;

    var temp = new obj.constructor(); // changed (twice)

    for(var key in obj)
        temp[key] = clone(obj[key]);
    return temp;
}


function iClone() { }
function iclone(obj) {
    iClone.prototype = obj;
    return new iClone();
}



// OBJECT METHODS

/* this is a clone not a deep copy
 * clone will be linked to its original
 * but you can add new unique props and methods to the clone
 */
Object.prototype.clone = function() {
    iClone.prototype = this;
    return new iClone();
}

// ARRAY FUNCTIONS

Array.prototype.contains = function(obj) {
    var lfound = false;
    for (var s = 0; s < this.length; s++) {
        if (this[s] == obj) {
            lfound = true;
            break;
        }
    }
    return(lfound);
}

Array.prototype.addIfUnique = function(obj) {
    if (!this.contains(obj)) {
        this.push(obj);
    }
}
Array.prototype.swap = function(a, b) {
    var tmp = this[a];
    this[a] = this[b];
    this[b] = tmp;
}


Node.prototype.getChildrenByTagName = function(ctagname) {

    var list = [];
    
    for ( var xitm, i = 0; ( xitm = this.childNodes.item(i) ); i++ )
    {
        if ( xitm.nodeName == ctagname )
            list.push(xitm);
    }
    return(list);
}

Node.prototype.getFirstChildByTagName = function(ctagname) {
    var xfc;

    for ( var xitm, i = 0; ( xitm = this.childNodes.item(i) ); i++ )
    {
        if ( xitm.nodeName == ctagname )
        {
            xfc = xitm;
            break;
        }
    }
    return(xfc);

}

Node.prototype.setClassAttribute = function(classname) {

    this.setAttribute("class", classname);
    // IE support....
    this.setAttribute("className", classname);
}

Node.prototype.setId = function(cidname) {

    this.setAttribute("id", cidname);
}



Node.prototype.getChildText = function(ctagname) {
    var txt = "";
    var xnode = this.getFirstChildByTagName(ctagname);

    if (xnode != undefined) {
        txt = xnode.getText();
    }

    return(txt);
}

Node.prototype.getText = function() {
    var txt = "";

    if (this.hasChildNodes()) {
        txt = this.childNodes[0].nodeValue;
    }
    return(txt);

}
Node.prototype.appendTagChild = function(tag, id, cclass) {
    var child = this.appendChild(document.createElement(tag));
    if (id != undefined) {
        if (id.length > 0) {
            child.setAttribute("id", id);
        }
    }
    if (cclass != undefined) {
        if (cclass.length > 0) {
            child.setClassAttribute(cclass);
        }
    }
    return(child);
}


Node.prototype.appendDivChild = function(id, cclass) {

    var child = this.appendChild(document.createElement("div"));
    if (id.length > 0) {
        child.setAttribute("id", id);
    }
    if (cclass.length > 0) {
        child.setClassAttribute(cclass);
    }
    return(child);
}
Node.prototype.addSelectOption = function(value, name, selvalue) {

    var eopt = this.appendChild(document.createElement("option"));
    eopt.value = value;
    eopt.innerHTML = name;
    if (selvalue == value) { 
        eopt.selected = true;
    }
    return(eopt);
}


/*
Node.prototype.addPopupHelpElement = function(id, helptext) {
    var ehlptxt = document.createElement("div");
    ehlptxt.innerHTML = helptext;
    ehlptxt.setClassAttribute("popup-help");
    ehlptxt.setAttribute("popup-help-" + id);
    eimg.setAttribute("onclick", "this.style.display='none';");

    var eimg = document.createElement("img");
    eimg.setAttribute("src", "generic/images/star.png");
    eimg.setAttribute("onclick", "document.getElementById('popup-help-" + id + "').style.display='block';");
    this.appendChild(ehlptxt);
    this.appendChild(eimg);
}
var et = document.getElementById('de-title');
et.addPopupHelpElement("wow", "Woooooow");
*/


// STRING FUNCTIONS
function xmlescape(cin) {
    var asearches = new Array("'", '"', "<", ">");
    var areplaces = new Array("&apos;", "&quot;", "&lt;", "&gt;");
	
    for (var s = 0; s < asearches.length; s++) {
		
        cin = cin.replaceall(asearches[s], areplaces[s]);
    }
    return(cin);
}

String.prototype.xmlescape = function() {
    var asearches = new Array("'", '"', "<", ">");
    var areplaces = new Array("&apos;", "&quot;", "&lt;", "&gt;");

    var cout = this;
    for (var s = 0; s < asearches.length; s++) {

        cout  = cout.replaceall(asearches[s], areplaces[s]);
    }

    return(cout);

}

String.prototype.replaceall = function(cfind, creplace) {
    var cin = new String(this);
    var cpre;
    var cpost;
    var npos = cin.indexOf(cfind);
    while(npos != -1) {
        cpre = cin.substring(0, npos);
        cpost = cin.substring(npos+cfind.length, cin.length);
        cin = cpre + creplace + cpost;
        npos = cin.indexOf(cfind);
    }
    return(cin);
}

String.prototype.left = function(nsize) {
    var cin = new String(this);
    if (cin.length > nsize) {
        cin = cin.substring(0, nsize);
    }
    return(cin);
}

String.prototype.right = function(nsize) {
    var cin = new String(this);
    if (cin.length > nsize) {
        cin = cin.substring((cin.length - nsize));
    }
    return(cin);
}


String.prototype.leftandmore = function(nsize) {
    var cin = new String(this);
    if (cin.length > nsize - 3) {
        cin = cin.substring(0, nsize) + "...";
    }
    return(cin);
}

String.prototype.paddright = function(nlen, cchar) {
    var newstr = this;
    while (newstr.length < nlen) {
        newstr += cchar;
    }
    return(newstr);
}

String.prototype.paddleft = function(nlen, cchar) {
    var newstr = "";
    while (newstr.length < (nlen - this.length)) {
        newstr += cchar;
    }
    newstr += this;

    return(newstr);
}

String.prototype.formattoprice = function() {

    // default we copy
    var cnew = this;
    
    cnew = cnew.replace(/\.(?=\d{0,2}$)/, ",");  // dot becomes comma at the end
    cnew = cnew.replace(/\,(?!\d{0,2}$)/, ",");  // commas become dots when not at the end
    cnew = cnew.replace(/\,(?=\d{0}$)/, ",00");  // , becomes ,00
    if (cnew.indexOf(",") == cnew.length-2) cnew += "0"; //,d becomes ,d0
    if (cnew.indexOf(",") < 0) cnew += ",00";   // d becomes d,00

    return(cnew);
}

String.prototype.formattimestamp = function() {
    return(this.substring(6, 8) + "-" + this.substring(4, 6) + "-" + this.substring(0, 4) + " " + this.substring(9,11) + ":" + this.substring(11, 13) + ":" + this.substring(13));
}

String.prototype.trim = function() {
    
    return (this.replace(/(^\s+|\s+$)/g,''));
}

String.prototype.containsDigitsOnly = function() {

    var patt=new RegExp(/^\d+$/);

    return (patt.test(this));
}
/**
 * we accept dates like 1-1-1900, 01-01-1900, 01-1-1900, 1-01-1900
 * 1-1-10 becomes 1-1-2010.
 */
String.prototype.toDate = function() {

    var ures;

    // only allow digits
    var re = /^\d{1,2}\-\d{1,2}\-(\d{2}|\d{4})$/;

    // only contain digits in the right format
    if (this.match(re)) {

        var sep1 = this.indexOf("-");
        var day = this.substring(0, sep1);
        var sep2 = this.indexOf("-", sep1 + 1);
        var month = this.substring(sep1 + 1, sep2);
        var year = this.substring(sep2 + 1);

        month--;

        // add current century if ommitted
        if (year.length == 2) {
            year = "20" + year;
        }

        var dt = new Date(year, month, day);

        if ((day == dt.getDate()) && (month == dt.getMonth()) && (year == dt.getFullYear()))
            ures = dt;
    }

    return(ures);
}


String.prototype.toxmlElement = function(ctagname, cattribs) {
    var out = "";
    if (cattribs.length + this.length > 0 ) {
        out = "<" + ctagname + cattribs + ">";
        out += this;
        out += "</" + ctagname + ">";
    }
    return(out);
}

String.prototype.toxmlAttribute = function(cattrname) {
    var out = "";
    if (this.length > 0 )
        out = " " + cattrname + "=\"" + this.xmlescape() + "\"";
    return (out);
}


String.prototype.toboolean = function() {
    var lbool = false;
    if (this == "Y") {
        lbool = true;
    }
    return(lbool);
}

String.prototype.striphtml = function() {
    return(this.replace(/(<([^>]+)>)/ig,""));
}

String.prototype.jw_addUrlParam = function(cparam, cvalue) {

    var out = this;
    var csep = "?";
    // set right separator
    if (this.indexOf(csep) != -1) csep = "&";

    // only add if not yet there
    if (this.indexOf(cparam + "=") == -1) {
        out += csep + cparam + "=" + cvalue;
    }
    return(out);
}



Boolean.prototype.tourlpar = function(cpar) {

    if (this == true) {
        cpar += "=1";
    } else {
        cpar += "=0";
    }
    return(cpar);
}
Boolean.prototype.toyn = function() {
    var str;

    if (this == true) {
        str = "Y";
    } else {
        str = "N";
    }
    return(str);
}


// URL functions

function geturlparam(cpname){
    var cret = "";
    var aparams = window.location.search.split("&");
	
    for (var i=0; i < aparams.length; i++) {
        var p0 = aparams[i];
        if (p0.indexOf(cpname + "=") > -1 ) {
            var afnd = p0.split("=");
            cret=afnd[1];
        }
    }
    return(cret);
}

function makeurlparam(cname, cvalue) {
    return("&" + cname + "=" + encodeURIComponent(cvalue));
}

function gup( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

// DATE FUNCTIONS

function clindate() {
    this.err = "";
    this.isgood = true;
    this.resultdate = new Date;
	
    this.set = set;
    this.setdatefromindex = setdatefromindex;
	
	
    // 20060408 month stars with 1
    function setdatefromindex(cindexdate) {
        var nyear = cindexdate.substring(0, 4);
        var nmonth= cindexdate.substring(4, 6);
        nmonth--;
        var nday = cindexdate.substring(6, 8);
        this.resultdate.setFullYear(nyear, nmonth, nday );
        return(this.resultdate);
    }
	
	
    function set(cdate) {
        var nyear;
        var nmonth;
        var nday;
        var npos = cdate.indexOf("-");
        var npos2;
		
        if(npos > -1) {
            nday = cdate.substring(0, npos);
            npos2 = cdate.indexOf("-", npos + 1);
            nmonth = cdate.substring(npos + 1, npos2);
            nyear = cdate.substring(npos2 + 1, cdate.length);
			
        } else {
            nday = cdate.substring(0,2);
            nmonth= cdate.substring(2,4);
            nyear = cdate.substring(4,8);
        }
		
        nday = nday * 1;
        nmonth = (nmonth * 1) -1;
        nyear = nyear * 1;
        if (nday < 1 || nday > 31 || nmonth < -1 || nmonth > 11 || nyear < 2000 || nyear > 2100) {
            this.isgood = false;
            this.err = "Ongeldige datum";
        }
		
        if (this.isgood) {
            this.resultdate.setFullYear(nyear, nmonth, nday );
			
            if (this.resultdate.getFullYear() != nyear || this.resultdate.getMonth() != nmonth || this.resultdate.getDate() != nday)
                this.isgood = false;
            this.err = "Ongeldige datum";
        }
		
        return(this.resultdate);
    }
}

// obsolete!

function checkdate(ofld) {
    var ind = new clindate();
    ind.set(ofld.value);
    if (!ind.isgood) alert(ind.err);
    if (ind.isgood) ofld.value = ind.resultdate.getFmDate();
}

Date.prototype.getFmDate = function() {
    return(this.getDate() + "-" + (this.getMonth() + 1) + "-" + this.getFullYear());
}

Date.prototype.isvalidDate = function(day, month, year) {

    var chkdt = new Date(year, month, day);

    var lvalid = true;

    if (!((day == chkdt.getDate()) && (month == chkdt.getMonth()) && (year == chkdt.getFullYear())))
        lvalid = false;

    return (lvalid);
    
}




Date.prototype.getindexDate = function() {
    var cday = this.getDate() + "";
    if (cday.length == 1) cday = "0" + cday;
    var nmonth = (this.getMonth() + 1);
    var cmonth = nmonth + "";
    if (cmonth.length == 1) cmonth = "0" + cmonth;
    return(this.getFullYear() + cmonth + cday);
}

// 20060408 month stars with 1
Date.prototype.setindexDate = function(cindexdate) {

    var nyear = cindexdate.substring(0, 4);
    var nmonth= cindexdate.substring(4, 6);
    nmonth--;
    var nday = cindexdate.substring(6, 8);
    this.setFullYear(nyear, nmonth, nday );
    this.setHours(0, 0, 0, 0);

}

Date.prototype.clean = function() {
    this.setHours(0, 0, 0, 0);
}



Date.prototype.readFromField = function(ofld) {
    // new properties
    this.isgood = true;
    this.err = "";


    var cdate = ofld.value;

    if (cdate.length > 0) {
        var nyear;
        var nmonth;
        var nday;
        var npos = cdate.indexOf("-");
        var npos2;

        if(npos > -1) {
            nday = cdate.substring(0, npos);
            npos2 = cdate.indexOf("-", npos + 1);
            nmonth = cdate.substring(npos + 1, npos2);
            nyear = cdate.substring(npos2 + 1, cdate.length);

        } else {
            nday = cdate.substring(0,2);
            nmonth= cdate.substring(2,4);
            nyear = cdate.substring(4,8);
        }

        nday = nday * 1;
        nmonth = (nmonth * 1) -1;
        nyear = nyear * 1;
        if (nday < 1 || nday > 31 || nmonth < -1 || nmonth > 11 || nyear < 2000 || nyear > 2100) {
            this.isgood = false;
            this.err = "Ongeldige datum";
        }

        if (this.isgood) {
            this.setFullYear(nyear, nmonth, nday );

            if (this.getFullYear() != nyear || this.getMonth() != nmonth || this.getDate() != nday) {
                this.isgood = false;
                this.err = "Ongeldige datum";
            }
        }

        if (!this.isgood) {
            // show the error
            alert(this.err);
        } else {
            // put the formatted date in the field
            ofld.value = this.getFmDate();
        }
    } else {
        this.isgood = false;
    }
    return(this.isgood);
}



function isvalidforurl(ldot) {
    var lvalid = true;
	
    var cgood = "0123456789_-qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    if (ldot) cgood += ".";
	
    var nlen = this.length;
	
    if (nlen > 40 || nlen == 0) lvalid = false;
	
    if (lvalid) {
        for( var i = 0; i < nlen; i++) {
            var cchar = this.charAt(i);
            if (cgood.indexOf(cchar) < 0) lvalid = false;
        }
    }
    return (lvalid);
}

String.prototype.isvalidforurl = isvalidforurl;


function dodeletion(cdeleteurl) {

    if (document.getElementById('chconfirm').checked) {
        go('confirmdelete', cdeleteurl);
    }
    else {
        alert("Vink het vakje aan bij 'ik weet het zeker'");
    }
}

function confirmandgo(cconfirmid, caction, curl) {
alert(1);
    if (document.getElementById(cconfirmid).checked) {
        go(caction, curl);
    }
    else {
        alert("Vink het vakje aan bij 'ik weet het zeker'");
    }
}


function toggle(celemid) {
    var e = document.getElementById(celemid);
    if (e.style.display == 'none') {
        e.style.display = 'block';
    } else {
        e.style.display = 'none';
    }
}


function appenddivchild(elem, classvalue) {
    var child = elem.appendChild(document.createElement("div"));
    setclassattribute(child, classvalue);
    return(child);
}

function setclassattribute(elem, value) {
    elem.setAttribute("class", value);
    elem.setAttribute("className", value);
}

// universal standard ajax object
// http://www.hunlock.com/blogs/The_Ultimate_Ajax_Object

function ajaxObject(url, callbackFunction) {
    var that=this;
    this.updating = false;
    this.abort = function() {
        if (that.updating) {
            that.updating=false;
            that.AJAX.abort();
            that.AJAX=null;
        }
    }
    this.update = function(passData,postMethod, luserandomparam) {
        if (that.updating) {
            return false;
        }
        that.AJAX = null;
        if (window.XMLHttpRequest) {
            that.AJAX=new XMLHttpRequest();
        } else {
            that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (that.AJAX==null) {
            return false;
        } else {
            that.AJAX.onreadystatechange = function() {
                if (that.AJAX.readyState==4) {
                    that.updating=false;
                    that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
                    that.AJAX=null;
                }
            }
            that.updating = new Date();
            if (/post/i.test(postMethod)) {
                var uri=urlCall;
                if (luserandomparam) {
                    uri += '?'+that.updating.getTime();
                }
                that.AJAX.open("POST", uri, true);
                //       that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
                //         that.AJAX.setRequestHeader("Content-type", "text/html;charset=UTF-8");
                that.AJAX.setRequestHeader("Content-Length", passData.length);
                that.AJAX.send(passData);
            } else {
                var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());
                that.AJAX.open("GET", uri, true);
                that.AJAX.send(null);
            }
            return true;
        }
    }
    var urlCall = url;
    this.callback = callbackFunction || function () { };
}


function handleErrorTalkBack(cmsg,curl,nline) {
    var cp = makeurlparam("msg", cmsg);
    cp += makeurlparam("url", curl);
    cp += makeurlparam("line", nline);

    var request = new ajaxObject("jstalkback.jsv", processjstalkback);
    request.update('or=' + norgid  + cp, 'POST', true);
    return true;
}

function processjstalkback() {
    alert("Er is een fout opgetreden.\nLet op of de laatste wijzigingen wel correct zijn doorgevoerd.\n");
}


function getxmlname(xelem) {
    var c1 = getnodevalue(xelem, "firstname");
    var c2 = getnodevalue(xelem, "middlename");
    var c3 = getnodevalue(xelem, "familyname");

    var cname = c1;
    if (cname.length > 0 && c2.length > 0) cname += " ";
    cname += c2;
    if (cname.length > 0 && c3.length > 0) cname += " ";
    cname += c3;


    return(cname);
}

function getnodevalue(xbase, cnodename) {
    var val = "";
    if (xbase.getElementsByTagName(cnodename).length > 0) {
        if (xbase.getElementsByTagName(cnodename)[0].hasChildNodes()) {
            val = xbase.getElementsByTagName(cnodename)[0].childNodes[0].nodeValue;
        }
    }

    return(val);
}

function alertoldIE() {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
        var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
        if (ieversion <8) {
            alert("Je gebruikt een oude versie van Internet Explorer.\n Voor het beheerdeel van de website is versie 8 of hoger vereist. \nInstalleer een nieuwe versie van Internet Explorer of kies een andere browser.");
        }
    }

}

function cltabcontrols(cbasename, selstyle, unselstyle, ntabs) {
    this.basename = cbasename;
    this.selstyle = selstyle;
    this.unselstyle = unselstyle;
    this.tabs = ntabs;

    cltabcontrols.prototype.activate = function(nact) {

        for (var i = 0; i < ntabs; i++) {
            document.getElementById(cbasename + "-area-" + i).style.display = "none";
            document.getElementById(cbasename + "-control-" + i).setClassAttribute(unselstyle);
        }
        document.getElementById(cbasename + "-area-" + nact).style.display = "block";
        document.getElementById(cbasename + "-control-" + nact).setClassAttribute(selstyle);
    }
}

function zinsertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
        myField.focus();
        var sel = document.selection.createRange();
        sel.text = myValue;
    }

    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
        + myValue
        + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}
/*
 * this is a simple xml writer, to be used in ajax communication
 */

function xmlDocument(ctag) {
    this.root = new xmlElement(ctag);

    xmlDocument.prototype.write = function(linclempty) {
        var str = "";
        str += this.root.write(linclempty);
        return(str);
    }
}

function xmlElement(ctag) {
    this.tag = ctag;
    this.children = new Array();
    this.attributes = new Array();
    this.content = "";

    xmlElement.prototype.addAttribute = function(cname, cvalue) {
        var xattr = new xmlAttribute(cname, cvalue);
        this.attributes.push(xattr);
        return(xattr);
    }
    xmlElement.prototype.createChild = function(ctag) {
        var xnode = new xmlElement(ctag);
        this.children.push(xnode);
        return(xnode);
    }

    xmlElement.prototype.addChild = function(xnode) {
        this.children.push(xnode);
        return(xnode);
    }


    xmlElement.prototype.setContent = function(ccontent) {
        this.content = "<![CDATA[" + ccontent + "]]>";
        return(this.content);
    }

    xmlElement.prototype.write = function(linclempty) {
        var str = "";

        if (linclempty || this.children.length > 0 || this.attributes.length > 0 || this.content.length > 0) {
            str += "<" + this.tag;

            // include attributes
            for (var a = 0; a < this.attributes.length; a++) {
                str += this.attributes[a].write(linclempty);
            }

            str += ">";


            // include children
            for (var i = 0; i < this.children.length; i++) {
                str += this.children[i].write(linclempty);
            }

            str += this.content;
            str += "</" + this.tag + ">";
        }
        return(str);
    }
}
/*
 * attriubute values are being escaped but no cdata has been applied
 *
 */

function xmlAttribute(cname, cvalue) {
    this.name = cname;
    this.value = (cvalue + "").xmlescape();

    xmlAttribute.prototype.write = function(linclempty) {
        var str = "";

        if (this.value.length > 0 || linclempty)
            str += " " + this.name + "=" + "\"" + this.value + "\"";

        return(str);

    }
}

// not tested yet
function syncthreads(callbackfunction) {
    this.callbackfunction = callbackfunction;
    this.wait = true;
    this.mythreads = new Array();

    syncthreads.prototype.register = function(obj) {
        this.mythreads.push(obj);
    }
    syncthreads.prototype.readyforexecution = function() {
        this.wait = false;
    }

    syncthreads.prototype.callready = function(obj) {

        for (var ni = 0 in this.mythreads) {
            if (this.mythreads[ni] == obj) {
                this.mythreads.splice(ni, 1);
                break;
            }
        }

        if (this.mythreads.length == 0 && !this.wait) {
            alert("execute");
            this.callbackfunction || function () { };
        }

    }

}








