﻿
function getDescriptionWordCount() {
    return $.trim($('#' + txtDescId).val()).split(/\s/).length;
}

function cvfDescription(source, args) {
    var txtDesc = $('#' + txtDescId);
    var valid = (txtDesc.val().length != 0) && (getDescriptionWordCount() <= DESCRIPTION_WORD_COUNT) && (txtDesc.val().length < 1500);

    if (valid) {
        txtDesc.removeClass(CSS_ERROR);
    } else {
        txtDesc.addClass(CSS_ERROR);
    }

    args.IsValid = valid;
}
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 cvfCompanyName(source, args) {
    var txtCompany = $('#' + txtCompanyId)
    args.IsValid = (txtCompany.val().length != 0) && (txtCompany.val().length <= 200);
    if (args.IsValid) {
        txtCompany.removeClass(CSS_ERROR);
    } else {
        txtCompany.addClass(CSS_ERROR);
    }
}
function cvfJobLocation(source, args) {
    var txtLocation = $('#' + txtLocationId);
    args.IsValid = (txtLocation.val().length != 0) && (txtLocation.val().length <= 200);
    if (args.IsValid) {
        txtLocation.removeClass(CSS_ERROR);
    } else {
        txtLocation.addClass(CSS_ERROR);
    }
}
function cvfWebsite(source, args) {
    var txtWeb = $('#' + txtWebId);
    
    var url = txtWeb.val();
    if ($.trim(url).length != 0 && url.toLowerCase().indexOf('http') < 0) {
        url = 'http://' + url;
        txtWeb.val(url);
    }
    
    var rgxWebsite = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&,#=]*)?");
    args.IsValid = (txtWeb.val().length != 0) && (txtWeb.val().length <= 255) && (rgxWebsite.test(txtWeb.val()));
    if (args.IsValid) {
        txtWeb.removeClass(CSS_ERROR);
    } else {
        txtWeb.addClass(CSS_ERROR);
    }
}
function cvfSkills(source, args) {
    var fck = FCKeditorAPI.GetInstance(txtSkillsId);
    args.IsValid = (fck.GetHTML().length != 0) && (fck.GetHTML().length <= 10000);
    if (args.IsValid) {
        fck.EditorDocument.body.className = '';
    } else {
        fck.EditorDocument.body.className = CSS_ERROR;
    }
}
function cvfIndustry(source, args) {
    var ddlIndustry = $('#' + ddlIndustryId);
    args.IsValid = (ddlIndustry.val() != '0');
    if (args.IsValid) {
        ddlIndustry.removeClass(CSS_ERROR);
    } else {
        ddlIndustry.addClass(CSS_ERROR);
    }
}

function cvfTitle(source, args) {
    var txtTitle = $('#' + txtTitleId);
    args.IsValid = (txtTitle.val().length != 0) && (txtTitle.val().length <= 200);
    if (args.IsValid) {
        txtTitle.removeClass(CSS_ERROR);
    } else {
        txtTitle.addClass(CSS_ERROR);
    }
}
function cvfApplicationUrl(source, args) {
    var txtUrl = $('#' + txtUrlId);

    var url = txtUrl.val();
    if ($.trim(url).length != 0 && url.toLowerCase().indexOf('http:') < 0) {
        url = 'http://' + url;
        txtUrl.val(url);
    }
    
    var rgxWebsite = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&,#=]*)?");
    var valid = (txtUrl.val().length != 0) && (txtUrl.val().length <= 255) && (rgxWebsite.test(txtUrl.val()));
    if (valid) {
        txtUrl.removeClass(CSS_ERROR);
    } else {
        txtUrl.addClass(CSS_ERROR);
    }

    args.IsValid = valid;
}
function validatePost() {
    Page_ClientValidate('vgPost');
    return Page_IsValid;
}

$(function() {
    // prevent scrolling to top of page on fail
    window.scrollTo = function() { }

    $('#postError').dialog({
        autoOpen: false,
        width: 480,
        closeOnEscape: true,
        dialogClass: 'dialog',
        modal: true,
        title: 'Validation Errors',
        buttons: {
            'OK': function() {
                $(this).dialog('close');
            }
        }
    });

    $('#' + txtDescId).bind('keyup click blur focus change paste', function() {
        var wordCount = getDescriptionWordCount();
        if ($(this).val() == '') {
            wordCount = 0;
        }

        if (wordCount > DESCRIPTION_WORD_COUNT) {
            $('#desc_wordCount').addClass('error');
        } else {
            $('#desc_wordCount').removeClass('error');
        }
        var wordDesc = 'Word Count: ' + wordCount + ' word' + ((wordCount != 1) ? 's' : '');
        $('#wordCount').html(wordDesc);
    });

    $('#' + ibPostThisJobId).click(function() {
        var valid = validatePost();
        if (!valid) {
            $('#postError').dialog('open');
        }

        return valid;
    });
});