﻿// the enter button is hit //
function hitEnter(event) {
    if (event.keyCode == 13) {
        var txtFocus = false;
        var textBoxes = document.getElementsByTagName('textarea');

        for (var i = 0; i < textBoxes.length; i++) {
            var elem = textBoxes[i];

            if (elem) {
                elem.onfocus = function() {
                    txtFocus = true;
                }
                elem.onblur = function() {
                    txtFocus = false;
                }
            }
        }
        if (!txtFocus) {
            // do nothing on click event
        } else {
            document.getElementById('btnSubmit').click();
            return false;
        }
    }
}

function validateName(source, args) {
    var RegExName1 = /^[a-zA-Z0-9\s\'`.]{5,50}$/;
    var RegExName2 = /^[a-zA-Z0-9\s\'`.]+$/;
    var name = document.getElementById('tbName').value;
    var cvName = document.getElementById('cvName');
    var error1 = '<div style="padding:6px 0 0 0;">Required</div>';
    var error2 = "Name field must contain at least 5 characters.";
    var error3 = "Allowable characters: a-z A-Z 0-9 ' `.";
    if (name != '') {
        if (RegExName2.test(name) == true) {
            if (RegExName1.test(name) == true) {
                args.IsValid = true;
            } else {
                cvName.innerHTML = error2;
                args.IsValid = false;
            }
        } else {
            cvName.innerHTML = error3;
            args.IsValid = false;
        }
    } else {
        cvName.innerHTML = error1;
        args.IsValid = false;

    }
}
function validateTitle(source, args) {
    var RegExItem1 = /^[a-zA-Z0-9\s\'`.]{3,50}$/;
    var RegExItem2 = /^[a-zA-Z0-9\s\'`.]+$/;
    var title = document.getElementById('tbTitle').value;
    var cvTitle = document.getElementById('cvTitle');
    var error1 = '<div style="padding:4px 0 0 0;">Please enter a title.</div>';
    var error2 = "Title field must contain at least 3 characters.";
    var error3 = "Allowable characters: a-z A-Z 0-9 ' `.";
    if (title != '') {
        if (RegExItem2.test(title) == true) {
            if (RegExItem1.test(title) == true) {
                args.IsValid = true;
            } else {
                cvTitle.innerHTML = error2;
                args.IsValid = false;
            }
        } else {
            cvTitle.innerHTML = error3;
            args.IsValid = false;
        }
    } else {
        cvTitle.innerHTML = error1;
        args.IsValid = false;

    }
}
function validateServiceAffiliation(source, args) {
    var RegExServiceAffiliation1 = /^[a-zA-Z0-9\s\'`.]{5,100}$/;
    var RegExServiceAffiliation2 = /^[a-zA-Z0-9\s\'`.]+$/;
    var serviceAffiliation = document.getElementById('tbServiceAffiliation').value;
    var cvServiceAffiliation = document.getElementById('cvServiceAffiliation');
    var error1 = '<div style="padding:4px 0 0 0;">Required</div>';
    var error2 = "Service Affiliation field must contain at least 5 characters.";
    var error3 = "Allowable characters: a-z A-Z 0-9 ' `.";
    if (serviceAffiliation != '') {
        if (RegExServiceAffiliation2.test(serviceAffiliation) == true) {
            if (RegExServiceAffiliation1.test(serviceAffiliation) == true) {
                args.IsValid = true;
            } else {
                cvServiceAffiliation.innerHTML = error2;
                args.IsValid = false;
            }
        } else {
            cvServiceAffiliation.innerHTML = error3;
            args.IsValid = false;
        }
    } else {
        cvServiceAffiliation.innerHTML = error1;
        args.IsValid = false;

    }
}
function validateQuestion(source, args) {
    var RegExQuestion1 = /^[a-zA-Z0-9\s\$!+-. @%\'&*()`%?\;:"\\\/]{20,1000}$/;
    var RegExQuestion2 = /^[a-zA-Z0-9\s\$!+-. @%\'&*()`%?\:;"\\\/]+$/;
    var question = document.getElementById('tbQuestion').value;
    var cvQuestion = document.getElementById('cvQuestion');
    var error1 = '<div style="padding:4px 0 0 0;">Required</div>';
    var error2 = "Question field must be between 20-1000 characters.";
    var error3 = "Allowable characters: a-z A-Z 0-9 $ @ ! + - ' . ` & * ( ) ? % \" : ; \\ \/";
    if (question != '') {
        if (RegExQuestion2.test(question) == true) {
            if (RegExQuestion1.test(question) == true) {
                args.IsValid = true;
            } else {
                cvQuestion.innerHTML = error2;
                args.IsValid = false;
            }
        } else {
            cvQuestion.innerHTML = error3;
            args.IsValid = false;
        }
    } else {
        cvQuestion.innerHTML = error1;
        args.IsValid = false;
    }
}
function validateEmail(source, args) {
    var RegExEmail1 = /^\w+([-+.\']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    var email = document.getElementById('tbEmail').value;
    var cvEmail = document.getElementById('cvEmail');
    var error1 = '<div style="padding:4px 0 0 0;">Required</div>';
    var error2 = "Must be in a valid email format. ex. <i>johndoe@lifequest.com</i>";
    if (email != '') {
        if (RegExEmail1.test(email) == true) {
            cvEmail.innerHTML = '';
            args.IsValid = true;
        } else {
            cvEmail.innerHTML = error2;
            args.IsValid = false;
        }
    } else {
        cvEmail.innerHTML = error1;
        args.IsValid = false;
    }
}
function showChar(showAll) {
    var tbChar = document.getElementById('tbCharLeft');
    if (showAll == false) {
        tbChar.value = '';
    } else {
        tbChar.value = 'Characters left: 1000';
    }
}
function countChar(this_field, show_word_count, show_char_count) {
    if (show_word_count == null) {
        show_word_count = true;
    }
    if (show_char_count == null) {
        show_char_count = false;
    }
    var char_count = this_field.value.length;
    var fullStr = this_field.value + " ";
    var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
    var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
    var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
    var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
    var splitString = cleanedStr.split(" ");
    var word_count = splitString.length - 1;
    var tbChar = document.getElementById('tbCharLeft');
    var tbDescription = document.getElementById('tbQuestion');
    var wordLength = 1000;

    if (fullStr.length < 2) {
        word_count = 0;
    }
    if (word_count == 1) {
        wordOrWords = " word";
    }
    else {
        wordOrWords = " words";
    }
    if (char_count == 1) {
        charOrChars = " character";
    } else {
        charOrChars = " characters";
    }
    if (show_word_count & show_char_count) {
        alert("Word Count:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
    }
    else {
        if (show_word_count) {
            alert("Word Count:  " + word_count + wordOrWords);
        }
        else {
            if (show_char_count) {
                //alert ("Character Count:  " + char_count + charOrChars);
                var charRemain = wordLength - char_count;
                if (char_count > wordLength) {
                    tbDescription.value = tbDescription.value.substring(0, wordLength);
                    tbChar.value = 'Characters left: 0';
                } else if (char_count >= 900) {
                    tbChar.style.color = "red";
                    tbChar.value = 'Characters left: ' + charRemain;
                } else {
                    tbChar.style.color = "black";
                    tbChar.value = 'Characters left: ' + charRemain;
                }
            } else {
                //alert("somethings wrong!"); 
            }
        }
    }
    return word_count;
}


