function catAdd() {
	if($("select#cats").children().length < 5) {
		var sectorKey = $("select#sector").val();
		var sectorVal = $("select#sector :selected").text();

		var serviceKey = $("select#service").val();
		var serviceVal = $("select#service :selected").text();

		var option = "<option value='"+sectorKey+"|"+serviceKey+"'>"+sectorVal+": "+serviceVal+"</option>";
		if(sectorKey != 0 && serviceKey != 0) {
			$("select#cats").append(option);
			$("select#service").val("0");
			$("select#sector").val("0");
		} else {
			alert("Please select both a service and a sector");
		}
	} else {
		alert("You can only select a maximum of 5 categories");
	}
}

function catRemove() {
	var catKey = $("select#cats :selected").remove();
}

function catSelectAll() {
	$("select#cats").children().attr("selected","selected");
}

function clientAdd() {
	var name = $("#client_name").val();
	var conf = $("#client_conf").attr("checked");
	conf = (conf == 0) ? "N" : "Y";


	var option = "<option value='"+name+"|"+conf+"'>"+name+" ("+conf+")</option>";
	if(name != "") {
		$("select#clients").append(option);
		$("input#client_name").val("");
		$("input#client_conf").attr("checked",false);
	}
}

function clientRemove() {
	var catKey = $("select#clients :selected").remove();
}

function clientSelectAll() {
	$("select#clients").children().attr("selected","selected");
}

function clientRegister() {
	clientSelectAll();
	$("input#submit").attr("disabled","disabled");
}

function colourise(target,mode) {
	switch(mode) {
		case 0:
			document.getElementById("logo_"+target).src = "/images/companies/small/"+target+"_null.png"
			break;
		case 1:
			document.getElementById("logo_"+target).src = "/images/companies/small/"+target+".png"
			break;
	}
}

function smallWindow(url) {
	window.open(url,'terms','width=400,height=600,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
}

function replaceChart(qs,target) {
	var data = escape("/includes/ofc/builder.php?")+qs;
	swfobject.embedSWF(
		"/flash/open-flash-chart.swf",
		target,
		"400",
		"400",
		"9.0.0",
		"expressInstall.swf",
		{"data-file":data,"id":target}
	);
}

$(function() {
    $('#browse_service, #browse_sector, #browse_company').change(function() {
        var ser = $('#browse_service :selected').val();
        var sec = $('#browse_sector :selected').val();
        var com = $('#browse_company :selected').val();

        $('#counter').html('<img src="/images/site/ajax-loader.gif" alt="loading" title="loading" class="loading" />');

        $.getJSON('/fetch.php', {mode: "count", service: ser, sector: sec, company: com}, function(data, status) {
            var nums = data['count'].split('');
            var out = '';

            for(var n in nums)
                out += '<img src="/images/site/counter/' + nums[n] + '.png" alt="' + nums[n] + '" title="' + nums[n] + '" />';

            out += ' results';
            $('#counter').html(out);
        });
    });
});