var popupWindowAssoc = new Object();

function openPopUpWindow(uri, name) {
    WIDTH = 630;
    HEIGHT = 550;
    if ((!popupWindowAssoc['name']) || (popupWindowAssoc['name'].closed)) {
        x = (screen.availWidth-WIDTH-10)/2;
        y = (screen.availHeight-HEIGHT-30)/2;
        popupWindowAssoc['name'] = open(uri, name, "menubar=no,status=yes,scrollbars=yes,dependent=no,resizable=yes,width=" + WIDTH + ",height=" + HEIGHT + ",left=" + x + ",top=" + y);
    }
    popupWindowAssoc['name'].innerWidth = WIDTH;
    popupWindowAssoc['name'].innerHeight = HEIGHT;
    popupWindowAssoc['name'].focus();
}

function openImagePopUpWindow(uri, name,beschreibung,titel) {
	if (!beschreibung) beschreibung = '';
	//beschreibung = encodeURI(beschreibung);
    WIDTH = 630;
    HEIGHT = 550;
    if ((!popupWindowAssoc['name']) || (popupWindowAssoc['name'].closed)) {
        x = (screen.availWidth-WIDTH-10)/2;
        y = (screen.availHeight-HEIGHT-30)/2;
        popupWindowAssoc['name'] = open("displayImage.php?image="+uri+"&text="+beschreibung+"&titel="+titel, name, "menubar=no,status=yes,scrollbars=yes,dependent=no,resizable=yes,width=" + WIDTH + ",height=" + HEIGHT + ",left=" + x + ",top=" + y);
    }
    popupWindowAssoc['name'].innerWidth = WIDTH;
    popupWindowAssoc['name'].innerHeight = HEIGHT;

    popupWindowAssoc['name'].focus();
}

function openImagePopUpWindow2(id) {
    WIDTH = 630;
    HEIGHT = 550;

    x = (screen.availWidth-WIDTH-10)/2;
    y = (screen.availHeight-HEIGHT-30)/2;
    popupWindowAssoc['name'] = open("displayImage.php?imageId="+id, "Bild", "menubar=no,status=yes,scrollbars=yes,dependent=no,resizable=yes,width=" + WIDTH + ",height=" + HEIGHT + ",left=" + x + ",top=" + y);

    popupWindowAssoc['name'].innerWidth = WIDTH;
    popupWindowAssoc['name'].innerHeight = HEIGHT;

    popupWindowAssoc['name'].focus();
}

function openWindow(uri, name) {
    var window;
    if ((!window) || (window.closed)) {
        window = open(uri, name);
    }
    window.focus();
}




function showCalendarPopup(fieldNames, anchorName, calendarPrefix, calendarDiv) {
	dateFields = fieldNames;

	try {
		tmp = document.getElementById( calendarDiv )
	} catch(e) {alert( "div "+calendarDiv +" nicht gefunden " ); }

	var calendar = new CalendarPopup(calendarDiv);
	calendar.setMonthNames('Januar','Februar','M?rz','April','Mag','Juni','Juli','August','September','Oktober','November','Dezember');
	calendar.setDayHeaders('S','M','D','M','D','F','S');
	calendar.setWeekStartDay(1);
	calendar.offsetX = -113;
	calendar.setTodayText("Heute");
	calendar.setReturnFunction("setMultipleDateValues");
	if ( calendarPrefix ) calendar.setCssPrefix( calendarPrefix );
	try {
		tmp = document.getElementById( anchorName )
	} catch(e) {alert( "anker "+ anchorName +" als div nicht gefunden" ); }
	try {
		tmp = document.getElementsByName( anchorName )
	} catch(e) {alert( "anker "+ anchorName +" als name nicht gefunden" ); }
	calendar.showCalendar( anchorName );
	return false;
}




var dateFields;

function setMultipleDateValues(y,m,d) {

	document.getElementsByName(dateFields[0])[0].value = ((d < 10)?'0':'') + d;
	document.getElementsByName(dateFields[1])[0].value = ((m < 10)?'0':'') + m;
	document.getElementsByName(dateFields[2])[0].value = y;
}

var imagesArray;
var imagesNames;
var imagesDir;

function setImagesDir(imgDir) {
	imagesDir = imgDir;
}

function showPreviewImage(name, id) {
	if (id == '') {
		document.getElementById(name + '_image_div').innerHTML = 'kein Bild gew?hlt';
		return;
	}

	for (j = 0; j < imagesArray.length; j++) {
		if (imagesArray[j][0] == id) {
			previewFilename = imagesArray[j][1];
			document.getElementById(name + '_image_div').innerHTML = '<img src="' + imagesDir + id + '/' + previewFilename + '" />';
			return;
		}
	}
}

function updateImageFields(imageNamesArrayString, imagesArrayString, updateImageSelectFields) {
	// save array
    imagesArray = eval(imagesArrayString);
	// is imageNamesArrayString set?
	if (imageNamesArrayString !== false) {
	    imageNames = eval(imageNamesArrayString);
	}
	if (updateImageSelectFields) {
		for (i = 0; i < imageNames.length; i++) {
			name = imageNames[i];
			// get select field
			selectField = document.getElementsByName(name + '_image_id')[0];
			// save current selected
			id = selectField.value;
			// delete options
			for (j = selectField.options.length-1; j >= 0; j--) {
				selectField.options[j] = null;
			}
			// add options
			isIdInList = false;
			for (j = 0; j < imagesArray.length; j++) {
				option = new Option(imagesArray[j][2], imagesArray[j][0]);
				selectField.options[j] = option;
				if (id == imagesArray[j][0]) {
					selectField.options[j].selected = true;
					isIdInList = true;
				}
			}
			showPreviewImage(name, id);
		}
	}
}

var filesArray;
var filesNames;

function updateFileFields(fileNamesArrayString, filesArrayString, updateFileSelectFields) {
	// save array
    filesArray = eval(filesArrayString);
	// is fileNamesArrayString set?
	if (fileNamesArrayString !== false) {
	    fileNames = eval(fileNamesArrayString);
	}
	if (updateFileSelectFields) {
		for (i = 0; i < fileNames.length; i++) {
			name = fileNames[i];
			// get select field
			selectField = document.getElementsByName(name + '_file_id')[0];
			// save current selected
			id = selectField.value;
			// delete options
			for (j = selectField.options.length-1; j >= 0; j--) {
				selectField.options[j] = null;
			}
			// add options
			for (j = 0; j < filesArray.length; j++) {
				option = new Option(filesArray[j][1], filesArray[j][0]);
				selectField.options[j] = option;
				if (id == filesArray[j][0]) {
					selectField.options[j].selected = true;
				}
			}
		}
	}
}


