function filter_Jquery(selector, option, customQuery) {
    var val = $(selector).val();
 
    switch (option) {
        case 'text':
            var pattern = new RegExp('[0-9- ]+', 'g');
            val = val.replace(pattern, '');
            break;

        case 'alpha':
            var pattern = new RegExp('[^a-zA-Z- ]+', 'g');
            val = val.replace(pattern, '');
            break;

        case 'number':
            var pattern = new RegExp('[^0-9- ]+', 'g');
            val = val.replace(pattern, '');
            break;

        case 'number_decimal':
            var pattern = new RegExp('[^0-9-. ]+', 'g');
            val = val.replace(pattern, '');
            break;

        case 'alphanumeric':
            var pattern = new RegExp('[^0-9a-zA-Z- ]+', 'g');
            val = val.replace(pattern, '');
            break;

        case 'custom':
            var pattern = new RegExp(customQuery, 'g');
            var val = val.replace(pattern, '');
            break;

        case 'nospace':
            var pattern = new RegExp('[ ]+', 'g');
            val = val.replace(pattern, '');
            break;
    }
    if (val != this.value)
        this.value = val;

    $(selector).val(val); //reset original box

    return val; //If reqested
}

$(document).ready(function() {

$("#ctl00_MainContent_txtCarbs").keyup(function() { filter_Jquery($(this), "number", ""); });
$("#ctl00_MainContent_txtProtien").keyup(function() { filter_Jquery($(this), "number", ""); });
$("#ctl00_MainContent_txtNewServings").keyup(function() { filter_Jquery($(this), "number", ""); });
$("#ctl00_MainContent_txtAddCalories").keyup(function() { filter_Jquery($(this), "number", ""); });
$("#ctl00_MainContent_txtFat").keyup(function() { filter_Jquery($(this), "number", ""); });
 });

//***Filter Call  
//**THIS IS NOT PERFECT, ALWAYS USE SERVER SIDE TESTING BEFORE SAVING ANYTHING TO THE DB
// EXAMPLE CALL:  this.txtBmiHeight.Attributes.Add("onkeyup", "myFilter(this, 'inValid', '1,2,3,a,s,d', '');");
//
function myFilter(control, filterMode, arguments, filterType)
{
    var controlText = control.value;
    var textLength = controlText.length;
    
    //Number or Letter Filter Type. EXAMPLE ~ myFilter(this, '', '', 'letters');
    if (filterType != '')
    {
        if (filterType == "numbers")//allow only numbers (0-9 and .)
        {
            var numberString = "0,1,2,3,4,5,6,7,8,9,.";
            filter(control, numberString, controlText, textLength, "valid");
        }
        else if (filterType == "letters")//allow only letters (a-z)
        {
            var letterString = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
            filter(control, letterString, controlText, textLength, "valid");
        }
    }

    if (filterType == '')//Custom EXAMPLE ~ (myFilter(this, 'valid', '1,2,3', '');
    {
        if (filterMode == "valid")//only allow these in argument
        {
            filter(control, arguments, controlText, textLength, "valid");
        }
        else if (filterMode == "inValid")//do not allow any of the following
        {
            filter(control, arguments, controlText, textLength, "inValid");
        }
    }   
}

function filter(control, argumentString, controlText, textLength, isValid)
{
    controlText = controlText.toLowerCase();
    var inputArray = controlText.split("");
    for (i = 0; i < inputArray.length; i++)
    {
        var isANumber = argumentString.indexOf(inputArray[i]);
        while (isANumber == -1 & isValid == "valid")//loop in case script is to slow to catch all
        {
            var startString = controlText.substring(0, i);
            var badChar = controlText.substring(i, i + 1);
            var endString = controlText.substring(i + 1, textLength);

            isANumber = argumentString.indexOf(badChar);
            if (isANumber >= 0) break;

            controlText = startString + endString;
            control.value = controlText;
        }
        while (isANumber != -1 & isValid == "inValid")//loop in case script is to slow to catch all
        {
            var startString = controlText.substring(0, i);
            var badChar = controlText.substring(i, i + 1);
            var endString = controlText.substring(i + 1, textLength);

            isANumber = argumentString.indexOf(badChar);
            if (isANumber == -1 || badChar == "") break;
            
            controlText = startString + endString;
            control.value = controlText;

        }
    }
}

//**********************************
// Used in Calorie Counter and Exercise Log Validation
//**********************************
//This Checks the Food being added
function checkFoodBeingAdded()
{
    var libraryFoodTransfer = document.getElementById('ctl00_MainContent_foodLibraryTransfer').value;
    var foodLibrarySplitArray = libraryFoodTransfer.split("|");

    var fullMealName = document.getElementById('ctl00_MainContent_txtAddFood').value;
    if (fullMealName != "")
    {
        var caloriesOfFood = document.getElementById('ctl00_MainContent_txtAddCalories').value;
        if (caloriesOfFood != "")
        {
            var foodCal = fullMealName + " - " + caloriesOfFood;

            //Compare and check for dups
            var isDup = false;
            for (i = 0; i < foodLibrarySplitArray.length; i++)
            {
                //Split current selection into 
                // 0 = Food - Cal
                // 1 = Title(medadata)
                // 2 = tags
                var seperateItems = foodLibrarySplitArray[i].split("~");

                if (seperateItems != "")
                {
                    if (seperateItems[0].toLowerCase() == foodCal.toLowerCase())
                    {
                        isDup = false; //Dup Found, a false will not submit
                        break;
                    }
                    else
                    {
                        isDup = true;
                    }
                }
            }

            if (document.getElementById('ctl00_MainContent_foodLibraryTransfer').value == "")//Check to see if no items in library
            {
                isDup = true;
            }
            if (!isDup)//simple check to look for dups and first food in library for new users
            {
                document.getElementById('ctl00_MainContent_foodAdditionErrorDiv').style.display = 'block';
                document.getElementById('ctl00_MainContent_lblFoodAdditionError').innerHTML = "That Food allready exist in your Library, please try again";
                return false;
            }
            else
            {
                return true;
            }
        }
        else//No Calorie Entered
        {
            document.getElementById('ctl00_MainContent_foodAdditionErrorDiv').style.display = 'block';
            document.getElementById('ctl00_MainContent_lblFoodAdditionError').innerHTML = "Please enter a Calorie amount";
            return false;
        }
    }
    else//No Food Entered
    {
        document.getElementById('ctl00_MainContent_foodAdditionErrorDiv').style.display = 'block';
        document.getElementById('ctl00_MainContent_lblFoodAdditionError').innerHTML = "Please enter a Food";
        return false;
    }
}

//Check Exercises being added.  Checks to see if the Manual Fields are filled out, we are not worried about dups, that is handeld on the server(deleted and replaced)
function checkExerciseBeingAdded() {

//GET selected exercise from ddl and write to javascript transfer

    //Ensure weight and duration is filled out for either options
    var exerciseDuration = document.getElementById('ctl00_MainContent_txtExerciseDuration').value;
    if (exerciseDuration != "") {
        var myWeight = document.getElementById('ctl00_MainContent_txtExerciseWeight').value;
        if (myWeight == "") {
            document.getElementById('ctl00_MainContent_exerciseAdditionErrorDiv').style.display = 'block';
            document.getElementById('ctl00_MainContent_lblExerciseAdditionError').innerHTML = "Please enter your weight or refresh to the page to have it added automatically";
            return false;
        }
    }
    else//No Food Entered
    {
        document.getElementById('ctl00_MainContent_exerciseAdditionErrorDiv').style.display = 'block';
        document.getElementById('ctl00_MainContent_lblExerciseAdditionError').innerHTML = "Please enter a Exercise Duration";
        return false;
    }


    if (document.getElementById('ctl00_MainContent_ckbExerciseManAdd').checked)
    {
        var exerciseName = document.getElementById('ctl00_MainContent_txtAddManExerciseName').value;
        if (exerciseName != "")
        {
            var caloriesOfExercise = document.getElementById('ctl00_MainContent_txtAddManExerciseCal').value;
            if (caloriesOfExercise == "")
            {
                document.getElementById('ctl00_MainContent_exerciseAdditionErrorDiv').style.display = 'block';
                document.getElementById('ctl00_MainContent_lblExerciseAdditionError').innerHTML = "Please enter a Calorie amount";
                return false;
            }
        }
        else//No Food Entered
        {
            document.getElementById('ctl00_MainContent_exerciseAdditionErrorDiv').style.display = 'block';
            document.getElementById('ctl00_MainContent_lblExerciseAdditionError').innerHTML = "Please enter a Exercise Name";
            return false;
        }

    }
}


//**********************************
// Use in Support Group Join or Create Validation
//**********************************
function CheckJoinValidation() {
    var workGroupName = document.getElementById('ctl00_MainContent_txtWorkGroupJoinName').value;
    var workGroupPass = document.getElementById('ctl00_MainContent_txtJoinPass').value;
    document.getElementById('ctl00_MainContent_lblJoinError').style.display = 'none';

    if (document.getElementById("ctl00_MainContent_ckJoinReadUnderstand").checked) {
        if (workGroupName != "ctl00_MainContent_ckCreateReadAndUnderstand") {
            if (workGroupPass == "") {
                document.getElementById('ctl00_MainContent_lblJoinError').style.display = 'block';
                document.getElementById('ctl00_MainContent_lblJoinError').innerHTML = "Password not entered";
            }
            else
            { return true; }
        }
        else {
            document.getElementById('ctl00_MainContent_lblJoinError').style.display = 'block';
            document.getElementById('ctl00_MainContent_lblJoinError').innerHTML = "Support Group Name not entered";
        }
    }
    else {
        document.getElementById('ctl00_MainContent_lblJoinError').style.display = 'block';
        document.getElementById('ctl00_MainContent_lblJoinError').innerHTML = "Please read and accept the support group joining terms by checking the 'I understand' box";
    }
    return false;
}

function CheckCreateValidation() {
    var workGroupName = document.getElementById('ctl00_MainContent_txtWorkGroupNameCreate').value;
    var workGroupPass = document.getElementById('ctl00_MainContent_txtWorkGroupCreatePass').value;
    var workGroupPassConfirm = document.getElementById('ctl00_MainContent_txtWorkGroupCreatePassConfirm').value;
    document.getElementById('ctl00_MainContent_lblCreateError').style.display = 'none';

    if (document.getElementById("ctl00_MainContent_ckCreateReadAndUnderstand").checked) {
        if (workGroupName != "") {
            if (workGroupPass == "") {
                document.getElementById('ctl00_MainContent_lblCreateError').style.display = 'block';
                document.getElementById('ctl00_MainContent_lblCreateError').innerHTML = "Password not entered";
            }
            else {
                if (workGroupPass != workGroupPassConfirm) {
                    document.getElementById('ctl00_MainContent_lblCreateError').style.display = 'block';
                    document.getElementById('ctl00_MainContent_lblCreateError').innerHTML = "Password and Password Confirm do not match";
                }
                else { return true; }
            }
        }
        else {
            document.getElementById('ctl00_MainContent_lblCreateError').style.display = 'block';
            document.getElementById('ctl00_MainContent_lblCreateError').innerHTML = "Support Group Name not entered";
        }
    }
    else {
        document.getElementById('ctl00_MainContent_lblCreateError').style.display = 'block';
        document.getElementById('ctl00_MainContent_lblCreateError').innerHTML = "Please read and accept the support group creation terms by checking the 'I understand' box";
    }
    return false;
}