﻿var MinQuantityText = 'Caption.MinQuantity';
var FalseOffsetSizeText = 'Caption.FalseOffsetSize';

//TEXTBOX  with css class numerictext
$(document).ready(function() {
SetValidateNumeric();
});

function SetValidateNumeric() {
   $("input.numerictext").keydown(function(event) {
      if (event.keyCode == 46 || event.keyCode == 8 || (event.keyCode >= 96 && event.keyCode <= 105)) {
         // let it happen, don't do anything
      } else {
         // Ensure that it is a number and stop the keypress
         if (event.keyCode > 31 && (event.keyCode < 48 || event.keyCode > 57)) {
            event.preventDefault();
         }
      }
   });
}

function checkQuantity(dom, strMinQuantity, strOffsetSize) {
   var el = $(dom);
   var minQuantity = parseInt(strMinQuantity);
   var offsetSize = parseInt(strOffsetSize);
   var val = parseInt(el.val());

   if (val < minQuantity) {
      alert(MinQuantityText);
      el.val(minQuantity);
   } else if (val % offsetSize) {
      var msg = FalseOffsetSizeText;   
      alert(msg.replace('{0}', offsetSize));
      el.val(minQuantity);
   }
}

//to allow only number enter, seem to HAVE PROBLEM WITH FIREFOX
function isNumberKey(evt) {
    if (!evt)
        evt = window.event;
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

//For local double format: only numberic and comma are allowed. Example 125,40 is valid 
function isMoneyNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        if (charCode != 44) //allow comma
            return false;
    }
    return true;
}

// Returns the version of Windows Internet Explorer or a -1
// (indicating the use of another browser).
function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

function ValidForAjaxReq() {
    var ver = getInternetExplorerVersion();
    if (ver > -1) {
        if (ver < 8.0) {
            return false;
        }
    }
    return true;
}

function checkIEVersion() {
    var ver = getInternetExplorerVersion();
    if (ver > -1) {
        if (ver < 8.0) {
            alert("Old version");
        }
    }
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}
function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}

function validateAddress(e) {
    var p = e.data.p;
    var postcode = $('#' + p + 'postcode');
    var huisnr = $('#' + p + 'huisnr');
    var address = $('#' + p + 'address');
    var place = $('#' + p + 'place');
    var canvalidatemsg = p == 'c' ? $('#canvalidatemsg') : $('#' + p + 'canvalidatemsg');
    var cantvalidatemsg = p == 'c' ? $('#cantvalidatemsg') : $('#' + p + 'cantvalidatemsg');
    var isvalidatingmsg = p == 'c' ? $('#isvalidatingmsg') : $('#' + p + 'isvalidatingmsg');

    if (postcode.val() != '' && huisnr.val() != '') {
        address.val('');
        place.val('');
        canvalidatemsg.hide();
        isvalidatingmsg.show();
        
        $.ajaxSetup({ cache: false });
        $.ajax({
            type: "GET",
            url: "../Connect.aspx/GetAdresByPostcode",
            data: { postcodeAndNr: postcode.val() + huisnr.val() },
            dataType: 'json',
            success: function(r) {
                if (r == null || r.plaatsnaam == '') {
                    canvalidatemsg.hide();
                    cantvalidatemsg.show();
                    isvalidatingmsg.hide();
                    address.attr('readonly', false);
                    place.attr('readonly', false);
                }
                else {
                    address.val(r.straatnaam);
                    place.val(r.plaatsnaam);
                    canvalidatemsg.show();
                    cantvalidatemsg.hide();
                    isvalidatingmsg.hide();
                }
            },
            error: function() {
                canvalidatemsg.hide();
                cantvalidatemsg.show();
                isvalidatingmsg.hide();
                address.attr('readonly', false);
                place.attr('readonly', false);
            }
        });
        
    }
}

function activateValidatorAddress(p) {
    var postcode = $('#' + p + 'postcode');
    var huisnr = $('#' + p + 'huisnr');
    var address = $('#' + p + 'address');
    var place = $('#' + p + 'place');
    var canvalidatemsg = p == 'c' ? $('#canvalidatemsg') : $('#' + p + 'canvalidatemsg');
    var cantvalidatemsg = p == 'c' ? $('#cantvalidatemsg') : $('#' + p + 'cantvalidatemsg');
        
    postcode.bind('blur', {p : p}, validateAddress);
    huisnr.bind('blur', {p : p}, validateAddress);
    address.attr('readonly', true);
    place.attr('readonly', true);

    postcode.attr('class', 'required');
    huisnr.attr('class', 'required');
    address.attr('class', 'required');
    place.attr('class', 'required');
    
    
    canvalidatemsg.show();
    cantvalidatemsg.hide();
}

function disableValidatorAddress(p) {
    var postcode = $('#' + p + 'postcode');
    var huisnr = $('#' + p + 'huisnr');
    var address = $('#' + p + 'address');
    var place = $('#' + p + 'place');
    var canvalidatemsg = p == 'c' ? $('#canvalidatemsg') : $('#' + p + 'canvalidatemsg');
    var cantvalidatemsg = p == 'c' ? $('#cantvalidatemsg') : $('#' + p + 'cantvalidatemsg');
    
    postcode.unbind('blur');
    huisnr.unbind('blur');
    address.attr('readonly', false);
    place.attr('readonly', false);

    postcode.removeClass('required');
    huisnr.removeClass('required');
    address.removeClass('required');
    place.removeClass('required');
    
    canvalidatemsg.hide();
    cantvalidatemsg.show();
}

function bindValidatorAddress() {
    var isvalidatingmsg = $('#isvalidatingmsg');
    
    var a = $('#countrycode');
    a.update = function() {
        if (a.val() == 'NL') {
            activateValidatorAddress('c');
        }
        else {
            disableValidatorAddress('c');
        }
    }
    a.change(a.update);
    isvalidatingmsg.hide();
    a.update();
}

function bindSeoncdValidatorAddress() {
    var isvalidatingmsg = $('#visvalidatingmsg');

    var b = $('#vcountrycode');
    b.update = function() {
        if (b.val() == 'NL') {
            activateValidatorAddress('v');
        }
        else {
            disableValidatorAddress('v');
        }
    }
    b.change(b.update);
    isvalidatingmsg.hide();
    b.update();
}
