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

	registerAgencyController.$inject = [ 'registrationService'];

	function registerAgencyController(registrationService) {
		var ctrl = this;
		ctrl.registerObj = {};
		ctrl.validationRules = {
			name : "[A-Za-z-][A-Za-z-']*",
			agencyName : "(?!\s)(?!.*\s$)(?=.*[a-zA-Z0-9])[a-zA-Z0-9 '~?!]{2,}",
			email : "[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,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]+",
			tinL2 : "[0-9]{2}",
			tinL7 : "[0-9]{7}"
		};
$('#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");
    } 
});
$('#ein_a').on('input', function(){
    var regex 	= /^[0-9]{2}/;
    var ein_a= $('#ein_a').val();
    if(regex.test(ein_a)){
$(this).attr("aria-invalid","false");
    	$('#tin_a_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#tin_a_error').html("Enter first 2 digits of your TIN");
    } 
});
$('#ein_b').on('input', function(){
    var regex 	= /^[0-9]{7}/;
    var ein_b= $('#ein_b').val();
    if(regex.test(ein_b)){
$(this).attr("aria-invalid","false");
    	$('#tin_b_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#tin_b_error').html("Enter rest of 7 digits of your TIN");
    } 
});
$('#agencyName').blur(function(){
    if(!$(this).val()){
$('#agencyName_error').html("Invalid Agency Name");
    
    } else {
    		$('#agencyName_error').html("");
    } 
});
$('#ownerFirstName').blur(function(){
    if(!$(this).val()){
$('#ownerFirstName_error').html("Invalid Owner First Name");
    
    } else {
    		$('#ownerFirstName_error').html("");
    } 
});
$('#ownerLastName').blur(function(){
    if(!$(this).val()){
$('#ownerLastName_error').html("Invalid Owner Last Name");
    
    } else {
    		$('#ownerLastName_error').html("");
    } 
});
$('#streetAddress').blur(function(){
    if(!$(this).val()){
$('#streetAddress_error').html("Enter Valid Street address");
    
    } else {
    		$('#streetAddress_error').html("");
    } 
});
$('#city').blur(function(){
    if(!$(this).val()){
$('#city_error').html("Enter valid City");
    
    } else {
    		$('#city_error').html("");
    } 
});
$('#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");
    } 
});
$('#contactEmail').on('input', function(){
    var regex 	= /^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
    var contactEmail = $('#contactEmail').val();
    if(regex.test(contactEmail)){
$(this).attr("aria-invalid","false");
    	$('#contactEmail_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#contactEmail_error').html("Invalid Email Id");
    } 
});
$('#contactPhone_a').on('input', function(){
    var regex 	= /^[0-9]{3}/;
    var contactPhonea = $('#contactPhone_a').val();
    if(regex.test(contactPhonea)){
$(this).attr("aria-invalid","false");
    	$('#phone_aa_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#phone_aa_error').html("Enter Valid 3 digit area code");
    } 
});
$('#contactPhone_b').on('input', function(){
    var regex 	= /^[0-9]{3}/;
    var contactPhoneb = $('#contactPhone_b').val();
    if(regex.test(contactPhoneb)){
$(this).attr("aria-invalid","false");
    	$('#phone_bb_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#phone_bb_error').html("Enter Valid first 3 digit contact number");
    } 
});
$('#contactPhone_c').on('input', function(){
    var regex 	= /^[0-9]{4}/;
    var contactPhonec = $('#contactPhone_c').val();
    if(regex.test(contactPhonec)){
$(this).attr("aria-invalid","false");
    	$('#phone_cc_error').html("");
    } else {
$(this).attr("aria-invalid","true");
    	$('#phone_cc_error').html("Enter Valid last 4 digit contact 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) {
                        console.log(response);
			parentCtrl.userData = {
			    userId : ctrl.registerObj.userId,
			    password : response.TemporaryPassword
			};
			parentCtrl.agencySuccess = true;
		}
		
		ctrl.register = function(parentCtrl) {
			ctrl.registerProgress = true;
			registrationService
					.registerAgency(ctrl.registerObj)
					.then(
							function(response) {
								if (response.MessageStatusCode === '0000') {
									console.log("SUCCESS>>>Agency>>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;
								}
							});
		}
	}
})();