function openAttachments(id, custom, common) {
    windowWidth = 600;
    windowHeight = 400;
    myleft = (screen.width) ? (screen.width - windowWidth) / 2 : 100;
    mytop = (screen.height) ? (screen.height - windowHeight) / 2 : 100;
    
    properties = "width=" + windowWidth + ",height=" + windowHeight + ",scrollbars=yes, toolbar=yes, menubar=yes, location=yes, resizable=yes, dependant=yes, top=" + mytop + ",left=" + myleft;
    newwindow = window.open('attach.php?fid=' + id + '&custom=' + custom + '&common=' + common, 'File', properties);
    if (window.focus) {
        newwindow.focus()
    }
}

var confirmBox = false;

function confirmSave() {
    if (confirmBox) {
        if (confirm('Would you like to save your changes before exiting?\nClick "OK" to save or "Cancel" to discard any changes')) {
            document.getElementById('javascriptSubmit').value = '1';
            document.getElementById('mainForm').submit();
        }
    }
}

function turnOnConfirm() {
    confirmBox = true;
}

function confirmAnchors() {
    var wrapper = document.getElementById('wrapper')
    var children = wrapper.getElementsByTagName('a');
    for (i = 0; i < children.length; i++) {
        if (children[i].getAttribute('href').substring(0, 11) != 'javascript:') {
            children[i].onclick = ( function(n) { return function() {document.getElementById('javascriptRedirect').value = n.getAttribute('href'); } } )(children[i])
        }
    }
}

function moreInfo(id) {
    windowWidth = 600;
    windowHeight = 400;
    myleft=(screen.width)?(screen.width-windowWidth)/2:100;    
    mytop=(screen.height)?(screen.height-windowHeight)/2:100;    
    
    properties = "width="+windowWidth+",height="+windowHeight+",scrollbars=yes, toolbar=yes, menubar=yes, location=yes, resizable=yes, dependant=yes, top="+mytop+",left="+myleft;               
    newwindow=window.open('information.php?id=' + id, 'More', properties);
    if (window.focus) {
        newwindow.focus()
    }
}
function printMe(pageName) {
    windowWidth = 970;
    windowHeight = 550;
    myleft=(screen.width)?(screen.width-windowWidth)/2:100;    
    mytop=(screen.height)?(screen.height-windowHeight)/2:100;    
    
    properties = "width="+windowWidth+",height="+windowHeight+",scrollbars=yes, toolbar=yes, menubar=yes, location=no, resizable=no, dependant=yes, top="+mytop+",left="+myleft;               
    newwindow=window.open(pageName, 'PrintReport', properties);
    if (window.focus) {
        newwindow.focus()
    }
}

$(document).ready(function(){

    // EXTERNAL WINDOWS
    $('a[rel=external]').click(function(){
        window.open($(this).attr('href')); return false;
    });

    $('a[rel=popup]').click(function(){
        window.open($(this).attr('href'), 'popUp', 'width=300,height=200,status=0,toolbar=0'); return false;
    });

    // DATE PICKER
    $('input.date').datepicker({dateFormat: 'dd/mm/yy'});
    $('#history #start').datepicker({dateFormat: 'dd/mm/yy'});
    $('#history #end').datepicker({dateFormat: 'dd/mm/yy'});

    // AUTOCOMPLETERS
    $('.autoCompleteE').autocomplete({
        source: 'autocomplete/php/autocomplete.php?field=dataEvidence&term=',
        minLength: 3
    });
    $('.autoCompleteA').autocomplete({
        source: 'autocomplete/php/autocomplete.php?field=dataAction&term=',
        minLength: 3
    });
    $('.autoCompleteP').autocomplete({
        source: 'autocomplete/php/autocomplete.php?field=dataProgress&term=',
        minLength: 3
    });

    // SLIDERS
    $('.slider').each(function(){
        var select = $(this).children('select').eq(0);
        $(select).hide();
        var notify = $(this).find('span').eq(0);
        var slider = $(this).children('div')
        $(slider).slider({ 
            min: 0,
            max: 10,
            step: 1,
            value: select[0].selectedIndex,
            animate: 'fast',
            slide: function( event, ui ) {
                select[0].selectedIndex = ui.value;
                turnOnConfirm();
                $(notify).html(ui.value);
            }
        });
        $(this).children('select').eq(0).change(function() {
            slider.slider( "value", this.selectedIndex );
        });

    });

});
