(function() {
	'use strict';
	angular.module('pruexpress').controller('registerFinancialController',
			registerFinancialController);

	registerFinancialController.$inject = [ 'registrationService'];

	function registerFinancialController(registrationService) {
		var ctrl = this;
		ctrl.registerObj = {};
		ctrl.validationRules = {
			name : "[A-Za-z-][A-Za-z-']*",
			email : "[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})",
			ssn : "[0-9]{4}",
			address : "[a-zA-Z0-9\\-\\s]+",
			numbersOnly : "\\d+",
			alphabetsOnly : "[a-zA-Z0-9\\-\\s]+", // [a-zA-Z]*Change the * to + if you don't
			// want to allow empty matches
			stateCode : "[a-zA-Z]{2}",
			zipCode : "[0-9]{5}",
			phoneL3 : "[0-9]{3}",
			phoneL4 : "[0-9]{4}",
			userId : "[a-z0-9]+"
		};
$('#firstName').on('input', function(){
    var regex 	= /^[A-Za-z-][A-Za-z-']+$/;
    var firstName = $('#firstName').val();
    if(regex.test(firstName)){
        $(this).attr("aria-invalid","false");
    	$('#fname_error').html("");
    } else {
        $(this).attr("aria-invalid","true");
    	$('#fname_error').html("Invalid First Name");
    } 
});
$('#lastName').on('input', function(){
    var regex 	= /^[A-Za-z-][A-Za-z-']+$/;
    var lastName = $('#lastName').val();
    if(regex.test(lastName)){
$(this).attr("aria-invalid","false");
    	$('#lname_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#lname_error').html("Invalid Last Name");
    }
});
$('#email').on('input', function(){
    var regex 	= /^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
    var email = $('#email').val();
    if(regex.test(email)){
$(this).attr("aria-invalid","false");
    	$('#email_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#email_error').html("Invalid Email Id");
    } 
});
$('#socialSecurityNum').on('input', function(){
    var regex 	= /^[0-9]{4}/;
    var socialSecurityNum = $('#socialSecurityNum').val();
    if(regex.test(socialSecurityNum)){
$(this).attr("aria-invalid","false");
    	$('#ssn_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#ssn_error').html("Enter last four digits of your SSN");
    } 
});
$('#stateCode').on('input', function(){
    var regex 	= /^[a-zA-Z]{2}/;
    var stateCode= $('#stateCode').val();
    if(regex.test(stateCode)){
$(this).attr("aria-invalid","false");
    	$('#statecode_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#statecode_error').html("Enter valid State Code");
    } 
});
$('#zipcode').on('input', function(){
    var regex 	= /^[0-9]{5}/;
    var zipcode= $('#zipcode').val();
    if(regex.test(zipcode)){
$(this).attr("aria-invalid","false");
    	$('#zipcode_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#zipcode_error').html("Enter valid zipcode");
    } 
});
$('#phone_a').on('input', function(){
    var regex 	= /^[0-9]{3}/;
    var phoneAnum= $('#phone_a').val();
    if(regex.test(phoneAnum)){
$(this).attr("aria-invalid","false");
    	$('#phone_a_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#phone_a_error').html("Enter Valid 3 digit area code");
    } 
});
$('#phone_b').on('input', function(){
    var regex 	= /^[0-9]{3}/;
    var phoneBnum= $('#phone_b').val();
    if(regex.test(phoneBnum)){
$(this).attr("aria-invalid","false");
    	$('#phone_b_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#phone_b_error').html("Enter Valid first 3 digit Phone number");
    } 
});
$('#phone_c').on('input', function(){
    var regex 	= /^[0-9]{4}/;
    var phoneCnum= $('#phone_c').val();
    if(regex.test(phoneCnum)){
$(this).attr("aria-invalid","false");
    	$('#phone_c_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#phone_c_error').html("Enter Valid last 4 digit Phone number");
    } 
});
$('#userId').on('input', function(){
    var regex 	= /^[a-z0-9]+$/;
    var userId= $('#userId').val();
    if(regex.test(userId)){
$(this).attr("aria-invalid","false");
    	$('#userid_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#userid_error').html("Enter valid userId");
    }
if ((userId).length < 6){
    $('#userid_min_error').html("At least 6 characters required");
}
    else {
$('#userid_min_error').html("");
    }
});
               ctrl.onSuccess = function(parentCtrl, response) {
			parentCtrl.userData = {
			    userId : ctrl.registerObj.userId,
			    password : response.TemporaryPassword
			};
			parentCtrl.financialSuccess = true;

                        
		}
		ctrl.register = function(parentCtrl) {
			ctrl.registerProgress = true;
			registrationService
					.registerFinancial(ctrl.registerObj)
					.then(
							function(response) {
								if (response.MessageStatusCode === '0000') {
									console.log("SUCCESS>>Financial>>>0000");
									ctrl.onSuccess(parentCtrl, response);
								} else {
									//ctrl.onSuccess(parentCtrl, {});
									if (typeof response.ReturnCodes !== "undefined") {
										ctrl.response = response;
									} else {
										ctrl.response = {
											"ReturnCodes" : [ {
												"Code" : "110",
												"Description" : response.MessageStatusDescription
											} ]
										};
									}

									ctrl.registerProgress = false;
								}
							});
		}
	}
})();