﻿/// <reference path="~/js/jquery/jquery.js" />
/// <reference path="~/js/jquery/jquery.idfix.js" />
/// <reference path="~/js/jquery/jquery.alphanumeric.js" />

$(document).ready(function() {

    DebugAdd('global.js -> SetNumericFields');
    SetNumericFields();

    DebugAdd('global.js -> ShowLoaderOnSubmit');
    ShowLoaderOnSubmit();
    
    // DebugAdd('global.js -> FixFormAction');
    // FixFormAction();
});


// Wherever we have a save button, show ajax loader when it's clicked
function ShowLoaderOnSubmit() {
    $('form.master').submit(function() {
        // Custom function which uses either custom function CustomValidation or Page_ClientValidate.
        // CustomValidation should also check Page_ClientValidate.
        if (PageClientValid()) { HideFormShowLoader(); }
    });
}

function HideFormShowLoader() {
    // var Height = $('div.form').height();
    $('div.form').css('display', 'none');

    // $('.loader').css('height', Height + 'px');
    $('.loader').css('display', 'block');
}

function PageClientValid() {
    var PageValid = true;                               // Check if function(s) exist. If not, page is valid.

    // Inverted function check. Why was is not always like this?..
    if (typeof (CustomValidation) == 'function') {
        DebugAdd('CustomValidation function found!');
        PageValid = CustomValidation();
    }
    else if (typeof (Page_ClientValidate) == 'function') {
        DebugAdd('Page_ClientValidate function found!');
        PageValid = Page_ClientValidate();
    }

/*
    if (typeof (Page_ClientValidate) == 'function') {
        DebugAdd('Page_ClientValidate function found!');
        PageValid = Page_ClientValidate();
    }
    else if (typeof (CustomValidation) == 'function') {
        DebugAdd('CustomValidation function found!');
        PageValid = CustomValidation();
    }
*/
    DebugAdd('PageClientValid() -> PageValid = ' + PageValid);

    return PageValid;
}

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.master').attr('action', '');
}

function SetNumericFields() {
    $('.numeric').numeric();

    $('.smallnr').numeric();
    $('.smallnr').attr('maxlength', 4);

    $('.nr').numeric();
    $('.nr').attr('maxlength', 10);

    $('.kvknr').numeric();
    $('.kvknr').attr('maxlength', 12);
}



/*
********************************************************************************
* DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG *
********************************************************************************
*/


var divDebug = null;

function DebugAdd(strText) {
    DebugPrintText(strText, true);
}

function DebugPrint(strText) {
    DebugPrintText(strText, false);
}

function DebugPrintText(strText, blnAdd) {
    if (divDebug == null) { divDebug = $('div.debug'); }
    if (blnAdd) {
        var strCurrentHtml = divDebug.html();
        divDebug.html(strCurrentHtml + '<br />' + strText);
    }
    else {
        divDebug.html(strText);
    }
}

function IsNumeric(n) {
    return !isNaN(n);
}
