﻿function checkallboxes() {
    // Select all checkbox below this level
    $("input[type=checkbox]:checked").each(function() {
        // Set Tick the current level
        var allSelectedChildren = true;
        jQuery(this).parents("ul").each(function() {
            if (!jQuery(this).attr("donecheck")) {
                 jQuery(this).attr("donecheck", true);
                allSelectedChildren = true;
                jQuery(this).parent().find(":checkbox").each(function() {
                    if (!jQuery(this).is(":checked")) {
                        allSelectedChildren = false;
                        return false;
                    }
                });
                jQuery(this).parent().prev().find(":checkbox").attr("checked", allSelectedChildren);
            }
        });
        jQuery(this).parents("ul").each(function() {
            jQuery(this).parent().prev().find("img.tick").toggle(true);
        });
    });
    // second level check.
    $("input[type=checkbox]:checked").each(function() {
        // Set Tick the current level
        var allSelectedChildren = true;
        jQuery(this).parents("ul").each(function() {
            if (!jQuery(this).attr("donecheck2")) {
                jQuery(this).attr("donecheck2", true);
                allSelectedChildren = true;
                jQuery(this).parent().find(":checkbox").each(function() {
                    if (!jQuery(this).is(":checked")) {
                        allSelectedChildren = false;
                        return false;
                    }
                });
                jQuery(this).parent().prev().find(":checkbox").attr("checked", allSelectedChildren);
            }
        });
    });
    // SIC Codes third check!
    $("input[type=checkbox][id*=sic]:checked").each(function() {
        // Set Tick the current level
        var allSelectedChildren = true;
        jQuery(this).parents("ul").each(function() {
            if (!jQuery(this).attr("donecheck3")) {
                jQuery(this).attr("donecheck3", true);
                allSelectedChildren = true;
                jQuery(this).parent().find(":checkbox").each(function() {
                    if (!jQuery(this).is(":checked")) {
                        allSelectedChildren = false;
                        return false;
                    }
                });
                jQuery(this).parent().prev().find(":checkbox").attr("checked", allSelectedChildren);
            }
        });
    });
}


/*
function checkallboxes() {
            // Select all checkbox below this level
        $("input[type=checkbox]:checked").each(function() {
            // Set Tick the current level
            var allSelectedChildren = true;
            jQuery(this).parents("ul").each(function() {
            allSelectedChildren = true;
            jQuery(this).parent().find(":checkbox").each(function() {
                if (!jQuery(this).is(":checked")) {
                    allSelectedChildren = false;
                    return false;
                }
            });
            jQuery(this).parent().prev().find(":checkbox").attr("checked", allSelectedChildren);
        });
        jQuery(this).parents("ul").each(function() {
            jQuery(this).parent().prev().find("img.tick").toggle(true);
        });
    });
}
*/

function removesuppresscheck(selectionid) {
jQuery("#suppresslist").children().find('span[RealValue="' + selectionid + '"]"').children("input[type=checkbox]:first").attr("checked", false);
}

function removeprox() {
jQuery("input[type=text][id*=PROXpostcode]").val('');
jQuery("input[type=text][id*=PROXproximity]").val('');
}

function editsection(stab, section) {
jQuery("#tabs").tabs('select', stab);
if (stab == 0) {

var sectionNo = section - 1;
var checkElement = $("#businessmenu").children("li.shade:eq(" + sectionNo + ")").next().children("ul");
var parent = $("#businessmenu").attr("id");

if (parent.length > 0) {
if ($('#' + parent).hasClass('noaccordion')) {
$(this).parent().next().children("ul").slideToggle('normal', function() {
if ($(this).is(':visible')) {
checkElement.parent().prev().find("img.expand").attr("src", "/images/treeview/minus.gif");
} else {

checkElement.parent().prev().find("img.expand").attr("src", "/images/treeview/plus.gif");
}
});
return false;
}

// Contract
if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
if ($('#' + parent).hasClass('collapsible')) {

$('#' + parent + ' ul:visible').slideUp('normal', function() {
$('#' + parent + ' img.expand').attr("src", "/images/treeview/plus.gif");
});
}
return false;
}

// Expand
if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
$('#' + parent + ' ul:visible').slideUp('normal', function() {
$('#' + parent + ' img.expand').attr("src", "/images/treeview/plus.gif");
});
checkElement.slideDown('normal', function() {
checkElement.parent().prev().find("img.expand").attr("src", "/images/treeview/minus.gif");
});
return false;
}
}
} else if (stab == 1) {
// Locations
if (section == 10) {
// RDA Region
} else if (section == 11) {
// Postal Area
} else if (section == 12) {
// County
} else if (section == 13) {
// Proximity
}
} else if (stab == 2) {
var sectionNo = section - 1;
var checkElement = $("#responsibilities").children("li.shade:eq(" + sectionNo + ")").next().children("ul");
var parent = $("#responsibilities").attr("id");

if (parent.length > 0) {
if ($('#' + parent).hasClass('noaccordion')) {
$(this).parent().next().children("ul").slideToggle('normal', function() {
if ($(this).is(':visible')) {

checkElement.parent().prev().find("img.expand").attr("src", "/images/treeview/minus.gif");
} else {

checkElement.parent().prev().find("img.expand").attr("src", "/images/treeview/plus.gif");
}
});
return false;
}

// Contract
if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
if ($('#' + parent).hasClass('collapsible')) {

$('#' + parent + ' ul:visible').slideUp('normal', function() {
$('#' + parent + ' img.expand').attr("src", "/images/treeview/plus.gif");
});
}
return false;
}

// Expand
if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
$('#' + parent + ' ul:visible').slideUp('normal', function() {
$('#' + parent + ' img.expand').attr("src", "/images/treeview/plus.gif");
});
checkElement.slideDown('normal', function() {
checkElement.parent().prev().find("img.expand").attr("src", "/images/treeview/minus.gif");
});
return false;
}
}
}
}

function removecheck(sectionidstr, section, value) {
secid = (2 * section);
jQuery("#" + sectionidstr).children("li:nth-child(" + secid + ")").find("span[RealValue=" + value + "]").children("input[type=checkbox]:first").attr("checked", false);
checkfunc(jQuery("#" + sectionidstr).children("li:nth-child(" + secid + ")").find("span[RealValue=" + value + "]").children("input[type=checkbox]:first"))
}

/* Hide Location Sections */
function postalhide() {
    $('#postalareatr').hide();
    $('#postalareatr').find(":checkbox").attr("checked", false);
    $('#postalareatr').find("img.tick").hide();
}
function countyhide() {
    $('#countytr').hide();
    $('#countytr').find(":checkbox").attr("checked", false);
    $('#countytr').find("img.tick").hide();
}
function rdahide() {
    $('#rdaregiontr').hide();
    $('#rdaregiontr').find(":checkbox").attr("checked", false);
    $('#rdaregiontr').find("img.tick").hide();
}
function proxhide() {
    $('#proximitytr').hide();
    $('#proximitytr').find(":input").attr("value", "");
}

/* Hide Fields Required Info */
function singlehide() {
    $('#singleusetr').hide();
}
function multihide() {
    $('#multiusetr').hide();
}
function initMenus() {
    $('ul.menu li a').unbind();
    $('ul.menu li a').click(
		        function(e) {
		            e.preventDefault();
		            var checkElement = $(this).parent().next().children("ul");
		            var parent = this.parentNode.parentNode.id;

		            if (parent.length > 0) {
		                if ($('#' + parent).hasClass('noaccordion')) {
		                    $(this).parent().next().children("ul").slideToggle('normal', function() {
		                        if ($(this).is(':visible')) {

		                            checkElement.parent().prev().find("img.expand").attr("src", "/images/treeview/minus.gif");
		                        } else {

		                            checkElement.parent().prev().find("img.expand").attr("src", "/images/treeview/plus.gif");
		                        }
		                    });
		                    return false;
		                }

		                // Contract
		                if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
		                    if ($('#' + parent).hasClass('collapsible')) {

		                        $('#' + parent + ' ul:visible').slideUp('normal', function() {
		                            $('#' + parent + ' img.expand').attr("src", "/images/treeview/plus.gif");
		                        });
		                    }
		                    return false;
		                }

		                // Expand
		                if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
		                    $('#' + parent + ' ul:visible').slideUp('normal', function() {
		                        $('#' + parent + ' img.expand').attr("src", "/images/treeview/plus.gif");
		                    });
		                    checkElement.slideDown('normal', function() {
		                        checkElement.parent().prev().find("img.expand").attr("src", "/images/treeview/minus.gif");
		                    });
		                    return false;
		                }
		            }
		        });
        }

    function checkfunc(currentcheck) {
        // Select all checkbox below this level
        $(currentcheck).closest("li").next().find(":checkbox").attr("checked", $(currentcheck).is(':checked'));

        // Set Tick all levels below this one
        jQuery(currentcheck).parent().next().find("img.tick").toggle($(currentcheck).is(':checked'));

        // Set Tick the current level
        jQuery(currentcheck).parent().find("img.tick").toggle($(currentcheck).is(':checked'));

        var allSelectedChildren = true;
        var anySelectedChildren = false;
        jQuery(currentcheck).parents("ul").each(function() {
            allSelectedChildren = true;
            anySelectedChildren = false;
            jQuery(this).parent().find(":checkbox").each(function() {
                if (!jQuery(this).is(":checked")) {
                    allSelectedChildren = false;
                } else {
                    anySelectedChildren = true;
                }
            });
            jQuery(this).parent().prev().find("img.tick").toggle(anySelectedChildren);
            jQuery(this).parent().prev().find(":checkbox").attr("checked", allSelectedChildren);
        });
    }


function initClicks() {
    jQuery(":checkbox").unbind();
    jQuery(":checkbox").click(function() {
        checkfunc(this);
    });
}

function selectLocation(selectedrad) {
    if (selectedrad == 10) {
        $('#rdaregiontr').show();

        postalhide();
        countyhide();
        proxhide();


    } else if (selectedrad == 11) {

        rdahide();

        $('#postalareatr').show();

        countyhide();
        proxhide();

    } else if (selectedrad == 12) {
        rdahide();

        postalhide();

        $('#countytr').show();

        proxhide();

    } else if (selectedrad == 13) {
        rdahide();

        postalhide();

        countyhide();

        $('#proximitytr').show();
    } else {
        rdahide();

        postalhide();

        countyhide();
        proxhide();
    }
}

function locationClicks() {
    jQuery("input[type=radio][id*=locationrb]").unbind();
    jQuery("input[type=radio][id*=locationrb]").click(function() {
        var selectedrad = $(this).attr("value");
        var selectedint = 0;
        if (selectedrad == "RDARegion") {
            selectedint = 10;
        } else if (selectedrad == "PostalArea") {
            selectedint = 11;
        } else if (selectedrad == "County") {
            selectedint = 12;
        } else if (selectedrad == "Proximity") {
            selectedint = 13;
        } 
        selectLocation(selectedint);
    });
}

// jQuery Load
$(document).ready(function() {

    jQuery("#tabs").tabs({ show: function(event, ui) { if (ui.index == "4") { postListener(); } } });

    hidestuff();
    jQuery("#starttd").show();
    jQuery('#rdaregiontr').find("img.tick").hide();
    jQuery('#postalareatr').find("img.tick").hide();
    jQuery('#countytr').find("img.tick").hide();
    jQuery('#proximitytr').find("img.tick").hide();

    $('#rdaregiontr').hide();
    $('#postalareatr').hide();
    $('#countytr').hide();
    $('#proximitytr').hide();

    singlehide();
    multihide();



});

function hidestuff() {
    $('#summaryscreen ul').hide();
    $("ul.menu").find("img.tick").hide();
    $(".expand").css("cursor", "hand");
    $('ul.menu ul').hide();
}

function showsummary() {
    $('#summaryscreen ul').show();
}

// .net Load
// Runs on Page Load (Firsttime and Postbacks)
function pageLoad() {

    if ($('#summaryscreen ul').length > 0) {
        $('#summaryscreen ul').hide();
    }

    $("#SaveDialog").dialog({  modal: true, closeOnEscape: false, open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); } });
    $("#UpdateDialog").dialog({ autoOpen: false, modal: true, closeOnEscape: false, open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); } });
    $("#OrderDialog").dialog({ autoOpen: false, modal: true, closeOnEscape: false, open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); } });

    if ($("#SaveDialog").dialog('isOpen')) {
        $("#SaveDialog").dialog('close');
    }
    if ($("#UpdateDialog").dialog('isOpen')) {
        $("#UpdateDialog").dialog('close');
    }
    if ($("#OrderDialog").dialog('isOpen')) {
        $("#OrderDialog").dialog('close');
    }
    
    initMenus();
    initClicks();
    locationClicks();
    checkhide();

    if (window.fieldsRequired) {
        fieldsRequired();
    }
    if (window.dataUsage) {
        dataUsage();
    }
}

function handleError() {
    alert('a');
    if ($("#SaveDialog").dialog('isOpen')) {
        $("#SaveDialog").dialog('close');
    }
    if ($("#UpdateDialog").dialog('isOpen')) {
        $("#UpdateDialog").dialog('close');
    }
    if ($("#OrderDialog").dialog('isOpen')) {
        $("#OrderDialog").dialog('close');
    }
}

function checkhide() {

    if (jQuery("input[type=radio][id*=datausagerb]").length > 0) {
        var selecteduse = jQuery("input[type=radio][id*=datausagerb]:checked").val();
        if (selecteduse == "SingleUse") {
            $('#singleusetr').show();
            if ($("input[type=radio][id*=rentusagerb1]").attr("checked") || $("input[type=radio][id*=rentusagerb2]").attr("checked") || $("input[type=radio][id*=rentusagerb3]").attr("checked")) {
                
                if ($("input[type=radio][id*=rentusagerb1]").attr("checked")) {
                
                    $("input[type=radio][id*=rentusagerb1]").closest(".conttable").parent().attr("style", "background-color:#EAF4FD;border:2px solid #0071BB;");
                } else if ($("input[type=radio][id*=rentusagerb2]").attr("checked")) {
                
                    $("input[type=radio][id*=rentusagerb2]").closest(".conttable").parent().attr("style", "background-color:#EAF4FD;border:2px solid #0071BB;");
                } else if ($("input[type=radio][id*=rentusagerb3]").attr("checked")) {
                
                    $("input[type=radio][id*=rentusagerb3]").closest(".conttable").parent().attr("style", "background-color:#EAF4FD;border:2px solid #0071BB;");
                }
                $('#loaded').show();
            } else {
                $('#loaded').hide();
            }
            multihide();
        } else if (selecteduse == "MultiUse") {
            singlehide();
            $('#multiusetr').show();
            $('#loaded').show();
        } else {
            singlehide();
            multihide();
            $('#loaded').hide();
        }
    }
}