﻿
function Validate() {
    //alert($find('#' + btnLogin.id));
    //$find('#'+btnLogin.id).attr("disabled", "disabled");
    ClearAllErrors();
    var message = ClientValidation();
    if (message != ''){
         ShowError(message);
         ClearPassword();
         SetFocus();
         return false;            
    }
    else
    {
        return true;
    }
}

function ClientValidation() {
    var message = '';
    var linebreak = '</br>';
    var clientValidation = true;
   
    $(":text").each(function(index, textInput) {
        clientValidation = ValidateEmptyField(textInput.id);
        if (clientValidation == true) {
            clientValidation = ValidateFieldForSpaces(textInput.id);
            if (clientValidation == false) {
                message += textInput.getAttribute("fieldname") + ' cannot have spaces.'+linebreak;
                SetErrorStyle($(this).parents()[0].id);
            }
        }
        else {
            message += textInput.getAttribute("fieldname") + ' cannot be blank.'+linebreak;
            SetErrorStyle($(this).parents()[0].id);
        }
        if (textInput.getAttribute("fieldname") == "Email" && (clientValidation == true)) {
            clientValidation = ValidateEmail(textInput.id);
            if (clientValidation == false) {
                message += 'Please enter a valid email address.'+linebreak;
                SetErrorStyle($(this).parents()[0].id);
            }
        }
    });
    $(":password").each(function(index, textInput) {
        if (textInput.getAttribute("fieldname") == "Password2" && textInput.getAttribute("canbeblank") != null && textInput.getAttribute("canbeblank") == "True")
            return true;
        clientValidation = ValidateEmptyField(textInput.id);
        if (clientValidation == false) {
            message += textInput.getAttribute("fieldname") + ' cannot be blank.' + linebreak;
            SetErrorStyle($(this).parents()[0].id);
        }
    });
     return message;
}

function ShowError(message){
    //$find('#' + '<%=uxBtnLogOn.ClientID%>').removeAttr("disabled");
    $('#' + divErrorClientId).show();
    $('#' + divErrorClientId).html(message);
 }
 function SetErrorStyle(spanId) {
     $('#' + spanId).attr('class', 'error');
 }
 function ClearAllErrors() {
     var staticSpanElements = $('#loginInformation span')
     for (i = 0; i < staticSpanElements.length; i++) {
         var spanId = staticSpanElements[i].id;
         $('#' + spanId).removeAttr("class");
     }
     $('#' + divErrorClientId).hide();
     $('#' + divErrorClientId).html("");

 }

 function ClearPassword() {
     $(":password").each(function(index, password) { password.value = ""; });
     SetFocus();
 }
    

