function comboKeyPress(e) {
	if(!e) e = window.event;
	var targetObject = e.srcElement||e.currentTarget;
	var key = String.fromCharCode(e.keyCode||e.which||e.charCode).toLowerCase();
	var startIdx = targetObject.selectedIndex||0;
	for(var i = startIdx + 1; i < startIdx + targetObject.options.length; i++) {
		var idx = i%targetObject.options.length;
		var text = targetObject.options[idx].text;
		var chr, j = 0;
		do {
			chr = text.charAt(j++).toLowerCase();
		} while(chr == '\u00A0' && j < text.length);
		if(chr == key) {
			targetObject.selectedIndex = idx;
			e.cancelBubble=true;
			e.returnValue=false;
			return false;
		}
	}
	return true;
}

function showPreview(messageId, contentType) {
    var action = "/newsletter/preview.action?messageId=" + messageId + "&contentType=" + contentType;
    var preview = window.open(action, 'preview', 'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=yes, status=no');
    return false;
}

function showSchoolCard(schoolId) {
    var action = "/school/card.action?id=" + schoolId;
    var card = window.open(action, 'card', 'height=200, width=500, menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=no, status=no');
    return false;
}

//schools
var cachedRegions = new Array();
var cachedCommunes = new Array();
var cachedSchools = new Array();

function onVoivodshipChange(v, r, c, s) {
    resetOptions(r);
    resetOptions(c);
    resetOptions(s);
    var id = v.options[v.selectedIndex].value;
    if (cachedRegions[id] != null) {
        populateOptions(r, cachedRegions[id]);
    } else {
        var action = "/data/region.action";
        var params = "id=" + id;
        var req = new Ajax.Request(action, {
			method: "post",
			parameters: params,
			asynchronous: true,
			onComplete: function(xmlHttpRequest) {
                var data = eval(xmlHttpRequest.responseText);
                cachedRegions[id] = data;
                populateOptions(r, data);
			}
        });
    }
}

function onRegionChange(r, c, s) {
    resetOptions(c);
    resetOptions(s);
    var id = r.options[r.selectedIndex].value;
    if (cachedCommunes[id] != null) {
        populateOptions(c, cachedCommunes[id]);
    } else {
        var action = "/data/commune.action";
        var params = "id=" + id;
        var req = new Ajax.Request(action, {
			method: "post",
			parameters: params,
			asynchronous: true,
			onComplete: function(xmlHttpRequest) {
                var data = eval(xmlHttpRequest.responseText);
                cachedCommunes[id] = data;
                populateOptions(c, data);
			}
        });
    }
}

function onCommuneChange(c, s) {
    resetOptions(s);
    var id = c.options[c.selectedIndex].value;
    if (cachedSchools[id] != null) {
        populateOptions(s, cachedSchools[id]);
    } else {
        var action = "/data/school.action";
        var params = "id=" + id;
        var req = new Ajax.Request(action, {
			method: "post",
			parameters: params,
			asynchronous: true,
			onComplete: function(xmlHttpRequest) {
                var data = eval(xmlHttpRequest.responseText);
                cachedSchools[id] = data;
                populateOptions(s, data);
			}
        });
    }
}

function resetOptions(l) {
    if (l != null) {
        while (l.options.length > 1) {
            l.options[1] = null;
        }
    }
}

function populateOptions(l, data) {
    if (l != null && data != null) {
        for (var i = 0; i < data.length; i++) {
            var option = new Option(data[i].name, data[i].id);
            if (data[i].title != null) {
                option.title = data[i].title;
            }
            l.options[i + 1] = option;

        }
    }
}
