﻿$(document).ready(function() {
    //  This is where we will wire up the CSS Elements to be checked  //
    formNotValid();
    $('.numAnswer').blur(function() {
        var tmp = $('#ctl00_ContentPlaceHolder1_C_EvaluationsV11_isValidated').val();
       
            if ($(this).val() == "" || $(this).val() == 0) {
                $(this).prev('selected'); ;
                $(this).css({ backgroundColor: "white" });
                $(this).val(0);
            }
            else {
                if (isOnetoFive($(this).val()) == false) {

                    $(this).prev('selected');
                    $(this).css({ backgroundColor: "red" });
                }
                else {
                    $(this).prev('selected'); ;
                    $(this).css({ backgroundColor: "white" });

                }
            }
      
        isFormValid();
    });

    $('#ctl00_ContentPlaceHolder1_ETxt_TargetNum').blur(function() {
        var tmp = $('#ctl00_ContentPlaceHolder1_ETxt_TargetNum').val();

        if (isWholeNum(tmp) == false) {
            $('#ctl00_ContentPlaceHolder1_ETxt_TargetNum').css({ backgroundColor: "red" });
            setTimeout("$('#ctl00_ContentPlaceHolder1_ETxt_TargetNum').focus();", 1);
            setTimeout("$('#ctl00_ContentPlaceHolder1_ETxt_TargetNum').select();", 1);
        }
        else {
            $('#ctl00_ContentPlaceHolder1_ETxt_TargetNum').css({ backgroundColor: "white" });
        }

    });
});

//  The validation functions go here   //
function formNotValid() {
    $('.numAnswer').each(function(i) {
        if ($(this).val() == "" || $(this).val() == "Null") {
            $(this).val(0);
            //$('.numAnswer:first')
           
        }
        else {
            var checked = isOnetoFiveTest($(this).val());
            if (!checked) {
                $(this).css({ backgroundColor: "red" });
                $('#ctl00_ContentPlaceHolder1_C_EvaluationsV11_isValidated').val(1);
            }
        }

    });
    $('.numAnswer:first').focus().select(); // :input:visible:enabled:first").focus();
}
function isFormValid() {
    var tmp = 0;
    $('.numAnswer').each(function(i) {
       // alert(this.style.backgroundColor);
        if (this.style.backgroundColor == 'red') {
            //tmp = $('#ctl00_ContentPlaceHolder1_C_EvaluationsV11_isValidated').val();

            tmp += 1;
            //alert(this.style.backgroundColor + "The count is now: " + tmp);
            $('#ctl00_ContentPlaceHolder1_C_EvaluationsV11_isValidated').val(tmp);
        }
        $('#ctl00_ContentPlaceHolder1_C_EvaluationsV11_isValidated').val(tmp);
    });
    
}

function isUpper(theVal) {
   
}

function isInt(theVal) {
    var myMatch = /^-?\d+$/;
    if (!(myMatch.test(theVal))) {
        alert("The value entered is not a valid Integar.  Please reenter that data");
        return false;
    }
    return true;
}

function isWholeNum(theVal) {
    var myMatch = /^-?\d+$/.test(theVal);
    if(myMatch == false)
    {
        alert("This field requires a numeric entry only.  Please reenter the value.");
        return false;
    }
    return true;
}

function isOnetoFive(theVal) {
    var myMatch = /[0,1,2,3,4,5]/.test(theVal);
    if (theVal.length > 1 || theVal.length < 1) {
        alert("The value entered may only be a single digit between 1 and 5.  Please try again.");
        return false;
    }
    if (myMatch == false) {
        alert("The value entered may only be a single digit between 1 and 5.  Please try again.");
        return false;
    }
    return true;
}

function isOnetoFiveTest(theVal) {
    var myMatch = /[0,1,2,3,4,5]/.test(theVal);
    if (theVal.length > 1 || theVal.length < 1) {
        
        return false;
    }
    if (myMatch == false) {
       
        return false;
    }
    return true;
}

function isLower(theVal) {

}

function hasNoNumber(theVal) {
var myMatch = /[^0-9]/;
if(!(myMatch.test(theVal)))
{
    alert("The value input cannot contain any numeric values.  Please reenter the value.");
    return false;
}
return true;
}

function isSSN(theval) {
var myMatch = /^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$/;
if(!(myMatch.test(theval)))
{
    alert("The input was not a valid SSN.  Please reenter the value");
    return false;
}
return true;
}

function isValidEmail(theVal) {
var myMatch = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
if(!(myMatch.test(theVal)))
{
    alert("The value entered is not a valid email.  Please reenter the value.");
    return false;
}
return true;
}

function isDecimal(theVal) {
var myMatch     = /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/;
if(!(myMatch.test(theVal)))
{
    alert("The value entered is not valid.  Please reenter the information.");
    return false;
}
return true;
}
