function checkBox(mouseevent, box, i, theclass) {
    selected_row_style = "selected";
    row = $('#tr' + i)
    if (mouseevent == "click") {
        if (box.attr('checked') == true) { 
            box.attr('checked', false);
            row.removeClass().addClass(theclass);
            $(this).trigger('mouseout');
        } else { 
            box.attr('checked', true);
            row.removeClass().addClass(theclass);
            $(this).trigger('mouseout');
        }
    } else if (mouseevent == "over") {
        if (box.attr('checked') == false) {
            row.removeClass().addClass(theclass);
            $(this).trigger('mouseout');
        }
    } else if(mouseevent == "out") {
        (box.attr('checked') == false) ? row.removeClass().addClass(theclass) : row.removeClass().addClass(selected_row_style);
    } else if (mouseevent == "toggle_all") {
        which = $('#toggle_all').attr('checked');
        $("input[type='checkbox']:not([disabled='disabled'])").attr('checked', which);
        $('table tr').each(function() {
            if ($(this).attr('class') != 'rows-header') {
                $(this).trigger('mouseout');
            }
        });
    }      
}

function page(p) {
    query_string = new Querystring();
    page_url = '?p=' + p;
    if (query_string.get('q')) {
        page_url = page_url + '&q=' + query_string.get('q');
    }
    if (query_string.get('s')) {
        page_url = page_url + '&s=' + query_string.get('s');
    }
    window.location = page_url;
}

function csv() {
    query_string = new Querystring();
    page_url = '?csv';
    if (query_string.get('q')) {
        page_url = page_url + '&q=' + query_string.get('q');
    }
    if (query_string.get('s')) {
        page_url = page_url + '&s=' + query_string.get('s');
    }
    window.location = page_url;
}

function set_required_fields(arr){
    for (i=0; i<arr.length; i++){
        $('#' + arr[i]).parent().parent().children(':first').children().addClass('required');
    }
}
function selections_validate(deliver_to, total_count, show_confirm) {
    var checkbox_choices = 0;

    for (counter = 0; counter < total_count; counter++) {
        var elem = 'chk' + (counter + 1);
        if (document.selections.elements[elem].checked) {
            checkbox_choices = checkbox_choices + 1;
        }
    }

    if (checkbox_choices < 1) {
        alert("Please tick at least one item from the list below.");
    } else {
        if (show_confirm) {
            if (confirm('Are you sure you want to delete all selected items.\nPLEASE NOTE: ALL related items will also be deleted!')) {
                document.selections.action = deliver_to;
                document.selections.submit();
            }
        } else {
            document.selections.action = deliver_to;
            document.selections.submit();
        }
    }
}

function toggle_add_new(element) {
    if ($('#add_new_cancel_'+ element).html() == "Cancel") {
        $('#id_'+ element).parent().html(original[element]);
    } else {
        original[element] = $('#id_'+ element).parent().html();
        new_html = '<input type="text" name="'+ element +'" id="id_'+ element +'" style="vTextInput" maxlength="35">';
        new_html = new_html + '&nbsp;<a href="#1" onClick="toggle_add_new(\''+ element +'\');" id="add_new_cancel_'+ element +'" class="add-new-link">Cancel</a>';
        $('#id_'+ element).parent().html(new_html);
    }
}

function init_add_new(element) {
    e = $('#id_'+ element);
    if (e) {
        html = e.parent().html();
        e.parent().html(html + '&nbsp;<a href="#1" onClick="toggle_add_new(\''+ element +'\');" class="add-new-link">Add New</a>');
        if (typeof(original) === 'undefined') {
            original = {};
        }
        original[element] = $('#id_'+ element).parent().html();
    }    
}

/* Print */
var message = "Print this Page";
function printpage(o){
    alert("Please set your printer orientation to " + o +" and your paper size to A4")
    window.print()
}

function prov(country){
    if (country == 'South Africa') {
        $('#id_province').parent().parent().hide();
        $('#id_sa_province').parent().parent().show();
    }
    else {
        
        $('#id_province').parent().parent().show();
        $('#id_province').val('');
        $('#id_sa_province').parent().parent().hide();
    }
}

