var preSelState = 0;
var preSelDistrict = 0;

function AutoFillStates(url, value, preSel)
    {
	$('state').options.length = 0;
    $('district').options.length = 1;	
	new Ajax.Request(
        url,
            {
                method:'get',
                parameters: {id: value},
                onSuccess: MyStatesSuccess(preSel) /*FillStates*/
        });
}

function AutoFillDistricts(url, value, preSel)
{
	$('district').options.length = 0;
	new Ajax.Request(
    url,
        {
            method:'get',
            parameters: {id: value},
            onSuccess: MyDistrictsSuccess(preSel) /*FillDistricts*/
    });
}

function MyDistrictsSuccess (preSel) {
	return function (rsp)
	{
	    var dists = eval('(' + rsp.responseText + ')');
	    
	    var theOption = new Option;
	    for(var i=0; i<dists.items.length; i++)
	    {
	    	theOption = new Option;
	    	theOption.text = dists.items[i].value;
	    	theOption.value = dists.items[i].id;
	    	if (theOption.value==preSel)
	    		theOption.selected = true;
	    	$('district').options[i] = theOption;
	    }
	}
}

function MyStatesSuccess (preSel) {
	return function (rsp)
	{
	    var states = eval('(' + rsp.responseText + ')');
	    
	    var theOption = new Option;
	    for(var i=0; i<states.items.length; i++)
	    {
	    	theOption = new Option;
	    	theOption.text = states.items[i].value;
	    	theOption.value = states.items[i].id;
	    	if (theOption.value==preSel)
	    		theOption.selected = true;
	    	$('state').options[i] = theOption;
	    }
	}
}

function DoPostBack(form, target) {
	var hid = document.getElementById('__EVENTTARGET');
	hid.value = target;
	form.submit();
	return false;
}

function AddAnchorToFormAction(form, anchor) {
	var url = form.action;
	if (url.indexOf('#')>=0)
		url = url.substring(0, url.indexOf('#'));
	url = url + '#' + anchor;
	form.action = url;
}

function openWindow(url, width, height, name){
	if (!name) { name = ""; }
    var win;
    win = window.open(url, name, "toolbar=no,location=no,directories=no,status=no,menubar=0,resizable=0,copyhistory=no,width=" + width + ",height=" + height + ",scrollbars=1");
    if (((navigator.appName.indexOf("Netscape") != -1) && (parseInt(navigator.appVersion) >=3)) || ((navigator.userAgent.indexOf("MSIE") != -1) && (parseInt(navigator.appVersion) >= 4))) {
        win.focus();
    }
}

function CheckDelete(url, text) {
	if (text=='')
		text = 'Wollen Sie diesen Datensatz wirklich löschen?';
	if (confirm(text))
	{
		location.href = url;
		return false;
	}
	else
		return false;
}

function Show30MoreLinks(lnkId, tblId, hlId) {
	var lnk = document.getElementById(lnkId);
	var tblToShow = document.getElementById(tblId);
	var lnkCnt = document.getElementById(hlId);
	lnk.style.display = 'none';
	tblToShow.style.display = 'block';
	lnkCnt.value = parseInt(lnkCnt.value) + 30;
}
