﻿var CSS_ERROR = 'inputError';

$(function() {

    $('#postError').dialog({
        autoOpen: false,
        width: 480,
        closeOnEscape: true,
        dialogClass: 'dialog',
        modal: true,
        title: 'Validation Errors',
        buttons: {
            'OK': function() {
                $(this).dialog('close');
            }
        }
    });

});

function cvfName(source, args) {
    var txtName = $('#' + txtNameId);
    var valid = (txtName.val().length != 0) && (txtName.val().length <= 200);

    if (valid) {
        txtName.removeClass(CSS_ERROR);
    } else {
        txtName.addClass(CSS_ERROR);
    }
    args.IsValid = valid;
}

function cvfEmailAddress(source, args) {
    var txtEmail = $('#' + txtEmailId);
    args.IsValid = validateEmail(txtEmail.val());
    if (args.IsValid) {
        txtEmail.removeClass(CSS_ERROR);
    } else {
        txtEmail.addClass(CSS_ERROR);
    }
}

function cvfWebsite(source, args) {
    var txtWebsite = $('#' + txtWebsiteId);

    var url = txtWebsite.val();
    if ($.trim(url).length != 0 && url.toLowerCase().indexOf('http') < 0) {
        url = 'http://' + url;
        txtWebsite.val(url);
    }
        
    var rgxWebsite = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&,#=]*)?");
    args.IsValid = (txtWebsite.val().length == 0) || ((txtWebsite.val().length <= 200) && (rgxWebsite.test(txtWebsite.val())));
    if (args.IsValid) {
        txtWebsite.removeClass(CSS_ERROR);
    } else {
        txtWebsite.addClass(CSS_ERROR);
    }
}

function cvfComment(source, args) {
    var txtComment = $('#' + txtCommentId);
    var valid = (txtComment.val().length != 0) && (txtComment.val().length <= 2500);

    if (valid) {
        txtComment.removeClass(CSS_ERROR);
    } else {
        txtComment.addClass(CSS_ERROR);
    }
    args.IsValid = valid;
}

