/*
 * Einrichtungssuche
 */
function getBundesland() {
    var myForm = document.forms['ctrl0'];
    if (!myForm) {
        myForm = document.ctrl0;
    }  

	var oSelect = myForm.bundesland;
	
	var selected_value;
	if( oSelect.selectedIndex != 0 )
	    selected_value = oSelect.options[oSelect.selectedIndex].value;
	
	if( isNaN( selected_value ) || selected_value == 0 )
		selected_value = 0;
	return selected_value;
}

function changeCarea( tabid, iCarea )
{
    var iBundesland = getBundesland();
    
    if( iBundesland != 0  )
        url = '/desktopdefault.aspx/tabid-' + tabid + '/carea-' + iCarea + '/idb-' + iBundesland + '/';
    else
        url = '/desktopdefault.aspx/tabid-' + tabid + '/carea-' + iCarea + '/';
    
    document.location.href = url;
    return false;
}

function getCarea()
{
    var sCarea;
    var url = document.location.href;
	iCarea = url.match(/carea-([0-9]+)/);
    if( iCarea == null )
        sCarea = '';
    else
        sCarea = '/carea-' + iCarea[1] + '/';
    return sCarea;
}

function escapeHyphen(string) {
	return escape(string.replace(/-/g, String.fromCharCode(150))); }


function ahb( id, catext ) {
	if( id == undefined || isNaN( id ) )
		return false;
	
	// Indikation 10 ist nur zur Gliederung da
	if( id == 175 )
	    return;
	
	// var sCarea = getCarea();
	sCarea = '/catext-' + escapeHyphen(catext) + '/';;
	
	var iBundesland = getBundesland();
	if( iBundesland == 0 )
		url = '/desktopdefault.aspx/tabid-106/categories-' + id + sCarea + 'cat-' + id + '/';
	else
		url = '/desktopdefault.aspx/tabid-106/categories-' + iBundesland + ',' + id + sCarea + 'cat-' + id + '/';
	document.location.href = url;
	return false;
}

function fach( id, catext ) {
	if( id == undefined || isNaN( id ) )
		return false;
		
	if( catext == undefined || catext == "" )
	    return false;
	
	var iBundesland = getBundesland();
	if( iBundesland == 0 )
		url = '/desktopdefault.aspx/tabid-107/categories-' + id +'/' + 'catext-' + escapeHyphen(catext) + '/';
	else
		url = '/desktopdefault.aspx/tabid-107/categories-' + iBundesland + ',' + id + '/' + 'catext-' + escapeHyphen(catext) + '/';
	document.location.href = url;
	return false;
}

function kb( id, catext ) {
	if( id == undefined || isNaN( id ) )
		return false;
		
	if( catext == undefined || catext == "" )
	    return false;
	    
	var iBundesland = getBundesland();
	if( iBundesland == 0 )
		url = '/desktopdefault.aspx/tabid-108/categories-' + id +'/' + 'catext-' + escapeHyphen(catext) + '/';
	else
		url = '/desktopdefault.aspx/tabid-108/categories-' + iBundesland + ',' + id + '/' + 'catext-' + escapeHyphen(catext) + '/';
	document.location.href = url;
	return false;
}

function region( ) {
	var iBundesland = getBundesland();

	if( iBundesland == 0 )
		return;
	else
		url = '/desktopdefault.aspx/tabid-113/categories-' + iBundesland + '/';
		
	document.location.href = url;
	return false;
}


function setSelected() {
    restoreFontsize();
	
    var url = document.location.href;
    var iSelect;
    var iValue;

    var myForm = document.forms['ctrl0'];
    if (!myForm) {
        myForm = document.ctrl0;
    }  

    // bundesland als kategorie
    id = url.match( /categories-([0-9]+)/ );
    if( id == null )
    {
        iSelect = 0;
    }
    else
        iSelect = id[1];
        
    // es könnte auch in der url stehen
    id2 = url.match(/idb-([0-9]+)/);
    if( id2 != null )
        iSelect = id2[1];
    
    var oSelect = myForm.bundesland;
    if( oSelect )
    {
        for (var i = 0; i <= oSelect.options.length; i ++) {
        	if( oSelect.options[i] ) {
	            if (oSelect.options[i].value == iSelect) {
	                oSelect.options[i].selected = true;
	                break;
	            }
	            else {
	                oSelect.options[i].selected = false;
	            }
	    	}
    	}
    }
}

function changeRegion()
{
	// get selected region and check if it is empty
	var iSelectedRegion = getBundesland();
	if( iSelectedRegion == 0 )
		return;

	// we have a new region so generate the url
	// there could be a selected category that has to be saved
	// 1. If there are two items like categories-x,y we now y needs to be saved
	var newCategory;
	var matches = document.location.href.match(/categories-([0-9]+)(,([0-9]+))?/);
	if( matches[2] == undefined || matches[2] == "" )
	{
		if( matches[1] == undefined || matches[1] == "" )
			newCategory = iSelectedRegion;
		else
		{
			if( isBundesland( matches[1] ) )
				newCategory = iSelectedRegion;
			else
				newCategory = iSelectedRegion + "," + matches[1];
		}
	}
	else
	{
		newCategory = iSelectedRegion + "," + matches[3];
	}

	var catext = document.location.href.match(/catext-([^\/]+)/);
	if( catext[1] == null || catext[1] == "" )
		newUrl = "/desktopdefault.aspx/tabid-" + tabid + "/categories-" + newCategory + "/";
	else
		newUrl = "/desktopdefault.aspx/tabid-" + tabid + "/categories-" + newCategory + "/catext-" + catext[1] + "/";	
	document.location.href = newUrl;
}

function isBundesland( id )
{
	if( id == undefined || id == 0 )
		return false;
		
	if( id >= 127 && id <= 141 )
		return true;
	else
		return false;
}


