var price_num = 0;
var item_num = 0;
var offer_num = 0;
var cck;

cck = {
    gallery_main_img: function (val, fid) {
        var img_name = jQuerOs(val).parent().find("img").attr("src").split("/").pop();
        jQuerOs.post("index.php?option=com_os_cck&task=ajax_instance&id=" + jQuerOs("[name='id']").val() + "&format=raw", { img: img_name, do: "main", "f_id": fid});
        jQuerOs("[value='Main']").val("Set Main").removeAttr("disabled");
        jQuerOs(val).val("Main").attr("disabled", "disabled");
    },
    gallery_delete_img: function (val, fid) {
        var img_name = jQuerOs(val).parent().find("img").attr("src").split("/").pop();
        jQuerOs.post("index.php?option=com_os_cck&task=ajax_instance&id=" + jQuerOs("[name='id']").val() + "&format=raw", { img: img_name, do: "delete", 'f_id': fid});
        jQuerOs(val).parent().parent().fadeOut(500, function () {
            jQuerOs(this).remove();
        });
    }
};

function cck_checkAll(all){
  //var boxchecked = document.getElementsByName('boxchecked');
  var c = document.getElementsByName('eiid[]');
  for(var i=0;i<c.length;i++)
    if(all.checked!=true){
      c[i].checked=false;
    }
    else{
      c[i].checked=true;
    }
}


function listItemTask(id, task, formName){
    
    if(!formName) formName='adminForm'
    var form = document.forms[formName];     
    // cb = eval( id ); old
    if (id) {
        //old:
        // cb.checked = true;
        // form.task.value = task;
        //new:
        if(jQuerOs('#'+id).length > 0){
            if(!jQuerOs('#'+id).is(':checked')){
                jQuerOs('#'+id).prop('checked', true);
            }
        }
        jQuerOs(form).find('[name=task]').val(task)
        //jQuerOs('form[name="'+ formName+'"]').find('[name=task]').val(task)
        
        form.action = replaceUrlParamLegacy(form.action, 'task',task);

        form.submit();
    }

    return false;
}

function replaceUrlParamLegacy(url, paramName, paramValue) {
    var pattern = new RegExp('(' + paramName + '=).*?(&|$)');
    var newUrl = url.replace(pattern, '$1' + paramValue + '$2');
    if (newUrl == url) {
        // If the parameter did not exist, add it
        newUrl = newUrl + (newUrl.indexOf('?') > -1 ? '&' : '?') + paramName + '=' + paramValue;
    }
    return newUrl;
}

//this method need for send Submit from Table Row (Publish/Unpublish/Approve/.. some item)
function cckSendForm(cid, task, formName){
    if(!formName) formName='adminForm'
    var form = document.forms[formName]; 
    if(cid){
        if(jQuerOs('#'+cid).length > 0){
            if(!jQuerOs('#'+cid).is(':checked')){
                jQuerOs('#'+cid).prop('checked', true);
            }
        }
        jQuerOs(form).find('[name=task]').val(task)

        form.action = replaceUrlParamLegacy(form.action, 'task',task);

        form.submit()
    }
}


function new_extra_field() {
    field_div = document.getElementById("select_items");
    button = document.getElementById("add_select_field");
    newitem = "<strong>" + ++item_num + ". </strong> Option name:\n" +
        "<input type='text' name='add_selectitem[" + item_num + "][0]' value='' size='15' maxsize='50'>\n" +
        "Option value: <input id='pp" + price_num + "' type='text' name='add_selectitem[" + item_num + "][1]' value='' size='15' maxsize='50'><br />";
    newnode = document.createElement("span");
    newnode.innerHTML = newitem;
    field_div.insertBefore(newnode, button);
}

var photos = 0;
function new_photos() {
    div = document.getElementById("items");
    button = document.getElementById("add");
    photos++;
    newitem = "<strong>" + "Photo " + photos + ": </strong>";
    newitem += "<input type=\"file\" name=\"new_photo_file[]";
    newitem += "\" value=\"\"size=\"45\"><br>";
    newnode = document.createElement("span");
    newnode.innerHTML = newitem;
    div.insertBefore(newnode, button);
}


function selectLayout(id, fk_eid, type, module_id, entity_type){
    document.getElementById("selected_layout").value = id;
   
    if(module_id){
        
        if(document.getElementById("layout_type")){
            document.getElementById("layout_type").value = type;
        }
        if(document.getElementById("jform_params_layout_type")){
            document.getElementById("jform_params_layout_type").value = type;
        }
        if(document.getElementById("jform_params_entity_type")){
            jQuery('#jform_params_entity_type').val(entity_type)
            // console.log('11111111111111', jQuery('#jform_params_entity_type'));
            // document.getElementById("jform_params_entity_type").value = entity_type;
        }
        jQuery('.arrow_box.step-1').hide()
        
        if(type == 'instance' || type == 'parent_child'){
            if(document.getElementById('category')){
                document.getElementById('category').parentNode.parentNode.style.display = 'none';
            }
            if(document.getElementById('jform_params_show_type')){
                document.getElementById('jform_params_show_type').parentNode.parentNode.style.display = 'none';
            }
            if(document.getElementById('category') != null){
                document.getElementById('category').parentNode.parentNode.style.display = 'none';
            }
            if(entity_type == 'os_cck_entity' || entity_type == 'custom_sql'){
                if(document.getElementById("instance")){
                    document.getElementById("instance").parentNode.parentNode.style.display = '';
                    document.getElementById("instance").value = '';
                }
                jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').hide();
                jQuery('#jform_params_user_choise_class_id').parents('.controls').hide();
                jQuery('#jform_params_user_choise_class-lbl').parents('.control-group').hide();
            }else if(entity_type == 'joomla_user'){
                
                if(document.getElementById("instance")){
                    document.getElementById("instance").parentNode.parentNode.style.display = 'none';
                }
                jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').show();
                jQuery('#jform_params_user_choise_class_id').parents('.controls').show();
                jQuery('#jform_params_user_choise_class-lbl').parents('.control-group').show();
                // if(document.getElementById("jform_params_user")){
                //     console.log('222222222222')
                //     jQuery('#jform_params_user').parents('.control-group').show();
                //     jQuery('#jform_params_user').val('')
                // }
            
            }
            
            if(type == 'instance' || type == 'parent_child'){
                jQuery('.instance-button').parent().append('<div class="arrow_box step-2"><div>Step 2</div></div>')   
                if(document.getElementById("changeLink") != null){
                    document.getElementById("changeLink").href =
                                    "index.php?option=com_os_cck&task=show_instance_modal&layout_type="+type
                                    +"&fk_eid="+fk_eid+"&module_id="+module_id+"&tmpl=component";
                    document.getElementById("changeLink").setAttribute('src', "index.php?option=com_os_cck&task=show_instance_modal&layout_type="+type
                                    +"&fk_eid="+fk_eid+"&module_id="+module_id+"&tmpl=component")
                    document.getElementById("jform_params_instance-lbl").innerHTML = 'Select Instance';
                    document.getElementById("changeLink").innerHTML = 'Select Instance';
                }
            }else{
                // document.getElementById("changeLink").href =
                //                  "index.php?option=com_os_cck&task=show_categories_modal&layout_type="+type
                //                  +"&fk_eid="+fk_eid+"&module_id="+module_id+"&tmpl=component";
                document.getElementById("changeLink").setAttribute('src', "index.php?option=com_os_cck&task=show_categories_modal&layout_type="+type
                                    +"&fk_eid="+fk_eid+"&module_id="+module_id+"&tmpl=component")
                document.getElementById("changeLink").setAttribute('href', "index.php?option=com_os_cck&task=show_categories_modal&layout_type="+type
                                        +"&fk_eid="+fk_eid+"&module_id="+module_id+"&tmpl=component")
                document.getElementById("changeLink").innerHTML = 'Select Category';
                if(document.getElementById("jform_params_instance-lbl")){
                    document.getElementById("jform_params_instance-lbl").innerHTML = 'Select Category';
                }
            }

        }else if(type == 'category'){
            
            jQuery('.category-button').parent().append('<div class="arrow_box step-2"><div>Step 2</div></div>')  
            document.getElementById("changeLink2").setAttribute('src', "index.php?option=com_os_cck&task=show_categories_modal&layout_type="+type
                            +"&fk_eid="+fk_eid+"&module_id="+module_id+"&tmpl=component")
            document.getElementById("changeLink2").setAttribute('href', "index.php?option=com_os_cck&task=show_categories_modal&layout_type="+type
                                +"&fk_eid="+fk_eid+"&module_id="+module_id+"&tmpl=component")
            // console.log('3333333333333333', document.getElementById("changeLink"))
            if(document.getElementById('category') != null){
                document.getElementById('category').parentNode.parentNode.style.display = '';
            }
            if(document.getElementById('instance')){
                document.getElementById('instance').parentNode.parentNode.style.display = 'none';
            }
        }else if(type == 'add_instance' || type == 'request_instance'){
            document.getElementById('category').parentNode.parentNode.style.display = 'none';
            if(document.getElementById('jform_params_show_type') != null){
                document.getElementById('jform_params_show_type').parentNode.parentNode.style.display = '';
            }
            if(document.getElementById('instance')){
                document.getElementById('instance').parentNode.parentNode.style.display = 'none';
            }
        }else{
            document.getElementById('category').parentNode.parentNode.style.display = 'none';
            if(document.getElementById('jform_params_show_type')){
                document.getElementById('jform_params_show_type').parentNode.parentNode.style.display = 'none';
            }
            if(document.getElementById('instance')){
                document.getElementById('instance').parentNode.parentNode.style.display = 'none';
                document.getElementById("instance").value = '';
            }
            jQuery('#jform_params_user').parents('.control-group').hide();
        }
//console.log('33333333333', type)
        if(type == 'user_instances'){
            jQuery('#jform_params_user').parents('.control-group').show();
        }else{
            if((type != 'instance' && type != 'parent_child') || ((type == 'instance') && entity_type == 'os_cck_entity')){
                if(document.getElementById("jform_params_user")){
                    jQuery('#jform_params_user').parents('.control-group').hide();
                }
            }
        }
        if(type == 'cart'){
            document.getElementById('jform_params_cart_type').parentNode.parentNode.style.display = '';
        }else{
            if(document.getElementById('jform_params_cart_type')){
                document.getElementById('jform_params_cart_type').parentNode.parentNode.style.display = 'none';
            }
        }



    }else{
        // console.log('6666666666666')
        if(document.getElementById("layout_type")){
            document.getElementById("layout_type").value = type;
        }
        if(document.getElementById("jform_params_layout_type")){
            document.getElementById("jform_params_layout_type").value = type;
        }
        if(document.getElementById("jform_params_entity_type")){
            jQuery('#jform_params_entity_type').val(entity_type)
            // console.log('11111111111111', jQuery('#jform_params_entity_type'));
            // document.getElementById("jform_params_entity_type").value = entity_type;
        }
        jQuery('.arrow_box.step-2').hide()
        // console.log('1111111111111111')
        if(type == 'instance'){
            
            if(entity_type == 'os_cck_entity' || entity_type == 'custom_sql'){
                jQuery('.instance-button').parent().append('<div class="arrow_box step-3"><div>Step 3</div></div>')    
                jQuery('#instance').parents('.control-group').show()
                if(document.getElementById("instance")){
                    // console.log('1111111111111111', jQuery('#instance').parents('.control-group'))
                    
                    
                    // document.getElementById("instance").parentNode.parentNode.style.display = '';
                }
                        
                // document.getElementById("instance").value = '';
                jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').hide();
                jQuery('#jform_params_user_choise_class_id').parents('.controls').hide();
                jQuery('#jform_params_user_choise_class-lbl').parents('.control-group').hide();
                // alert('222222222222')
            }else if(entity_type == 'joomla_user'){
                jQuery('#instance').parents('.control-group').hide()
                jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').show();
                jQuery('#jform_params_user_choise_class_id').parents('.controls').show();
                jQuery('#jform_params_user_choise_class-lbl').parents('.control-group').show();
            }
            // console.log('33333333333333')
            document.getElementById("changeLink").setAttribute('src', "index.php?option=com_os_cck&task=show_instance_modal&layout_type="+type+"&fk_eid="
                                                        +fk_eid+"&tmpl=component");
            document.getElementById("changeLink").setAttribute('href', "index.php?option=com_os_cck&task=show_instance_modal&layout_type="+type+"&fk_eid="
                                                        +fk_eid+"&tmpl=component");
                                                        // alert('222222222222')
        }else if(type == 'parent_child'){
            if(entity_type == 'os_cck_entity' || entity_type == 'custom_sql'){
                jQuery('.instance-button').parent().append('<div class="arrow_box step-3"><div>Step 3</div></div>')    
                document.getElementById("instance").parentNode.parentNode.style.display = '';
                document.getElementById("instance").value = '';
                jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').hide();
                jQuery('#jform_params_user_choise_class_id').parents('.controls').hide();
                jQuery('#jform_params_user_choise_class-lbl').parents('.control-group').hide();
                if(document.getElementById("jform_params_user")){
                    jQuery('#jform_params_user').parents('.control-group').hide();
                }
            }else if(entity_type == 'joomla_user'){
                
                if(document.getElementById("instance")){
                    document.getElementById("instance").parentNode.parentNode.style.display = 'none';
                }
                jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').show();
                jQuery('#jform_params_user_choise_class-lbl').parents('.control-group').show();
                jQuery('#jform_params_user_choise_class_id').parents('.controls').show();
                if(document.getElementById("jform_params_user")){
                    
                }
            
            }
            
            // document.getElementById("instance").value = '';
            
            document.getElementById("changeLink").setAttribute('src', "index.php?option=com_os_cck&task=show_instance_modal&layout_type="+type+"&fk_eid="
                                                        +fk_eid+"&tmpl=component");
            document.getElementById("changeLink").setAttribute('href', "index.php?option=com_os_cck&task=show_instance_modal&layout_type="+type+"&fk_eid="
                                                        +fk_eid+"&tmpl=component");
        }else{
            document.getElementById("instance").parentNode.parentNode.style.display = 'none';
        }
        // alert('111111111111')
        if(type == 'category'){
            jQuery('.category-button').parent().append('<div class="arrow_box step-3"><div>Step 3</div></div>')    
            document.getElementById("category").parentNode.parentNode.style.display = '';
            document.getElementById("category").value = '';
            document.getElementById("changeLink2").setAttribute('src', "index.php?option=com_os_cck&task=show_categories_modal&layout_type="+type+"&fk_eid="
                                                        +fk_eid+"&tmpl=component");
            document.getElementById("changeLink2").setAttribute('href', "index.php?option=com_os_cck&task=show_categories_modal&layout_type="+type+"&fk_eid="
                                                        +fk_eid+"&tmpl=component");
        }else{
            document.getElementById("category").parentNode.parentNode.style.display = 'none';
        }
  
        if(type == 'user_instances'){
            // console.log('777777777777777', jQuery('#userModal_jform_params_user_choise_class'))
              jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').show();
              jQuery('#jform_params_user_choise_class-lbl').parents('.control-group').show();
              jQuery('#jform_params_user_choise_class_id').parents('.controls').show();
              
              jQuery('#jform_params_user_choise_class_id').change(function(){
                  jQuery('.menu-next').show();
              })
              
        }
        // jQuery('#toolbar-apply').hide()
        // jQuery('#toolbar-dropdown-save-group').hide()
        // // selected_layout
        // // jQuery('#selected_layout').parents('.control-group').hide();
        // jQuery('#instance').parents('.control-group').hide();
        // jQuery('#category').parents('.control-group').hide();
        // jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').hide();
        // jQuery('#jform_params_show_type').parents('.control-group').hide();
        // jQuery('#jform_params_back_button').parents('.control-group').hide();
        // jQuery('#jform_params_hover_position').parents('.control-group').hide();
        // var html = '<div class="menu-help-buttons"><div class="menu-next btn btn-warning" step="1">NEXT: Step 2</div><div class="menu-or" style="display: none;"> OR </div><div class="menu-finish btn btn-success" style="display: none;">FINISH (Select default values)</div></div>'
        // jQuery('#fieldset-params .form-grid').append(html)
        // jQuery('.menu-next').attr('step', '2')
        // jQuery('.menu-next').on('click', function(){
        //     nextStep(fk_eid)
        // })
        
        // jQuery('.menu-finish').on('click', function(){
        //     jQuery('#toolbar-apply').show()
        //     jQuery('#toolbar-dropdown-save-group').show()
        //     jQuery('#selected_layout').parents('.control-group').show();
        //     jQuery('#jform_params_show_type').parents('.control-group').show();
        //     jQuery('#jform_params_back_button').parents('.control-group').show();

        //     if(jQuery('#jform_params_show_type').val() == '4'){
        //         jQuery('#jform_params_hover_position').parents('.control-group').show();
        //     }

        //     if(type == 'instance' || type == 'parent_child'){
        //         jQuery('#instance').parents('.control-group').show();
        //     }else if(type == 'category'){
        //         jQuery('#category').parents('.control-group').show();
        //     }else if(type == 'user_instances'){
        //         jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').show();
        //         jQuery('#jform_params_user_choise_class_id').parents('.controls').show();
        //     }
        //     jQuery('.menu-step').hide()
        //     jQuery('.menu-help-buttons').hide()
        // })

        
    }
    
    if(typeof SqueezeBox != 'undefined'){
        SqueezeBox.close();
    }else{
        jQuery('#close_layout_modal').trigger('click')
        jQuery('#layouts').hide()
        jQuery('#layouts').removeClass('show')
        jQuery('#layouts').removeAttr('aria-modal')
        jQuery('#layouts').removeAttr('role')
        jQuery('#layouts').attr('aria-hidden', 'true')
        jQuery('.modal-backdrop').remove()
        jQuery('body').css('overflow', 'unset')
        jQuery('body').css('padding-right', 'unset')
        jQuery('body').removeClass('modal-open')
        jQuery('body').attr('style', '')
        
    }
}
function nextStep(fk_eid){
    var step = jQuery('.menu-next').attr('step')
    jQuery('#toolbar-apply').hide()
    jQuery('#toolbar-dropdown-save-group').hide()
    // selected_layout
    jQuery('#selected_layout').parents('.control-group').hide();
    jQuery('#instance').parents('.control-group').hide();
    jQuery('#category').parents('.control-group').hide();
    jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').hide();
    jQuery('#jform_params_show_type').parents('.control-group').hide();
    jQuery('#jform_params_back_button').parents('.control-group').hide();
    jQuery('#jform_params_hover_position').parents('.control-group').hide();

    // var position = jQuery('#jform_params_layout_type').parents('.control-group')[0]
    // var html = '<div class="menu-step menu-step-1">Step 1: Select the layout to display</div>'
    
    // jQuery(position).after(html)

    
    // var html = '<div class="menu-next btn btn-warning">NEXT</div>'
    // jQuery('#fieldset-params .form-grid').append(html)


    type = document.getElementById('jform_params_layout_type').value
    // console.log('3333333333333', type)
    // console.log('4444444444444', step)
    var step_text = ''
    if(step == '1'){
    //   console.log('1111111111111111')
        jQuery('.menu-next').hide()
      if(type == 'instance' || type == 'parent_child'){
          step_text = 'Step 2: Select an instance to display'
          document.getElementById("instance").parentNode.parentNode.style.display = '';
          document.getElementById("instance").value = '';
          // document.getElementById("instance").value = '';
          
          document.getElementById("changeLink").setAttribute('src', "index.php?option=com_os_cck&task=show_instance_modal&layout_type="+type+"&fk_eid="
                                                      +fk_eid+"&tmpl=component");
      }else{
          document.getElementById("instance").parentNode.parentNode.style.display = 'none';
      }

      if(type == 'category'){
          step_text = 'Step 2:  Select a category to display'
          document.getElementById("category").parentNode.parentNode.style.display = '';
          document.getElementById("category").value = '';
          document.getElementById("changeLink2").setAttribute('src', "index.php?option=com_os_cck&task=show_categories_modal&layout_type="+type+"&fk_eid="
                                                      +fk_eid+"&tmpl=component");
          document.getElementById("changeLink2").setAttribute('href', "index.php?option=com_os_cck&task=show_categories_modal&layout_type="+type+"&fk_eid="
                                                        +fk_eid+"&tmpl=component");
      }else{
          document.getElementById("category").parentNode.parentNode.style.display = 'none';
      }

      if(type == 'user_instances'){
            jQuery('#userModal_jform_params_user_choise_class').parents('.control-group').show();
            jQuery('#jform_params_user_choise_class_id').parents('.controls').show();
            step_text = 'Step 2: Select the user whose instances will be displayed'
            jQuery('#jform_params_user_choise_class_id').change(function(){
                jQuery('.menu-next').show();
            })
            
      }

      if(step_text != ''){
        jQuery('.menu-step').text(step_text)
      }else{
        jQuery('#jform_params_show_type').parents('.control-group').show();
        step_text = 'Step 2: Select the type of display'
        jQuery('.menu-step').text(step_text)
        jQuery('.menu-next, .menu-or, .menu-finish').show();
      }
      jQuery('.menu-next').text('NEXT: Step 3')
      jQuery('.menu-next').attr('step', '2')
    }else if(step == '2'){
        
        var step_text = ''
        if(type == 'instance' || type == 'parent_child' || type == 'user_instances' || type == 'category'){
            step_text = 'Step 3: Select the type of display'
            jQuery('#jform_params_show_type').parents('.control-group').show();
            jQuery('.menu-or').show()
            jQuery('.menu-finish').show()
        }else{
            jQuery('#jform_params_back_button').parents('.control-group').show();
            step_text = 'Step 3: Select options for the Back Button'
            jQuery('.menu-step').text(step_text)
            jQuery('.menu-next, .menu-or').hide();
            jQuery('.menu-finish').text('FINISH')
        }
        jQuery('.menu-step').text(step_text)
        jQuery('.menu-next').text('NEXT: Step 4')
        jQuery('.menu-next').attr('step', '3')
    }else if(step == '3'){
        var step_text = ''
        if(type == 'instance' || type == 'parent_child' || type == 'user_instances' || type == 'category'){
            step_text = 'Step 4: Select options for the Back Button'
            jQuery('#jform_params_back_button').parents('.control-group').show();
        }else{

        }
        jQuery('.menu-next').hide()
        jQuery('.menu-or').hide()
        jQuery('.menu-finish').text('FINISH')
        jQuery('.menu-step').text(step_text)
        var html = '<div class="menu-next btn btn-warning" step="1">NEXT: Step 2</div>'
        jQuery('.menu-next').text('NEXT: Step 4')
    }

  }

function selectInstance(id){
    document.getElementById("instance").value = id;
    if(typeof SqueezeBox != 'undefined'){
        SqueezeBox.close();
    }else{
        
        jQuery('#instances').hide()
        jQuery('#instances').removeClass('show')
        jQuery('#instances').removeAttr('aria-modal')
        jQuery('#instances').removeAttr('role')
        jQuery('#instances').attr('aria-hidden', 'true')
        jQuery('#instances').hide()
        jQuery('#instancies').removeClass('show')
        
        jQuery('.modal-backdrop').remove()
        jQuery('body').css('overflow', 'unset')
        jQuery('body').css('padding-right', 'unset')
        jQuery('body').removeClass('modal-open')


    }
    jQuery('.arrow_box.step-3, .arrow_box.step-2').hide()
    jQuery('.menu-next').show()
}

function selectCategory(id, module_id){
    
    if(module_id){
        
        if(document.getElementById("instance")){
            document.getElementById("instance").value = id;
        }
        if(document.getElementById("category")){
            document.getElementById("category").value = id;
        }
    }else{
        document.getElementById("category").value = id;
        jQuery('.menu-next').show()
    }
    if(typeof SqueezeBox != 'undefined'){
        SqueezeBox.close();
    }else{
        
        jQuery('#categories, #instances').hide()
        jQuery('#categories, #instances').removeClass('show')
        jQuery('#categories, #instances').removeAttr('aria-modal')
        jQuery('#categories, #instances').removeAttr('role')
        jQuery('#categories, #instances').attr('aria-hidden', 'true')
        jQuery('.modal-backdrop').remove()
        jQuery('body').css('overflow', 'unset')
        jQuery('body').css('padding-right', 'unset')
        jQuery('body').removeClass('modal-open')

        
    }
    jQuery('.arrow_box.step-3, .arrow_box.step-2').hide()
}


function MM_findObj(n, d){ //v4.01
    var p,i,x;
    if(!d) d=document;
    if((p=n.indexOf("?"))>0&&parent.frames.length){
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
    }
    if(!(x=d[n])&&d.all) x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n);
    return x;
}


function MM_swapImage(){ //v3.0
    var i,j=0,x,a=MM_swapImage.arguments;
    document.MM_sr=new Array;
    for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null){
        document.MM_sr[j++]=x;
        if (!x.oSrc) x.oSrc=x.src;
        x.src=a[i+2];
    }
}


function MM_swapImgRestore(){ //v3.0
    var i,x,a=document.MM_sr;
    for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


function MM_preloadImages(){ //v3.0
    var d=document;
    if(d.images){
        if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
        for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){
            d.MM_p[j]=new Image;
            d.MM_p[j++].src=a[i];
        }
    }
}
if(window.jQuerOs){
    jQuerOs(document).ready(function() {
        //highlight 
        jQuerOs('table.filters select, table.filters input[name=search]').each(function(index, value){
            if(jQuerOs(value).val() != '-1' 
                && jQuerOs(value).val() != '0'
                && jQuerOs(value).val() != ''
                && jQuerOs(value).val() != ' ')
            {
                jQuerOs(value).css('border','2px solid #46a546');
            }
        })

    });
}

function addChildSelect(field_json = false,lid = false,value = false,path = false,fid = false, unique_parent_id, eiid){
// console.log("addChildSelect");    
    jQuerOs("select[id^=fi_text_select_list_"+fid+"], select[id^=os_cck_search_text_select_list_"+fid+"]").unbind('change');

    jQuerOs("select[id^=fi_text_select_list_"+fid+"], select[id^=os_cck_search_text_select_list_"+fid+"]").change(function(){
        addChildSelectFunc(field_json,lid,value,path,fid, unique_parent_id, this)

    })
    // jQuerOs(window).on('load', function(){ andrew comment - other not run all depend select child tree
        jQuerOs('.text_select_list_'+fid).each(function(){
            addChildSelectFunc(field_json,lid,value,path,fid, unique_parent_id, this, eiid)
        })
    // })
}

function addChildSelectFunc(field_json = false,lid = false,value = false,path = false,fid = false, unique_parent_id, old_this, eiid){
// console.log("addChildSelectFunc");    
    var field = JSON.stringify(field_json);
    var selectObject = jQuerOs(old_this);
    var currentValue = jQuerOs(old_this).val();
    if(eiid == undefined){
        var eiid_params = ''
    }else{
        var eiid_params = '&eiid='+eiid
    }

    unique_parent_id = fid
    if(selectObject.closest('.drop-item').attr('data-parent') != undefined){
        unique_parent_id = selectObject.closest('.drop-item').attr('data-parent');
    }

    if(selectObject.closest('.drop-item').find('span.field-name').attr('style') != undefined){
        parent_span_style = selectObject.closest('.drop-item').find('span.field-name').attr('style');
    }
    
    jQuerOs.ajax({
        type:"POST",
        async:false,
        url: path+"index.php?option=com_os_cck&task=getChildSelect&format=raw",
        data:"field="+field+"&lid="+lid+"&value="+value+"&currentValue="+currentValue+"&unique_parent_id="+unique_parent_id+eiid_params,
        success:function(data){

            if(data && data.trim() != ''){

                var selectParams = jQuerOs(data).find('span.select-params').data('selectParams');
                if(selectParams == undefined) return;
                var html_start = '<div class="drop-item" data-parent="'+unique_parent_id+'" data-content="">'+
                    '<span class="f-inform-button"></span><!-- clear back/front title --><span class="field-name" data-field-name="'+
                    selectParams.db_field_name+'" data-field-type="text_select_list" style="'+parent_span_style+'">'+
                    selectParams.field_name+'</span><span class="col_box admin_aria_hidden">';

                var html_end = '</span><input class="f-params" name="fi_Params_'+selectParams.db_field_name+'" type="hidden" value="{}"></div>';

                data = html_start+data+html_end;

                removeChildSelect(unique_parent_id, selectObject);
                selectObject.closest('.drop-item').after(data);
                selectObject.closest('.drop-item').find('~ .drop-item[data-parent='+unique_parent_id+']').css('display','none').show(400);

            }else{
                removeChildSelect(unique_parent_id, selectObject);
            }

        }
    })
}

function removeChildSelect(unique_parent_id, selectObject){

    selectObject.closest('.drop-item').find('~ .drop-item[data-parent='+unique_parent_id+']').hide(400,function(){
        jQuerOs(this).remove();
    });
}


function remove_class_for_hover_animate(element, hover_effect){
    setTimeout(function() {
        jQuerOs(element).removeClass('animated'); 
        jQuerOs(element).removeClass(hover_effect);
    }, 2000)
}

  
  function added_hover_animated_with_element(element, hover_effect){
        jQuerOs(element).css('display','block');
        var timer;
        jQuerOs(element).hover(
                
               function(){ 
                //jQuerOs(this).addClass('wow'); 
                jQuerOs(this).addClass('animated');
                jQuerOs(this).addClass(hover_effect); 
               },
               function(){ 
                   
                   //timer = setTimeout(function() {
                   //jQuerOs(this).removeClass('wow'); 
                   remove_class_for_hover_animate (element, hover_effect);
                       
                   //}, 5000);
                   //clearTimeout(timer);
               }
        );
    
}
if(window.jQuerOs){
    jQuerOs(window).on('load',function($) {
        var href = window.location.href;
        if(href.indexOf('administrator') == -1){
            if (window.WOW) {
              wow = new WOW(
                {
                  //offset: 300,
                }
              )
              wow.init();
            }            
        }

        var hover_animated_element = jQuerOs('[hover_animated]');
        for(index = 0; index < hover_animated_element.length; ++index){
            var hover_effect = jQuerOs(hover_animated_element[index]).attr('hover_animated');
            if(hover_effect && hover_effect != 'none'){
                added_hover_animated_with_element(hover_animated_element[index], hover_effect);
            }

        }
        if(jQuerOs('select#limit').length > 0){
            var val = jQuerOs('select#limit').val();
            var selected = false;
            for(var i=0; jQuerOs('select#limit option').length > i; i++){
                if(jQuerOs('select#limit option')[i].hasAttribute('selected')){
                    selected = true;
                }
            }
            
            if(!selected){
                var elem = jQuerOs('select#limit');
                jQuerOs(elem[0]).find('option[value=50]').attr('selected', 'selected')
//                jQuerOs(elem[0] + ' option[value=50]').prop('selected', true);
            }
            
        }
        
        jQuerOs(".cck_hover_side").each(function(){

            var widthScreen =  jQuerOs(window).width();
            var widthMegaMenu = jQuerOs(this).find(".os_cck_menu_child").width();
            var positionMegaMenu = jQuerOs(this).find(".os_cck_menu_child").offset();
            var positionItemMenu = jQuerOs(this).offset();
            var difference = widthScreen - widthMegaMenu;
            var newWidthMegaMenu = widthScreen-10;
            var newPositionMegaMenu = positionItemMenu.left;
            var widthMegaMenuForm = jQuerOs(this).find("form").width();

            if(positionMegaMenu != undefined && widthMegaMenu >= widthScreen){
                jQuerOs(this).find(".os_cck_menu_child").css({'width':newWidthMegaMenu});
                jQuerOs(this).find("form").css({'width':newWidthMegaMenu});
                jQuerOs(this).find(".os_cck_menu_child").css({'left':-newPositionMegaMenu+2});
            }

            if(positionMegaMenu != undefined && widthMegaMenu < widthScreen && positionMegaMenu.left < 0){
                jQuerOs(this).find(".os_cck_menu_child").css({'left':-newPositionMegaMenu+2});
                jQuerOs(this).find(".os_cck_menu_child").css({'width':widthMegaMenuForm});
            }

            if(positionMegaMenu != undefined && widthMegaMenu < widthScreen && ((positionMegaMenu.left + widthMegaMenu) > widthScreen)){
                jQuerOs(this).find(".os_cck_menu_child").css({'left':-newPositionMegaMenu + difference - 2});
                jQuerOs(this).find(".os_cck_menu_child").css({'width':widthMegaMenuForm});
            }


        })
        
    });

    jQuerOs(document).ready(function() {

          jQuerOs(".field-name").removeAttr("title");
          jQuerOs('.table-responsive').html(function(i,h){ 
                return h.replace(/&nbsp;/g,'<span class="hide"></span>'); 
          });
            //jQuerOs('.table-responsive .cck_tableC td .hide').parent().hide();
            
          

    });
    
    
}
function calculated_price(eiid, juri){

    
    var fields_calculate_price = jQuerOs('[eiid='+eiid+'] .calculated_price_value');
    
    for(var i=0; i<fields_calculate_price.length; ++i){

        var number_format = calculate_number_format[jQuerOs(fields_calculate_price[i]).attr('id')]
        // console.log('4444444444444444', number_format)
        var ordering_calculate_field = jQuerOs(fields_calculate_price[i]).attr('ordering_calculate');
//        var price_fields = jQuerOs('[eiid='+eiid+'] .hidden-price:not(#os_cck_layout .hidden-price)');
        var modal_layout = jQuerOs(fields_calculate_price[i]).parents('.os_cck_modal-body')
        
        if(modal_layout.length > 0){
            var price_fields = jQuerOs(fields_calculate_price[i]).parents('.instance_body:first').find('.hidden-price');
        }else{
            var price_fields = jQuerOs(fields_calculate_price[i]).parents('.instance_body').find('.hidden-price:not(.os_cck_modal-body .hidden-price)');
        }
        
        // jQuerOs('[eiid=87] .hidden-price:not(#os_cck_layout .hidden-price)')
        var json_price_fields = [];
        var quantity_fields = jQuerOs('[eiid='+eiid+'] .quantity_input');
        // console.log(':111111111111111111111:', quantity_fields);
        

        for(index = 0; index < price_fields.length; ++index){
            var fid = jQuerOs(price_fields[index]).attr('fid');

            var lid = jQuerOs(price_fields[index]).attr('lid');
            var calculated = jQuerOs(price_fields[index]).attr('calculated');
            json_price_fields[index] ={}
            json_price_fields[index]['fid'] = fid;
            json_price_fields[index]['lid'] = lid;
            json_price_fields[index]['calculated'] = calculated;
            if(quantity_fields.length > 0){
                var qf_index = 0
                while(qf_index < quantity_fields.length){
                    var qf_fid = jQuerOs(quantity_fields[qf_index]).attr('fid')
                    if(qf_fid == fid){

                        json_price_fields[index]['quantity'] = jQuerOs(quantity_fields[qf_index]).val();
                    }
                    qf_index++;
                }
            }
            // console.log('555555555555555555', price_fields[index])
            json_price_fields[index]['value'] = jQuerOs(price_fields[index]).val();
        }
        // console.log('22222222222222222:', json_price_fields);
        json_price_fields = JSON.stringify(json_price_fields);
        
        jQuerOs.ajax({
                dataType: "json",
                type: 'POST',
                url: juri+'index.php?option=com_os_cck&format=raw',
                data: {
                    task: "ajaxCalculatedPrice",
                    jsonPriceFields: json_price_fields,
                    eiid: eiid,
                    orderingCalculateField: ordering_calculate_field,
                    index: i,
                    number_format: number_format
                },
                success: function(data){
                    // console.log('333333333333333', data)
                    if(data.success){
                        jQuerOs(fields_calculate_price[data.index]).text(data.print_value);
                    }
                }
            });
        
        
    }

}

function get_total_price(currency_position, currency, number_format){
      var prices = jQuerOs('.hidden_calculated_price');
      //var currency_position = '<?php echo $os_cck_configuration->get("currency_position", "0"); ?>';
      
//      number_format = JSON.parse(number_format);
      
      var total_price = 0;
      for(var i =0; i < prices.length; ++i){
          var price = jQuerOs(prices[i]).val();
          
          total_price = total_price + Number(price);
          total_price = Math.floor(total_price * 100) / 100;
          
      }
      
      if(typeof number_format == 'string'){
          number_format = JSON.parse(number_format)
      }
      if(number_format != undefined && number_format.decimal_separator != undefined){
          
          total_price = numberFormat(total_price, number_format.digits_points, number_format.decimal_separator, number_format.thousands_separator)
      }
      
      //var total_price
      //var currency = '<?php echo getProductCurrency(); ?>'
      var new_total_price_html = '';
      if(currency_position == '0'){
          //jQuerOs('.total_price').html('<span class="new_total_price">' + total_price + ' ' + currency + '</span>');
          new_total_price_html = '<span class="new_total_price">' + total_price + ' ' + currency + '</span>';
      }else if(currency_position == '1'){
          new_total_price_html = '<span class="new_total_price">' + currency + ' ' + total_price + '</span>';
      }
      
      var old_total_price_html = '';
      var old_total_price = jQuerOs('.total_price').attr('old_total_price');

    //   console.log('33333333333', jQuerOs('#os_cck_coupon').length)
      
      if(old_total_price != undefined && old_total_price > total_price && jQuerOs('#os_cck_coupon').length > 0 && jQuerOs('#os_cck_coupon').val() != ''){
        // console.log('111111111111111')
          if(currency_position == '0'){
              //jQuerOs('.total_price').html('<span class="new_total_price">' + total_price + ' ' + currency + '</span>');
              old_total_price_html = '<span class="old_total_price">' + old_total_price + ' ' + currency + '</span>';
          }else if(currency_position == '1'){
              old_total_price_html = '<span class="old_total_price">' + currency + ' ' + old_total_price + '</span>';
          }
      }
      //console.log('111111111111', jQuerOs('#os_cck_coupon').val())
      //if(jQuerOs('#os_cck_coupon').val() != ''){
        jQuerOs('.total_price').html(old_total_price_html + ' ' + new_total_price_html);
      //}
      jQuerOs('.total_price').attr('old_total_price', total_price);
      

}


function checkRequireFields(form){

    ret_scroll = -1 ;

    //let required_fields = form.getElementsByClassName("required");
    let required_fields = jQuerOs("#"+form + " .required");
    jQuerOs(required_fields).each(function(index, el) {
     
      jQuerOs(el).removeClass("os_cck_required_error");

      if(jQuerOs(el).attr("name") == "new_audio"){
      var audio_src = jQuerOs(el).parent().parent().parent().find('.audio_src').val()
      
        audio = false;
        jQuerOs("[id*='new_upload_audio']").each(function(index, input) {
          if(jQuerOs(input).val()){
            audio = true;
          }
        });
        if(audio_src != ''&& audio_src != 0 && audio_src != undefined){
            audio = true;
        }
        if(!audio){
          //alert("<?php echo cck_lang_text('COM_OS_CCK_ADMIN_INFOTEXT_JS_EDIT_REQUIRED_AUDIO');?>");
          jQuerOs(el).addClass("os_cck_required_error");
          if(ret_scroll == -1 || ret_scroll > findPosY(el)-100){
            ret_scroll = findPosY(el)-100
          }
          ret = true;
        }
      }else if(jQuerOs(el).attr("name") == "new_video"){
        var video_src = jQuerOs(el).parent().parent().parent().find("[name*='video']").val();
        var youtube_src = jQuerOs(el).parent().parent().parent().find("[name*='youtube_code']").val();
        video = false;
        jQuerOs("[id*='new_upload_video']").each(function(index, input) {
          if(jQuerOs(input).val()){
            video = true;
          }
        });
        if((video_src != 0 && video_src != '' && video_src != undefined && video_src != 'Add video source file') 
                || (youtube_src != 0 && youtube_src != '' && youtube_src != undefined)){
            video = true;
        }
        if(!video){
          //alert("<?php echo cck_lang_text('COM_OS_CCK_ADMIN_INFOTEXT_JS_EDIT_REQUIRED_VIDEO');?>");
          jQuerOs(el).addClass("os_cck_required_error");
          if(ret_scroll == -1 || ret_scroll > findPosY(el)-100){
            ret_scroll = findPosY(el)-100
          }
          ret = true;
        }
      }//else if(jQuerOs(".rev_rating").length){
      else if(jQuerOs(el).parent(".rev_rating").length){
      
        if(!jQuerOs("[name*='fi_rating_field_']").val()){
          //alert("<?php echo cck_lang_text('COM_OS_CCK_ADMIN_INFOTEXT_JS_EDIT_REQUIRED');?>");
          jQuerOs(el).addClass("os_cck_required_error");
          if(ret_scroll == -1 || ret_scroll > findPosY(el)-100){
            ret_scroll = findPosY(el)-100
          }
          ret = true;
        }
      }else if(jQuerOs(el).parent().find("[name*='text_radio_buttons']").length){
      
        if(!jQuerOs(el).parents(".controls").find("[name*='text_radio_buttons']:checked").length){
          jQuerOs(el).addClass("os_cck_required_error");
          if(ret_scroll == -1 || ret_scroll > findPosY(el)-100){
            ret_scroll = findPosY(el)-100
          }
          ret = true;
        //   return false;
        }
      }else if(jQuerOs(el).parent().find("[name*='single_checkbox_onoff']").length){
      
        if(jQuerOs(el).prop('checked') == false){
            if(ret_scroll == -1 || ret_scroll > findPosY(el)-200){
                ret_scroll = findPosY(el)-200
              }
              var parents_elem = jQuerOs(el).parents('.drop-item')
              if(parents_elem.length > 0){
                var parent_el = parents_elem[0]
              }else{
                var parent_el = false
              }
              if(parent_el){
                jQuerOs(parent_el).addClass("os_cck_required_error");
              }
          //alert("<?php echo cck_lang_text('COM_OS_CCK_ADMIN_INFOTEXT_JS_EDIT_REQUIRED');?>");
          ret = true;
        }else{
            var parents_elem = jQuerOs(el).parents('.drop-item')
              if(parents_elem.length > 0){
                var parent_el = parents_elem[0]
              }else{
                var parent_el = false
              }
              if(parent_el){
                jQuerOs(parent_el).removeClass("os_cck_required_error");
              }
        }
      }else if(jQuerOs(el).parent().find("[name*='fi_text_url_']").length){
      
        if(!jQuerOs(el).val() || jQuerOs(el).val() == 'http://'){
          jQuerOs(el).addClass("os_cck_required_error");
          if(ret_scroll == -1 || ret_scroll > findPosY(el)-100){
            ret_scroll = findPosY(el)-100
          }
          ret = true;
        }
      }else if(jQuerOs(el).parent("[id*='galleryfield_']").length){
      var img_value = jQuerOs(el).parent().parent().parent().find("[id*='wrapper_'] input").val();
        if(img_value == '' || img_value == '[]'){
          jQuerOs(el).addClass("os_cck_required_error");
          if(ret_scroll == -1 || ret_scroll > findPosY(el)-100){
            ret_scroll = findPosY(el)-100
          }
          ret = true;
        }
      }else if(typeof jQuerOs(el).attr('name') != 'undefined' && (jQuerOs(el).attr('name').indexOf('imagefield') > -1
          || jQuerOs(el).attr('name').indexOf('filefield') > -1)){
      
        if(!jQuerOs(el).val() && jQuerOs(el).attr('value') == ''){
          jQuerOs(el).addClass("os_cck_required_error");
          if(ret_scroll == -1 || ret_scroll > findPosY(el)-100){
            ret_scroll = findPosY(el)-100
          }
          ret = true;
        }
      }else if(jQuerOs(el).parent().find("[name*='fi_text_textarea_']").length && typeof sceditor != 'undefined'){
          
          for(var i=0; i < text_area_arr.length; i++){
            if(text_area_arr[i] == jQuerOs(el).attr('id')) {
                var editor = window[text_area_arr[i]]
                var value = sceditor.instance(editor).val()
                if(value == '' || value == '<p><br></p>'){
                    jQuerOs(el).parent().find('div.sceditor-container').addClass("os_cck_required_error");
                      if(ret_scroll == -1 || ret_scroll > findPosY(el)-200){
                        ret_scroll = findPosY(el)-200
                      }
                      ret = true;
                }else{
                    jQuerOs(el).parent().find('div.sceditor-container').removeClass("os_cck_required_error");
                }
            }

          }
      }else if(jQuerOs(el).attr('data_field_name') != undefined && jQuerOs(el).attr('data_field_name').indexOf("fi_text_textarea") > -1 
                && typeof tinymce != 'undefined'){

          var editors = tinymce.get();
          editors.forEach(editor => {
              if(editor.id == jQuerOs(el).attr('data_field_name')){
                  var value = editor.getContent()
                  if(value == ''){
                    jQuerOs(el).find(".js-editor-tinymce").addClass("os_cck_required_error");
                      if(ret_scroll == -1 || ret_scroll > findPosY(el)-200){
                        ret_scroll = findPosY(el)-200
                      }
                      ret = true;
                }else{
                    jQuerOs(el).find(".js-editor-tinymce").removeClass("os_cck_required_error");
                }
              }
          });
          
      }else{

        if(!jQuerOs(el).hasClass('g-recaptcha')){
            if(!jQuerOs(el).val() || jQuerOs(el).val() == '' || (jQuerOs(el).parent().find("[name*='categories']").length && jQuerOs(el).val() == '0')){
                jQuerOs(el).addClass("os_cck_required_error");
                  if(ret_scroll == -1 || ret_scroll > findPosY(el)-200){
                    ret_scroll = findPosY(el)-200
                  }
                  ret = true;
            }
        }
      }
    });

}

function getBuyRentRequestCalculatedPrice(type, eiid, lid, number_format, juri){
    //    console.log('111111111111111', window.location)
    var ordering_calculate_field = 51;
    if(!eiid){
        var eiid = jQuerOs('[name=fk_eiid]').val();
    }

    if(!juri){
        juri = ''
    }

    var price_fields = jQuerOs('div.instance_body[eiid='+eiid+'] .hidden-price');
    if( price_fields == undefined || price_fields.length == 0 ){
        price_fields = jQuerOs(' .hidden-price');
    }    
    var json_price_fields = [];
    var quantity_fields = jQuerOs('div.instance_body[eiid='+eiid+'] .quantity_input');
    if( quantity_fields == undefined  || quantity_fields.length == 0){
        quantity_fields = jQuerOs(' .quantity_input');
    }    
    var coupon = jQuerOs('div.instance_body[eiid='+eiid+'] #os_cck_coupon').attr('coup_id');
    if( coupon == undefined ){
        coupon = jQuerOs(' #os_cck_coupon').attr('coup_id');
    }    
    
    var eid = jQuerOs('div.instance_body[eiid='+eiid+'] [name=eid]').val();
    if( eid == undefined ){
        eid = jQuerOs('[name=eid]').val();
    }    

    if(!lid){
        var lid = jQuerOs('div.instance_body[eiid='+eiid+'] [name=lay_type]').val();
        if( lid == undefined ){
            lid = jQuerOs('[name=lay_type]').val();
        }    
    }

    for(var index = 0; index < price_fields.length; ++index){
        // var number_format = calculate_number_format[jQuerOs(fields_calculate_price[i]).attr('id')]
        var fid = jQuerOs(price_fields[index]).attr('fid');

        var price_lid = jQuerOs(price_fields[index]).attr('lid');
        var calculated = jQuerOs(price_fields[index]).attr('calculated');
        json_price_fields[index] ={}
        json_price_fields[index]['fid'] = fid;
        json_price_fields[index]['lid'] = price_lid;
        json_price_fields[index]['calculated'] = calculated;
        if(quantity_fields.length > 0){
            var qf_index = 0
            while(qf_index < quantity_fields.length){
                var qf_fid = jQuerOs(quantity_fields[qf_index]).attr('fid')
                if(qf_fid == fid){
                    json_price_fields[index]['quantity'] = jQuerOs(quantity_fields[qf_index]).val();
                }
                qf_index++;
            }
        }
        json_price_fields[index]['value'] = jQuerOs(price_fields[index]).val();
    }


    json_price_fields = JSON.stringify(json_price_fields);
    
    if(price_fields.length == 0){

        if(jQuerOs('div.instance_body[eiid='+eiid+'] [name=price_fields]').val() != undefined 
            && jQuerOs('div.instance_body[eiid='+eiid+'] [name=price_fields]').val() != ''){
            
            json_price_fields=JSON.parse(jQuerOs('div.instance_body[eiid='+eiid+'] [name=price_fields]').val());
        }else if(jQuerOs('[name=price_fields]').val() != undefined && jQuerOs('[name=price_fields]').val() != ''){
            json_price_fields=JSON.parse(jQuerOs('[name=price_fields]').val());
        }
        //in cart: many prices, not in cart 1 price
        if( json_price_fields[0][eiid] ) json_price_fields=JSON.stringify(json_price_fields[0][eiid] );
        else json_price_fields=JSON.stringify(json_price_fields) ;
    }
    
    if(!type){
        if(jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').parents('.buy_request_wrapper').length > 0){
            type = 'buy_request_instance';
        }else{
            type = 'rent_request_instance';
        }
    }
    if(type == 'rent_request_instance'){
        var task = 'ajax_rent_calcualete'
    }else{
        var task = 'ajaxCalculatedPrice'
    }
    
    if(type == 'rent_request_instance'){
        
        var rent_from = jQuerOs('div.instance_body[eiid='+eiid+'] [class^=rent_from_rent_request_instance]').val();
        if( rent_from == undefined ){
            rent_from = jQuerOs('[class^=rent_from_rent_request_instance]').val();
        }  

        if(rent_from != undefined && rent_from.indexOf(' ') > -1){
            time_from = rent_from.slice(rent_from.indexOf(' ')+1)
            
            if(time_from == '00:00:00'){
                var time_from = jQuerOs('div.instance_body[eiid='+eiid+'] [name=time_from]').val();
                if( time_from == undefined ){
                    time_from = jQuerOs('[name=time_from]').val();
                }  
            }
            rent_from = rent_from.slice(0, rent_from.indexOf(' '))
        }else{
            var time_from = jQuerOs('div.instance_body[eiid='+eiid+'] [name=time_from]').val();
            if( time_from == undefined ){
                time_from = jQuerOs('[name=time_from]').val();
            }  
        }
    //    var rent_until = jQuerOs('div.instance_body[eiid='+eiid+'] [name=rent_until]').val();
        var rent_until = jQuerOs('div.instance_body[eiid='+eiid+'] [class^=rent_until_rent_request_instance]').val();
        if( rent_until == undefined ){
            rent_until = jQuerOs('[class^=rent_until_rent_request_instance]').val();
        }  

        if(rent_until != undefined && rent_until.indexOf(' ') > -1){
            time_until = rent_until.slice(rent_until.indexOf(' ')+1)
            if(time_until == '00:00:00'){
                var time_until = jQuerOs('div.instance_body[eiid='+eiid+'] [name=time_until]').val()
                if( time_until == undefined ){
                    time_until = jQuerOs('[name=time_until]').val();
                }  
            }
            rent_until = rent_until.slice(0, rent_until.indexOf(' '))
        }else{
            var time_until = jQuerOs('div.instance_body[eiid='+eiid+'] [name=time_until]').val();
            if( time_until == undefined ){
                time_until = jQuerOs('[name=time_until]').val();
            }  
        }
    }else{
        rent_from = '';
        time_from = '';
        rent_until = '';
        time_until = '';
    }


//    var time_until = jQuerOs('div.instance_body[eiid='+eiid+'] [name=time_until]').val();
    
    //run ajax only if TRUE
    if(rent_from != "" && rent_until != "" ){
        //rent calculate
        jQuerOs.ajax({
            dataType: "json",
            type: 'POST',
            url: juri+'index.php?option=com_os_cck&format=raw',
            data: {
                task: task,
                jsonPriceFields: json_price_fields,
                eiid: eiid,
                ceid: eid,
                rent_from: rent_from + ' ' + time_from,
                rent_until: rent_until + ' ' + time_until,
                current_lid: lid,
                orderingCalculateField: ordering_calculate_field,
                coupon: coupon,
                numberFormat: number_format
            },
            success: function(data){
            
                if(data.success){
                    
                    var old_price = jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').attr('old_price');

                    if(old_price != data.print_value){

                        if(jQuerOs('div.instance_body[eiid='+eiid+'] #os_cck_coupon').val() != undefined 
                            && jQuerOs('div.instance_body[eiid='+eiid+'] #os_cck_coupon').val() != '' 
                            && old_price != undefined && old_price != ''){
                            
                            jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').html('<span class="old_total_price">'+old_price+'</span> <span class="new_total_price">'+data.print_value+'</span>');
                        }else if(jQuerOs('div.instance_body[eiid='+eiid+']').lenght > 0){
                            jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').text(data.print_value);
                        }else{
                            jQuerOs('#buy_requesr_calculated_price').text(data.print_value);
                        }
                    }
                    jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').attr('old_price', data.print_value);
                }else{
                    jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').text(data.calculated_currency[0]);
                }
            }
        });
    } else {
        //buy calculate
        jQuerOs.ajax({
            dataType: "json",
            type: 'POST',
            url: juri+'index.php?option=com_os_cck&format=raw',
            data: {
                task: task,
                jsonPriceFields: json_price_fields,
                eiid: eiid,
                ceid: eid,
                current_lid: lid,
                orderingCalculateField: ordering_calculate_field,
                coupon: coupon,
                numberFormat: number_format
            },
            success: function(data){
                
                if(data.success){
                    
                    var old_price = jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').attr('old_price');
                    
                    if(old_price != data.print_value){

                        if(jQuerOs('div.instance_body[eiid='+eiid+'] #os_cck_coupon').val() != undefined && jQuerOs('div.instance_body[eiid='+eiid+'] #os_cck_coupon').val() != '' && old_price != undefined && old_price != ''){
                            
                            jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').html('<span class="old_total_price">'+old_price+'</span> <span class="new_total_price">'+data.print_value+'</span>');
                        }else if(jQuerOs('div.instance_body[eiid='+eiid+']').lenght > 0){
                            jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').text(data.print_value);
                        }else{
                            jQuerOs('#buy_requesr_calculated_price').text(data.print_value);
                        }
                    }
                    jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').attr('old_price', data.print_value);
                }else{
                    jQuerOs('div.instance_body[eiid='+eiid+']').find('#buy_requesr_calculated_price').text(data.calculated_currency[0]);
                }
            }
        });
    }

}

function CckAddToCart(eiid, onclick_event, onclick_event_value, elem, path_to_site){
    var this_instance =  jQuerOs(elem).parents('.instance_body');
    var price_fields = jQuerOs(this_instance).find('.hidden-price');
    
    var json_price_fields = [];
    var quantity_fields = jQuerOs('.quantity_input');
    for(index = 0; index < price_fields.length; ++index){
        var calculated = jQuerOs(price_fields[index]).attr('calculated');
        if(calculated == 0){
             price_fields.splice(index, 1);
        }
    }
    
    for(index = 0; index < price_fields.length; ++index){
//        console.log('333333333333333333', jQuerOs(price_fields[index]))
        if(jQuerOs(price_fields[index]).attr('eiid') == eiid){
            
            var fid = jQuerOs(price_fields[index]).attr('fid');
            var lid = jQuerOs(price_fields[index]).attr('lid');
            var calculated = jQuerOs(price_fields[index]).attr('calculated');
            if (calculated == 1){
                json_price_fields[index] ={}
                json_price_fields[index]['fid'] = fid;
                json_price_fields[index]['lid'] = lid;
                json_price_fields[index]['calculated'] = calculated;
                if(quantity_fields.length > 0){
                    var qf_index = 0
                    while(qf_index < quantity_fields.length){
                        var qf_fid = jQuerOs(quantity_fields[qf_index]).attr('fid')
                        if(qf_fid == fid){
                            json_price_fields[index]['quantity'] = jQuerOs(quantity_fields[qf_index]).val();
                        }
                        qf_index++;
                    }
                }else if(index == 0){
                    json_price_fields[index]['quantity'] = 1;
                }
                json_price_fields[index]['value'] = jQuerOs(price_fields[index]).val();
            }
        }
    }
//    console.log('111111111111', json_price_fields)
    json_price_fields = JSON.stringify(json_price_fields);
    
    jQuerOs.ajax({
        dataType: "json",
        type: 'POST',
        url: path_to_site+'index.php?option=com_os_cck&format=raw',
        data: {
            task: 'addInstanceToCart',
            eiid: eiid,
            price_fields: json_price_fields,
        },
        success: function(data){
            
            
            if(onclick_event == 0){
                
            }else{
                location=onclick_event_value;
            }
            var small_carts = jQuerOs('.small_cart');
            
            for(var index=0; index < small_carts.length; ++index){
                
                
                
                //var change_effect = jQuerOs(small_carts[index]).attr('change_effect');
                var html = '<div>'+data.cart_count+'</div>';
                jQuerOs(small_carts[index]).find('.small_cart_count').html(html);
                
                
                if(cart_count == 0 && data.cart_count > 0){
                    var text_full_cart = jQuerOs(small_carts[index]).attr('full_text');
                    var img_full_cart = jQuerOs(small_carts[index]).attr('img_full');
                    
                    if(img_full_cart.indexOf('img') == 0 && img_full_cart.length > 4){
                        var img_full_cart_value = '<img src="'+path_to_site+'images/os-cck/'+img_full_cart.substr(img_full_cart.indexOf(' ')+1)+'">';
                    }else if(img_full_cart.indexOf('icon') == 0 && img_full_cart.length > 5){
                        var img_full_cart_value = '<div class="fa '+img_full_cart.substr(img_full_cart.indexOf(' ')+1)+'"></div>'
                    }else{
                        var img_full_cart_value = '<div class="fa fa-shopping-bag"></div>'
                    }
                    
                    jQuerOs(small_carts[index]).find('.text_cart').html(text_full_cart);
                    jQuerOs(small_carts[index]).find('.small_cart_img').html(img_full_cart_value);
                }
                
                jQuerOs(small_carts[index]).addClass('addItem');
                //jQuerOs(small_carts[index]).find('a').addClass('animated');
                setTimeout(removeAnimateClass,1000,jQuerOs(small_carts[index])); 
            }
            
            jQuerOs('.small_cart_img').addClass('cart_fixed');
            setTimeout(removeFixesClass,1000); 
            
            cart_count = data.cart_count;
            
        },
        // error: function(){
        //     alert('Error!!!')
        // }
    })
    
    
}

function removeAnimateClass(element){
    //jQuerOs(element).removeClass('animated');
    jQuerOs(element).removeClass('addItem');
}

function removeFixesClass(){
    jQuerOs('.small_cart_img').removeClass('cart_fixed');
}

function loadInstance(){
    //jQuerOs("*[class*='hide-field-name-'] , .delete-field, .delete-row, .delete-layout").remove();
    jQuerOs(".delete-field, .delete-row, .delete-layout").remove();
  
    jQuerOs("div[class *='cck-row-'], div[id *='cck_col-']").each(function(index, el) {
      jQuerOs(el).attr("style",jQuerOs(el).data("block-styling"));
    });
    jQuerOs("div[class *='cck-row-']").addClass('row');
    ////jQuerOs("span.cck-help-string").remove()

    jQuerOs(function () {
      jQuerOs('[data-toggle="tooltip"]').tooltip()
    })

    var delete_empty_child_entity = jQuerOs('.delete_empty_child_entity');
    if(delete_empty_child_entity.length > 0){
        for(index = 0; index < delete_empty_child_entity.length; ++index){
            jQuerOs(delete_empty_child_entity[index]).parent().parent().remove();
        }
    }
}

function changeRadioPrice(name, rand, show_quantity, fid, eiid, lid, type, numberFormat, juri){
    var select_value = jQuerOs('[name=fi_'+name+rand+']:checked').val();
    
    jQuerOs.ajax({
        dataType: "json",
        type: 'POST',
        url: juri+'index.php?option=com_os_cck&format=raw',
        data: {
            task: "ajaxCalculatedCurrency",
            selectValue: select_value,
            fid: fid,
            eiid: eiid,
            lid: lid,
            numberFormat: numberFormat
        },
        success: function(data){
            if(data.success){
//                console.log('6666666666666666666', jQuerOs('div.instance_body[eiid='+eiid+']').find('#'+name+'_price_value'))
                if(type == 'value'){
                    jQuerOs('div.instance_body[eiid='+eiid+']').find('#'+name+'_price_value .'+name+'_price_value').text(data.print_value);
                }else if(type == 'percent'){
                    jQuerOs('div.instance_body[eiid='+eiid+']').find('#'+name+'_price_value .'+name+'_price_value').text(data.value);
                }

                if(show_quantity == 'on'){
                    jQuerOs('div.instance_body[eiid='+eiid+']').find('#'+name+'_quantity_value').text(data.quantity);
                }
                jQuerOs('div.instance_body[eiid='+eiid+']').find('[name=fi_'+name+'_hidden]').val(select_value);
                calculated_price(eiid,juri);
                getBuyRentRequestCalculatedPrice(false, eiid, 0, "", juri);
            }
        }
    });
}

function changeSelectPrice(name, rand, show_quantity, fid, eiid, lid, numberFormat, juri){
    var select_value = jQuerOs('#fi_'+name).val();

    jQuerOs.ajax({
        dataType: "json",
        type: 'POST',
        url: juri+'index.php?option=com_os_cck&format=raw',
        data: {
            task: "ajaxCalculatedCurrency",
            selectValue: select_value,
            fid: fid,
            eiid: eiid,
            lid: lid,
            numberFormat: numberFormat
        },
        success: function(data){
            if(data.success){
                jQuerOs('div.instance_body[eiid='+eiid+']').find('#'+name+'_price_value .'+name+'_price_value').text(data.print_value);

                if(show_quantity == 'on'){
                    jQuerOs('div.instance_body[eiid='+eiid+']').find('#'+name+'_quantity_value').text(data.quantity);
                }
                jQuerOs('div.instance_body[eiid='+eiid+']').find('[name=fi_'+name+'_hidden]').val(select_value);
                calculated_price(eiid,juri);
                getBuyRentRequestCalculatedPrice(false, eiid, 0, "", juri);
            }
        }
    });
}

function loadGalleryfield(name, rand, slider_height, object_fit, number_of_plugin, layout_instanse_id){
    jQuerOs(window).on('load',function($){
        var width = jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container').width();
          
        jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container .swiper-slide img').height(width*slider_height);
        jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container .swiper-slide img').css('object-fit',object_fit);
        
        jQuerOs('#show_request_layout_'+layout_instanse_id).on('shown.bs.os_cck_modal', function (e) {
            if(window.gallery != undefined){
                gallery.init();
            }
            var width = jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container').width();

            jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container .swiper-slide img').height(width*slider_height);
            var mySwiper = document.querySelector('#cck-slider-container-'+name+'-'+rand+' .swiper-container').swiper
            
        })
        
    })
      
    if(number_of_plugin == '1') number_of_plugin = '';
    
    jQuerOs('.shadetabs li a[rel=country_cck'+number_of_plugin+']').on('click', function(){
        jQuerOs('#country_cck'+number_of_plugin).show();
        
        if(window.gallery != undefined){
            gallery.init();
        }
        var width = jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container').width();
        
        jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container .swiper-slide img').height(width*slider_height);
        var mySwiper = document.querySelector('#cck-slider-container-'+name+'-'+rand+' .swiper-container').swiper    
        
        
        
    })
    
    jQuerOs('.os_cck_menu_child').parent().hover(
                
           function(){ 
            
                if(window.gallery != undefined){
                    gallery.init();
                }
                var width = jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container').width();
                
                jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container .swiper-slide img').height(width*slider_height);
                var mySwiper = document.querySelector('#cck-slider-container-'+name+'-'+rand+' .swiper-container').swiper    
           },
           function(){ 

               //timer = setTimeout(function() {
               //jQuerOs(this).removeClass('wow'); 
//               remove_class_for_hover_animate (element, hover_effect);

               //}, 5000);
               //clearTimeout(timer);
           }
    );
    jQuerOs(window).resize(function(){
        var width = jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container').width();
        jQuerOs('#cck-slider-container-'+name+'-'+rand+' .swiper-container .swiper-slide img').height(width*slider_height);
    })
}

function checkUploadedFiles(form, post_max_size, upl_max_fsize, file_upl, error_upload_off, 
    error_post_max_size, error_upl_max_size, js_track_kind, js_track_lang, js_track_title){
    
    var total_file_size = 0;
    
    for (i = 1;document.getElementById('new_upload_video'+i); i++){
        if(document.getElementById('new_upload_video'+i).files.length){
          total_file_size += document.getElementById('new_upload_video'+i).files[0].size;
          if(document.getElementById('new_upload_video'+i).value != ''){
            if(!file_upl){
              window.scrollTo(0,findPosY(document.getElementById('new_upload_video'+i))-100);
              document.getElementById('error_video').innerHTML = error_upload_off;
              document.getElementById('new_upload_video'+i).style.borderColor = "#FF0000";
              document.getElementById('new_upload_video'+i).style.color = "#FF0000";
              document.getElementById('error_video').style.color = "#FF0000";
              
              return;
            }
            if(document.getElementById('new_upload_video'+i).files[0].size >= post_max_size){
              window.scrollTo(0,findPosY(document.getElementById('new_upload_video'+i))-100);
              document.getElementById('error_video').innerHTML = error_post_max_size;
              document.getElementById('new_upload_video'+i).style.borderColor = "#FF0000";
              document.getElementById('new_upload_video'+i).style.color = "#FF0000";
              document.getElementById('error_video').style.color = "#FF0000";
              return;
            }
            if(document.getElementById('new_upload_video'+i).files[0].size >= upl_max_fsize){
              window.scrollTo(0,findPosY(document.getElementById('new_upload_video'+i))-100);
              document.getElementById('error_video').innerHTML = error_upl_max_size;
              document.getElementById('new_upload_video'+i).style.borderColor = "#FF0000";
              document.getElementById('new_upload_video'+i).style.color = "#FF0000";
              document.getElementById('error_video').style.color = "#FF0000";
              return;
            }
          }
        }
      }

      for (i = 1;document.getElementById('new_upload_audio'+i); i++){
        if(document.getElementById('new_upload_audio'+i).files.length){
          if(document.getElementById('new_upload_audio'+i).value != ''){
            total_file_size += document.getElementById('new_upload_audio'+i).files[0].size;
            if(!file_upl){
              window.scrollTo(0,findPosY(document.getElementById('new_upload_audio'+i))-100);
              document.getElementById('error_video').innerHTML = error_upload_off;
              document.getElementById('new_upload_audio'+i).style.borderColor = "#FF0000";
              document.getElementById('new_upload_audio'+i).style.color = "#FF0000";
              document.getElementById('error_audio').style.color = "#FF0000";
              return;
            }
            if(document.getElementById('new_upload_audio'+i).files[0].size >= post_max_size){
              window.scrollTo(0,findPosY(document.getElementById('new_upload_audio'+i))-100);
              document.getElementById('error_video').innerHTML = error_post_max_size;
              document.getElementById('new_upload_audio'+i).style.borderColor = "#FF0000";
              document.getElementById('new_upload_audio'+i).style.color = "#FF0000";
              document.getElementById('error_audio').style.color = "#FF0000";
              return;
            }
            if(document.getElementById('new_upload_audio'+i).files[0].size >= upl_max_fsize){
              window.scrollTo(0,findPosY(document.getElementById('new_upload_audio'+i))-100);
              document.getElementById('error_video').innerHTML = error_upl_max_size;
              document.getElementById('new_upload_audio'+i).style.borderColor = "#FF0000";
              document.getElementById('new_upload_audio'+i).style.color = "#FF0000";
              document.getElementById('error_audio').style.color = "#FF0000";
              return;
            }
          }
        }
      }

      if(total_file_size >= post_max_size){
        if(document.getElementById('error_video')){
          window.scrollTo(0,findPosY(document.getElementById('error_video'))-100);
          document.getElementById('error_video').innerHTML = error_post_max_size;
          document.getElementById('error_video').style.borderColor = "#FF0000";
          document.getElementById('error_video').style.color = "#FF0000";
          document.getElementById('error_video').style.color = "#FF0000";
          return;
        }
        if(document.getElementById('error_audio')){
          window.scrollTo(0,findPosY(document.getElementById('error_audio'))-100);
          document.getElementById('error_audio').innerHTML = error_post_max_size;
          document.getElementById('error_audio').style.borderColor = "#FF0000";
          document.getElementById('error_audio').style.color = "#FF0000";
          document.getElementById('error_audio').style.color = "#FF0000";
          return;
        }
      }

      if(form.new_upload_track_url1){
        for (i = 1;document.getElementById('new_upload_track_url'+i); i++) {
          if(document.getElementById('new_upload_track'+i).value != ''
            || document.getElementById('new_upload_track_url'+i).value != ''){
              if(document.getElementById('new_upload_track_kind'+i).value == ''){
                window.scrollTo(0,findPosY(document.getElementById('new_upload_track_kind'+i))-100);
                document.getElementById('new_upload_track_kind'+i).placeholder = js_track_kind;
                document.getElementById('new_upload_track_kind'+i).style.borderColor = "#FF0000";
                document.getElementById('new_upload_track_kind'+i).style.color = "#FF0000";
                return;
              }else if(document.getElementById('new_upload_track_scrlang'+i).value == ''){
                window.scrollTo(0,findPosY(document.getElementById('new_upload_track_scrlang'+i))-100);
                document.getElementById('new_upload_track_scrlang'+i).placeholder = js_track_lang;
                document.getElementById('new_upload_track_scrlang'+i).style.borderColor = "#FF0000";
                document.getElementById('new_upload_track_scrlang'+i).style.color = "#FF0000";
                return;
              }else if(document.getElementById('new_upload_track_label'+i).value == ''){
                window.scrollTo(0,findPosY(document.getElementById('new_upload_track_label'+i))-100);
                document.getElementById('new_upload_track_label'+i).placeholder = js_track_title;
                document.getElementById('new_upload_track_label'+i).style.borderColor = "#FF0000";
                document.getElementById('new_upload_track_label'+i).style.color = "#FF0000";
                return;
              }
            }
          }
        }
}

function loadAddLayout(){
     jQuerOs("div[class *='cck-row-'], div[id *='cck_col-']").each(function(index, el) {
      jQuerOs(el).attr("style",jQuerOs(el).data("block-styling"))
    });
     jQuerOs("div[class *='cck-row-']").addClass('row');
}

function button_hidden(is_hide,moduleId) {
  var moduleSuffix = '';
  if (moduleId){
    moduleSuffix = '_cckmod_'+moduleId;
  }
  if (moduleId) {
    var el = document.getElementById('button_hidden_review'+moduleSuffix);
    var el2 = document.getElementById('hidden_review'+moduleSuffix);
  }else{
    var el = document.getElementById('button_hidden_review');
    var el2 = document.getElementById('hidden_review');
  }
  if (is_hide) {
    el.style.display = 'none';
    el2.style.display = 'block';
  } else {
    el.style.display = 'block';
    el2.style.display = 'none';
  }
}

function findPosY(obj) {
  var curtop = 0;
  if (obj.offsetParent) {
    while (1) {
      curtop+=obj.offsetTop;
      if (!obj.offsetParent) {
        break;
      }
      obj=obj.offsetParent;
    }
  } else if (obj.y) {
    curtop+=obj.y;
  }
  return curtop-20;
}

function checkStepNumberFields(){
    if(jQuerOs("input[name*='fi_decimal_textfield_']:not(.hidden-price)").length){
      jQuerOs("input[name*='fi_decimal_textfield_']:not(.hidden-price)").each(function(index, el) {

        jQuerOs(el).removeClass("os_cck_required_error");

        if(jQuerOs(this).val()){
          step = jQuerOs(this).attr("step");
          if(step.indexOf(".") >= 0){
            point = step.length - 1 - step.indexOf(".");
            numb = step.length-1-point;
          }else{
            numb = jQuerOs(this).attr("max");
            point = 0;
          }

          if(step.indexOf(".") >= 0){
            vallen = jQuerOs(this).val().length;
            if(numb < vallen-1-jQuerOs(this).val().indexOf(".")){
              window.scrollTo(0,findPosY(el)-100);
              jQuerOs(el).addClass("os_cck_required_error");
              alert("Format: "+step);
              ret = true;
              return false;
            }
            if(point < vallen - 1 - jQuerOs(this).val().indexOf(".")){
              window.scrollTo(0,findPosY(el)-100);
              jQuerOs(el).addClass("os_cck_required_error");
              alert("Format: "+step);
              ret = true;
              return false;
            }
          }else{
            if(numb < parseInt(jQuerOs(this).val())){
              window.scrollTo(0,findPosY(el)-100);
              jQuerOs(el).addClass("os_cck_required_error");
              alert("Max: "+numb);
              ret = true;
              return false;
            }
          }
        }
      });
    }
}

function check_reg_expr(form_name){
    var error = false;
     // console.log(":00000000000000:",form_name );

    //check for regular exp // set array of field=>exp in fields above(checkExpr)
    if( typeof checkExpr === 'undefined' ) return ;
     // console.log(":1111111111111111111111:",checkExpr );

    jQuerOs(checkExpr).each(function(index, el) {
         // console.log(':4444444444444:', this)
      if(typeof el == "object"){
        jQuerOs("[name='fi_"+el[0]+"']").removeClass("os_cck_required_error");
        regEx = eval(decodeURI(el[1]));
        if(regEx){
          str = jQuerOs("form[name='"+form_name+"'] [name='fi_"+el[0]+"']").val();
            // console.log(':555555555555555:', str)
          
          if (str.length === 0 ) return ;

          if(!regEx.test(str)){
            if(el[2]){
              errorText = el[2];
            }else{
              errorText = '';
            }
            // console.log('1111111111111', el)
            window.scrollTo(0,findPosY(jQuerOs("[name='fi_"+el[0]+"']")[0])-100);
            // jQuerOs("[name='fi_"+el[0]+"']").attr("placeholder", errorText);
            jQuerOs("."+el[0]+"_error_text").text(errorText);
            // jQuerOs("[name='fi_"+el[0]+"']").css("borderColor", "#FF0000"); 15.11.2025
            // jQuerOs("[name='fi_"+el[0]+"']").css("color","#FF0000");
            jQuerOs("[name='fi_"+el[0]+"']").addClass("os_cck_required_error");
            error = true;
          };
        }
      }
    });
    if(error){ret = error}
}

function isset(obj) {
    if (typeof obj === 'undefined' || obj === null ) {
    return false;
    } else {
    return true;
    }
}
function numberFormat( number, decimals, dec_point_sep, thousands_sep ) {  // Format a number with grouped thousands

    var i, j, kw, kd, km;

    if( isNaN(decimals = Math.abs(decimals)) && decimals > 0){

        decimals = 2;

    }

    if( dec_point_sep == undefined ){

        dec_point_sep = ",";

    }

    if( thousands_sep == undefined ){

        thousands_sep = ".";
    }
    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

    if( (j = i.length) > 3 ){
        j = j % 3;
    } else{
        j = 0;
    }
    km = (j ? i.substr(0, j) + thousands_sep : "");

    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    
    if(Number.isInteger(number)){
        kd = '';
    }else{
        kd = (decimals ? dec_point_sep + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
    }
    
    return km + kw + kd;
}

function changeBookingCalendarAjax(elem, juri){
    
    var form = jQuerOs(elem).parents('form')
    // console.log('1111111111111111', jQuerOs(form).find('#month_selected'))
    var month_selected = jQuerOs(form).find('[name=month_selected]').val()
    var year_selected = jQuerOs(form).find('[name=year_selected]').val()
    var eiid = jQuerOs(elem).parents('.cck-body.instance_body').attr('eiid')
    
    var eid = jQuerOs(elem).parents('.cck-body.instance_body').attr('eid')
    var lid = jQuerOs(elem).parents('.cck_booking_calendar').attr('lid')
    
    jQuerOs.ajax({
        dataType: "json",
        type: 'POST',
        url: juri+'index.php?option=com_os_cck&format=raw',
        data: {
            task: 'getBookingCalendarAjax',
            month_selected: month_selected,
            year_selected: year_selected,
            eiid: eiid,
            eid: eid,
            lid: lid
        },
        success: function(data){
            jQuerOs(elem).parents('.cck_booking_calendar').html(data.html)
            addBookCalListener();
        }
    })
    // console.log('1111111111111111', month_selected, year_selected)

}

function changeCalendarAjax(elem, juri){
    
    var form = jQuerOs(elem).parents('form')
    // console.log('1111111111111111', jQuerOs(form).find('#month_selected'))
    var month_selected = jQuerOs(form).find('[name=month]').val()
    var year_selected = jQuerOs(form).find('[name=year]').val()
    
    // var eid = jQuerOs(elem).parents('.cck-body.instance_body').attr('eid')
    var lid = jQuerOs(elem).parents('.cck-calendar').attr('lid')
    
    // return;
    jQuerOs.ajax({
        dataType: "json",
        type: 'POST',
        url: 'index.php?option=com_os_cck&format=raw',
        data: {
            task: 'getCalendarAjax',
            month: month_selected,
            year: year_selected,
            lid: lid
        },
        success: function(data){

            jQuerOs(elem).parents('.cck-calendar').parent().html(data.html)
            addCalListener();
        }
    })
    // console.log('1111111111111111', month_selected, year_selected)

}



