﻿//**********************************************************************************
// THIS SCRIPT WILL FORCE CLIENT SIDE VALIDATION ON BLUR OF THE CONTROL
// TO WORK MAKE THIS WORK:
// 1. ADD THE CONTROL AND ASSOCIATED ASP CUSTOM VALIDATOR. 
// 2. NAME THE CONTROLS SIMLIAR IE: txtControl valControlToValidate
// 3. ADD onblur="forceClientValidation(this.id) TO THE CONTROL THAT
//    WILL TRIGGER THE VALIDATION.
// 4. ADD THIS TO THE HTML <input type="hidden" id="hidControlToValidate" />
//    (IF USING CONTENT PAGES, PLACE HIDDEN FIELD ON MASTER PAGE)
// 5. IF PAGE WITH VALIDATION IS NOT A CONTENT PAGE, CALL
//    onblur="forceClientValidationNonContentPage(this.id)
// BY DEFAULT, IF THERE IS AN ERROR LOCATING THE CONTROL DURING THE VALIDATION
// THE SCRIPT WILL RETURN ISVALID TO ALLOW THE FORM TO POST BACK TO THE SERVER
//**********************************************************************************/
function forceClientValidation(controlToValidate) {
    //****************************************************************************************
    //Find the first capital letter in the name of the control that was passed to the function
    //trim all letters before that capital letter and replace with 'val'
    //it is IMPORTANT!!! to name all validation similar to the control it will be validating
    //i.e.: txtMyTextbox should have a corresponding validator valMyTextBox
    //****************************************************************************************
    
    //USE THIS IF USING CONTENT PAGES
    var controlIdStart;         //location of first _ from end of string
    var controlPrefix;          //holder for the prefix ie: ctrl100_Main_
    var updatedControlID;       //updated controld id for validation ie: valMyTextBox

    //START AT THE ENND OF THE STRING AND GO BACKWARDS TO FIRST _
    //TO GET THE ACTUAL CONTROL ID THAT IS BENG VALIDATED
    for (var x = controlToValidate.length - 1; x >= 0; x--) {
        var char = controlToValidate.substring(x, x + 1);
        if (char == '_') {
            controlPrefix = controlToValidate.substring(0, x + 1);
            //alert(controlPrefix);
            updatedControlID = controlToValidate.substring(x + 1);
            //alert(updatedControlID);
            controlIdStart = x + 1;
            break;
        }
    }

    //FROM THE ACTUAL CONTROL ID, START AT THE BEGINNING AND LOOK FOR THE FIRST CAPITAL LETTER
    //REPLACE EVERYTHING PRIOR TO THE FIRST CAPITAL WITH 'val'
    for (var z = 0; z < updatedControlID.length; z++) {
        var char = updatedControlID.substring(z, z + 1);
        if (char.match(/^[A-Z]/) != null && x > 0) {
            updatedControlID = 'val' + updatedControlID.substring(z);
            break;
        }
    }

    var val = document.getElementById(controlPrefix + updatedControlID);

    //if we were able to find a validator by this name...
    if (val != null) {
        //Get reference to the hiddenField so we can update its value to the correct control to validate
        //-var hidControlToValidate = document.getElementById('hidControlToValidate');
        //-if (hidControlToValidate != null) {
            //Update the reference -- validation will search for the control by the id contained in this field
        //-    hidControlToValidate.value = controlToValidate;
            //Cause validation
            ValidatorValidate(val);
        //-}
    }
}

//*****************************************
//DOES NOT TRY TO ACCOMMODATE FOR APPENDED
//CONTROL PREFIX THAT IS ADDED TO CONTROLS
//THAT ARE INSIDE A CONTENT PAGE
//IE: CT100_MAIN_....
//*****************************************
function forceValidationNonContentPage(controlToValidate) {

    var substringStart;

    for (var x = 0; x < controlToValidate.length; x++) {
        var char = controlToValidate.substring(x, x + 1);
        if (char.match(/^[A-Z]/) != null && x > 0) {
            substringStart = x;
            break;
        }
    }

    var val = document.getElementById('val' + controlToValidate.substring(substringStart));

    //if we were able to find a validator by this name...
    if (val != null) {
        //Get reference to the hiddenField so we can update its value to the correct control to validate
        var hidControlToValidate = document.getElementById('hidControlToValidate');
        if (hidControlToValidate != null) {
            //Update the reference -- validation will search for the control by the id contained in this field
            hidControlToValidate.value = controlToValidate;
            //Cause validation
            ValidatorValidate(val);
        }
    }
}
	
//***********************************************************
//VALIDATE THAT A DROP DOWN HAS A SELECTED INDEX > 0
//***********************************************************
function validateRequiredDDL(source, args) {
    /*
    //Locate the dropdown that that just fired the onblur event and validate it
    var hiddenControlID = document.getElementById('hidControlToValidate').value;
    if(hiddenControlID != null && hiddenControlID != ''){    
        var dropdown = document.getElementById(hiddenControlID);
        if (dropdown != null) {
            if (dropdown.selectedIndex > 0) {
                args.IsValid = true;
                //dropdown.style.borderColor = '';
            }
            else {
                args.IsValid = false;
                //dropdown.style.borderColor = 'red';
            }
        }
        //******************************************************
        //THIS SHOULD NEVER HAPPEN BUT IF WE COULDN'T FIND
        //REFERENCE TO THE CORRECT CONTROL, CLEAR THE VALIDATION
        //AND RELY ON SERVERSIDE VALIDATION!!!
        //CHANGE THIS TO FALSE TO FORCE INVALID IF THIS HAPPENS
        //******************************************************
        else {
            args.IsValid = true;
        }
        document.getElementById('hidControlToValidate').value = '';
    }
    *//*
    if (args.Value != "--Select--" && args.Value != "MM" && args.Value != "YYYY") {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }*/

}
    
//*******************************************************
//VALIDATE THAT A TEXTBOX IS NOT EMPTY
//*******************************************************	
function validateRequiredTXT(source, args) {
    //Locate the dropdown that that just fired the onblur event and validate it
    var text = document.getElementById(document.getElementById('hidControlToValidate').value);
    if (text != null) {
        if (text.value != '') {
            args.IsValid = true;
            //text.style.borderColor = '';
        }
        else {
            args.IsValid = false;
            //text.style.borderColor = 'red';
        }
    }
    //******************************************************
    //THIS SHOULD NEVER HAPPEN BUT IF WE COULDN'T FIND
    //REFERENCE TO THE CORRECT CONTROL, CLEAR THE VALIDATION
    //AND RELY ON SERVERSIDE VALIDATION!!!
    //CHANGE THIS TO FALSE TO FORCE INVALID IF THIS HAPPENS
    //******************************************************
    else {
        args.IsValid = true;
    }
}
    
//*********************************************************
//VALIDATE THAT A TEXTBOX CONTAINS AN EMAIL ADDRESS
//*********************************************************
function validateEmail(source, args) {
    //Locate the dropdown that that just fired the onblur event and validate it
    var emailText = document.getElementById(document.getElementById('hidControlToValidate').value);
    if (emailText != null) {
        if(emailText.value.match(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/)){
            args.IsValid = true;
            //emailText.style.borderColor = '';
        }
        else {
            args.IsValid = false;
            //emailText.style.borderColor = 'red';
        }
    }
    //******************************************************
    //THIS SHOULD NEVER HAPPEN BUT IF WE COULDN'T FIND
    //REFERENCE TO THE CORRECT CONTROL, CLEAR THE VALIDATION
    //AND RELY ON SERVERSIDE VALIDATION!!!
    //CHANGE THIS TO FALSE TO FORCE INVALID IF THIS HAPPENS
    //******************************************************
    else{
        args.IsValid = true;
        //emailText.style.borderColor = '';
    }
}
