// function validates a form, will be run onSubmit of form
// checks on required and format
function validateForm(elForm) {
    invalidClassName = 'error';
    emailRegex = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/);
    phoneRegex1 = new RegExp(/^\+([0-9]){10,14}$/);
    phoneRegex2 = new RegExp(/^0([0-9]){9}$/);
    zipcodeRegex = new RegExp(/^([0-9]{4})( )?([A-Z]){2}$/);
    numberRegex = new RegExp(/^([0-9])*$/);
    allValid = true;
    // check all inputs
    $A($(elForm).getElementsByTagName('input')).each(function(elInput) {
        elInput = $(elInput);
        curValid = true;
        format = elInput.getAttribute('format');
        isRequired = elInput.getAttribute('required');
        if (isRequired == '1') {
            isRequired = true;
        } else {
            isRequired = false;
        }
        // check required
        if (isRequired) {
            switch (elInput.getAttribute('type')) {
                case 'password':
                case 'text':
                    // check on required
                    if (!elInput.present()) {
                        curValid = false;
                    }
                    break;
                case 'checkbox':
                case 'radio':
                    // check on required
                    cntChecked = 0;
                    $A(document.getElementsByName(elInput.name)).each(function(elInput) {
                        if (elInput.checked) {
                            cntChecked++;
                        }
                    });
                    if (cntChecked == 0) {
                        curValid = false;
                    }
                    break;
            }
        }
        // check format
        if ((elInput.getAttribute('type') == 'text') && (format !== false)) {
            // trim all values
            elInput.value = trim(elInput.getValue());
            if (elInput.getValue() !== '') {
                switch (elInput.getAttribute('format')) {
                    case 'email':
                        curValid = emailRegex.test(elInput.getValue());
                        break;
                    case 'phone':
                        elInput.value = elInput.getValue().replace(/ |\-|/g, '');
                        if (elInput.getValue().substr(0, 2) == '00') {
                            elInput.value = '+' + elInput.getValue().substr(2)
                        }
                        curValid = phoneRegex1.test(elInput.getValue()) || phoneRegex2.test(elInput.getValue());
                        break;
                    case 'zipcode':
                        elInput.value = elInput.getValue().toUpperCase();
                        curValid = zipcodeRegex.test(elInput.getValue());
                        elInput.value = elInput.getValue().replace(/ /g, '');
                        break;
                    case 'number':
                        curValid = numberRegex.test(elInput.getValue());
                        break;
                }
            }
        }
        // CHECKED
        // add the invalid class if so, else remove it if present
        elInput.removeClassName(invalidClassName);
        if (!curValid) {
            elInput.addClassName(invalidClassName);
        }
        // check if anything was invalid
        if (!curValid) allValid = false;
    });
    // check all selects
    $A($(elForm).getElementsByTagName('select')).each(function(elInput) {
        elInput = $(elInput);
        curValid = true;
        isRequired = elInput.getAttribute('required');
        if (isRequired == '1') {
            isRequired = true;
        } else {
            isRequired = false;
        }
        // check required
        if (isRequired) {
            if (elInput.getValue() == '') {
                curValid = false;
            }
        }
        // CHECKED
        // add the invalid class if so, else remove it if persent
        elInput.removeClassName(invalidClassName);
        if (!curValid) {
            elInput.addClassName(invalidClassName);
        }
        // check if anything was invalid
        if (!curValid) allValid = false;
    });
    // check all textarea's
    $A($(elForm).getElementsByTagName('textarea')).each(function(elInput) {
        elInput = $(elInput);
        curValid = true;
        isRequired = elInput.getAttribute('required');
        if (isRequired == '1') {
            isRequired = true;
        } else {
            isRequired = false;
        }
        // check required
        if (isRequired) {
            if (trim(elInput.getValue()) == '') {
                curValid = false;
            }
        }
        // CHECKED
        // add the invalid class if so, else remove it if persent
        elInput.removeClassName(invalidClassName);
        if (!curValid) {
            elInput.addClassName(invalidClassName);
        }
        // check if anything was invalid
        if (!curValid) allValid = false;
    });
    return allValid;
}

// simple form check functie van Rene
function checkForm(form) {
    var ret = true;
    for (var x=0; form.elements[x]; x++ ) {
        if (form.elements[x].value == ""){
            form.elements[x].style.backgroundColor='pink';
            ret = false ;
        }else{
            form.elements[x].style.backgroundColor='white';
        }
    }
    if(ret==false){
        return confirm('U bent de gekleurde vakken vergeten in te vullen. Klopt dit?');
    }
    return ret;
}

// counts and trims an input or textarea to a maxlimit and outputs remaining count into a field
function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) { // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters left' counter
    }
    countfield.value = maxlimit - field.value.length;
}

// check on contactdata
function containsContactData(elTextarea, arrForbiddenWords) {
    elTextarea = $(elTextarea);
    phoneRegex = new RegExp(/^([\d][\D]?){7,}$/);
    emailRegex = new RegExp(/@/);
    if (phoneRegex.test(elTextarea.getValue()) || emailRegex.test(elTextarea.getValue())) {
        return true;
    } else {
        return false;
    }
}

// shows one element in a list of divs, hides the others
function showGroupElement(elElement, strGroup) {
    $A(document.getElementsByTagName('div')).each(function(elDiv) {
        elDiv = $(elDiv);
        if (elDiv.getAttribute('group') == strGroup) {
            if (elDiv.id == $(elElement).id) {
                elDiv.show();
            } else {
                elDiv.hide();
            }
        }
    });
}

/////////////////////////////////////////////
// Image swapping
/////////////////////////////////////////////
function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr;
    for (i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments;
    document.MM_sr=new Array;
    for (i=0;i<(a.length-2);i+=3) {
        if ((x=MM_findObj(a[i]))!=null) {
            document.MM_sr[j++]=x;
            if (!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
        }
    }
}
function MM_findObj(n, d) { //v4.01
    var p,i,x;
    if (!d) d=document;
    if ((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
    }
    if (!(x=d[n])&&d.all) x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for (i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if (!x && d.getElementById) x=d.getElementById(n);
    return x;
}

/////////////////////////////////////////////
// Extending functions
/////////////////////////////////////////////

// string trimming
function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function uncheckAllMultiSelect(multiSelect) {
    var obj = document.getElementById(multiSelect);
    for (var i=0; i < obj.options.length; i++) {
    obj.options[i].selected = false;
    }
}

function uncheckAllRadio(radio) {
    var obj = document.getElementsByName(radio);
    for (var i=0; i < obj.length; i++) {
        obj[i].checked = false;
    }
}

function OpenPopup(mypage,myname,w,h,scroll){
    var win = null;
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',toolbar=no, menubar=no, location=0'
win = window.open(mypage,myname,settings)
}

// shows the dependant inputs/selects/textarea; hides the others
function showDependant(name, selectedValue) {
    // hide & disable all
    $A(document.getElementsByTagName('span')).each(function(Element) {
        Element = $(Element);
        if (Element.getAttribute('id') == name) {
            // disabled inlaying inputs/selects/textareas
            Element.hide();
            $A(Element.getElementsByTagName('input')).each(function(Element) {
                Element = $(Element);
                Element.disable();
            });
            $A(Element.getElementsByTagName('select')).each(function(Element) {
                Element = $(Element);
                Element.disable();
            });
            $A(Element.getElementsByTagName('textarea')).each(function(Element) {
                Element = $(Element);
                Element.disable();
            });
        }
    });
    // show & enable selected
    $$('span.val_'+selectedValue).each(function (Element) {
        Element = $(Element);
        Element.show();
        $A(Element.getElementsByTagName('input')).each(function(Element) {
            Element = $(Element);
            Element.enable();
        });
        $A(Element.getElementsByTagName('select')).each(function(Element) {
            Element = $(Element);
            Element.enable();
        });
        $A(Element.getElementsByTagName('textarea')).each(function(Element) {
            Element = $(Element);
            Element.enable();
        });
    });
}
//-->
