﻿
/// <reference path="jquery-1.3.2-vsdoc2.js" />


// Validates a string is a valid email address.
function isEmailAddress(str) {
    if (str.replace(' ', '') == '')
        return false;

    var filter = /^.+@.+\..{2,3}$/;
    return (filter.test(str));
}


// Used by ASP.NET CustomValidator controls that need to validate an email address in a textbox.
function CustomValidateEmailAddress(source, arguments) {
	arguments.IsValid = isEmailAddress(arguments.Value);
}

/// Validates that exactly 1 item in a collection of input:radio controls with the same 'name' attribute (GroupName in ASP.NET RadioButton) is selected.
function CustomValidateRadioButtonGroup(source, arguments) {
	var groupName = source.GroupName; // source.GroupName should be set server-side via call to Page.ClientScript.RegisterExpandoAttribute(Me.cvField.ClientID, "GroupName", "nameOfGroup", True)

	if (groupName == null) {
		window.alert("GroupName expando attribute has not been registered on validator " + source.id);
		args.IsValid = false;
		return;
	}

	var count = $("input[@name=" + groupName + "]:checked").length;
	arguments.IsValid = count == 1;
}


// Used by CheckBoxValidator controls that need to validate an ASP.NET CheckBox control.
function CheckBoxValidatorEvaluateIsValid(val) {
    var control = document.getElementById(val.controltovalidate);
    //var mustBeChecked = Boolean(val.mustBeChecked);
    var mustBeChecked = val.mustBeChecked == 'true' ? true : false;

    return control.checked == mustBeChecked;
}


// Used by CheckBoxListValidator controls that need to validate an ASP.NET CheckBoxList control.
function CheckBoxListValidatorEvaluateIsValid(val) {
    var control = document.getElementById(val.controltovalidate);
    var minimumNumberOfSelectedCheckBoxes = parseInt(val.minimumNumberOfSelectedCheckBoxes);

    var selectedItemCount = 0;
    var liIndex = 0;
    var currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    while (currentListItem != null) {
        if (currentListItem.checked)
            selectedItemCount++;
        liIndex++;
        currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    }

    return selectedItemCount >= minimumNumberOfSelectedCheckBoxes;
}

