﻿function SaveData(FIDNummer, DisplayIndicator, DisplayIndicatorPosH, DisplayIndicatorPosV) {
    y = '<?xml version="1.0" encoding="UTF-8"?>';
    y = y + '<root>';
    PAGINANRGedaan = false;
    ASSPakketNummerGedaan = false;
    SIDGedaan = false;
    UniekIDGedaan = false;
    GIDGedaan = false;
    FRMIDGedaan = false;
    FRMFormVersieIDGedaan = false;
    SlotFRMFormVersieIDGedaan = false;
    ASSUserPakketIDGedaan = false;
    ASSPakketOutletKortingGedaan = false;
    x = document.getElementsByTagName('input');

    if (FIDNummer == 'Dummy')
        y = y + '<rec id="FromOnLoad" value="yes" />'
        
    for (i = 0; i < x.length; i++) {
    
        xobj = x[i];

        if (xobj.getAttribute('id') != null) {
            id = xobj.getAttribute('id');
            
            if ((ASSPakketNummerGedaan == false && id.indexOf('ASSPakketNummer') > -1) || (PAGINANRGedaan == false && id.indexOf('PAGINANR') > -1) || (UniekIDGedaan == false && id.indexOf('UniekID') > -1) || (SIDGedaan == false && id.indexOf('SID') > -1) || (GIDGedaan == false && id.indexOf('GID') > -1) || (FRMIDGedaan == false && id.indexOf('FRMID') > -1) || (FRMFormVersieIDGedaan == false && id.indexOf('FRMFormVersieID') > -1) || (SlotFRMFormVersieIDGedaan == false && id.indexOf('SlotFRMFormVersieID') > -1) || (ASSUserPakketIDGedaan == false && id.indexOf('ASSUserPakketID') > -1) || (ASSPakketOutletKortingGedaan == false && id.indexOf('ASSPakketOutletKorting') > -1) || id.indexOf(FIDNummer) > -1 || id.indexOf('PAR') > -1) {
                if (id.indexOf('PAGINANR') > -1) PAGINANRGedaan = true;
                if (id.indexOf('ASSPakketNummer') > -1) ASSPakketNummerGedaan = true;
                if (id.indexOf('UniekID') > -1) UniekIDGedaan = true;
                if (id.indexOf('SID') > -1) SIDGedaan = true;
                if (id.indexOf('GID') > -1) GIDGedaan = true;
                if (id.indexOf('FRMID') > -1) FRMIDGedaan = true;
                if (id.indexOf('FRMFormVersieID') > -1) FRMFormVersieIDGedaan = true;
                if (id.indexOf('SlotFRMFormVersieID') > -1) SlotFRMFormVersieIDGedaan = true;
                if (id.indexOf('ASSUserPakketID') > -1) ASSUserPakketIDGedaan = true;
                if (id.indexOf('ASSPakketOutletKorting') > -1) ASSPakketOutletKortingGedaan = true;
                y = y + '<rec';
                y = y + ' id="';
                y = y + id;
                y = y + '"';
                y = y + ' value="';

                if (xobj.getAttribute('type') == 'radio') {
                    if (xobj.checked) {
                        y = y + '1';
                    }
                    else {
                        y = y + '0';
                    }
                }
                else {
                    if (xobj.getAttribute('type') == 'checkbox') {
                        if (xobj.checked) {
                            y = y + '1';
                        }
                        else {
                            y = y + '0';
                        }
                    }
                    else {
                        y = y + SpecialChars(xobj.value);
                    }
                }

                y = y + '"';
                y = y + '/>';
            }
        }
        else {
            alert("input without id detected! Form processing cancelled.");
            return false;
        }
    }
    x = document.getElementsByTagName('select');
    for (i = 0; i < x.length; i++) {
        xobj = x[i];
        id = xobj.getAttribute('id');
        if (id.indexOf(FIDNummer) > -1) {
            y = y + '<rec';
            y = y + ' id="';
            y = y + id;
            y = y + '"';
            y = y + ' value="';
            y = y + SpecialChars(xobj.value);
            y = y + '"';
            y = y + '/>';
        }
    }
    x = document.getElementsByTagName('textarea');
    for (i = 0; i < x.length; i++) {
        xobj = x[i];
        id = xobj.getAttribute('id');
        if (id.indexOf(FIDNummer) > -1) {
            y = y + '<rec';
            y = y + ' id="';
            y = y + id;
            y = y + '"';
            y = y + ' value="';
            y = y + SpecialChars(xobj.value);
            y = y + '"';
            y = y + '/>';
        }
    }
    y = y + '</root>';

    if (DisplayIndicator == 'J') {
        ShowIndicator(DisplayIndicatorPosH, DisplayIndicatorPosV);
    }

    FRMServices.SaveData(y, OnComplete, OnTimeOut, OnError);

    return (true);
}

function SpecialChars(input) {
    lclvalue = input;
    lclvalue2 = lclvalue.replace(/&/g, '&amp;');

    lclvalue = lclvalue2;
    lclvalue2 = lclvalue.replace(/"/g, '&quot;');

    lclvalue = lclvalue2;
    lclvalue2 = lclvalue.replace(/</g, '&lt;');

    lclvalue = lclvalue2;
    lclvalue2 = lclvalue.replace(/>/g, '&gt;');

    lclvalue = lclvalue2;
    lclvalue2 = lclvalue.replace(/'/g, '&apos;');

    return lclvalue2
}

function OnComplete(arg) {
    HideIndicator();

    try //Internet Explorer
        {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(arg);
    }
    catch (e) {
        try //Firefox, Mozilla, Opera, etc.
            {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(arg, "text/xml");
        }
        catch (e) {
            alert(e.message);
            return;
        }
    }

    x = xmlDoc.getElementsByTagName("rec");
    for (i = 0; i < x.length; i++) {
        id = x[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
        instr = x[i].getElementsByTagName('instr')[0].childNodes[0].nodeValue;

        if (instr == 'reload') {
            window.location = x[i].getElementsByTagName('value')[0].childNodes[0].nodeValue + '&Reloaded=J';
        }

        if (instr == 'puterror') {
            if (document.getElementById('FIDFout' + id)) {
               if (document.getElementById('FIDFout' + id) != null) {
                    if (x[i].getElementsByTagName('value')[0].childNodes[0].nodeValue == '[[PlaatsHelemaalNiets]]') {
                        document.getElementById('FIDFout' + id).innerHTML = '';
                    }
                    else {
                        document.getElementById('FIDFout' + id).innerHTML = x[i].getElementsByTagName('value')[0].childNodes[0].nodeValue;
                    }
                    document.getElementById('FIDFout' + id).style.display = '';
                }
            }
        }

        if (instr == 'show' || instr == 'hide' || instr == 'put' || instr == 'putlist' || instr == 'putselected') {
            if (instr == 'show') {
                if (document.getElementById('TRID' + id) != null) {
                    document.getElementById('TRID' + id).style.display = '';
                }
                if (document.getElementById('TRIDD' + id) != null) {
                    document.getElementById('TRIDD' + id).style.display = '';
                }
                if (document.getElementById('TRIDDD' + id) != null) {
                    document.getElementById('TRIDDD' + id).style.display = '';
                }
                if (document.getElementById('TRIDDDD' + id) != null) {
                    document.getElementById('TRIDDDD' + id).style.display = '';
                }
                if (document.getElementById(id) != null) {
                    document.getElementById(id).style.display = '';
                }
                if (document.getElementById('TRGRID' + id) != null) {
                    document.getElementById('TRGRID' + id).style.display = '';
                }
            }
            if (instr == 'hide') {
                if (document.getElementById('TRID' + id) != null) {
                    document.getElementById('TRID' + id).style.display = 'none';
                }
                if (document.getElementById('TRIDD' + id) != null) {
                    document.getElementById('TRIDD' + id).style.display = 'none';
                }
                if (document.getElementById('TRIDDD' + id) != null) {
                    document.getElementById('TRIDDD' + id).style.display = 'none';
                }
                if (document.getElementById('TRIDDDD' + id) != null) {
                    document.getElementById('TRIDDDD' + id).style.display = 'none';
                }
                if (document.getElementById(id) != null) {
                    document.getElementById(id).style.display = 'none';
                }
                if (document.getElementById('TRGRID' + id) != null) {
                    document.getElementById('TRGRID' + id).style.display = 'none';
                }
            }
            if (document.getElementById('FID' + id)) {
                if (document.getElementById('FID' + id) != null) {
                    if (instr == 'put') {

                        if (document.getElementById('FID' + id).type == 'checkbox' || document.getElementById('FID' + id).type == 'radio') {
                            if (x[i].getElementsByTagName('value')[0].childNodes[0].nodeValue == 1)
                                document.getElementById('FID' + id).checked = true;
                            else
                                document.getElementById('FID' + id).checked = false;
                        }
                        else {
                            if (x[i].getElementsByTagName('value')[0].childNodes[0].nodeValue == '[[PlaatsHelemaalNiets]]') {
                                document.getElementById('FID' + id).value = '';
                            }
                            else {
                                document.getElementById('FID' + id).value = x[i].getElementsByTagName('value')[0].childNodes[0].nodeValue;
                            }
                        }
                    }

                    if (instr == 'putselected') {
                        document.getElementById('FID' + id).selectedIndex = x[i].getElementsByTagName('value')[0].childNodes[0].nodeValue;
                    }
                }
                if (instr == 'putlist') {

                    for (j = (document.getElementById('FID' + id).options.length - 1); j >= 0; j--) {
                        document.getElementById('FID' + id).options[j] = null;
                    }
                    listboxstr = x[i].getElementsByTagName('value')[0].childNodes[0].nodeValue;
                    pos = listboxstr.indexOf(";");
                    while (pos > -1) {
                        text = listboxstr.substring(0, pos);
                        listboxstr = listboxstr.substring(pos + 1);
                        pos = listboxstr.indexOf(";");
                        value = listboxstr.substring(0, pos);
                        listboxstr = listboxstr.substring(pos + 1);
                        var o = document.createElement("option");
                        document.getElementById('FID' + id).options.add(o);
                        o.text = text;
                        o.value = value;
                        pos = listboxstr.indexOf(";");
                    }
                    document.getElementById('FID' + id).options[0].selected = true;
                }
            }
        }
        else {
            value = x[i].getElementsByTagName('value')[0].childNodes[0].nodeValue;
        }
    }
}

function OnTimeOut(arg) {
    
}

function OnError(arg) {
    alert('Error has occured: ' + arg._message)
}

function ShowIndicator(DisplayIndicatorPosH, DisplayIndicatorPosV) {
    if (document.getElementById('AjaxLoader') != null) {
        document.getElementById('AjaxLoader').style.display = '';
    }
    if (document.getElementById('AjaxLoaderImageDiv') != null) {
        document.getElementById('AjaxLoaderImageDiv').style.display = '';
    }
}

function HideIndicator() {
    if (document.getElementById('AjaxLoader') != null) {
        document.getElementById('AjaxLoader').style.display = 'none';
    }
    if (document.getElementById('AjaxLoaderImageDiv') != null) {
        document.getElementById('AjaxLoaderImageDiv').style.display = 'none';
    }
}

function SetSpacing() {
    if (document.getElementById('PaginaSpacingBoven') != null) {
        value = document.getElementById('PaginaSpacingBoven').value;
        if (document.getElementById('FormulierBody') != null) {
            document.getElementById('FormulierBody').style.marginTop = value;
        }
    }
    if (document.getElementById('PaginaSpacingOnder') != null) {
        value = document.getElementById('PaginaSpacingOnder').value;
        if (document.getElementById('FormulierBody') != null) {
            document.getElementById('FormulierBody').style.marginBottom = value;
        }
    }
    if (document.getElementById('PaginaSpacingRechts') != null) {
        value = document.getElementById('PaginaSpacingRechts').value;
        if (document.getElementById('FormulierBody') != null) {
            document.getElementById('FormulierBody').style.marginRight = value;
        }
    }
    if (document.getElementById('PaginaSpacingLinks') != null) {
        value = document.getElementById('PaginaSpacingLinks').value;
        if (document.getElementById('FormulierBody') != null) {
            document.getElementById('FormulierBody').style.marginLeft = value;
        }
    }

    if (document.getElementById('PaginaPaddingBoven') != null) {
        value = document.getElementById('PaginaPaddingBoven').value;
        if (document.getElementById('FormulierBody') != null) {
            document.getElementById('FormulierBody').style.paddingTop = value + 'px';
        }
    }
    if (document.getElementById('PaginaPaddingOnder') != null) {
        value = document.getElementById('PaginaPaddingOnder').value;
        if (document.getElementById('FormulierBody') != null) {
            document.getElementById('FormulierBody').style.paddingBottom = value + 'px';
        }
    }
    if (document.getElementById('PaginaPaddingRechts') != null) {
        value = document.getElementById('PaginaPaddingRechts').value;
        if (document.getElementById('FormulierBody') != null) {
            document.getElementById('FormulierBody').style.paddingRight = value + 'px';
        }
    }
    if (document.getElementById('PaginaPaddingLinks') != null) {
        value = document.getElementById('PaginaPaddingLinks').value;
        if (document.getElementById('FormulierBody') != null) {
            document.getElementById('FormulierBody').style.paddingLeft = value + 'px';
        }
    }
}

function changecss(theClass, element, value) {
    //Last Updated on June 23, 2009
    //documentation for this script at
    //http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
    var cssRules;

    var added = false;
    for (var S = 0; S < document.styleSheets.length; S++) {

        if (document.styleSheets[S]['rules']) {
            cssRules = 'rules';
        } else if (document.styleSheets[S]['cssRules']) {
            cssRules = 'cssRules';
        } else {
            //no rules found... browser unknown
        }

        for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
            if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
                if (document.styleSheets[S][cssRules][R].style[element]) {
                    document.styleSheets[S][cssRules][R].style[element] = value;
                    added = true;
                    break;
                }
            }
        }
        if (!added) {
            if (document.styleSheets[S].insertRule) {
                document.styleSheets[S].insertRule(theClass + ' { ' + element + ': ' + value + '; }', document.styleSheets[S][cssRules].length);
            } else if (document.styleSheets[S].addRule) {
                document.styleSheets[S].addRule(theClass, element + ': ' + value + ';');
            }
        }
    }
}
