//WHEN DOM LOADS
$(document).ready(function () {

    if ($('.form').length) {
        $('input').blur(function() {
            validForm();
        });
    }
});

/*VALIDATION FIELDS*/

function validForm() {

    var errorContainer = $(".errorMsg");

    var reqEmail = "Error, fill in the e-mail correctly.";
    var reqName = "Error, fill in the name correctly.";
    var reqSurname = "Error, fill in the surname correctly";
	var reqCompany = "Error, fill in the company correctly"
	
    var sucesso = false;
    errorContainer.html("");

	//console.log($('input.surname').val());
    if ( $('input.surname').val() == '') 
	{
        errorContainer.html(reqSurname);
        sucesso = false;
    }
    else 
	{
        sucesso = true;
		
		if($('input.name').val() == '')
		{
			errorContainer.html(reqName);
       		sucesso = false;
		}
		else
		{
			sucesso = true;
		
			if ($('input.company').val() == '') 
			{
                        errorContainer.html(reqCompany);
                        sucesso = false;
            } 
			else
			{		
				sucesso = true;
	
				if ($('input.email').val() == '')
				{
					errorContainer.html(reqEmail);
					sucesso = false;
				}
				else 
				{
					var inputValue = String($('input.email').val());
					if (!inputValue.match(/^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,6}$/i)) 
					{
						errorContainer.html(reqEmail);
					   // console.log("fail email");
						sucesso = false;
					}
				}
			}
		}
	}

	if (sucesso) {
    	errorContainer.html("");
        $('.btnSubscribe').removeAttr('disabled');
		$('.btnSubscribe').css('background','#ff8200');
    }
    else {
        $('.btnSubscribe').attr('disabled', 'disabled')
		$('.btnSubscribe').css('background','#737a7e');
        //return false;
     }

}
