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

{% block body %}

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

    <form id="clientform" action="{{ path('DorcasTabithaBundle_clientinformation_new') }}" method="post" {{ form_enctype(form) }}>
        {% include 'DorcasTabithaBundle:ClientInformation:clientinformationlayout.html.twig' %}
    </form>
		
{% endblock %}

{% block javascripts %}

    {{ parent() }}

<script type="text/javascript">

$(function() {
//    $( ".datepicker" ).datepicker({ showAnim: "slideDown" });
    $( ".datepicker" ).datepicker({ changeMonth: true, changeYear: true, yearRange:"-90:+0", maxDate: "+0d"
        });
        });

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

warnusers = function(message) {
	$('#override_javascript_alertx').html(message).dialog({
		modal: true,
                title:'Tabitha Warning Message',
			buttons: {
				"Continue to Home Page": function() {
                                        var form = document.forms["clientform"];
                                        form.submit();
					$( this ).dialog( "close" );
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			}
		});
	};


/**--------------------------
//* Validate Date Field script- By JavaScriptKit.com
//* For this script and 100s more, visit http://www.javascriptkit.com
//* This notice must stay intact for usage
---------------------------**/

function checkdate(input){
    var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
    var returnval=false
    if (!validformat.test(input))
    {
//        alert("Invalid Date Format. Please correct and submit again.")
    }
    else{ //Detailed check for valid date ranges
        var monthfield=input.split("/")[0]
        var dayfield=input.split("/")[1]
        var yearfield=input.split("/")[2]
        var dayobj = new Date(yearfield, monthfield-1, dayfield)
        if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
        {
//            alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
        }
        else
            returnval=true
    }
//    if (returnval==false) input.select()
    return returnval
}


function submitFormClient(navnumber) {
    var form = document.forms["clientform"];
    var hiddenField = document.createElement("input"); 
    hiddenField.setAttribute("type", "hidden"); 
    hiddenField.setAttribute("name", "passnav"); 
    hiddenField.setAttribute("value", navnumber); 
    form.appendChild(hiddenField);
    if (navnumber == 9)
    {
        savenext(true)
    }
    else
    {
        savenext(false);
    }
}

function savenext(nav)
{ 
    // Read all values
    var ssn = document.getElementById('clientinformationform_ssn').value;
    var firstName = document.getElementById('clientinformationform_first_name').value;
    var lastName = document.getElementById('clientinformationform_last_name').value;
    var city = document.getElementById('clientinformationform_city').value;
    var state = document.getElementById('clientinformationform_state').value;
    var genderF = document.getElementById('clientinformationform_gender_f').checked;
    var genderM = document.getElementById('clientinformationform_gender_m').checked;
    var birthDate = document.getElementById('clientinformationform_birth_date').value;
    var otherAdultOneSsn = document.getElementById('clientinformationform_other_adult_oneSsn').value;
    var otherAdultTwoSsn = document.getElementById('clientinformationform_other_adult_twoSsn').value;
    var homePhone = document.getElementById('clientinformationform_home_phone').value;
    var cellPhone = document.getElementById('clientinformationform_cell_phone').value;
    var workPhone = document.getElementById('clientinformationform_work_phone').value;
    var email = document.getElementById('clientinformationform_email').value;

    okaytosubmit = true;
    alertMessage = "";

    // Check SSN for blank or length < 11 characters
    if (!ssn || ssn.length < 11)
    {
        alertMessage = alertMessage + '&#8227 Please enter Social Security Number. <br/>';
        okaytosubmit = false;
    }

    // Check First Name for blank
    if (!firstName)
    {
        alertMessage = alertMessage + '&#8227 Please enter First Name. <br/>';
        okaytosubmit = false;
    }

    // Check Last Name for blank
    if (!lastName)
    {
        alertMessage = alertMessage + '&#8227 Please enter Last Name. <br/>';
        okaytosubmit = false;
    }

    // Check City for blank
    if (!city)
    {
        alertMessage = alertMessage + '&#8227 Please enter City. <br/>';
        okaytosubmit = false;
    }


    // Check State for blank
    if (!state)
    {
        alertMessage = alertMessage + '&#8227 Please enter City. <br/>';
        okaytosubmit = false;
    }

    // Check Gender for checked
    if (!genderF && !genderM)
    {
        alertMessage = alertMessage + '&#8227 Please enter Gender. <br/>';
        okaytosubmit = false;
    }

    // Check Home Phone length < 14 characters
    if (homePhone && homePhone.length < 14)
    {
        alertMessage = alertMessage + '&#8227 Please correct Home Phone. <br/>';
        okaytosubmit = false;
    }

    // Check Cell Phone length < 14 characters
    if (cellPhone && cellPhone.length < 14)
    {
        alertMessage = alertMessage + '&#8227 Please correct Cell Phone. <br/>';
        okaytosubmit = false;
    }

    // Check Work Phone length < 14 characters
    if (workPhone && workPhone.length < 14)
    {
        alertMessage = alertMessage + '&#8227 Please correct Work Phone. <br/>';
        okaytosubmit = false;
    }

    // Check Birth Date for blank
    if (!birthDate)
    {
        alertMessage = alertMessage + '&#8227 Please enter Birth Date. <br/>';
        okaytosubmit = false;
    }

    // Check Birth Date for valid date
    if (birthDate && !checkdate(birthDate))
    {
        alertMessage = alertMessage + '&#8227 Please enter a valid Birth Date. <br/>';
        okaytosubmit = false;
    }


    // Check Other Adult One SSN for not blank and length = 11 characters
    if (otherAdultOneSsn && otherAdultOneSsn.length < 11)
    {
        alertMessage = alertMessage + '&#8227 Please correct Other Adult #1 SSN. <br/>';
        okaytosubmit = false;
    }

    // Check Other Adult Two SSN for not blank and length = 11 characters
    if (otherAdultTwoSsn && otherAdultTwoSsn.length < 11)
    {
        alertMessage = alertMessage + '&#8227 Please correct Other Adult #2 SSN. <br/>';
        okaytosubmit = false;
    }
    
    // Check Email for valid (kinda) address   
    if ( email )
    {
        var atpos = email.indexOf("@");
        var dotpos = email.lastIndexOf(".");
        if ( atpos <1 || dotpos < atpos+2 || dotpos+2 >= email.length)
        //if ( email && email.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
    {
        // If HOME Nav is selected, then it is okay to redirect without saving
            if(nav)
            {
                alertMessage = '<p style = "color:red;"> Warning: Your changes to Client Information will not be saved! </p>';
                warnusers(alertMessage);
            }
            else
            {
                alert(alertMessage);
            }
    }
}



</script>
    
{% endblock %}