$(document).ready(function () {

    $('textarea[maxlen]').keyup(function () {
        //get the limit from maxlength attribute  
        var limit = parseInt($(this).attr('maxlen'));
        //get the current text inside the textarea  
        var text = $(this).val();
        //count the number of characters in the text  
        var chars = text.length;

        //check if there are more characters then allowed  
        if (chars <= limit) {
            //if valid, remember the text to compare for the next change.
            $(this).data("val", text);
        } else {
            var lastchars = 0;
            if ($(this).data("val") != null) {
                lastchars = $(this).data("val").length;
            }

            var currentCursorPosition = $(this).getCursorPosition() - 1;
            var currentScrollPosition = $(this).scrollTop();
            //and if there are use substr to get the text before the limit  

            if (chars - lastchars <= 2) {
                var new_text = $(this).data("val");
            } else {

                var new_text = text.substr(0, limit);
            }
            //and change the current text with the new text  
            $(this).val(new_text);

            $(this).scrollTop(currentScrollPosition);
            $(this).setCursorPosition(currentCursorPosition);

        }


        if ($(this).attr('lendisplay') != null && $("#" + $(this).attr('lendisplay')) != null) {
            $("#" + $(this).attr('lendisplay')).text($(this).val().length);
        }


    });

});

(function ($, undefined) {
    $.fn.getCursorPosition = function() {
        var el = $(this).get(0);
        var pos = 0;
        if('selectionStart' in el) {
            pos = el.selectionStart;
        } else if('selection' in document) {
            el.focus();
            var Sel = document.selection.createRange();
            var SelLength = document.selection.createRange().text.length;
            Sel.moveStart('character', -el.value.length);
            pos = Sel.text.length - SelLength;
        }
        return pos;
    }
})(jQuery);

(function ($, undefined) {
    $.fn.setCursorPosition = function (pos) {
        if ($(this).get(0).setSelectionRange) {
            $(this).get(0).setSelectionRange(pos, pos);
        } else if ($(this).get(0).createTextRange) {
            var range = $(this).get(0).createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        }
    }
})(jQuery);

function fill_badge_name() {
    document.getElementById(badgename).value = document.getElementById(salutation).value + " " + document.getElementById(firstname).value + " " + document.getElementById(lastname).value;
}
function fill_badge_org() {
    document.getElementById(badgeorg).value = document.getElementById(organization).value;
}

function KeyPressReturnCancelBubble(event) {
    var intKeyCode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
    if (intKeyCode == 13) {
        alert('here');
        event.cancelBubble = true;
    }
    return true;
}

function CheckGrade(thisRb) {
    var marker = document.getElementById("GradeRequiredMarker");
    var markerText;

    if (thisRb.value == "Government") {
        markerText = "*";
    } else {
        markerText = "";
    }

    if (document.all) { //test to see which browser
        marker.innerText = markerText;
    } else {
        marker.textContent = markerText;
    }
}

function CheckSsn(thisCb) {
    var marker = document.getElementById("SsnRequiredMarker");
    var markerText;

    if (!thisCb.checked) {
        markerText = "*";
    } else {
        markerText = "";
    }

    if (document.all) { //test to see which browser
        marker.innerText = markerText;
    } else {
        marker.textContent = markerText;
    }
}



function CheckHowheardOther(thisDd) {
    var marker = document.getElementById("HowheardOtherRequiredMarker");
    var markerText;

    SelectedItemObj = thisDd.options(thisDd.selectedIndex);
    SelectedItemValue = SelectedItemObj.value;

    if (SelectedItemValue.toLowerCase() == "other") {
        markerText = "*";
    } else {
        markerText = "";
    }

    if (document.all) { //test to see which browser
        marker.innerText = markerText;
    } else {
        marker.textContent = markerText;
    }

}

function CheckCitizenshipOther(thisDd) {
    var marker = document.getElementById("CitizenshipOtherRequiredMarker");
    var markerText;

    SelectedItemObj = thisDd.options(thisDd.selectedIndex);
    SelectedItemValue = SelectedItemObj.value;

    if (SelectedItemValue.toLowerCase() == "other") {
        markerText = "*";
    } else {
        markerText = "";
    }

    if (document.all) { //test to see which browser
        marker.innerText = markerText;
    } else {
        marker.textContent = markerText;
    }

}



function ValidateGrade(objSource, objArgs) {
    var boolvalid = true;

    if (document.getElementById("ctl00_MainContentPlaceHolder_Attendeetype_1").value == 'Government') {
        var myObj = document.getElementById("ctl00_MainContentPlaceHolder_Attendeetype_1");
    } else {
        var myObj = document.getElementById("ctl00_MainContentPlaceHolder_Attendeetype_0");
    }

    if (myObj.checked && objArgs.Value == '') {
        boolvalid = false;
    }
    objArgs.IsValid = boolvalid;
}


function ValidateHowheardOther(objSource, objArgs) {
    var boolvalid = true;

    DropDownObj = document.getElementById("ctl00_MainContentPlaceHolder_Howheard");
    SelectedItemObj = DropDownObj.options(DropDownObj.selectedIndex);
    SelectedItemValue = SelectedItemObj.value;

    if (SelectedItemValue.toLowerCase() == "other" && objArgs.Value == '') {
        boolvalid = false;
    }

    objArgs.IsValid = boolvalid;
}

function ValidateCitizenshipOther(objSource, objArgs) {
    var boolvalid = true;

    DropDownObj = document.getElementById("ctl00_MainContentPlaceHolder_Citizenship");
    SelectedItemObj = DropDownObj.options(DropDownObj.selectedIndex);
    SelectedItemValue = SelectedItemObj.value;

    if (SelectedItemValue.toLowerCase() == "other" && objArgs.Value == '') {
        boolvalid = false;
    }

    objArgs.IsValid = boolvalid;
}

function ValidateSsn(objSource, objArgs) {
    //    var boolvalid = true;
    //    alert(boolvalid);
    //    
    //    CheckBoxObj = document.getElementById("ctl00_MainContentPlaceHolder_Ssn_willcall");

    //    if (!CheckBoxObj.checked && (objArgs.Value == '' || objArgs.Value.length != 9 || !IsNumeric(objArgs.Value))) {
    //        boolvalid = false;
    //    }    
    objArgs.IsValid = true;
}


function ValidateExp(objSource, objArgs) {
    //line 140
    var boolvalid = true;

    monthListObj = document.getElementById(ExpMonth);
    monthListSelectedIndex = monthListObj.selectedIndex;
    selectedOptionObj = monthListObj.options[monthListSelectedIndex];
    var month = selectedOptionObj.value;

    yearListObj = document.getElementById(ExpYear);
    yearListSelectedIndex = yearListObj.selectedIndex;
    selectedOptionObj2 = yearListObj.options[yearListSelectedIndex];
    var year = 20 + selectedOptionObj2.value;
    var expDateString = month + '/' + getLastDayOfMonth(month, year) + '/' + year;

    expDateMs = Date.parse(expDateString);

    thisDateObj = new Date();
    thisDateMs = thisDateObj.getTime()


    if (thisDateMs > expDateMs) {
        boolvalid = false;
    }

    objArgs.IsValid = boolvalid;
}

function getLastDayOfMonth(month, year) {

    if (month == 9 || month == 4 || month == 6 || month == 11) {
        return 30;

    } else if (month == 2) {

        //leap year if year-2000 is divisable by 4
        var mynumber = (year - 2000) / 4

        if (Math.round(mynumber) == mynumber) {
            return 29;
        } else {
            return 28;
        }

    } else {
        return 31;
    }
}

function openCSC() {
    mynewwin = window.open("https://www.fbcinc.com/images/csc_large.gif", "mywin", "height=150,width=400");
}

function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

////////////////////////////////////////////////////
// I copied the following email validation 
// function from somewhere on the internet. 
////////////////////////////////////////////////////

function validEmail(email) {
    invalidChars = " /:,;\"";
    if (email == "") {						// cannot be empty
        return false;
    }
    for (i = 0; i < invalidChars.length; i++) {	// does it contain any invalid characters?
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar, 0) > -1) {
            return false;
        }
    }
    atPos = email.indexOf("@", 1); 		// there must be one "@" symbol
    if (atPos == -1) {
        return false;
    }
    if (email.indexOf("@", atPos + 1) != -1) {	// and only one "@" symbol
        return false;
    }
    periodPos = email.indexOf(".", atPos);
    if (periodPos == -1) {					// and at least one "." after the "@"
        return false;
    }
    if (periodPos + 3 > email.length) {		// must be at least 2 characters after the "."
        return false;
    }

    return true;
}

