﻿
function ValidateRequired(pContainerID) {

    var bOk = true;

    $('#' + pContainerID + ' .required:visible').each(function(index, item) {

        var oInput = ($('input', item));
        var sLabel = $('label', item).html();
        var oSelect = ($('select', item));

        if (
                (oInput.is(':visible') && oInput[0] != null && (oInput[0].value == '' || _removeWhiteSpace(oInput[0].value) == '')) ||
                (oSelect[0] != null && oSelect[0].value == '' && oSelect.is(':visible')) ||
                (oInput.is(':visible') && oInput != null && oInput[0] != null && oInput[0].type == 'checkbox' && oInput[0].checked != true)
                ) {
            $('.fielderrorrequired', item).show();
            bOk = false;
        }
        else {
            $('.fielderrorrequired', item).hide();
        }
    });

    return bOk;
}


function ValidCardNumber(number) {

    var bOk = true;

    // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
    number = number.replace(/\D/g, '');
    if (number.length == 0) {
        bOk = false;
    }
    else {
        // Set the string length and parity
        var number_length = number.length;
        var parity = number_length % 2;

        // Loop through each digit and do the maths
        var total = 0;
        for (i = 0; i < number_length; i++) {
            var digit = number.charAt(i);
            // Multiply alternate digits by two
            if (i % 2 == parity) {
                digit = digit * 2;
                // If the sum is two digits, add them together (in effect)
                if (digit > 9) {
                    digit = digit - 9;
                }
            }
            // Total up the digits
            total = total + parseInt(digit);
        }

        // If the total mod 10 equals 0, the number is valid
        if (total % 10 != 0) {
            bOk = false;
        }
    }
    return bOk;
}

function _removeWhiteSpace(pStr) {
    return (pStr).replace(/^\s*|\s*$/g, '');
}

function ValidNumber(number, minlength) {
    
    if (number.replace(/\D/g, '').length < minlength) {
        return false;
    }
    return true;  
}
