﻿
var controlFromWhichSubmitCame = null;
var groupName = null;
var inputList = null;

var errorList = null;
var notificationList = null;

//Required fields with init conditions.
function RegisterControlForRequiredFieldValidatorWithInitialCondition(grpName, controlName, validationMessage, initialCondition) {

    if (inputList == null) {

        inputList = new Array();

    }

    inputList.push(new InputObj(grpName, controlName, "RequiredFieldValidator", validationMessage, "", "", initialCondition));

}

//Required fields without init conditions.
function RegisterControlForRequiredFieldValidator(grpName, controlName, validationMessage) {

    if (inputList == null) {

        inputList = new Array();

    }

    inputList.push(new InputObj(grpName, controlName, "RequiredFieldValidator", validationMessage, "", "", ""));
    
}

//Required email validation.
function RegisterControlForEmailValidator(grpName, controlName, validationMessage) {

    if (inputList == null) {

        inputList = new Array();

    }

    inputList.push(new InputObj(grpName, controlName, "EmailValidator", validationMessage, "", ""));
}

//Required regular expression validation.
function RegisterControlForRegularExpressionValidator(grpName, controlName, validationMessage, regularExpression) {

    if (inputList == null) {

        inputList = new Array();

    }

    inputList.push(new InputObj(grpName, controlName, "RegularExpressionValidator", validationMessage, regularExpression, "", ""));

}

//Required compare
function RegisterControlForCompareValidator(grpName, controlName, validationMessage, controlToCompare) {

    if (inputList == null) {

        inputList = new Array();

    }

    inputList.push(new InputObj(grpName, controlName, "CompareValidator", validationMessage, "", controlToCompare, ""));
}

//Custom object for error structure.
function InputObj(grpName, controlName, validationType, validationMessage, regularExpression, controlToCompare, initialCondition) {

    this.CosmosValidationGroup = grpName;
    this.CosmosControlName = controlName;
    this.CosmosValidationType = validationType;
    this.CosmosValidationMessage = validationMessage;
    this.CosmosRegularExpression = regularExpression;
    this.CosmosControlToCompair = controlToCompare;
    this.CosmosInitialCondition = initialCondition;
}

function IsValidGroup(currentControlGroupName) {

    var splitList = groupName.split(";");

    for (var count = 0; count < splitList.length; count++) {

        if (splitList[count] == currentControlGroupName) {

            return true;
        }
    }

    return false;

}

//Form validation function.
function validate_form(thisForm) {

    //Reset buffers.
    errorList = null;
    notificationList = null;

    if (controlFromWhichSubmitCame != null) {

        if (inputList != null) {            

            for (var count = 0; count < inputList.length; count++) {                

                if ((inputList[count].CosmosValidationGroup != null) && (inputList[count].CosmosValidationType != null) && (inputList[count].CosmosValidationMessage != null)) {

                    if (IsValidGroup(inputList[count].CosmosValidationGroup) == true) {
                       
                        ValdationSelector(inputList[count]);
                    }
                }               
            }

            // Clear control ref.
            controlFromWhichSubmitCame = null;
            grpName = null;

            if (errorList != null) {
                
                if (errorList.length > 0) {
                    
                    //Has errors and build message list
                    $.fn.notificationBar.showMessage({ MsgTitle: 'Required information has not been provided.', TitleIcon: 'Warning', MsgList: errorList, DefaultIcon: 'Alert', Time: 8000 });
                    
                    //Has errors and build identifier list                  
                    $.fn.notificationBar.highlightValidatedAreas(notificationList);

                    //Clear error list stack.
                    errorList = null;

                    return false;
                }
                else {

                    // no errors 
                    return true;
                }
            }
            else {
                return true;
            }
        }
        else {

            return true;
        }
    }
    else {

        return true;
    }

}

//Function which is called on button click
function validate_Submit(grpName, control) {

    controlFromWhichSubmitCame = control;
    groupName = grpName;
}

//Internal method which determines which type of validation to be performed.
function ValdationSelector(inputObj) {

    if (errorList == null) {

        errorList = new Array();
    }

    if (notificationList == null) {

        notificationList = new Array();

    }

    var control = document.getElementById(inputObj.CosmosControlName);
    var typeOfValidation = inputObj.CosmosValidationType;
    var validationMessage = inputObj.CosmosValidationMessage;

    if (control != null) {

        if (typeOfValidation == "RequiredFieldValidator") {

            //determine if select or input
            if ((control.type == "text") || (control.type == "password")) {

                if ((control.value == null) || (control.value == inputObj.CosmosInitialCondition)) {

                    errorList.push({ Msg: validationMessage });
                    notificationList.push(control);
                }
            }
            else if (control.type == "select-one") {                

                if ((control.selectedIndex == -1) && (inputObj.CosmosInitialCondition == "")) {

                    errorList.push({ Msg: validationMessage });
                    notificationList.push(control);

                } else if ((control.options[control.selectedIndex].value == null) || (control.options[control.selectedIndex].value == inputObj.CosmosInitialCondition)) {

                    errorList.push({ Msg: validationMessage });
                    notificationList.push(control);
                }
            }
            else if (control.type == "textarea") {              

                if ((control.value == null) || (control.value == inputObj.CosmosInitialCondition)) {

                    errorList.push({ Msg: validationMessage });
                    notificationList.push(control);
                }
            }
        } //End of RequiredFieldValidator

        if (typeOfValidation == "EmailValidator") {

            if ((control.value != null) && (control.value != "")) {

                var regEx = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;

                var isValid = regEx.test(control.value.toString());
                
                if (isValid == false) {

                    errorList.push({ Msg: validationMessage });
                    notificationList.push(control);
                }
            }
        } //End of EmailValidator

        if (typeOfValidation == "RegularExpressionValidator") {           

            if (control.value != null && control.value != "") {

                if (inputObj.CosmosRegularExpression != null) {

                    var regEx = new RegExp(inputObj.CosmosRegularExpression);

                    var isValid = regEx.test(control.value);                    

                    if (isValid == false) {

                        errorList.push({ Msg: validationMessage });
                        notificationList.push(control);
                    }

                }
                else {

                    errorList.push({ Msg: "CosmosRegularExpression : Attribute should be specified for the use of 'RegularExpressionValidator'." });

                }
            }
        } //End of RegularExpressionValidator

        if (typeOfValidation == "CompareValidator") {

            var controlToCompair = document.getElementById(inputObj.CosmosControlToCompair);

            if (control != null) {

                //determine if select or input
                if (control.type == "text") {

                    if (control.getAttribute("value") != null || control.getAttribute("value") != "") {

                        if (controlToCompair != null) {

                            if (control.getAttribute("value") != controlToCompair.getAttribute("value")) {

                                errorList.push({ Msg: validationMessage });
                                notificationList.push(control);
                            }
                        }
                        else {

                            errorList.push({ Msg: "CompareValidator : Control to compare is not defined or cannot be found'." });

                        }

                    }
                }
                else if (control.type == "textarea") {

                    if (control.value != null || control.value != "") {

                        if (controlToCompair != null) {

                            if (control.value != controlToCompair.value) {

                                errorList.push({ Msg: validationMessage });
                                notificationList.push(control);
                            }
                        }
                        else {

                            errorList.push({ Msg: "CompareValidator : Control to compare is not defined or cannot be found'." });

                        }

                    }

                }
                else if (control.type == "select-one") {

                    if ((control.selectedIndex == -1) && (inputObj.CosmosInitialCondition == " ")) {

                        if (controlToCompair != null) {

                            if (control.options[control.selectedIndex].value != controlToCompair.options[controlToCompair.selectedIndex].value) {

                                errorList.push({ Msg: validationMessage });
                                notificationList.push(control);
                            }
                        }
                        else {

                            errorList.push({ Msg: "CompareValidator : Control to compare is not defined or cannot be found'." });
                        }

                    } else if ((control.options[control.selectedIndex].value != null) || (control.options[control.selectedIndex].value != "")) {

                        if (controlToCompair != null) {

                            if (control.options[control.selectedIndex].value != controlToCompair.options[controlToCompair.selectedIndex].value) {

                                errorList.push({ Msg: validationMessage });
                                notificationList.push(control);
                            }
                        }
                        else {

                            errorList.push({ Msg: "CompareValidator : Control to compare is not defined or cannot be found'." });
                        }
                    }
                }
            }

        } //End of CompareValidator
    
    }//End of !Control.

}
