/// <reference path="~/js/jquery-1.4.2.js" />
/// <reference path="~/js/global.js" />


function NumericOnlyCheckAfterPaste(element) {
    var strValue = $(element).val();
    if (strValue == null || strValue == '') return;

    DebugAdd('Value changed -> ' + strValue);

    if (isNaN(strValue)) {
        alert('Alleen numerieke waarden invoeren.');
        $(element).val('');
        window.setTimeout(function() { $(element).focus(); ; }, 300);
        
    }
    else {
        DebugAdd('Value is oke');
    }
}

(function($) {

    $.fn.alphanumeric = function(p) {

        p = $.extend({
            ichars: "!@#$%^&*()+=[]\\\';,/{}|\":<>?~`.- ",
            nchars: "",
            allow: ""
        }, p);

        return this.each
			(
				function() {

				    if (p.nocaps) p.nchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
				    if (p.allcaps) p.nchars += "abcdefghijklmnopqrstuvwxyz";

				    s = p.allow.split('');
				    for (i = 0; i < s.length; i++) if (p.ichars.indexOf(s[i]) != -1) s[i] = "\\" + s[i];
				    p.allow = s.join('|');

				    var reg = new RegExp(p.allow, 'gi');
				    var ch = p.ichars + p.nchars;
				    ch = ch.replace(reg, '');

				    $(this).change(function() {
				        NumericOnlyCheckAfterPaste(this);
				    });

				    $(this).keypress
						(
							function(e) {
							    if (!e.charCode) {
							        k = String.fromCharCode(e.which);
							    }
							    else {
							        k = String.fromCharCode(e.charCode);
							    }

							    DebugPrint('e.keyCode = ' + e.keyCode + '<br />' +
							               'key = ' + k.toString());


							    // Always allow the following keys:
							    // 35 = Home
							    // 36 = End
							    // 37 = Arrow Left
							    // 38 = Arrow Up
							    // 39 = Arrow Right
							    // 40 = Arrow Down
							    // 46 = Delete

							    // Key combinations
							    // 86 = Ctrl + V (paste text)
							    // 87 = Ctrl + C (copy text)
							    // 88 = Ctrl + X (cut text)

							    // TODO; what if the user pastes chars in that are not allowed?

							    // To detect keycodes
							    // (do not alert on Shift, Ctrl or Backspace (otherwise we cannot detect combinations))
							    if (e.keyCode != 16 && e.keyCode != 17 && e.keyCode != 8) {
							        // alert('e.keyCode = ' + e.keyCode);
							    }

							    //  && k == 'v'
							    if (e.ctrlKey) {
							        DebugAdd('Ctrl key detected, allow cut/copy/paste etc');
							        // $(this).change will check value
							        // window.setTimeout(function() { NumericOnlyCheckAfterPaste(this); }, 200);
							        // e.preventDefault();
							    }
							    else {
							        var KeysAllowed = '35, 36, 37, 38, 39, 40, 64, 86, 87, 88';
							        var arrKeysAllowed = KeysAllowed.split(',');
							        if (IsInArray(e.keyCode, arrKeysAllowed) == false) {
							            if (ch.indexOf(k) != -1) {
							                DebugAdd('preventing default');
							                e.preventDefault();
							            }
							        }

							    }


							    /*
							    // old check method:
							    if ( e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40 && e.keyCode != 46 && 
							    e.keyCode != 86 && e.keyCode != 87 && e.keyCode != 88 )
							    {
							    if (ch.indexOf(k) != -1) e.preventDefault();
							    //if (e.ctrlKey && k=='v') e.preventDefault();        // Allow pasting with Ctrl + V ?
							    }
							    */
							}
						);

				    // disable context menu on rmb
				    // $(this).bind('contextmenu', function() { return false });
				}
			);
    };

    $.fn.numeric = function(p) {

        var az = "abcdefghijklmnopqrstuvwxyz";
        az += az.toUpperCase();

        p = $.extend({
            nchars: az
        }, p);

        return this.each(function() {
            $(this).alphanumeric(p);
        });

    };

    $.fn.alpha = function(p) {

        var nm = "1234567890";

        p = $.extend({
            nchars: nm
        }, p);

        return this.each(function() {
            $(this).alphanumeric(p);
        }
		);

    };

})(jQuery);

function IsInArray(objValue, arrValues)
{
    var blnIsInArray = false;
    for ( intTeller = 0; intTeller < arrValues.length; intTeller += 1 ) {
        if ( objValue == arrValues[intTeller] )
        {
            blnIsInArray = true;
            break;
        }
    }
    return blnIsInArray;
}
