$(document).ready(function () {
    
   //setup the date pickers 
   var picker = loadDatePickers();
   //setup the tool tips
   var tool_tips = ini_toolTips();
   //setup the confirm dialog boxes
   var promts = ini_confirm();
   
   $('.error p').click(function(){
       $(this).toggle();
   });     
   
});

function ini_confirm(){
    if($('.js-prompt').length){
        
        $('.js-prompt').click(function(e) {
            var $tag = $(this)[0].tagName;
            e.preventDefault();           
            
            //its a link
            if($(this).attr('href') !== undefined)
            {
                 var targetUrl = $(this).attr("href");
            }
            //else should get the rel
            else
            {
                var targetUrl = $(this).attr("rel");
            }
            
            if (confirm($(this).attr("title"))) {
                window.location = targetUrl;
            }else{
                console.log(targetUrl);
            }
        });
    }
}

function ini_toolTips() {
    if($('.tip').length){
        $('.tip').tooltip({
        track: false, 
        delay: 0, 
        showURL: false, 
        showBody: " - ", 
        extraClass: "tooltip",
        fade: 250});
    }
    
}

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
    return true;
}

function round(num, n) {
    var tmp = Math.pow(10, n);
    var n = Math.round(num * tmp) / tmp;
    return n.toFixed(2);
}

function check(arr)
{
    var error = false;
    for( var i in arr)
    {
        if($('#'+arr[i]).val() == null || $('#'+arr[i]).val() == '')
        {
            $('#'+arr[i]).css('border','1px solid red').css('background-color','#fab8b8');
            error = true;
        }
        else $('#'+arr[i]).css('border','1px solid #aaa').css('background-color','#fff');
    }
    
    if(error) return false;
    else return true;
}

function validate(f) {
    var o=/^[a-zA-Z0-9]$/;
    return o.test(f);
}

function setMessage(message_type, message) {
    
    
    var box = '<div id="notifacition"><div class="' + message_type + '"><p>' + message + '</p><small>Close Message</small></div></div>';
    $('#notices').after(box);
    fadeMessageBox();
    
}


function editqty(id)
{
    $('#edit_'+id).hide();
    $('#update_'+id).show();
    $('#qty_'+id).attr('disabled','').focus().select();
}

/* finds the date picker and loads them*/
function loadDatePickers(){
    if($(".date-picker").length)
    {
        $(function() {
            $(".date-picker").datepicker({dateFormat: 'dd-mm-yy'});
        }); 
    }
    
}

function ini_tabs() {
    if($("#tabs").length){
        $("#tabs").children('ul.tabs').children('li').each(function(){
            $($(this).attr('rel')).hide();
            $(this).click(function() {
                var el = $('#'+$(this).attr('rel'));
                $("#tabs").children('div').hide();
                $('.tabs').children('li').children('span').removeClass('selected-tab');
                
                $(el).fadeIn();
                $(this).children('span').addClass('selected-tab');
            });    
        });
    }
}

