﻿function customValidationRequired(source, args) {
    var lblId = source.id.replace('cv', 'lbl');
    var label = document.all ? document.all[lblId] : document.getElementById(lblId);
    args.IsValid = (args.Value != '');
    setControls(args.IsValid, label, source.id);

    return args.IsValid;
}

function customValidationMinPassword(source, args) {
    var lblId = source.id.replace('cv', 'lbl').replace('Min', '');
    var label = document.all ? document.all[lblId] : document.getElementById(lblId);

    source.validationexpression = '^.{8,}$';
    args.IsValid = RegularExpressionValidatorEvaluateIsValid(source);
    setControls(args.IsValid, label, source.id);

    return args.IsValid;
}

function customValidationMinChars(source, args) {
    var lblId = source.id.replace('cv', 'lbl').replace('Chars', '');
    var label = document.all ? document.all[lblId] : document.getElementById(lblId);

    source.validationexpression = '^.*[0-9]+.*$';
    args.IsValid = RegularExpressionValidatorEvaluateIsValid(source);
    setControls(args.IsValid, label, source.id);

    return args.IsValid;
}

function customValidationEmail(source, args)
{
     var lblId = source.id.replace('cv', 'lbl').replace('Invalid', '');
     var label = document.all ? document.all[lblId] : document.getElementById(lblId);

    if(args.Value == '')
        args.IsValid = false;
   else
        {
            source.validationexpression = '^([a-zA-Z0-9_\\-])+(\\.([a-zA-Z0-9_\\-])+)*@((\\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\\]))|((([a-zA-Z0-9])+(([\\-])+([a-zA-Z0-9])+)*\\.)+([a-zA-Z])+(([\\-])+([a-zA-Z0-9])+)*))$';
            args.IsValid = RegularExpressionValidatorEvaluateIsValid(source);
    }
    setControls(args.IsValid, label, source.id);

    return args.IsValid;
}

function customValidationType(source, args)
{
    var lblId = source.id.replace('cv', 'lbl').replace('Type','');
    var label = document.all ? document.all[lblId] : document.getElementById(lblId);
    var extString = "*.doc.docx.txt.pdf";

    var extension = args.Value.substring(args.Value.lastIndexOf('.'));
    args.IsValid = (extString.indexOf(extension) > 0);

    setControls(args.IsValid, label, source.id);
    return args.IsValid;
}

function customValidationSize(source, args)
{
   
}


function setControls(isValid, label, validatorId) {
    var allValid;
        
    changeLabel(isValid, label);
    allValid = setPageValidator(validatorId, isValid);

    changePanelVisibility(allValid);
    
    ValidatorUpdateIsValid(); //This is a method created by asp.net to let the global variables know if the page is valid or not
    ValidationSummaryOnSubmit(''); //This is a method created by asp.net to make sure the validation summary knows which fields are not valid and to update it's display.
}

function changeLabel(isValid, label) {
    if (isValid)
        label.style['color'] = '#535355';
    else
        label.style['color'] = 'red';
}

function changePanelVisibility(allValid) {
    var div = document.all ? document.all['divErrorSummary'] : document.getElementById('divErrorSummary');

    if (div == null) {
        return;
    }

    if (allValid) {
        div.style.display = 'none';
    }
    else {
        div.style.display = 'block';
    }
}

function setPageValidator(validatorId, isValid) {
    var validators = Page_Validators;
    var allValid = true;
    
    if ((typeof (validators) != "undefined") && (validators != null)) {
        var i;
        for (i = 0; i < validators.length; i++) {
            if (validators[i].id == validatorId) {
                validators[i].isvalid = isValid;
            }

            if (allValid && !validators[i].isvalid) {
                allValid = false;
            }
        }
    }

    return allValid;
}

function validateForgotPasswordUsername(source, args) {
    var div = document.all ? document.all['divUserNameError'] : document.getElementById('divUserNameError'); 
    
    args.IsValid = (args.Value != '');

    if (!args.IsValid) {
        div.style.display = 'block';
    }
    else {
        div.style.display = 'none';
    }

    return args.IsValid;
}

