function filterSelectPrimary() {
    var primaryCatId = $j('#pri_cat').val();

	if (primaryCatId > 0) {

        var secCat = $j('#sec_cat');

		secCat.empty();
		secCat.disabled = true;
		secCat.append('<option value="">Loading...</option>');

        $j("#degree_type").empty();

		$j.ajax({
            url: '/applejacks/ajax.homepagefilter.php',
			data: 'action=select_concentration&pid=' + primaryCatId,
			type: 'post',
            dataType:'script',
			success: function(code) {
                $j.globalEval(code);
				secCat.disabled = false;
			}
		});

        filterSelectConcentration();
	}
}

function filterSelectConcentration() {

    var degreeType = $j("#degree_type");
    var secondaryCatId = $j('#sec_cat').val();
    var filterByCatId = (secondaryCatId > 0) ? secondaryCatId : $j('#pri_cat').val();
    
    degreeType.empty();
    degreeType.disabled = true;
    degreeType.append('<option value="">Loading...</option>');

    $j.ajax({
        url: '/applejacks/ajax.homepagefilter.php',
        data: 'action=select_degree_type&cid='+ filterByCatId + '&sid=' + degreeType[0].id,
        type: 'post',
        dataType:'script',
        success: function(code) {
            $j.globalEval(code);
            degreeType.prepend('<option value="0" selected="selected"></option>');
            degreeType.disabled = false;
        }
    });
}

function doSubmit() {
    var pri_cat = $j('#pri_cat').val();
    var sec_cat = $j('#sec_cat').val();
    var degree_type = $j('#degree_type').val();
    var url = '';
    
     $j.ajax({
        url: '/applejacks/ajax.homepagefilter.php',
        data: 'action=get_correct_url&pc_id='+ pri_cat + '&c_id=' + sec_cat + '&dl_id='+degree_type,
        type: 'POST',
        dataType:'script',
        success: function(textResponse) {
            url = textResponse;
            window.location = url;
        }
    });
 	return false;
/*
    var pri_cat_selected_text = $j('option', pri_cat).get(pri_cat.attr("selectedIndex")).text;
    
    var url = '';

    if (degree_type.val() == "0") {
        url = "/Degrees/" + cleanName(pri_cat_selected_text) + "-" + pri_cat.val() + "/";
    }
    else {
        var dl_selected_text = $j('option', degree_type).get(degree_type.attr("selectedIndex")).text;
        url = "/DegreeLevels/" + cleanName(dl_selected_text) + "-" + degree_type.val() + "/" + cleanName(pri_cat_selected_text) + "-" + pri_cat.val() + "/";
    }
    
    if (sec_cat.val() > 0) {
        var sec_cat_text = $j('option', sec_cat).get(sec_cat.attr("selectedIndex")).text;
        url += cleanName(sec_cat_text) + "-" + sec_cat.val() + "/";
    }
   */ 
}

function cleanName(text) {
    var step1 = text.replace(/^\s*(\S.*\S)\s*$/, function(match, p1) {return p1;});
    return step1.replace(/[^A-z]/g,'-');
}
