

$(document).ready(function(){
	//var DOMAIN = "/ad_agency";
	$("#sentMessage").on('submit',(function(e){

				e.preventDefault();
				
				var valid;	
				valid = validateContact();
				if(valid) {
					$.ajax({
					url: DOMAIN+"/includes/testMail.php",
					type: "POST",
					data:  new FormData(this),
					contentType: false,
					cache: false,
					processData:false,
					success: function(data){
					//console.log(data);
						$('#loader-icon').show();
						//$("#mail-status").html(data);
						$('#loader-icon').hide();
						$('#userName').val('');
						$('#email').val('');
						$('#phone').val('');
						$('#message').val('');
						$('#name').val('');
					
					},
				
					error: function(){} 	        
					
					});
				}
			}));

	   function validateContact() {
		var valid = true;	 
		return valid; 
		}
  fetch_company();
  function fetch_company(){

    $.ajax({
      url : DOMAIN+"/includes/userProcess.php",
      method : "get",
      dataType : "json",
      data : {getCompany:1},
      async: false,
      success : function(data){
      			//console.log(data);
       			$("#company_name").text(data[0]['company']);
       			
        
        
      }
    })
  }
  fetch_CurrentFinancialYearId();
  function fetch_CurrentFinancialYearId(){

    $.ajax({
      url : DOMAIN+"/includes/userProcess.php",
      method : "get",
      
      data : {getCurrentFinancialYearId:1},
      async: false,
      success : function(data){
          $("#log_year").html(data);
      }
    })
  }
	////////login
	$("#form_login").on("submit",function(){
		var user = $("#log_user");
		var pass = $("#log_password");
		var financial_year = $("#log_year").val();
		var status = false;
		
		if (user.val() == "") {
			user.addClass("border-danger");
			$('#error_message').show();
			$("#error_message").html("Please Enter Username!!");
			setTimeout(function(){  
                 $('#error_message').fadeOut("Slow");  
            }, 2000);
			status = false;
		}else{
			user.removeClass("border-danger");
			$('#error_message').hide();
			$("#error_message").html("");
			status = true;
		}
		if (pass.val() == "") {
			pass.addClass("border-danger");
			$('#error_message').show();
			$("#error_message").html("Please Enter Password!!");
			setTimeout(function(){  
                 $('#error_message').fadeOut("Slow");  
            }, 2000);
			status = false;
		}else{
			pass.removeClass("border-danger");
			$('#error_message').hide();
			$("#error_message").html("");
			status = true;
		}
		if (status) {
			$(".overlay").show();
			$.ajax({
				url : DOMAIN+"/includes/userProcess.php",
				method : "POST",
				data : $("#form_login").serialize(),
				async: false,
				success : function(data){
				    console.log(data);
					if(data == "USERNAME_NOT_MATCHED"){
						$(".overlay").hide();
						$('#success_message').hide();
						$('#error_message').show();
						user.addClass("border-danger");
						$("#error_message").html("Please Enter Correct Username!!");
						setTimeout(function(){  
			                 $('#error_message').fadeOut("Slow");  
			            }, 2000);
						status = false;
					}
					else if(data == "PASSWORD_NOT_MATCHED"){
						$(".overlay").hide();
						$('#error_message').show();
						pass.addClass("border-danger");
						$("#error_message").html("Please Enter Correct Password!!");
						setTimeout(function(){  
			                 $('#error_message').fadeOut("Slow");  
			            }, 2000);
						status = false;
					}else if(data == "1"){
						$(".overlay").hide();
						pass.removeClass("border-danger");
						$('#error_message').hide();
						$('#success_message').show();
						$("#success_message").html("Login Successfull!!");
						setTimeout(function(){  
			                 $('#success_message').fadeOut("Slow");
			                 window.location.href = DOMAIN+"/purchase.php";  
			            }, 1000);

						
					}

				},
				error: function (error) {
                    console.log(error);
}
			})
		}
	})
	$("#register_form").on("submit",function(){
		var username 			= 		$('#username').val();
		var password 			=		$('#password').val();
		var confirm_password 	=		$('#confirm_password').val();
		var email  				= 		$('#email').val();
		$.ajax({
			url : DOMAIN+"/includes/userProcess.php",
			method : "POST",
			data : $("#register_form").serialize(),
			async: false,
			success : function(data){
			//console.log(data);
				 if(data == 1){
				 	alert("User Added Successfully");
					$("#register_form").trigger( "reset" );
				}else{
					
					alert("Something went wrong..Please try again!!");
					
				}
			}
		})
		
	})
	
})