﻿/// <reference path="~/js/jquery-1.4.2.js" />
/// <reference path="~/js/jquery.idfix.js" />
/// <reference path="~/js/jquery.alphanumeric.js" />
/// <reference path="~/js/BodyFix.js" />
/// <reference path="~/js/debug.js" />
/// <reference path="~/aspnet_client/system_web/1_1_4322/webuivalidations.js" />

$(document).ready(function() {
    SetNumericFields();
    FixFormAction();
    ShowLoaderOnSubmit();
});


function ArrayCount(arr) {
    var count = 0
    for (var i in arr) count++
    return count
}
function SetNumericFields() {
    $('.numeric').numeric();

    $('.smallnr').numeric();
    $('.smallnr').attr('maxlength', 4);

    $('.nr').numeric();
    $('.nr').attr('maxlength', 10);

    $('.kvknr').numeric();
    $('.kvknr').attr('maxlength', 12);
}


function PageClientValid() {
    // Check if function exists. If not, page is valid.
    var PageValid = true;

    if (typeof (Page_ClientValidate) == "function") {
        PageValid = Page_ClientValidate();
    }
    else if (typeof (CustomValidation) == "function") {
        PageValid = CustomValidation();
    }

    return PageValid;
}


// Wherever we have a save button, show ajax loader when it's clicked
function ShowLoaderOnSubmit() {
    // $(GetRealID('btnSave')).click(function() {
    $('form.main').submit(function() {
        if (PageClientValid()) {
            $('div.form').css('display', 'none');               // hide('fast');
            $('img.loader').css('display', 'block');            // show('normal');
        }
    });   
}

function FixFormAction() {
    /*  
    Problem:    we get 'Validation of viewstate MAC failed' when posting the form.
    Analyze:    the form action is set to data.aspx?etcetc BUT we're using URL Rewriting 
    so it posts to /prv/usertype/data.aspx?etcetc. This is not where data.aspx is located. 
    Therefor, validation fails.
    Workaround: Set the form's action attribute to an empty string.
    */

    /*
    var Action = $('form.main').attr('action');
    Action = "/users/" + Action;
    $('form.main').attr('action', Action);  // alert(Action);
    */

    $('form.main').attr('action', '');
}
// Auto focus on first input[type=text] element
// Niet handig om dit overal te doen, bijv /inspecteurs laad maar een deel van de pagina zien bij lage resolutie door de autofocus
function FocusOnFirstTextField() {
    /*

    $('input:text:first').focus();
    
    Nice way of selecting the first element, but gives a warning in FireFox :(
    Warning:        Unknown pseudo-class or pseudo-element 'text'.
    Line:           0
    
    Same goes for $('input[type=text]:first').focus();
    Warning:        Unknown pseudo-class or pseudo-element 'first'.
    Line:           0
    */

    // Solution (not the prettiest but it works without warnings)
    $('input[type=text]').each(function() { $(this).focus(); return false; });
}

function DateReadOnly() {
    $('input.date').keypress(function() {
        return false;
    });
}


function DisableOtherCheckboxes(strClasses) {
    $('input[type=checkbox]').each(function() {
        // Have to sent parent to the function which is the span which has the class
        if (!HasClasses($(this).parent(), strClasses)) $(this).attr('disabled', 'true');
    });
}

function HasClasses(Element, strClasses) {
    var Classes = strClasses.split(' ');
    var teller;
    var strClass;
    for (teller = 0; teller < Classes.length; teller += 1) {
        strClass = $.trim(Classes[teller]);
        if (!$(Element).hasClass(strClass)) return false;
    }


    return true;
}


