﻿$.ajaxSetup({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    timeout: 5000
});



function reorderOnLoad() {
    var sE = document.getElementById('sorteerbareElementen');
    var titelDiv = document.getElementById('titelDiv');
    var voorlettersDiv = document.getElementById('voorlettersDiv');
    var tussenvoegselDiv = document.getElementById('tussenvoegselDiv');
    var achternaamDiv = document.getElementById('achternaamDiv');
    var emailDiv = document.getElementById('emailDiv');
    var emailherhaalDiv = document.getElementById('emailherhaalDiv');
    var adressBlockDiv = document.getElementById('adressBlockDiv');
    var telefoon1Div = document.getElementById('telefoon1Div');
    var telefoon2Div = document.getElementById('telefoon2Div');
    var geboortedatumDiv = document.getElementById('geboortedatumDiv');
    var machtingBlockDiv = document.getElementById('machtigingBlokDiv');
    var loginBlokDiv = document.getElementById('loginBlokDiv');
    var organisatieDiv = document.getElementById('organisatieDiv');   
    var extra1Div = document.getElementById('extra1Div');
    var extra2Div = document.getElementById('extra2Div');
    var extra3Div = document.getElementById('extra3Div');
    var extra4Div = document.getElementById('extra4Div');

    if (sE != null) {
        var sorted_array = [titelDiv, voorlettersDiv, tussenvoegselDiv, achternaamDiv, emailDiv, emailherhaalDiv, adressBlockDiv, telefoon1Div, telefoon2Div, geboortedatumDiv, machtingBlockDiv, loginBlokDiv, organisatieDiv, extra1Div, extra2Div, extra3Div, extra4Div].bubble_sort();

        for (var i = 0; i < sorted_array.length; i++) {
            //alert(sorted_array[i].id + ' | ' + sorted_array[i].order);                
            sE.appendChild(sorted_array[i]);
        }
    }
    //sorted_array = [1, 4, 7, 2, 1, 3, 2, 1, 4, 2, 3, 2, 1].bubble_sort();
    //alert(sorted_array);      
}

Array.prototype.bubble_sort = function() {
    var i, j;
    var newarray = this.slice(0);
    var swap = function(j, k) {
        var temp = newarray[j];
        newarray[j] = newarray[k];
        newarray[k] = temp;
        return (true);
    }
    var swapped = false;
    for (i = 1; i < newarray.length; i++) {
        try {
            for (j = 0; j < newarray.length - i; j++) {
                if (parseInt(newarray[j + 1].getAttribute('order')) < parseInt(newarray[j].getAttribute('order'))) {
                    swapped = swap(j, j + 1);
                }
            }
        }
        catch (exception) { }

        if (!swapped) break;
    }
    return (newarray)
}

function reorderPostcode(landen, valid) 
{      
    if (valid != "False") {
        var huisnummerDiv = document.getElementById('huisnummerDiv');
        var huisnummertoevoegingDiv = document.getElementById('huisnummertoevoegingDiv');
        var postcodeDiv = document.getElementById('postcodeDiv');
        var straatnaamDiv = document.getElementById('straatnaamDiv');
        var landDD = document.getElementById('landDD');

        var land = landDD.value;
        land = land.toLowerCase();

        if (landen.indexOf("|" + land + "|") > -1) {
            postcodeDiv.parentNode.insertBefore(postcodeDiv, straatnaamDiv);
            huisnummerDiv.parentNode.insertBefore(huisnummerDiv, straatnaamDiv);
            huisnummertoevoegingDiv.parentNode.insertBefore(huisnummertoevoegingDiv, straatnaamDiv);
        }
        else {
            straatnaamDiv.parentNode.insertBefore(straatnaamDiv, huisnummerDiv);
        }
    }
}

function DisplayLoadingImg(value) {
    document.getElementById(value).style.display = '';
}


function open_popup(popupid, width, height) {
    window.open('popup.aspx?popupid=' + popupid, '', 'top=200,left=200,width=' + width + ',height=' + height + ',scrollbars=no,toolbar=no,location=no');
}

// webservice postcode check - begin
var busySearching = false;
var lastPostcodeVal = '';
var lastHuisnummerVal = '';

function CheckPostcode(postcode, huisnummer, landen, RegexPostcode, RegexHuisnummer) {
    
    var valid = false;
    if (postcode.search(RegexPostcode) == -1)
        valid = false;
    else
        valid = true;

    if (huisnummer.search(RegexHuisnummer) == -1)
        valid = false;
    else
        valid = true;

    if (valid) {
        var land = document.getElementById('landDD');
        land = land.value;
        land = land.toLowerCase();

        if (landen.indexOf("|" + land + "|") > -1) {
            if (lastPostcodeVal != postcode || lastHuisnummerVal != huisnummer) {
                busySearching = false;
                lastPostcodeVal = postcode;
                lastHuisnummerVal = huisnummer;

                straatTxt = document.getElementById('straatnaamTxt');
                plaatsTxt = document.getElementById('woonplaatsTxt');

                straatTxt.value = '';
                plaatsTxt.value = '';

                if (postcode.trim().length > 0 && huisnummer.trim().length > 0) {
                    if (!busySearching) {
                        FillWithDots('straatnaamTxt');
                        FillWithDots('woonplaatsTxt');
                    }

                    $.ajax({
                    url: "InputChecks.asmx/DoPostCodeCheck",
                        data: "{'postcode': '" + postcode + "', 'huisnummer': '" + huisnummer + "'}",
                        success: function(msg) {
                            var results = msg.d;
                            OnCheckPostcodeComplete(results);
                        },
                        error: function(xhr, desc, exceptionobj) {
                            ShowError(straatTxt);
                            ShowError(plaatsTxt);
                        }
                    });

                }
            }
        }
    }
}

function OnCheckPostcodeComplete(results) {
    if (results.trim().replace('|', '').length > 0)
        FillAddress(results.split('|')[0], results.split('|')[1]);
    else
        FillAddress('', '');

    busySearching = false;
}

function FillAddress(street, city) {

    straatTxt = document.getElementById('straatnaamTxt');
    plaatsTxt = document.getElementById('woonplaatsTxt');

    if (street.length == 0) 
    {
        if (straatTxt.value.trim().length == 0 || plaatsTxt.value.trim().length == 0) 
        {
            straatTxt.value = 'niet gevonden';
            plaatsTxt.value = 'niet gevonden';          
        }
        else 
        {
            straatTxt.value = 'niet gevonden';
            plaatsTxt.value = 'niet gevonden';        
        }
    }
    else 
    {    
        straatTxt.value = street;
        plaatsTxt.value = city;

        SetValidBorderColor('straatnaamTxt', true);
        SetValidBorderColor('woonplaatsTxt', true); 
    }
}

function ShowError(element) {
    element.value = '';
}

function FillWithDots(element) {
    busySearching = true;
    var objelement = document.getElementById(element)
    if (objelement.value.replace(/\./g, '').length == 0) {
        if (objelement.value.length <= 40) {
            objelement.value += '.';
            setTimeout('FillWithDots(\'' + element + '\')', 400);
        }
        else {
            busySearching = false;
        }
    }
}
// webservice postcode check - end

// webservice username check - begin
function CheckUsername(username, Regex) 
{  
    if (username.search(Regex) == -1) 
    {       
        SetValidBorderColor('gebruikersnaamTxt', false);
        ShowDiv('gebruikersnaamMessage1Div', false);
    }
    else 
    {        
        $.ajax({
            url: "InputChecks.asmx/DoCheckUsername",
            data: "{'username': '" + username + "'}",
            success: function(msg) {
                var results = msg.d;
                OnCheckUsernameComplete(results);
            },
            error: function(xhr, desc, exceptionobj) {
                ShowError(gebruikersnaamTxt);
            }
        });
    }
}

function OnCheckUsernameComplete(results) 
{
    if (results == 'reedsBezet') 
    {
        SetValidBorderColor('gebruikersnaamTxt', false);
        SetHiddenFieldValue('UsernameValidHf', false);
        ShowDiv('gebruikersnaamMessage1Div', true);
    }
    else 
    {
        SetValidBorderColor('gebruikersnaamTxt', true);
        SetHiddenFieldValue('UsernameValidHf', true);
        ShowDiv('gebruikersnaamMessage1Div', false);
    }
}

function OnCheckUsernameTimeout(results) {
    //alert(results);
}
// webservice username check - end


// webservice username check - begin
function CheckUsernameChange(username, Regex) 
{
    if (username.search(Regex) == -1) 
    {
        SetValidBorderColor('wijzigGebruikersnaamTxt', false);
        ShowDiv('gebruikersnaamMessage2Div', false);       
    }
    else 
    {
        $.ajax({
            url: "InputChecks.asmx/DoCheckUsername",
            data: "{'username': '" + username + "'}",
            success: function(msg) {
                var results = msg.d;
                OnCheckUsernameChangeComplete(results);
            },
            error: function(xhr, desc, exceptionobj) {
                ShowError(gebruikersnaamTxt);
            }
        });
    }
}

function OnCheckUsernameChangeComplete(results) {
    if (results == 'reedsBezet') {
        SetValidBorderColor('wijzigGebruikersnaamTxt', false);
        SetHiddenFieldValue('UsernameValidHf', false);
        ShowDiv('gebruikersnaamMessage2Div', true);
    }
    else {
        SetValidBorderColor('wijzigGebruikersnaamTxt', true);
        SetHiddenFieldValue('UsernameValidHf', true);
        ShowDiv('gebruikersnaamMessage2Div', false);
    }
}

function OnCheckUsernameChangeTimeout(results) {
    //alert(results);
}
// webservice username check - end


// webservice date check - begin
function CheckDate(day, month, year) {

    if (day != '' && month != '' && year.length == 4) 
    {
        var date = year + '/' + month + '/' + day;

        $.ajax({
            url: "InputChecks.asmx/DoCheckDate",
            data: "{'date': '" + date + "'}",           
            success: function(msg) {
            var valid = msg.d;               
                OnCheckDateComplete(valid);
            },
            error: function(xhr, desc, exceptionobj) {
                //ShowError(gebruikersnaamTxt);
            }
        });
    }
}

function OnCheckDateComplete(valid) 
{
    if (valid) {
        SetValidBorderColor('datumDagTxt', true);
        SetValidBorderColor('datumMaandTxt', true);
        SetValidBorderColor('datumJaarTxt', true);
        SetHiddenFieldValue('DateValidHf', true); 
    }
    else {
        SetValidBorderColor('datumDagTxt', false);
        SetValidBorderColor('datumMaandTxt', false);
        SetValidBorderColor('datumJaarTxt', false);
        SetHiddenFieldValue('DateValidHf', false);      
    }
}
// webservice date check - end


function SetValidBorderColor(id, valid) 
{
    element = document.getElementById(id);

    if (!valid) {
        with (element.style) {
            borderWidth = 1;
            borderStyle = 'solid';
            borderColor = 'red';
        }
    }
    else {
        with (element.style) {
            borderWidth = 1;
            borderStyle = 'solid';
            borderColor = 'silver';
        }
    }
}

function SetHiddenFieldValue(id, valid) {
    element = document.getElementById(id);
    if (valid)
        element.value = 'true';
    else
        element.value = 'false';
}

function ShowDiv(id, display) {
    if (display)
        document.getElementById(id).style.display = '';
    else
        document.getElementById(id).style.display = 'none';
}

function CheckElement(value, id, Regex) {
    if (value.search(Regex) == -1)
        SetValidBorderColor(id, false);
    else
        SetValidBorderColor(id, true);
}

function CheckValidPasswword(value, id, Regex1, Regex2, Regex3) {  
    if (value.search(Regex1) == -1 || value.search(Regex2) == -1 || value.search(Regex3) == -1)
        SetValidBorderColor(id, false);
    else
        SetValidBorderColor(id, true);
}

function CheckPasswordConfirm(id, emailValue, emailConfirm) {
    if (emailValue != emailConfirm)
        SetValidBorderColor(id, false);
    else
        SetValidBorderColor(id, true);
}

function CheckEmailConfirm(id, emailValue, emailConfirm) {
    if (emailValue != emailConfirm)
        SetValidBorderColor(id, false);
    else
        SetValidBorderColor(id, true);
}

function CheckAccountNumber(id, value) {
    valid = elfProef(value);
    if (valid.length > 0)
        SetValidBorderColor(id, false);
    else
        SetValidBorderColor(id, true);
}

function elfProef(value) {

    var input = value;
    var tot = 0;
    var deel = 0;
    var rest = 0;

    if (input.length > 10 || input.length == 8 || input.length <= 5) {
        return "\r\nDit is géén juist banknummer ";
    }
    else {
        if (input.length > 8) {
            if (input.length = 9)
                input = "0" + input
            for (i = 0; i < input.length; i++) {
                getal = input.substr(i, 1);
                tot += getal * (10 - i);
            }
            deel = tot / 11;

            rest = tot % 11;
            if (rest != 0) {
                return "\r\nDit is géén juist banknummer";
            }
            else {
                return "";
            }
        }
        else
            return ""
    }
}

function DisplayGebruikersnaamMessage1Div(show) {

    var gebruikersnaamMessage1Div = document.getElementById('gebruikersnaamMessage1Div');

    if (gebruikersnaamMessage1Div != null) {
        if (show) {
            document.getElementById('gebruikersnaamMessage1Div').style.display = '';
        }
        else {
            document.getElementById('gebruikersnaamMessage1Div').style.display = 'none';
        }
    }
}


