﻿// common form validation
function processForm(source, args) {
    if (!args.IsValid) {
        getFieldDiv(source).addClass('ips-error');
    } else {
        getFieldDiv(source).removeClass('ips-error');
    }
}

function getFieldDiv(source) {
    return $j('#' + source.id).parent();
}

function getControls(control, source) {
    fieldDiv = getFieldDiv(source);
    if (fieldDiv[0].style.display == 'none') {
        return new Array();
    }
    div = 'div' + '#' + fieldDiv.attr('id');
    return $j(div + ' ' + control);
}

function validateInputRequired(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        value = $j.trim(inputs[0].value);
        args.IsValid = value.length > 0;
    }
    processForm(source, args);
}

function validateTextareaRequired(source, args) {
    textareas = getControls('textarea', source);
    if (textareas.length > 0) {
        value = $j.trim(textareas[0].value);
        args.IsValid = value.length > 0;
    }
    processForm(source, args);
}

function validateInputCharsNumber(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        value = $j.trim(inputs[0].value);
        args.IsValid = value.length >= 2;
    }
    processForm(source, args);
}

function validateTextareaCharsNumber(source, args) {
    textareas = getControls('textarea', source);
    if (textareas.length > 0) {
        value = $j.trim(textareas[0].value);
        args.IsValid = value.length >= 5;
    }
    processForm(source, args);
}

function validateRadioButtons(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        args.IsValid = inputs[0].checked || inputs[1].checked;
    }
    processForm(source, args);
}

function validateCheckboxRequired(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        args.IsValid = inputs[0].checked;
    }
    processForm(source, args);
}

function validateSelectRequired(source, args) {
    selects = getControls('select', source);
    if (selects.length > 0) {
        valid = true;
        for (var i = 0; i < selects.length; i++) {
            if (selects[i].value.length == 0) {
                valid = false;
                break;
            }
        }
        args.IsValid = valid;
    }
    processForm(source, args);
}

function validateSelectDate(source, args) {
    selects = getControls('select', source);
    if (selects.length > 0) {
        if (selects.length == 3) {
            year = parseInt(selects[0].value);
            month = parseInt(selects[1].value);
            day = parseInt(selects[2].value);
            if (!isNaN(month) && !isNaN(day) && !isNaN(year)) {
                date = new Date(year, month - 1, day);
                args.IsValid = date.getFullYear() == year && date.getMonth() == month - 1 && date.getDate() == day;
            } else if (!isNaN(month) || !isNaN(day) || !isNaN(year)) {
                args.IsValid = false;
            } else {
                args.IsValid = true;
                return;
            }
        } else {
            args.IsValid = false;
        }
    }
    processForm(source, args);
}

function validateBirthDate(source, args) {
    selects = getControls('select', source);
    if (selects.length > 0) {
        if (selects.length == 3) {
            year = parseInt(selects[0].value);
            month = parseInt(selects[1].value);
            day = parseInt(selects[2].value);
            if (!isNaN(month) && !isNaN(day) && !isNaN(year)) {
                date = new Date(year, month - 1, day);
                if (date.getFullYear() == year && date.getMonth() == month - 1 && date.getDate() == day) {
                    now = new Date();
                    args.IsValid = date <= now;
                }
            } else if (!isNaN(month) || !isNaN(day) || !isNaN(year)) {
                args.IsValid = false;
            } else {
                args.IsValid = true;
                return;
            }
        } else {
            args.IsValid = false;
        }
    }
    processForm(source, args);
}

function validateEmail(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        value = $j.trim(inputs[0].value);
        if (value.length > 4) {
            emailPattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            args.IsValid = emailPattern.test(value);
        } else {
            args.IsValid = false;
        }
    }
    processForm(source, args);
}

function validateUrl(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        value = $j.trim(inputs[0].value);
        if (value.length > 2) {
            //TODO: validate url
            //urlPattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            //args.IsValid = urlPattern.test(value);
        } else {
            args.IsValid = false;
        }
    }
    processForm(source, args);
}

function validatePostalcodeByPattern(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        value = $j.trim(inputs[0].value);
        if (value.length > 0) {
            country = $j('select[id*="ddlCountry"]')[0];
            postalcodePattern = $j(country.options[country.selectedIndex]).attr('postalcode');
            if (postalcodePattern && postalcodePattern.length > 0) {
                postalcodePattern = new RegExp(postalcodePattern);
                args.IsValid = postalcodePattern.test(value);
            } else {
            args.IsValid = true;
            }
        } else {
            args.IsValid = false;
        }
    }
    processForm(source, args);
}

function validatePhoneNumberByPattern(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        value = $j.trim(inputs[0].value);
        if (value.length > 0) {
            country = $j('select[id*="ddlCountry"]')[0];
            phoneNumberPattern = $j(country.options[country.selectedIndex]).attr('phonenumber');
            if (phoneNumberPattern && phoneNumberPattern.length > 0) {
                phoneNumberPattern = new RegExp(phoneNumberPattern);
                args.IsValid = phoneNumberPattern.test(value);
            } else {
                args.IsValid = true;
            }
        } else {
            args.IsValid = false;
        }
    }
    processForm(source, args);
}

function validatePhoneNumber(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        value = $j.trim(inputs[0].value);
        if (value.length > 0) {
            phoneNumberPattern = /^[0-9\-\(\)\ \+]+$/;
            args.IsValid = phoneNumberPattern.test(value);
        } else {
            args.IsValid = false;
        }
    }
    processForm(source, args);
}

function validateChildren(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        value = parseInt($j.trim(inputs[0].value));
        args.IsValid = !isNaN(value) && (value >= 0 && value <= 20);
    }
    processForm(source, args);
}

function validateHouseNumber(source, args) {
    inputs = getControls('input', source);
    if(inputs.length>0){
        value = parseInt($j.trim(inputs[0].value));
        args.IsValid = !isNaN(value) && value >= 0;
    }
    processForm(source, args);
}

function validateProductNumber(source, args) {
    inputs = getControls('input', source);
    if (inputs.length > 0) {
        value = parseInt($j.trim(inputs[0].value));
        args.IsValid = !isNaN(value) && value >= 0;
    }
    processForm(source, args);
}
