function errorPlacement(error, element) {
    var placement = $(element).data('error');
    if (placement) {
        $(placement).append(error)
    } else {
        error.insertAfter(element);
    }
}
//Start Custom Alert System
var ALERT_TITLE = "Oops!";
var ALERT_BUTTON_TEXT = "Ok";

if (document.getElementById) {
    window.alert = function (txt) {
        createCustomAlert(txt);
    }
}

function createCustomAlert(txt) {
    d = document;
    if (d.getElementById("modalContainer"))
        return;

    mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
    mObj.id = "modalContainer";
    mObj.style.height = d.documentElement.scrollHeight + "px";
    alertObj = mObj.appendChild(d.createElement("div"));
    alertObj.id = "alertBox";
    alertObj.style.top = '30%';
    alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth) / 2 + "px";
    alertObj.style.visiblity = "visible";
    h1 = alertObj.appendChild(d.createElement("h1"));
    h1.appendChild(d.createTextNode(ALERT_TITLE));
    msg = alertObj.appendChild(d.createElement("p"));
    msg.innerHTML = txt;
    btn = alertObj.appendChild(d.createElement("a"));
    btn.id = "closeBtn";
    btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
    btn.href = "#";
    btn.focus();
    btn.onclick = function () {
        removeCustomAlert();
        return false;
    }
    alertObj.style.display = "block";
}

function removeCustomAlert() {
    document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}
function ful() {
    alert('Alert this pages');
}
//End of Custom Alert System

function setnumkeys2(inputFiledId) {
    jQuery('.numkey2').click(function (event) {
        
        var numBox = document.getElementById(inputFiledId);
        numBox.value = numBox.value + this.innerHTML;
        event.stopPropagation();
    });
    $('.btnPin2').click(function (event) {
        
        if (this.innerHTML == 'DEL') {
            var numBox = document.getElementById(inputFiledId);
            if (numBox.value.length > 0) {
                numBox.value = numBox.value.substring(0, numBox.value.length - 1);
            }
        } else {
            document.getElementById(inputFiledId).value = '';
        }
        event.stopPropagation();
    });
}

function setnumkeys(inputFiledId) {
    jQuery('.numkey').click(function (event) {
        
        var numBox = document.getElementById(inputFiledId);
        numBox.value = numBox.value + this.innerHTML;
        event.stopPropagation();
    });
    $('.btnPin').click(function (event) {
        
        if (this.innerHTML == 'DEL') {
            var numBox = document.getElementById(inputFiledId);
            if (numBox.value.length > 0) {
                numBox.value = numBox.value.substring(0, numBox.value.length - 1);
            }
        } else {
            document.getElementById(inputFiledId).value = '';
        }
        event.stopPropagation();
    });
}



function checkBlock(checkId, blockId) {
    if ($("#" + checkId).is(":checked")) {
        $("#" + blockId).slideDown();
    } else {
        $("#" + blockId).slideUp(500);
    }
    $("#" + checkId).click(function () {
        if ($(this).is(":checked")) {
            $("#" + blockId).slideDown();
        } else {
            $("#" + blockId).slideUp();
        }
    });
    manageWindowHeight();
}
function toggleContent(contentBlock) {
    
    $('#' + contentBlock).slideToggle(function () {
        $(".menu-info a").find('span').toggleClass('glyphicon-minus glyphicon-plus');
    });
}

function goBack() {
    window.history.back();
}

function manageWindowHeight() {
    if ($('.bodyitem').length > 0 && $('.left-item').length > 0) {
        var windowHeight = $(window).height();
        var headerHeight = $('#gkposHeader').height();
        var footerHeight = $('#gkposFooter').height();
        var bodyContentHeight = $('.bodyitem').height();
        var leftContentHeight = $('.left-item').height();
        var RightContentHeight = 0;
        if ($('.right-item').length > 0) {
            RightContentHeight = $('.right-item').height();
        }
        var calculatedLeftItemHeight = windowHeight - (headerHeight + footerHeight);
        if (leftContentHeight > windowHeight && leftContentHeight > RightContentHeight) {
            $('.left-item').css({'minHeight': leftContentHeight + 'px'});
            if ($('.right-item').length > 0) {
                $('.right-item').css({'minHeight': leftContentHeight + 'px'});
            }
            $('.bodyitem').css({'minHeight': leftContentHeight + 'px'});
        } else if (bodyContentHeight > leftContentHeight || bodyContentHeight > windowHeight) {
            $('.left-item').css({'minHeight': bodyContentHeight + 'px'});
            if ($('.right-item').length > 0) {
                $('.right-item').css({'minHeight': bodyContentHeight + 'px'});
            }
            $('.bodyitem').css({'minHeight': bodyContentHeight + 'px'});
        } else if (RightContentHeight > leftContentHeight || RightContentHeight > windowHeight) {
            $('.left-item').css({'minHeight': RightContentHeight + 'px'});
            if ($('.right-item').length > 0) {
                $('.right-item').css({'minHeight': RightContentHeight + 'px'});
            }
            $('.bodyitem').css({'minHeight': RightContentHeight + 'px'});
        } else {
            $('.left-item').css({'minHeight': calculatedLeftItemHeight + 'px'});
            if ($('.right-item').length > 0) {
                $('.right-item').css({'minHeight': calculatedLeftItemHeight + 'px'});
            }
            $('.bodyitem').css({'minHeight': calculatedLeftItemHeight + 'px'});
        }
        $(window).bind('resize', function () {
            var windowHeight = $(window).height();
            var headerHeight = $('#gkposHeader').height();
            var footerHeight = $('#gkposFooter').height();
            var bodyContentHeight = $('.bodyitem').height();
            var leftContentHeight = $('.left-item').height();
            var RightContentHeight = 0;
            if ($('.right-item').length > 0) {
                RightContentHeight = $('.right-item').height();
            }
            var calculatedLeftItemHeight = windowHeight - (headerHeight + footerHeight);
            if (leftContentHeight > windowHeight && leftContentHeight > bodyContentHeight) {
                $('.left-item').css({'minHeight': leftContentHeight + 'px'});
                if ($('.right-item').length > 0) {
                    $('.right-item').css({'minHeight': leftContentHeight + 'px'});
                }
                $('.bodyitem').css({'minHeight': leftContentHeight + 'px'});
            } else if (bodyContentHeight > leftContentHeight || bodyContentHeight > windowHeight) {
                $('.left-item').css({'minHeight': bodyContentHeight + 'px'});
                if ($('.right-item').length > 0) {
                    $('.right-item').css({'minHeight': bodyContentHeight + 'px'});
                }
                $('.bodyitem').css({'minHeight': bodyContentHeight + 'px'});
            } else if (RightContentHeight > leftContentHeight || RightContentHeight > windowHeight) {
                $('.left-item').css({'minHeight': RightContentHeight + 'px'});
                if ($('.right-item').length > 0) {
                    $('.right-item').css({'minHeight': RightContentHeight + 'px'});
                }
                $('.bodyitem').css({'minHeight': RightContentHeight + 'px'});
            } else {
                $('.left-item').css({'minHeight': calculatedLeftItemHeight + 'px'});
                if ($('.right-item').length > 0) {
                    $('.right-item').css({'minHeight': calculatedLeftItemHeight + 'px'});
                }
                $('.bodyitem').css({'minHeight': calculatedLeftItemHeight + 'px'});
            }
        });
    }
}
function addjqueryValidatorFunction() {
    jQuery.validator.addMethod("lettersonly", function (value, element) {
        return this.optional(element) || /^[a-z\s]+$/i.test(value);
    }, "Only alphabetical characters");
    jQuery.validator.addMethod('phone', function (value, element) {
        return this.optional(element) || value.length > 9 &&
                value.match(/^(((\+44)? ?(\(0\))? ?)|(0))( ?[0-9]{3,4}){3}$/);
    }, 'Please specify a valid phone number');
    jQuery.validator.addMethod("postcodeUK", function (value, element) {
        return this.optional(element) || /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i.test(value);
    }, "Please specify a valid Postcode");

    $.validator.addMethod("loginRegex", function (value, element) {
        return this.optional(element) || /^[A-Za-z][a-z0-9\-\s]+$/i.test(value);
    }, "Username must contain only letters, numbers, or dashes.");

    $.validator.addMethod("email", function (value, element) {
        return this.optional(element) || /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value);
    }, "Please specify valid email addresss");
}

function getPage(url, info) {
    
    $.ajax({
        url: url,
        type: "POST",
        data: {
            info: info
        },
        beforeSend: function () {
            $('#GkposAjaxPageLoader').toggle('slow');
        },
        success: function (output) {
            $('#GkposAjaxPageLoader').toggle('slow');
            $('#KeyboardSetting').html('');
            $('#MiddleContent').html('');
            $('#userPageHeading').html('');
            $('#userPageHeading').html('');
            if (Array.isArray(info)) {
                $('#userPageHeading').append(info[2]);
            } else {
                $('#userPageHeading').append(info);
            }
            $('#MiddleContent').append(output);
            if ($('#OnlineOrderBtnsVisibility').length > 0) {
                $('.online-order-btn-visibility').show();
            } else {
                $('.online-order-btn-visibility').hide();
            }
            manageWindowHeight();
        }
    });
}

function getBaseAjaxPage(url, info, orders) {
    
    $('#GkposAjaxPageLoader').toggle('slow');
    var flag = true;
    if (typeof orders !== 'undefined') {
        var accessor = orders[0];
        var creator = orders[1];
        var is_loggedin = orders[2];
        var username = orders[3];
        if (accessor == creator) {
            flag = true;
        } else if (accessor != creator && is_loggedin == 0) {
            url += '/' + accessor;
            flag = true;
        } else {
            flag = false;
            $.confirm({
                'title': 'Order Processing Warning!!!',
                'message': 'Sorry! ' + username + ' is processing this order',
                'buttons': {
                    'OK': {
                        'class': 'btn btn-success',
                        'action': function () {}
                    },
                }
            });
        }
    }
    if (flag) {
        $.ajax({
            url: url,
            type: "POST",
            data: {
                info: info
            },
            success: function (output) {
                $('#GkposAjaxPageLoader').toggle('slow');
                $('#body').html('');
                $('#body').html('');
                $('#body').html(output);
                if ($('#OnlineOrderBtnsVisibility').length > 0) {
                    $('.online-order-btn-visibility').show();
                } else {
                    $('.online-order-btn-visibility').hide();
                }
                manageWindowHeight();
            }
        });
    }

}


function addHeaderKeyboard() {
    if ($('#clientsCTA').is(':visible')) {
        $('#clientsDropDown #clientsDashboard').fadeOut('slow');
    } else {
        if ($('#clientsCTA1').is(':visible')) {
            $('#clientsDropDown1 #clientsDashboard1').fadeOut('slow');
            return false;
        }
    }
    $('#clientsDropDown1').html('<div id="clientsDashboard1"><p id="clientsCTA1"></p></div>');
    keyboard('clientsCTA1');
    $('#clientsDropDown1 #clientsDashboard1').slideToggle({
        direction: "down"
    }, 300);
    $(this).toggleClass('clientsClose1');
}
function addFooterKeyboard() {
    if ($('#clientsCTA1').length > 0) {
        $('#clientsDropDown1 #clientsDashboard1').fadeOut('slow');
    }
    $('#clientsDropDown1').html('');
    keyboard('clientsCTA');
    $('#clientsDropDown #clientsDashboard').slideToggle({
        direction: "up"
    }, 300);
    $(this).toggleClass('clientsClose');
}
function playSound() {
    document.getElementById('play').play();
}

function removeResponseOverLayer() {
    $("#feedback_bar").removeAttr('class');
}
function manageCategoryListingHeight() {
    $('.category-listing').css({'min-height': '78vh', 'max-height': '78vh', "overflow-y": "auto", "overflow-x": "hidden"});
    $('.category-navigation').height(77);
    $('#catPagination').css({'background': '#f5fffa none repeat scroll 0 0', 'border-radius': '15px', 'height': '64px', 'margin-top': '2px', 'padding': '10px'});
    $(window).bind('resize', function () {
        $('.category-listing').css({'min-height': '78vh', 'max-height': '78vh', "overflow-y": "auto", "overflow-x": "hidden"});
        $('.category-navigation').height(77);
        $('#catPagination').css({'background': '#f5fffa none repeat scroll 0 0', 'border-radius': '15px', 'height': '64px', 'margin-top': '2px', 'padding': '10px'});
        $('.category-listing').jScrollPane();
    });
    $('.category-listing').jScrollPane();
}
function manageMenuAreaHeight() {
    $('.order-menu-list').css({"min-height": "53vh", "max-height": "53vh", "overflow-y": "auto", "overflow-x": "hidden"});
    $(window).bind('resize', function () {
        $('.order-menu-list').css({"min-height": "53vh", "max-height": "53vh", "overflow-y": "auto", "overflow-x": "hidden"});
        //$('.order-menu-list').jScrollPane();
    });
    //$('.order-menu-list').jScrollPane();
}
function ClosePopupBox(boxId) {
    $('#' + boxId).modal('hide');
}
function closecolorbox() {
    $.colorbox.close();
}
function hideModal() {
    $(".modal").removeClass("in");
    $(".modal-backdrop").remove();
    $('body').removeClass('modal-open');
    $('body').css('padding-right', '');
    $(".modal").hide();
}
function closeColorbox() {
    $.colorbox.close();
}
function keyboard(keyboardId) {
    jsKeyboard.init(keyboardId);
    var $firstInput = $(':input').first().focus();
    jsKeyboard.currentElement = $firstInput;
    jsKeyboard.currentElementCursorPosition = 0;
}
function kitchenkeyboard(keyboardId) {
    jsKeyboard.init(keyboardId);
    var $firstInput = $(':input').first().focus();
    jsKeyboard.currentElement = $firstInput;
    jsKeyboard.currentElementCursorPosition = 0;
}

$(document).ready(function () {
    addjqueryValidatorFunction();
    manageWindowHeight();
});

