$(function() {


    function popErrorDialog(msg) {
        $("#error-dialog").dialog("destroy");
        $("#error-dialog>#errMsg").html(msg);
		$("#error-dialog").dialog({
			height: 140,
			modal: true,
            buttons: {
                'OK': function() {
					$("#error-dialog").dialog('close');
				}
            }
		});
    }


    function checkLength(o,n,min,max) {

        if ( o.val().length > max || o.val().length < min ) {
            o.addClass('ui-state-error');
            popErrorDialog("Length of " + n + " must be between "+min+" and "+max+" characters.")
            return false;
        } else {
            return true;
        }

    }

    function checkPhoneNumber(o)
    {
        var scrubbedNumber = o.val().replace(/[^0-9]/g,"");

        if (scrubbedNumber.length > 0)
        {
            if (scrubbedNumber.length != 10)
            {
                o.addClass('ui-state-error');
                popErrorDialog("Phone number must have 10 digits.");
                return false;
            } else {
                return true;
            }
        } else
        {
            return true;
        }
    }

    function checkEntered(o){
        if (o.val().length == 0)
        {
            o.addClass('ui-state-error');
            popErrorDialog("<b>" + o.prev('label').html().replace(":","") + "</b> is required.");
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o,regexp,n) {

        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass('ui-state-error');
            popErrorDialog('Email is incorrect. Must be in the format: ' + n);
            return false;
        } else {
            return true;
        }

    }



    function checkMatch(o,n){
        if (o.val() != n.val())
        {
            o.addClass('ui-state-error');
            popErrorDialog("<b>" + o.prev('label').html().replace(":","") + "</b> and <b>" + n.prev('label').html().replace(":","") + "</b> do not match.");
            return false;
        } else
        {
            o.removeClass('ui-state-error');
            n.removeClass('ui-state-error');
            return true;
        }
    }


    $("input#phone").keyup(function() {

        var curchr = this.value.length;
        var curval = $(this).val();

        if (curchr == 3) {
            $("input#phone").val( curval + "-");
        } else if (curchr == 7) {
            $("input#phone").val(curval + "-");
        }
    });

    //$("#updateuth input").live("focus", function() {$(this).removeClass('ui-state-error');});
        $("form[id='interest']").submit(function(event){
            

            //event.preventDefault();


            // get form values

            var firstname   = $('form#interest input#firstname');
            var lastname    = $('form#interest input#lastname');
            var email       = $('form#interest input#email');
            var phone       = $('form#interest input#phone');


            var bValid = true;
            //bValid = bValid && checkMatch(password, retypepassword);
            //bValid = bValid && checkSecurityQuestion(securityquestion, securityanswer);
            bValid = bValid && checkEntered(firstname);
            bValid = bValid && checkEntered(lastname);
            bValid = bValid && checkPhoneNumber(phone);
            bValid = bValid && checkEntered(email);
            bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i," me@example.com");

            if (bValid)
            {

                return true;

            } // end if bValid
            else
            {
                return false;
            }
        });

        $("input").focus(function() {
            $(this).removeClass('ui-state-error');
        });

});
