var height_1 = 0;

$(document).ready(function(){
    
    
     function  crypt_careers(password = ''){
        var pass= password;
        if (pass != " ")
        {
            var hvalue = CryptoJS.SHA256(pass);
            //alert(hvalue);
            return hvalue;
        }
      }
function  timercountdown_careers(timer,timerdisplayattarid,resendotpbuttonattrid='')
{
    //alert(timerdisplayattarid);
    //alert("test");
    //alert(timer);
    var timer2 = timer;
    var interval = setInterval(function() {
      var timer = timer2.split(':');
      //by parsing integer, I avoid all extra string processing
      var minutes = parseInt(timer[0], 10);
      var seconds = parseInt(timer[1], 10);
      --seconds;
      minutes = (seconds < 0) ? --minutes : minutes;
      if (minutes < 0) clearInterval(interval);
      seconds = (seconds < 0) ? 59 : seconds;
      seconds = (seconds < 10) ? '0' + seconds : seconds;
      //minutes = (minutes < 10) ?  minutes : minutes;
      if(seconds > 0)
      {
        data = 'Resend OTP '+ minutes + ':' + seconds;
        if(data == 'Resend OTP -1:59')
        {
            $('#'+timerdisplayattarid).html(' ');
        }
        else
        {
            $('#'+timerdisplayattarid).html('Resend OTP '+ minutes + ':' + seconds);
        
        }

      }
      else
      {
          $('#'+timerdisplayattarid).html(' ');
          //$('#'+timerdisplayattarid).hide();
          $("#"+resendotpbuttonattrid).show();
          //timer2="00:00";
      }
      timer2 = minutes + ':' + seconds;
    }, 1000);
}



$("#resend_otp_btn_careers").hide();
    $("span#generate_otp_btn_careers").on("click", function(e) {
        e.preventDefault();
        $(".error").html("");
        $(".error").css("display", "none");
        var mobile = $("#contact").val();
        var isd = $("#foot_country_careers").val();
        var footercaptcha = $("#footercaptcha_careers").val();
        if (mobile !== '' && isd !== '' && footercaptcha !== '') {
            $.ajax({
                type: 'POST',
                data: {
                    foot_mobile: mobile,
                    foot_isd: isd,
                    captcha:footercaptcha,
                    iscareers:'1'
                },
                url: CI_ROOT + "message/generateOtp",
                dataType: "json",
                beforeSend: function() {
                    $("#preloader").show();
                    $("#status").show()
                },
                success: function(result) {
                    $("#preloader").hide();
                    $("#status").fadeOut();
                    $('#footercaptcha_careers').val(''); 
                    captcharefresh();
                    if (result.status == "exist") {
                        swal({
                            title: "Already Exist",
                            icon: "error",
                            text: "Entered mobile number already used. Please use different mobile number."
                        })
                    } else if (result.status == "success") {
                        $("span#generate_otp_btn_careers").hide();
                        //$("span#resend_otp_btn").show();
                        $("#otp_text_careers").slideDown();
                        timercountdown_careers("0:59","countdown_careers","resend_otp_btn_careers");
                        
                    } else if (result.status == "error") {
                        swal({
                            title: "Error",
                            icon: "error",
                            text: result.msg
                        })
                    } else if (result.status == "invalid") {
                        swal({
                            title: "Invalid Request!",
                            icon: "error",
                            text: "Something went wrong ! Please try again."
                        })
                    } else if (result.status == "blocked") {
                        swal({
                            title: "Blocked",
                            icon: "error",
                            text: "Your mobile no is blocked as you have reached maximum attempt limit for otp verification."
                        })
                    }
                    else if(result.status == "CaptchaIncorrect"){
                    swal({
                      title: "Captcha is Incorrect !",
                      icon: "error",
                      text: ""
                    });
                  }else if (result.foot_isd == "country") {
                        $("span#foot_country_careers").text("select the country.");
                        $(".error").css("display", "block");
                        $('input[name="foot_country_careers"]').focus()
                    } else if (result.foot_mobile == "mobile") {
                        $("span#contact").text("mobile number is required.");
                        $(".error").css("display", "block");
                        $('input[name="contact"]').focus()
                    } else {
                        $.each(result, function(i, v) {
                            $("span#" + i).html(v)
                        });
                        var keys = Object.keys(result);
                        $(".error").css("display", "block");
                        $('input[name="' + keys[0] + '"]').focus()
                    }
                }
            })
        } else if (mobile == '') {
            $("span#contact").html("mobile number is required.");
            $(".error").css("display", "block");
            $('input[name="contact"]').focus()
        } else if (isd == '') {
            $("span#foot_country_careers").html("select the country.");
            $(".error").css("display", "block");
            $('input[name="foot_country_careers"]').focus()
        }else if(footercaptcha == ''){  
          $("label[for='footercaptcha_careers']").html("captcha is required.");
          $(".error").css("display","block");
          $('#footercaptcha_careers').focus(); 
        }
        
    });
    
    $("span#resend_otp_btn_careers").on("click", function(e) {
        e.preventDefault();
        $(".error").html("");
        $(".error").css("display", "none");
        var mobile = $("#contact").val();
        var isd = $("#foot_country_careers").val();
        var footercaptcha = $("#footercaptcha_careers").val();
        if (mobile !== '' && isd !== '' && footercaptcha !== '') {
            $.ajax({
                type: 'POST',
                data: {
                    foot_mobile: mobile,
                    foot_isd: isd,
                    captcha:footercaptcha,
                    iscareers:'1'
                },
                url: CI_ROOT + "message/generateOtp", 
                dataType: "json",
                beforeSend: function() {
                    $("#preloader").show();
                    $("#status").show()
                },
                success: function(result) {
                    $("#preloader").hide();
                    $("#status").fadeOut();
                    $('#footercaptcha_careers').val(''); 
                    captcharefresh();
                    if (result.status == "exist") {
                        swal({
                            title: "Already Exist",
                            icon: "error",
                            text: "Entered mobile number already used. Please use different mobile number."
                        })
                    } else if (result.status == "success") {
                        $("span#resend_otp_btn_careers").hide();
                        $("#otp_text_careers").slideDown();
                        $("#countdown_careers").html('');
                        timercountdown_careers("0:59","countdown_careers","resend_otp_btn_careers");

                    } else if (result.status == "error") {
                        swal({
                            title: "Error",
                            icon: "error",
                            text: result.msg
                        })
                    } else if (result.status == "invalid") {
                        swal({
                            title: "Invalid Request!",
                            icon: "error",
                            text: "Something went wrong ! Please try again."
                        })
                    }else if(result.status == "CaptchaIncorrect"){
                    swal({
                      title: "Captcha is Incorrect !",
                      icon: "error",
                      text: ""
                    });
                  } else if (result.status == "blocked") {
                        swal({
                            title: "Blocked",
                            icon: "error",
                            text: "Your mobile no is blocked as you have reached maximum attempt limit for otp verification."
                        })
                    } else if (result.foot_isd == "country") {
                        $("span#foot_country_careers").text("select the country.");
                        $(".error").css("display", "block");
                        $('input[name="foot_country_careers"]').focus()
                    } else if (result.foot_mobile == "mobile") {
                        $("span#contact").text("mobile number is required.");
                        $(".error").css("display", "block");
                        $('input[name="contact"]').focus()
                    } else {
                        $.each(result, function(i, v) {
                            $("span#" + i).html(v)
                        });
                        var keys = Object.keys(result);
                        $(".error").css("display", "block");
                        $('input[name="' + keys[0] + '"]').focus()
                    }
                }
            })
        } else if (mobile == '') {
            $("span#contact").html("mobile number is required.");
            $(".error").css("display", "block");
            $('input[name="contact"]').focus()
        } else if (isd == '') {
            $("span#foot_country_careers").html("select the country.");
            $(".error").css("display", "block");
            $('input[name="foot_country_careers"]').focus()
        }
        else if(footercaptcha == ''){  
          $("label[for='footercaptcha_careers']").html("captcha is required.");
          $(".error").css("display","block");
          $('#footercaptcha_careers').focus(); 
        }
    });
    
    



  $('.sameHeight').each(function(index) {
    var maxHeight = $(this).height();
    if(maxHeight > height_1){
      height_1 = maxHeight;
    }
      
    $('.sameHeight').height(height_1);	
  });

  $("#careerEnquiryForm").on("submit",function(e){
    e.preventDefault();     
    $(".error").html("");
    $(".error").css("display","none");
    var optval = $('#foot_otp_careers').val(); 
        if(optval !== '')
        {
         var ogoptval = optval;
        var hasotp = crypt_careers(optval);
        $('#foot_otp_careers').val(hasotp); 
    var formdata = new FormData(this);
   
    $.ajax({
      type:'POST',
      data:formdata,
      url:CI_ROOT+"careers/submitJobApplication",
      mimeType : 'multipart/form-data',
      contentType: false,
      processData: false,
      dataType: "json",
      beforeSend:function(){
        $("#preloader").show(); 
        $("#status").show(); 
      },
      success:function(result){ 
        $("#preloader").hide(); 
        $("#status").fadeOut();
        if(result.status == "success"){
          $('#careerEnquiryForm')[0].reset();
          $.fancybox.close();
          swal({
            title: "Thank You !",
            icon: "success",
            text: "we will contact you very soon."
          });
        }else if(result['status'] == "invalid"){
          swal({
            title: "Invalid Request!",
            icon: "error",
            text: "Something went wrong ! Please try again."
          });
        }else{
          $.each(result, function(i, v) {
            $("label[for='"+i+"']").html(v);
          });
                    
          var keys = Object.keys(result);
          $(".error").css("display","block");
          $('input[name="'+keys[0]+'"]').focus();
        }
      }
    
    });
        }
  });
});

function show(ref,e){
  $('#careerEnquiryForm')[0].reset();
  $(".error").html("");
  $(".error").css("display","none");  

  $(".roleName").html("");
  $(".roleName").html("Role : "+ref);
  $("#role").val(ref);
}

