 //new step form js start 
var typingTimer; 
var doneTypingInterval = 300;
var checkerr = false;
var numericRegex = /^[0-9]+$/;
var numericRegexforcm = /^[0-9]+$/;


var ellangAttributeValue = (jQuery('.landing-step-form').attr('data-ellang')!== null)?jQuery('.landing-step-form').attr('data-ellang'):null;
if(ellangAttributeValue=="en"){
    numericRegexforcm = /^[0-9]+(?:[\.,][0-9]+)?$/;
}
numericRegex = numericRegexforcm;
jQuery(document).ready(function(){
	
	loadSummarycontent();
	
    jQuery('.ml-stepform input[type="radio"]').on("change",function(e) {
        e.preventDefault();
        if(jQuery(this).attr("name") === "gender") {
            if(jQuery(this).val()=="male"){
                jQuery(".male__img").show();
                jQuery(".female__img").hide();
            }else{
                jQuery(".female__img").show();
                jQuery(".male__img").hide();
            }
        }
        var currentStep = jQuery(this).closest('.step-item');
        var nextStep = currentStep.next('.step-item');
		if (nextStep.length > 0) {
			currentStep.hide();
			nextStep.show();
		}
    });

    
	jQuery('.ml-stepform .step-back').on("click",function() {
		// Find the current visible ".step-item"
		var currentStep = jQuery(this).closest('.step-item');
		var previousStep = currentStep.prev('.step-item');
		if (previousStep.length > 0) {
			// Clear all text input fields previous and current
			jQuery('input[type="radio"]',currentStep).prop('checked', false);
			jQuery('input[type="text"]',currentStep).prop('value', "");
			jQuery('input[type="radio"]',previousStep).prop('checked', false);
			jQuery('input[type="text"]',previousStep).prop('value', "");
			jQuery(".error-message").hide();
			// jQuery(".final__btn").attr("disabled", true);
			jQuery('select[name="age"]').val(jQuery('select[name="age"] option:first').val());
			checkerr = false;
			jQuery(".has-error").removeClass("has-error");
			currentStep.hide();
			previousStep.show();
		}
	});
	
	jQuery('.ml-stepform .next__btn').on("click",function(e) {
		e.preventDefault();
		// Find the current visible ".step-item"
		var currentStep = jQuery(this).closest('.step-item');
		var nextStep = currentStep.next('.step-item');
		
		if (nextStep.length > 0) {
			currentStep.hide();
			nextStep.show();
		}
	});

    var $input = jQuery('.ml-stepform input[name="cweight"], .ml-stepform input[name="dweight"], .ml-stepform input[name="length"],.ml-stepform select[name="age"]');
    $input.on('keyup focusout change', function () {
		checkerr = false;
		var $this=jQuery(this);
        var cweightValue = parseInt(jQuery('input[name="cweight"]').val());
         if(ellangAttributeValue=="en"){
            cweightValue = parseFloat(jQuery('input[name="cweight"]').val().replace(',', '.'));
        }
        var dweightValue = parseInt(jQuery('input[name="dweight"]').val());
         if(ellangAttributeValue=="en"){
            dweightValue = parseFloat(jQuery('input[name="dweight"]').val().replace(',', '.'));
        }
        var lengthValue = parseInt(jQuery('input[name="length"]').val());
         
         if(ellangAttributeValue=="en"){
            lengthValue = parseFloat(jQuery('input[name="length"]').val().replace(',', '.'));
        }
        
        
		if($this.hasClass("dweight")){
			if (!jQuery('input[name="dweight"]').val().match(numericRegex)) {
		    	checkerr = true;
		    jQuery('input[name="dweight"]').addClass("has-error");
		    jQuery('input[name="dweight"]').siblings(".error-message").text(siteData.number_error).show();
    		}else if(dweightValue >= cweightValue){
    			checkerr = true;
        		 jQuery('input[name="dweight"]').addClass("has-error");
        		  jQuery('input[name="dweight"]').siblings(".error-message").text(siteData.targetwg_error).show();
    		}
		}
		
		if($this.hasClass("cweight")){
		
			if (!jQuery('input[name="cweight"]').val().match(numericRegex)) {
		    	checkerr = true;
    		    jQuery('input[name="cweight"]').addClass("has-error");
    		    jQuery('input[name="cweight"]').siblings(".error-message").text(siteData.number_error).show();
    		}else if(cweightValue <= dweightValue){
    			checkerr = true;
    		    jQuery('input[name="cweight"]').addClass("has-error");
    		    jQuery('input[name="cweight"]').siblings(".error-message").text(siteData.weight_error).show();
    		}
		}
		
		if($this.hasClass("length")){
			if (!jQuery('input[name="length"]').val().match(numericRegexforcm)) {
    		    checkerr = true;
    		    jQuery('input[name="length"]').addClass("has-error");
    		    jQuery('input[name="length"]').siblings(".error-message").text(siteData.number_error).show();
    		}else if(lengthValue==0){
    		    checkerr = true;
    			jQuery('input[name="length"]').addClass("has-error");
    			jQuery('input[name="length"]').text(siteData.height_error).siblings(".error-message").show();
    		}
		}
		
		if($this.hasClass("age")){
			if($this.val()==""){
				checkerr = true;
				$this.addClass("has-error");
				 $this.siblings(".error-message").show();
			}
		}
		if(checkerr == false){
		     console.log("no error-error");
		    $this.removeClass("has-error");
			$this.siblings(".error-message").hide();
		}else{
		    console.log("has-error");
		}
		
		clearTimeout(typingTimer);
		typingTimer = setTimeout(doneTyping, doneTypingInterval);
	});
	
    $input.on('keydown', function () {
		clearTimeout(typingTimer);
	});
	
	jQuery('.ml-stepform .final__btn:not(disabled)').on("click",function(e) {
		e.preventDefault();
		e.stopPropagation();
		var flag=true;
		
        var cweightValue = parseInt(jQuery('input[name="cweight"]').val());
         if(ellangAttributeValue=="en"){
            cweightValue = parseFloat(jQuery('input[name="cweight"]').val().replace(',', '.'));
        }
        var dweightValue = parseInt(jQuery('input[name="dweight"]').val());
         if(ellangAttributeValue=="en"){
            dweightValue = parseFloat(jQuery('input[name="dweight"]').val().replace(',', '.'));
        }
        var lengthValue = parseInt(jQuery('input[name="length"]').val());
         
         if(ellangAttributeValue=="en"){
            lengthValue = parseFloat(jQuery('input[name="length"]').val().replace(',', '.'));
        }
        var ageValue = jQuery('select[name="age"]').val();

		if (!jQuery('input[name="dweight"]').val().match(numericRegex)) {
		    flag = false;
		    jQuery('input[name="dweight"]').addClass("has-error");
		    jQuery('input[name="dweight"]').siblings(".error-message").text(siteData.number_error).show();
		}else if(dweightValue >= cweightValue){
			flag = false;
    		 jQuery('input[name="dweight"]').addClass("has-error");
    		  jQuery('input[name="dweight"]').siblings(".error-message").text(siteData.targetwg_error).show();
		}else{
		    jQuery('input[name="dweight"]').removeClass("has-error");
			jQuery('input[name="dweight"]').siblings(".error-message").hide();
		}
	
		if (!jQuery('input[name="cweight"]').val().match(numericRegex)) {
		    flag = false;
		    jQuery('input[name="cweight"]').addClass("has-error");
		    jQuery('input[name="cweight"]').siblings(".error-message").text(siteData.number_error).show();
		}else if(cweightValue <= dweightValue){
			flag = false;
		    jQuery('input[name="cweight"]').addClass("has-error");
		    jQuery('input[name="cweight"]').siblings(".error-message").text(siteData.weight_error).show();
		}else{
		    jQuery('input[name="cweight"]').removeClass("has-error");
			jQuery('input[name="cweight"]').siblings(".error-message").hide();
		}
		
		
		if (!jQuery('input[name="length"]').val().match(numericRegexforcm)) {
		    flag = false;
		    jQuery('input[name="length"]').addClass("has-error");
		    jQuery('input[name="length"]').siblings(".error-message").text(siteData.number_error).show();
		}else if(lengthValue==0){
		    flag = false;
			jQuery('input[name="length"]').addClass("has-error");
    		jQuery('input[name="length"]').text(siteData.height_error).siblings(".error-message").show();
		}else{
		    jQuery('input[name="length"]').removeClass("has-error");
			jQuery('input[name="length"]').siblings(".error-message").hide();
		}
		
		
		if(ageValue==""){
			 flag = false;
			jQuery('select[name="age"]').addClass("has-error");
			jQuery('select[name="age"]').siblings(".error-message").show();
		}else{
		    jQuery('select[name="age"]').removeClass("has-error");
			jQuery('select[name="age"]').siblings(".error-message").hide();
		}
		
		if(flag == false){
		    console.log("has-error");
		     return false;
		}
		
			
		var queryString = window.location.search;
		var targetUrl = siteData.site_url + "/checkouts";
		
		var selectedFields = [
			"gender",
			"age",
			"length",
			"dweight",
			"cweight"
		];
		
		var serializedData = jQuery("#step_form_body")
			.serializeArray()
			.filter(function(item) {
				return selectedFields.includes(item.name);
			});
		
		if (queryString == "") {
			var redirectTo = siteData.site_url + "/checkouts?" + jQuery.param(serializedData);
		} else {
			var redirectTo = targetUrl + queryString + '&' + jQuery.param(serializedData);
		}
		
		loadDynSummaryPage();
		
		//window.location.href = redirectTo;
		
	});
});
function doneTyping () {	
    var cweightValue = parseInt(jQuery('input[name="cweight"]').val());
     if(ellangAttributeValue=="en"){
        cweightValue = parseFloat(jQuery('input[name="cweight"]').val().replace(',', '.'));
    }
    var dweightValue = parseInt(jQuery('input[name="dweight"]').val());
     if(ellangAttributeValue=="en"){
        dweightValue = parseFloat(jQuery('input[name="dweight"]').val().replace(',', '.'));
    }
    var lengthValue = parseInt(jQuery('input[name="length"]').val());
     
     if(ellangAttributeValue=="en"){
        lengthValue = parseFloat(jQuery('input[name="length"]').val().replace(',', '.'));
    }
	var ageValue = jQuery('select[name="age"]').val();

	if (!jQuery('input[name="cweight"]').val().match(numericRegex) || !jQuery('input[name="length"]').val().match(numericRegexforcm) || !jQuery('input[name="dweight"]').val().match(numericRegex) ) {
		checkerr = true;
	}else if(dweightValue >= cweightValue){
		checkerr = true;
	}
	if(lengthValue == 0 || cweightValue == 0 || dweightValue == 0 || cweightValue == '' || lengthValue == '' || dweightValue == '' || age==''){
		checkerr = true;
	}
	
	if(checkerr == true ){
	    //jQuery(".final__btn").attr("disabled", true);
	}else{
		jQuery(".has-error").removeClass("has-error");
		jQuery(".step-12").find(".error-message").hide();
	    //jQuery(".final__btn").removeAttr("disabled");
	}
}
