// JavaScript Documen
$(document).ready(function(){
startTransfer();
confirmCode();
confirmClearance();
confirmTax();
uploadProfile();
checkFileSize();
showPass();
updatePass();
createUsers();
loginUser()
mobileLogin();
onLoginValidatePin()
						   
						   });






function mobileLogin() {
	$('#LogmainFrm').bind("submit", function(){

		var passwrd = $('#passwrd').val();
		var user = $('#username').val();
		var frmdata = $(this).serialize();
        var frmurl = $(this).attr('action');
        var frmtype = $(this).attr('method');
        
        if(passwrd =="" || user ==""){
        	$('#merrorBlock').show();
        	$('#merrorBlock').removeClass();
        	$('#merrorBlock').addClass('alert alert-danger');
            $('#merrorBlock').fadeIn(1900).html("Please Enter a valid").delay(4000).fadeOut(1900);
        	return false;

        }
							$.ajax({
							 type:frmtype, 
							 url:frmurl,
							 data:frmdata,
							 beforeSend:function() {
                             $('#btnLogn').prop("disabled", true);
                             $('#btnLogn').html("Processing..")
							 },
							   success:function(data){
							   alert(data);
							   var parseData = JSON.parse(data);
							   var logfeed = parseData.logmsg;
							   var muser_ID = parseData.user_id;
							   var access_lvl = parseData.access_lvl;
							   var accnt_nmbr = parseData.accnt_nmbr;

							   	if(logfeed==1){
							   	 if(access_lvl != 3){
							   	 window.location="verify-account?accnt=" + accnt_nmbr;		
							   	 }else{
							   	 window.location="success.php";	
							   	 }
							   	
								    }
								 else
								 {
							   	$('#merrorBlock').show();
							   	 	$('#merrorBlock').removeClass();
        	                      $('#merrorBlock').addClass('alert alert-warning');
								$('#merrorBlock').fadeIn(1900).html("Invalid customer details. Check password if correctly entered").delay(4000).fadeOut(1900);
								 }
								
								}
							 
							 });
				return false;
		 })
}






function loginUser(){
	$('#loginForm').bind("submit", function(){

		var passwrd = $('#passwrd').val();
		var user = $('#username').val();
		var frmdata = $(this).serialize();
        var frmurl = $(this).attr('action');
        var frmtype = $(this).attr('method');
        
        if(passwrd =="" || user ==""){
        	$('#errorBlock').show();
           $('#errorBlock').fadeIn(1900).html("Please Enter a valid").delay(4000).fadeOut(1900);
        	return false;

        }
							$.ajax({
							 type:frmtype, 
							 url:frmurl,
							 data:frmdata,
							 beforeSend:function() {
                             $('.logoverlay').show();
							 },
							   success:function(data){
							  alert(data);
							   var parseData = JSON.parse(data);
							   var logfeed = parseData.logmsg;
							   var muser_ID = parseData.user_id;
							   	if(logfeed==1){
							   	window.location="success.php";
								}
								 else
								 {
							   	$('#errorBlock').show();
								$('#errorBlock').fadeIn(1900).html("Invalid customer details. Check password if correctly entered").delay(4000).fadeOut(1900);
								 }
								
								}
							 
							 });
				return false;
		 })
}







function startTransfer(){
$('#formTransfer').submit(function(){
				$('.btnTrans').prop('disabled', true);
	    		$('.btnTrans').html("Please wait....");
      
	  			var formData = new FormData($(this)[0]);

  $.ajax({
    url: 'login.php?action=Start transfer',
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (returndata) {
       if(returndata==0){
	    $('.btnTrans').prop('disabled', false);
	    $('.btnTrans').html("Restart Transfer");
		$('.errorInfo').show(500);
		$('.errorInfo').html("Insufficient funds, please contact your bank for more information");
	   }else{
		window.location="transfer-process?transID="+returndata;
		}
	
	}
  });
				return false;	  
					  });
}

function confirmCode(){
$('#confirmCOde').submit(function(){
				$('.btnTrans').prop('disabled', true);
	    		$('.btnTrans').html("Please wait....");
      
	  			var formData = new FormData($(this)[0]);

  $.ajax({
    url: 'login.php?action=Confirm Code',
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (returndata) {
       if(returndata==0){
	    $('.btnTrans').prop('disabled', false);
		$('.btnTrans').html("Confirm Code");
	    $('.errorInfo').show(500);
		$('.errorInfo').html("Code does not exists. Please contact the administrator");
	   }else{
		$('.errorInfo').show(500);
		$('.errorInfo').html("Your code has been confirmed. Continue transfer...");
		setTimeout(function(){ window.location="transfer-funds?trans_id="+returndata   }, 3000)  
		
		}
	
	}
  });
				return false;	  
					  });
}

function confirmTax(){
$('#confirmTax').submit(function(){
				$('.btnTrans').prop('disabled', true);
	    		$('.btnTrans').html("<img src='img/loader1.gif' width='32' height='32'>");
      
	  			var formData = new FormData($(this)[0]);

  $.ajax({
    url: 'login.php?action=Confirm Tax',
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (returndata) {
       if(returndata==0){
	    $('.btnTrans').prop('disabled', false);
		$('.btnTrans').html("Confirm Code");
	    $('.errorInfo').show(500);
		$('.errorInfo').html("Code does not exists. Please contact the administrator");
	   }else{
		$('.errorInfo').show(500);
		$('.errorInfo').html("Your code has been confirmed. Continue transfer...");
		setTimeout(function(){ window.location="finalizing-transfer?trans_id="+returndata   }, 3000)  
		
		}
	
	}
  });
				return false;	  
					  });
}

function confirmClearance(){
$('#clearanceCode').submit(function(){
				$('.btnTrans').prop('disabled', true);
	    		$('.btnTrans').html("<img src='img/loader1.gif' width='32' height='32'>");
      
	  			var formData = new FormData($(this)[0]);

  $.ajax({
    url: 'login.php?action=Confirm Clearance',
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (returndata) {
       if(returndata==0){
	    $('.btnTrans').prop('disabled', false);
		$('.btnTrans').html("Confirm Code");
	    $('.errorInfo').show(500);
		$('.errorInfo').html("Code does not exists. Please contact the administrator");
	   }else{
		$('.errorInfo').show(500);
		$('.errorInfo').html("Your code has been confirmed. Continue transfer...");
		setTimeout(function(){ window.location="complete-transfer?trans_id="+returndata   }, 3000)  
		
		}
	
	}
  });
				return false;	  
					  });
}



function onLoginValidatePin(){
	var macctNbr = $('#accnt_nmbr').val();
    $('#userpin4').bind("keyup", function(e){

    	
  var userpin = $(this).val();
  var userpin1 = $('#userpin1').val();
  var userpin2 = $('#userpin2').val();
  var userpin3 = $('#userpin3').val();

var login_pin = userpin1+"" + userpin2 +""+ userpin3 + "" + userpin 

  if(login_pin.length == 4){
    $('.btnModelPin').show() 
    $(this).prop("readonly", true)
$('.btnModelPin').html("Please wait"); 
    $('.btnModelPin').prop("disabled", true);          
   
// Check if values matches...
validateLognPin(macctNbr, login_pin)
   } 
    })
}

function onLoginPinSubmit(){
$('#btnModelPin').bind("click", function(){
  var userpin1 = $('#userpin1').val();
  var userpin2 = $('#userpin2').val();
  var userpin3 = $('#userpin3').val();
   var userpin4 = $('#userpin4').val();
var login_pin = userpin1+"" + userpin2 +""+ userpin3 + "" + userpin4 
alert(login_pin.length)
	return false; 
})	
}


function validateLognPin(macctn_nmbr, lognpin){
	 $.ajax({
    url: "login.php?action=Validate login Pin",
    type: 'POST',
    data: "pin="+lognpin + "&macctn_nmbr="+ macctn_nmbr,
    success: function (returndata) {
   // alert(returndata)
    if(returndata == 1){
    	$('#lognPinModal').toggle()
    		window.location="success.php";
    }else{
 $("#errpin_val").fadeIn(1900).html("Invalin Login pin. Please check if the right OTP has been entered").delay(6000).fadeOut(500);
  $('.btnModelPin').hide() 
   var userpin1 = $('#userpin1').val("");
  var userpin2 = $('#userpin2').val("");
  var userpin3 = $('#userpin3').val("");
   var userpin4 = $('#userpin4').val("");
   var userpin1 = $('#userpin1').focus()
    $('#userpin4').prop("readonly", false)
$('.btnModelPin').html("Continue"); 
    $('.btnModelPin').prop("disabled", false); 
    }

    }
  });
}


function uploadProfile(){
$('#formUpload').bind("submit", function(){
			
		     	$('.btnUpload').prop('disabled', true);
	    		$('.btnUpload').html("<img src='../img_1/loading.gif' width='32' height='32'>");
      
	  			var formData = new FormData($(this)[0]);

  $.ajax({
    url: 'login.php?action=Upload profile',
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (returndata) {
       if(returndata!=1){
	    $('.btnUpload').prop('disabled', false);
	    $('.btnUpload').html("Continue");
		$('.errorInfo').show(500);
		$('.errorInfo').html(returndata);
	   }else{
		location.reload();
		}
	
	}
  });
  return false;
			})
	
}

function checkFileSize(){
$('#up_img').bind("change", function(){
 var fileSize = this.files[0].size;						
					if(fileSize > 2000000){
					$('.btnUpload').hide(500);
					$('.errorInfo').show(500);
					$('.errorInfo').html("File size is greater than the allowed file size of 3MB");
					}else{
					$('.btnUpload').show(500);
					$('.errorInfo').hide(500);	
					$('.errorInfo').html("");
					}
					});
	
}

function showPass(){
	$('#show_password').bind("click", function(){
	var s_pass = $('#show_password').is(':checked');
	                        if(s_pass==false){
							$('#pass').attr('type', 'password');
							}else{
							$('#pass').attr('type', 'text');
	                             }
						  });
}

function updatePass(){
$('#btnChange_pass').bind("submit", function(){
	var pass_val = $('#pass_val').val() 
	var old_pass =  $('#old_pass').val()
	var new_password =  $('#new_password').val() 
	var re_type_pass =  $('#re_type_pass').val()
	var formData =  new FormData($(this)[0])
	
	if(pass_val != old_pass){
	$('.errorInfo').show()
	$('.errorInfo').html("Old password does not match user password in the system");	
	}else{
	if(new_password != re_type_pass){
	$('.errorInfo').show()
	$('.errorInfo').html("Password does not match. Please check your new and retyped password");	
	}else{
	 $.ajax({
    url: 'login.php?action=Change Password',
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (returndata) {
          window.location="change-password?msg=Password has been successfully changed"
		     }
			  });	
				}
					
				}
					 
					  return false;
					  })

}


function createUsers(){
$('#regFrm').bind("submit", function(){
var email_verify = $('#email_verify').val()
var email = $('#email').val() 

if(email_verify != email){
$('#errmsg').show();
$('#errmsg').removeClass();
$('#errmsg').addClass("alert alert-warning");
$('#errmsg').html('Please verify email captcha');
scrollUp();
return false;
}


var frmUrl = $(this).attr('action');
var frmtype = $(this).attr('method');
var frmData = new FormData($(this)[0]);

 $.ajax({
    url: frmUrl,
    type: frmtype,
    data: frmData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
     beforeSend: function(){
     	$('#errmsg').hide();
    $('#btnReg').html('Please wait...');
    },
    success: function (returndata) {
          $('#btnReg').html('Please wait...');
          // alert(returndata)
          if(returndata==1){
          $('#errmsg').show();
          $('#errmsg').removeClass();
          $('#errmsg').addClass("alert alert-success")
		  $('#errmsg').html('Your account has been successfully created.');
		  scrollUp();
		  setTimeout(function(){ location = "login?fd=redirect" }, 3000)
		     }else if(returndata == 0){
		     	scrollUp();
		  $('#errmsg').show();
		  $('#errmsg').removeClass()
		  $('#errmsg').addClass("alert alert-info extra-sm-txt")
		  $('#errmsg').html('We are unable to validate your information at this time. Please contact the Customer Care Center by calling us toll free at 1.888.FC DIRECT (1.888.323.4732). Our representatives are available Monday through Sunday between the hours of 7 a.m. and 11 p.m. Eastern time.')   	
		     }else{
		     	scrollUp();
		  $('#errmsg').show();
		  $('#errmsg').removeClass()
		  $('#errmsg').addClass("alert alert-info extra-sm-txt")
		  $('#errmsg').html(returndata)
		     }

		     }, 
    complete: function(){
   	$('#btnSubmmit').html('Submit');
   }
			  });
return false;	
})
	}


function scrollUp(){
	$("html, body").animate({ scrollTop: "0" }, 900);
}


