{% extends "::base.html.twig" %}        

{% block body %}

<div id="override_javascript_alert"></div>
<div id="override_javascript_alertx"></div>

<div id="navbar" style="font-size:18px;">   
    <ul>       
        <li><a href="{{ path('DorcasTabithaBundle_homepage' ) }}">Home</a></li>
        <li><a href="{{ path('DorcasTabithaBundle_search_show' ) }}">Client Search</a></li> 
        <li><a href="{{ path('DorcasTabithaBundle_clientinformation_new' ) }}">New Client</a></li> 
        {% if is_granted('ROLE_ADMIN') == false %}
            <li><a href="{{ path('DorcasTabithaBundle_profile_show' ) }}" 
                style="background-color:#00b5cb; font-size:30px;">User Profile</a></li>
        {% endif %}  
    </ul> 
</div>

    {% if app.session.hasFlash('notice') %}
                    <div class="flash-notice" >
                        <br />
                        {{ app.session.flash('notice') }}
                    </div>
        {% endif %}

    <section class="user" id="users">
                {# .. #}
       
    {% if valid is not defined %}
        {% if form is defined %}
            <div style="color:maroon;">
            {{ form_errors(form) }}
            </div>
        {% endif %}
    {% endif %}

        {% render 'DorcasTabithaBundle:Profile:update' %}
    </section>

    </div>
</div>

{% endblock %}

{% block javascripts %}

    {{ parent() }}           
 
<script type="text/javascript">


window.alert = function(message) {
	$('#override_javascript_alert').html(message).dialog({
		modal:true,
		title:'Tabitha Validation Message',
		buttons: {
			'OK':function(){
				$(this).dialog('close');
			}
		}
	});
};


function save(nav)
{ 
    // Read all values

    var username = document.getElementById('userform_username');
    var firstname = document.getElementById('userform_first_name');
    var lastname = document.getElementById('userform_last_name');
    var passwordfirst = document.getElementById('userform_password_first');
    var passwordsecond = document.getElementById('userform_password_second');
    var phone = document.getElementById('userform_phone');
    var email = document.getElementById('userform_email');

    okaytosubmit = true;
    alertMessage = "";

    // Check username for length < 5 characters
    if ( username.value.length < 5 )
    {
        alertMessage = alertMessage + '&#8227 Username must have at least 5 <br/>&nbsp; characters.<br/>';
        okaytosubmit = false;
    }

    // Check Password First not blank and for length < 8 chanracters
    if ( passwordfirst.value != '' && passwordfirst.value.length < 8 )
    {
        alertMessage = alertMessage + '&#8227 Password must have at least 8 <br/>&nbsp; characters.<br/>';
        okaytosubmit = false;
    }

    // Check Password Second not blank and for length < 8 chanracters
    else if ( passwordsecond.value != '' && passwordsecond.value.length < 8 )
    {
        alertMessage = alertMessage + '&#8227 Confirm Password must have at least 8 <br/>&nbsp; characters.<br/>';
        okaytosubmit = false;
    }

    // Check that passwords match
    if ( passwordfirst.value != passwordsecond.value )
    {
        alertMessage = alertMessage + '&#8227 Passwords do not match.<br/>';
        okaytosubmit = false;
    }


    // Check First Name not blank
    if ( firstname.value == '' )
    {
        alertMessage = alertMessage + '&#8227 Please enter a First Name.<br/>';
        okaytosubmit = false;
    }

    // Check Last Name not blank
    if ( lastname.value == '' )
    {
        alertMessage = alertMessage + '&#8227 Please enter a Last Name.<br/>';
        okaytosubmit = false;
    }

    // Check Phone for < 14 characters
    if ( phone.value && phone.value.length < 14 )
    {
        alertMessage = alertMessage + '&#8227 Please complete Phone number.<br/>';
        okaytosubmit = false;
    }

    // Check Email for valid (kinda) address
    var atpos = email.value.indexOf("@");
    var dotpos = email.value.lastIndexOf(".");
    if ( email.value && atpos <1 || dotpos < atpos+2 || dotpos+2 >= email.value.length)
    //if ( email.value && email.value.indexOf("@") == -1 )
    {
        alertMessage = alertMessage + '&#8227 Please enter a valid Email address.<br/>';
        okaytosubmit = false;
    }

    if (okaytosubmit == true)
    {
        var form = document.forms["clientform"];
        form.submit();
    }
    else
    {
        alert(alertMessage);
    }
}

</script>  

{% endblock %}