$(document).ready(function() {

    function  crypt(password = ''){
        var pass= password;
        if (pass != " ")
        {
            var hvalue = CryptoJS.SHA256(pass);
            //alert(hvalue);
            return hvalue;
        }
      }
function  timercountdown(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").hide();
    $("span#generate_otp_btn").on("click", function(e) {
        e.preventDefault();
        $(".footer_error").html("");
        $(".footer_error").css("display", "none");
        var mobile = $("#foot_mobile").val();
        var isd = $("#foot_country").val();
        var footercaptcha = $("#footercaptcha").val();
        if (mobile !== '' && isd !== '' && footercaptcha !== '') {
            $.ajax({
                type: 'POST',
                data: {
                    foot_mobile: mobile,
                    foot_isd: isd,
                    captcha:footercaptcha,
                },
                url: CI_ROOT + "message/generateOtp",
                dataType: "json",
                beforeSend: function() {
                    $("#preloader").show();
                    $("#status").show()
                },
                success: function(result) {
                    $("#preloader").hide();
                    $("#status").fadeOut();
                    $('#footercaptcha').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").hide();
                        //$("span#resend_otp_btn").show();
                        $("#otp_text").slideDown();
                        timercountdown("0:59","countdown","resend_otp_btn");
                        
                    } 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").text("select the country.");
                        $(".footer_error").css("display", "block");
                        $('input[name="foot_country"]').focus()
                    } else if (result.foot_mobile == "mobile") {
                        $("span#foot_mobile").text("mobile number is required.");
                        $(".footer_error").css("display", "block");
                        $('input[name="foot_mobile"]').focus()
                    } else if (result.foot_mobile == "mobile_invalid") {
                        $("span#foot_mobile").text("mobile number is invalid.");
                        $(".footer_error").css("display", "block");
                        $('input[name="foot_mobile"]').focus()
                    } else if (result.foot_mobile == "mobile_length") {
                        $("span#foot_mobile").text("mobile number is invalid.");
                        $(".footer_error").css("display", "block");
                        $('input[name="foot_mobile"]').focus()
                    } else {
                        $.each(result, function(i, v) {
                            $("span#" + i).html(v)
                        });
                        var keys = Object.keys(result);
                        $(".footer_error").css("display", "block");
                        $('input[name="' + keys[0] + '"]').focus()
                    }
                }
            })
        } else if (mobile == '') {
            $("span#foot_mobile").html("mobile number is required.");
            $(".footer_error").css("display", "block");
            $('input[name="foot_mobile"]').focus()
        } else if (isd == '') {
            $("span#foot_country").html("select the country.");
            $(".footer_error").css("display", "block");
            $('input[name="foot_country"]').focus()
        }else if(footercaptcha == ''){  
          $("label[for='footercaptcha']").html("captcha is required.");
          $(".error").css("display","block");
          $('#footercaptcha').focus(); 
        }
        
    });
    
    $("span#resend_otp_btn").on("click", function(e) {
        e.preventDefault();
        $(".footer_error").html("");
        $(".footer_error").css("display", "none");
        var mobile = $("#foot_mobile").val();
        var isd = $("#foot_country").val();
        var footercaptcha = $("#footercaptcha").val();
        if (mobile !== '' && isd !== '' && footercaptcha !== '') {
            $.ajax({
                type: 'POST',
                data: {
                    foot_mobile: mobile,
                    foot_isd: isd,
                    captcha:footercaptcha
                },
                url: CI_ROOT + "message/generateOtp",
                dataType: "json",
                beforeSend: function() {
                    $("#preloader").show();
                    $("#status").show()
                },
                success: function(result) {
                    $("#preloader").hide();
                    $("#status").fadeOut();
                    $('#footercaptcha').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").hide();
                        $("#otp_text").slideDown();
                        $("#countdown").html('');
                        timercountdown("0:59","countdown","resend_otp_btn");

                    } 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").text("select the country.");
                        $(".footer_error").css("display", "block");
                        $('input[name="foot_country"]').focus()
                    } else if (result.foot_mobile == "mobile") {
                        $("span#foot_mobile").text("mobile number is required.");
                        $(".footer_error").css("display", "block");
                        $('input[name="foot_mobile"]').focus()
                    } else {
                        $.each(result, function(i, v) {
                            $("span#" + i).html(v)
                        });
                        var keys = Object.keys(result);
                        $(".footer_error").css("display", "block");
                        $('input[name="' + keys[0] + '"]').focus()
                    }
                }
            })
        } else if (mobile == '') {
            $("span#foot_mobile").html("mobile number is required.");
            $(".footer_error").css("display", "block");
            $('input[name="foot_mobile"]').focus()
        } else if (isd == '') {
            $("span#foot_country").html("select the country.");
            $(".footer_error").css("display", "block");
            $('input[name="foot_country"]').focus()
        }
        else if(footercaptcha == ''){  
          $("label[for='footercaptcha']").html("captcha is required.");
          $(".error").css("display","block");
          $('#footercaptcha').focus(); 
        }
    });
    $("#footerForm").on("submit", function(e) {
        //alert($("#foot_message_token").val());
        e.preventDefault();
        $(".footer_error").html("");
        $(".footer_error").css("display", "none");
        var optval = $('#foot_otp').val(); 
        if(optval !== '')
        {
         var ogoptval = optval;
        var hasotp = crypt(optval);
        $('#foot_otp').val(hasotp); 
        
        var formdata = $(this).serialize();
        $.ajax({
            type: 'POST',
            data: formdata,
            url: CI_ROOT + "message/messageAction",
            dataType: "json",
            beforeSend: function() {
                $("#preloader").show();
                $("#status").show()
            },
            success: function(result) {
                $("#preloader").hide();
                $("#status").fadeOut();
                $('#foot_otp').val(ogoptval); 
                if (result.status == "success") {
                    $('#footerForm')[0].reset();
                    $("span#generate_otp_btn").show();
                    $("span#resend_otp_btn").hide();
                    $("#otp_text").hide();
                    swal({
                        title: "Thank You !",
                        icon: "success",
                        text: "We will connect with you shortly."
                    })
                } else if (result.status == "invalid") {
                    swal({
                        title: "Invalid Request!",
                        icon: "error",
                        text: "Something went wrong ! Please try again."
                    })
                } else if (result.status == "otp_missing") {
                    swal({
                        title: "Verify Mobile",
                        icon: "error",
                        text: "Please verify your mobile number by clicking Generate OTP."
                    })
                } 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 {
                    $.each(result, function(i, v) {
                        $("span#" + i).html(v)
                    });
                    var keys = Object.keys(result);
                    $(".footer_error").css("display", "block");
                    $('input[name="' + keys[0] + '"]').focus()
                }
            }
        })
        }
        else
        {
          if(optval == ''){  
             swal({
                  title: "Please verify your mobile number by clicking Generate OTP.",
                  icon: "error",
                  text: ""
                });
        }
        }
    });
    $("span#ad_resend_otp").hide();
    $(".otpData").hide();
    $("span#ad_generate_otp").on("click", function(e) {
        e.preventDefault();
        $(".error").html("");
        $(".error").css("display", "none");
        var mobile = $("#ad_mobile").val();
        var isd = $("#ad_country").val();
        if (mobile !== '' && isd !== '') {
            $.ajax({
                type: 'POST',
                data: {
                    ad_mobile: mobile,
                    ad_isd: isd
                },
                url: CI_ROOT + "advertise/generateOtp",
                dataType: "json",
                beforeSend: function() {
                    $("#preloader").show();
                    $("#status").show()
                },
                success: function(result) {
                    $("#preloader").hide();
                    $("#status").fadeOut();
                    if (result.status == "success") {
                        $("span#ad_generate_otp").hide();
                        $("span#ad_resend_otp").show();
                        $(".otpData").slideDown()
                    } 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.ad_isd == "country") {
                        $("label[for='ad_country']").text("select the country.");
                        $(".error").css("display", "block");
                        $('input[name="ad_country"]').focus()
                    } else if (result.ad_mobile == "mobile") {
                        $("label[for='ad_mobile']").text("mobile number is required.");
                        $(".error").css("display", "block");
                        $('input[name="ad_mobile"]').focus()
                    }  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()
                    }
                }
            })
        } else if (mobile == '') {
            $("label[for='ad_mobile']").html("mobile number is required.");
            $(".error").css("display", "block");
            $('input[name="ad_mobile"]').focus()
        } else if (isd == '') {
            $("label[for='ad_country']").html("select the country.");
            $(".error").css("display", "block");
            $('select[name="ad_country"]').focus()
        }
    });
    $("span#ad_resend_otp").on("click", function(e) {
        e.preventDefault();
        $(".footer_error").html("");
        $(".footer_error").css("display", "none");
        var mobile = $("#ad_mobile").val();
        var isd = $("#ad_country").val();
        if (mobile !== '' && isd !== '') {
            $.ajax({
                type: 'POST',
                data: {
                    ad_mobile: mobile,
                    ad_isd: isd
                },
                url: CI_ROOT + "advertise/generateOtp",
                dataType: "json",
                beforeSend: function() {
                    $("#preloader").show();
                    $("#status").show()
                },
                success: function(result) {
                    $("#preloader").hide();
                    $("#status").fadeOut();
                    if (result.status == "success") {
                        $(".otpData").slideDown()
                    } 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.ad_isd == "country") {
                        $("label[for='ad_country']").text("select the country.");
                        $(".error").css("display", "block");
                        $('input[name="ad_country"]').focus()
                    } else if (result.ad_mobile == "mobile") {
                        $("label[for='ad_mobile']").text("mobile number is required.");
                        $(".error").css("display", "block");
                        $('input[name="ad_mobile"]').focus()
                    } else if (result.ad_mobile == "mobile_invalid") {
                        $("label[for='ad_mobile']").text("mobile number is invalid.");
                        $(".error").css("display", "block");
                        $('input[name="ad_mobile"]').focus()
                    } else if (result.ad_mobile == "mobile_length") {
                        $("label[for='ad_mobile']").text("mobile number is invalid.");
                        $(".error").css("display", "block");
                        $('input[name="ad_mobile"]').focus()
                    } 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()
                    }
                }
            })
        } else if (mobile == '') {
            $("label[for='ad_mobile']").html("mobile number is required.");
            $(".error").css("display", "block");
            $('input[name="ad_mobile"]').focus()
        } else if (isd == '') {
            $("label[for='ad_country']").html("select the country.");
            $(".error").css("display", "block");
            $('select[name="ad_country"]').focus()
        }
    });
    $("#advertiseForm").on("submit", function(e) {
        e.preventDefault();
        $(".error").html("");
        $(".error").css("display", "none");
        var formdata = $(this).serialize();
        $.ajax({
            type: 'POST',
            data: formdata,
            url: CI_ROOT + "advertise/advertiseAction",
            dataType: "json",
            beforeSend: function() {
                $("#preloader").show();
                $("#status").show()
            },
            success: function(result) {
                $("#preloader").hide();
                $("#status").fadeOut();
                if (result.status == "success") {
                    $('#advertiseForm')[0].reset();
                    $("span#ad_generate_otp").show();
                    $("span#ad_resend_otp").hide();
                    $(".otpData").hide();
                    swal({
                        title: "Thank You !",
                        icon: "success",
                        text: "We will connect with you shortly."
                    })
                } else if (result.status == "invalid") {
                    swal({
                        title: "Invalid Request!",
                        icon: "error",
                        text: "Something went wrong ! Please try again."
                    })
                } else if (result.status == "otp_missing") {
                    swal({
                        title: "Verify Mobile",
                        icon: "error",
                        text: "Please verify your mobile number by clicking Generate OTP."
                    })
                } 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 {
                    $.each(result, function(i, v) {
                        $("label[for='" + i + "']").html(v)
                    });
                    var keys = Object.keys(result);
                    $(".error").css("display", "block");
                    $('input[name="' + keys[0] + '"]').focus()
                }
            }
        })
    })
})