﻿//This function will handle any of the .Net validators as long as the button OnClientClick call passes in 
//the UserControl name and ErrorLabel name
function ConfirmSave(formName, errorLabel)
{
    var customIsValid = true;
    if (!Page_ClientValidate() && !Page_IsValid)
    {
        var invalidControls = new Object(); // keeps track of controls that have a validation error

        for (var i = 0; i < Page_Validators.length; i++)
        {
            if (Page_Validators[i].controltovalidate != null)
            {
                //alert("id[" + Page_Validators[i].id + "] controldtovalidate:" + Page_Validators[i].controltovalidate + " index:" + Page_Validators[i].controltovalidate.indexOf(formName) + " valid?" + Page_Validators[i].isvalid);
                if (Page_Validators[i].controltovalidate.indexOf(formName) >= 0 && !Page_Validators[i].isvalid)
                {
                    customIsValid = false;
                    SetErrorStyle(Page_Validators[i].controltovalidate);
                    invalidControls[Page_Validators[i].controltovalidate] = true;
                }
                else
                {
                    if (!invalidControls[Page_Validators[i].controltovalidate]) // don't remove error style if there was already an error
                    RemoveErrorStyle(Page_Validators[i].controltovalidate);
                }
            }
        } 
    }
    Page_IsValid = customIsValid;
    if (customIsValid)
    {
        for (var i = 0; i < Page_Validators.length; i++)
        {
            if (Page_Validators[i].controltovalidate.indexOf(formName) < 0)
                ValidatorEnable(Page_Validators[i], false);
        }            
        var theForm = document.forms['ctl04'];
        if (!theForm)
            theForm = document.ctl04;
    }
    else
        document.getElementById(errorLabel).style.display = "";
}

function SetErrorStyle(id)
{
    document.getElementById(id).style.border = "solid 1px #cc6666";
    document.getElementById(id).style.backgroundColor =  "#ffe9e9";
}
function RemoveErrorStyle(id)
{
    document.getElementById(id).style.borderStyle = "";
    document.getElementById(id).style.backgroundColor = "#ffffff";
}

function CheckValidationField(controlName)
{
    for (var i = 0; i < Page_Validators.length; i++)
    {
        if(Page_Validators[i].controltovalidate != undefined && Page_Validators[i].controltovalidate.indexOf(controlName) >= 0)
        {
            if (ValidatorValidate(document.getElementById(Page_Validators[i].id)) && Page_Validators[i].isvalid)
                RemoveErrorStyle(Page_Validators[i].controltovalidate);
            else
                SetErrorStyle(Page_Validators[i].controltovalidate);
        }
    }
}