function PhoneNumberValidator_Validate(validator, args) {
    ctrl = $('#' + validator.controltovalidate);
    var val = ctrl.val();

    var phone = val;
    phone = phone.replace(/\s/g, '');

    if (phone.length > 0) {
        if (phone.charAt(0) == '+') {
            if (phone.charAt(1) == '+') {
                if (phone.IndexOf('+', 2) >= 0) {
                    // problem: "We only recognize at most two + characters in an international code."
                    args.IsValid = false;
                    return;
                }
            }
            phone.Replace('+', '');
            if (phone.charAt(0) == '4' && phone.charAt(1) == '4') {
                phone = phone.substring(2);
            }
        }
        else if (phone.charAt(0) == '0') {
            phone = phone.substring(1);
        }
        else {
            // problem: "The phone number does not begin with a zero."
            switch (validator.Type) {
                case 'Both':
                    validator.errormessage = validator.ErrorInvalid;
                    $(validator).html(validator.errormessage);
                    break;
                case 'Land':
                    validator.errormessage = validator.ErrorInvalidLand;
                    $(validator).html(validator.errormessage);
                    break;
                case 'Mobile':
                    validator.errormessage = validator.ErrorInvalidMobile;
                    $(validator).html(validator.errormessage);
                    break;
            }
            args.IsValid = false;
            return;
        }
    }
    else {
        // Zero length phone number
        args.IsValid = true;
        return;
    }

    if (!phone.match("^[0-9 ]*$")) {
        validator.errormessage = validator.ErrorBadDigits;
        $(validator).html(validator.errormessage);
        args.IsValid = false;
        return;
    }

    if (phone.length > 0 && PhoneNumberValidator_IsValidSecondDigit(validator.Type, phone.charAt(0))) {
        if (validator.Type == 'Mobile' && phone.length != 10) { // MobileNumbers must be 11 digits (and we have removed the leading 0
            validator.errormessage = validator.ErrorInvalidMobile;
            $(validator).html(validator.errormessage);
            args.IsValid = false;
            return;
        }
        if (phone.Length < 9 || phone.Length > 10) {	// we've removed leading 0, so the remaining string should be 9 or 10 digits
            // "The number appears to be the wrong length.  We only recognize numbers with 10 or 11 digits."
            validator.errormessage = validator.ErrorBadLength;
            $(validator).html(validator.errormessage);
            args.IsValid = false;
            return;
        }
    }
    else {
        switch (validator.Type) {
            case 'Both':
                validator.errormessage = validator.ErrorInvalid;
                $(validator).html(validator.errormessage);

                break;
            case 'Land':
                if (phone.charAt(0) == '7') {
                    validator.errormessage = validator.ErrorExpectingLandlineIsMobile;
                    $(validator).html(validator.errormessage);
                }
                else {
                    validator.errormessage = validator.ErrorInvalidLand;
                    $(validator).html(validator.errormessage);
                }

                break;
            case 'Mobile':
                validator.errormessage = validator.ErrorInvalidMobile;
                $(validator).html(validator.errormessage);
                break;
        }
        args.IsValid = false;
        return;
    }
    args.IsValid = true;
}

function PhoneNumberValidator_IsValidSecondDigit(t, c) {
    switch (c) {
        case '1':
        case '2':
            return t != 'Mobile';
        case '7':
            return t != 'Land';
        default:
            return false;
    }
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();