﻿$.validator.addMethod("dropdown", function(value, element) {
    return this.optional(element) || element.selectedIndex > 0;
}, "Please make a selection from the list");

$.validator.addMethod("phone", function(ph, element) {
    if (ph == null) {
        return false;
    }
    var stripped = ph.replace(/[\s()+-]|ext\.?/gi, "");
    // 10 is the minimum number of numbers required
    return ((/\d{10,}/i).test(stripped));
}, "Please enter a valid phone number");

$.validator.addMethod("ip", function(ipAddress, element) {
    if (ipAddress == null) {
        return false;
    }
    return ((/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/).test(ipAddress));
}, "Please enter a valid ip address.");

$.validator.addMethod("currency", function(value, element) {
    if (value == null || value.length == 0) {
        return true;
    }
    return ((/^\s*([-\+])?((\d{1,3}(\,\d{3})+)|\d*)\.?(\d{0,2})\s*$/).test(value));
}, "Please enter a valid currency value.");

$.validator.addMethod("currencyDE", function(value, element) {
    if (value == null || value.length == 0) {
        return true;
    }
    return ((/^\s*([-\+])?((\d{1,3}(\.\d{3})+)|\d*)\,?(\d{0,2})\s*$/).test(value));
}, "Please enter a valid currency value.");

$.validator.addMethod("maxCurrency", function(value, element, param) {
    if (value == null || value.length == 0) {
        return true;
    }
    var strippedValue = Number(value.replace(",", ""));
    var strippedParam = Number(param.replace(",", ""));
    return strippedValue <= strippedParam;
}, $.format("Please enter a value less than or equal to {0}."));

$.validator.addMethod("minCurrency", function(value, element, param) {
    if (value == null || value.length == 0) {
        return true;
    }
    var strippedValue = Number(value.replace(",", ""));
    var strippedParam = Number(param.replace(",", ""));
    return strippedValue >= strippedParam;
}, $.format("Please enter a value greater than or equal to {0}."));

$.validator.addMethod('postalCode', function(value) {
    return /^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$/.test(value);
}, 'Please enter a valid US or Canadian postal code.');

