// This file is part of Photos
// Copyright (c) 2001-2004 Alex King
// see LICENSE.txt for more information

function setURL() {
	url = document.selectImageForm.URLfile.value;
	if (url.indexOf('\\')) { // check to swap slashes for backslashes if on a Windows server
		pattern = /\\/g;
		url = url.replace(pattern,'\/');
	}
	url = url.slice(url.lastIndexOf('gallery_photos/'));
	document.selectImageForm.cleanURL.value = url;
}

function setPath() {
	url = document.selectPathForm.URLfile.value;
	if (url.indexOf('\\')) { // check to swap slashes for backslashes if on a Windows server
		pattern = /\\/g;
		url = url.replace(pattern,'\/');
	}
	url = url.slice(url.lastIndexOf('gallery_photos/'));
	url = url.substr(0, url.lastIndexOf('/'));
	document.selectPathForm.path.value = url;
}

function validateFileName(fileName) {
	fileName = fileName.toLowerCase();
	if (fileName.substr((fileName.length - 4), 4) == ".jpg") {
		return true;
	}
	else {
		return false;
	}
}

function setCopyEntryValueType(radioValue) {
	radioArray = document.entryShortcutForm.entry_shortcut_type;
	for (i = 0; radioArray[i]; i++) {
		if (radioArray[i].value == radioValue) {
			radioArray[i].checked = true;
		}
	}
}

function copyEntryData(type, entry) { // fill the entry fields with the previously entered image data

	if (type == "detect") {
		radioArray = document.entryShortcutForm.entry_shortcut_type;
		for (i = 0; radioArray[i]; i++) {
			if (radioArray[i].checked == true) {
				type = radioArray[i].value;
			}
		}
	}
	
	if (type == "last") {
		entry = document.entryShortcutForm.copy_entry_list.options[document.entryShortcutForm.copy_entry_list.selectedIndex].value;
		document.imageForm.title.value = quickEntryData[entry][1];
		document.imageForm.description.value = quickEntryData[entry][2];
		document.imageForm.keywords.value = quickEntryData[entry][3];
		document.imageForm.people.value = quickEntryData[entry][4];
		document.imageForm.event.value = quickEntryData[entry][5];
		if (quickEntryData[entry][6] != "") {
			setSelectToValue(document.imageForm.indoors, quickEntryData[entry][6]);
		}
		document.imageForm.location.value = quickEntryData[entry][7];
		document.imageForm.state.value = quickEntryData[entry][8];
		document.imageForm.country.value = quickEntryData[entry][9];
		document.imageForm.photographer.value = quickEntryData[entry][10];
		
		setFocus();
		return false;
	}
	else if (type == "retrieved") {
		document.imageForm.title.value = quickEntryDataID[1];
		document.imageForm.description.value = quickEntryDataID[2];
		document.imageForm.keywords.value = quickEntryDataID[3];
		document.imageForm.people.value = quickEntryDataID[4];
		document.imageForm.event.value = quickEntryDataID[5];
		if (quickEntryDataID[6] != "") {
			setSelectToValue(document.imageForm.indoors, quickEntryDataID[6]);
		}
		document.imageForm.location.value = quickEntryDataID[7];
		document.imageForm.state.value = quickEntryDataID[8];
		document.imageForm.country.value = quickEntryDataID[9];
		document.imageForm.photographer.value = quickEntryDataID[10];
		
		setFocus();
		return false;
	}
	else if (type == "id") {
		if (entry == '') {
			alert(js_enter_id_to_copy_from);
			document.entryShortcutForm.entry_shortcut_id.focus();
			return false;
		}
		document.entryShortcutForm.shortcut_cleanURL.value = document.imageForm.URL.value;
		document.entryShortcutForm.shortcut_datetaken.value = document.imageForm.datetaken.value;

		return true;
	}
	else {
		return false;
	}
}

function navigateList(command) { 
	document.listControlForm.nav_command.value = command;
	document.listControlForm.submit();
}

function clearEntryData() {
	document.imageForm.title.value = "";
	document.imageForm.description.value = "";
	document.imageForm.keywords.value = "";
	document.imageForm.people.value = "";
	document.imageForm.event.value = "";
	setSelectToValue(document.imageForm.indoors, "");
	document.imageForm.location.value = "";
	document.imageForm.state.value = "";
	document.imageForm.country.value = "";
	document.imageForm.photographer.value = "";
	setFocus();
}

function clearSearchData() {
	setSelectToValue(document.imageForm.orientation, "");
	setSelectToValue(document.imageForm.needdetails, "");
	setSelectToValue(document.imageForm.portfolio, "");
	setSelectToValue(document.imageForm.flash, "");
	setSelectToValue(document.imageForm.camera_make, "");
	setSelectToValue(document.imageForm.camera_model, "");
	setSelectToValue(document.imageForm.fnumber_more, "");
	setSelectToValue(document.imageForm.fnumber_less, "");
	setSelectToValue(document.imageForm.ISO_more, "");
	setSelectToValue(document.imageForm.ISO_less, "");
	setSelectToValue(document.imageForm.shutter_more, "");
	setSelectToValue(document.imageForm.shutter_less, "");
	setSelectToValue(document.imageForm.focal_length_more, "");
	setSelectToValue(document.imageForm.focal_length_less, "");
	document.imageForm.title.value = "";
	document.imageForm.id.value = "";
	document.imageForm.keywords.value = "";
	document.imageForm.people.value = "";
	document.imageForm.event.value = "";
	setSelectToValue(document.imageForm.indoors, "");
	document.imageForm.location.value = "";
	setSelectToValue(document.imageForm.state, "");
	setSelectToValue(document.imageForm.country, "");
	setSelectToValue(document.imageForm.photographer, "");
	setDateDropDowns('clear', document.imageForm.datetaken_from_month, document.imageForm.datetaken_from_day, document.imageForm.datetaken_from_year);
	setDateDropDowns('clear', document.imageForm.datetaken_to_month, document.imageForm.datetaken_to_day, document.imageForm.datetaken_to_year);
	setFocus();
}

function sizeImg(newWidth, newHeight, imgToSize) {
	imgToSize = "imgToSize = document." + imgToSize + ";";
	eval(imgToSize)
	imgToSize.style.width = newWidth;
	imgToSize.style.height = newHeight;
}

function previewImage(url, orientation) {
	viewImg.src = url;
	
	if (!orientation || orientation == 0) {
		sizeImg('320', '213', 'viewImg');
	}
	else {
		sizeImg('213', '320', 'viewImg');
	}
}

function confirmDelete() {
	toggleDiv('delete');
}

function confirmDeleteFile() {
	if (document.getElementById('delete_file_checkbox') && document.getElementById('delete_file_checkbox').checked == true) {
		toggleDiv('delete');
		toggleDiv('delete_file');
		
		return true;
	}
	else {
		return false;
	}
}

function setFocus() {
	if (document.imageForm && document.imageForm.title) {
		document.imageForm.title.focus();
	}
	else if (document.checkPathForm && document.checkPathForm.path) {
		document.checkPathForm.path.focus();
	}
}

function showLoadFolderThumb(imgURL) {
	document.getElementById('loadFolderThumb').src = imgURL;
}

function validateData() {
	thisDate = document.imageForm.datetaken.value;
	if (thisDate != "") { // date is set
		firstDash = thisDate.indexOf("-");
		secondDash = thisDate.lastIndexOf("-");
		thisLength = thisDate.length;
		if (firstDash == 4 && secondDash == 7 && thisDate.length == 10) {
			return true;
		}
		else {
			alert(js_date_taken_format);
			return false;
		}
	}
	else { // date isn't set
		return true;
	}
}

function validateSearchData() {
	var errors = new Array();
	var fromDay;
	var fromMonth;
	var fromYear;
	var toDay;
	var toMonth;
	var toYear;
	fromDay = document.imageForm.datetaken_from_day.options[document.imageForm.datetaken_from_day.options.selectedIndex].value;
	fromMonth = document.imageForm.datetaken_from_month.options[document.imageForm.datetaken_from_month.options.selectedIndex].value;
	fromYear = document.imageForm.datetaken_from_year.options[document.imageForm.datetaken_from_year.options.selectedIndex].value;
	toDay = document.imageForm.datetaken_to_day.options[document.imageForm.datetaken_to_day.options.selectedIndex].value;
	toMonth = document.imageForm.datetaken_to_month.options[document.imageForm.datetaken_to_month.options.selectedIndex].value;
	toYear = document.imageForm.datetaken_to_year.options[document.imageForm.datetaken_to_year.options.selectedIndex].value;
	from = new Date(parseFloat(fromYear), parseFloat(fromMonth)-1, parseFloat(fromDay));
	to = new Date(parseFloat(toYear), parseFloat(toMonth)-1, parseFloat(toDay));
	if (!validateDate(fromDay, fromMonth, fromYear)) {
		errors[errors.length] = js_date_taken_from_invalid;
	}
	if (!validateDate(toDay, toMonth, toYear)) {
		errors[errors.length] = js_date_taken_to_invalid;
	}
	if (from > to) {
		errors[errors.length] = js_date_taken_range_invalid;
	}
	if (!compareSelectValues(document.imageForm.ISO_more, document.imageForm.ISO_less)) {
		errors[errors.length] = js_ISO_range_invalid;
	}
	if (!compareSelectValues(document.imageForm.fnumber_more, document.imageForm.fnumber_less)) {
		errors[errors.length] = js_aperture_range_invalid;
	}
	if (!compareSelectValues(document.imageForm.shutter_more, document.imageForm.shutter_less)) {
		errors[errors.length] = js_exposure_range_invalid;
	}
	if (!compareSelectValues(document.imageForm.focal_length_more, document.imageForm.focal_length_less)) {
		errors[errors.length] = js_focal_length_range_invalid;
	}
	fromDay = document.imageForm.dateentered_from_day.options[document.imageForm.dateentered_from_day.options.selectedIndex].value;
	fromMonth = document.imageForm.dateentered_from_month.options[document.imageForm.dateentered_from_month.options.selectedIndex].value;
	fromYear = document.imageForm.dateentered_from_year.options[document.imageForm.dateentered_from_year.options.selectedIndex].value;
	toDay = document.imageForm.dateentered_to_day.options[document.imageForm.dateentered_to_day.options.selectedIndex].value;
	toMonth = document.imageForm.dateentered_to_month.options[document.imageForm.dateentered_to_month.options.selectedIndex].value;
	toYear = document.imageForm.dateentered_to_year.options[document.imageForm.dateentered_to_year.options.selectedIndex].value;
	from = new Date(parseFloat(fromYear), parseFloat(fromMonth)-1, parseFloat(fromDay));
	to = new Date(parseFloat(toYear), parseFloat(toMonth)-1, parseFloat(toDay));
	if (!validateDate(fromDay, fromMonth, fromYear)) {
		errors[errors.length] = js_last_modified_from_invalid;
	}
	if (!validateDate(toDay, toMonth, toYear)) {
		errors[errors.length] = js_last_modified_to_invalid;
	}
	if (from > to) {
		errors[errors.length] = js_last_modified_range_invalid;
	}
	if (errors.length > 0) {
		var errorString = js_errors_to_correct + "\n";
		for (i = 0; i < errors.length; i++) {
			errorString += errors[i] + "\n";
		}
		alert(errorString);
		return false;
	}
	else { // date range is ok
		return true;
	}
}

function compareSelectValues(moreThan, lessThan) {
	moreThan = moreThan.options[moreThan.options.selectedIndex].value;
	lessThan = lessThan.options[lessThan.options.selectedIndex].value;
	if (moreThan != "" && lessThan != "" && parseFloat(moreThan) > parseFloat(lessThan)) {
		return false;
	}
	else {
		return true;
	} 
}

function validateDate(day, month, year) {
	if (day.length == 0 && month.length == 0 && year.length == 0) {
		return true;
	}
	else if (day != "" || month != "" || year != "") {
		var validDate = new Date(parseFloat(year), parseFloat(month)-1, parseFloat(day));
		if (validDate.getDate() != day || validDate.getMonth() != (month - 1) || validDate.getFullYear() != year) {
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return false;
	}
}

function highlightMenuItem(thisDiv) {
	if (thisDiv.className == "popupMenuItemOn") {
		thisDiv.className = "popupMenuItem";
	}
	else {
		thisDiv.className = "popupMenuItemOn";
	}
}

function setDateDropDowns(action, selMonth, selDay, selYear) {
	if (action == "today") {
		setSelectToValue(selMonth, currentMonth);
		setSelectToValue(selDay, currentDay);
		setSelectToValue(selYear, currentYear);
	}
	else if (action == "last_week") {
		setSelectToValue(selMonth, lastWeekMonth);
		setSelectToValue(selDay, lastWeekDay);
		setSelectToValue(selYear, lastWeekYear);
	}
	else if (action == "last_30") {
		setSelectToValue(selMonth, last30Month);
		setSelectToValue(selDay, last30Day);
		setSelectToValue(selYear, last30Year);
	}
	else if (action == "clear") {
		setSelectToValue(selMonth, "");
		setSelectToValue(selDay, "");
		setSelectToValue(selYear, "");
	}
}

function selectedDate(formName, prefix) {
	var selectedYear;
	var selectedMonth;
	var selectedDay;
	eval("selectedYear = document." + formName + "." + prefix + "_year.options[document." + formName + "." + prefix + "_year.selectedIndex].value;");
	eval("selectedMonth = document." + formName + "." + prefix + "_month.options[document." + formName + "." + prefix + "_month.selectedIndex].value;");
	eval("selectedDay = document." + formName + "." + prefix + "_day.options[document." + formName + "." + prefix + "_day.selectedIndex].value;");
	if ((selectedYear == "" || selectedMonth == "" || selectedDay == "") && (selectedYear != "" || selectedMonth != "" || selectedDay != "")) {
		return "";
	}
	else {
		return selectedYear + "-" + selectedMonth + "-" + selectedDay;
	}
}

function showPicker(type, date) {
// date is expected in YYYY-MM-DD format
	var URL;
	var stats;
	URL = "picker.php?type=" + type + "&date=" + date;

	if (type.indexOf("parent") != -1) {
		stats = "height=500, width=450, scrollbars=yes, toolbar=no, location=no, status=yes";
	}
	else if (type.indexOf("date") != -1) {
		var height = (150 * calMonths) + 160;
		stats = "height=" + height + ", width=315, scrollbars=yes, toolbar=no, location=no, status=yes";
	}
	else {
		stats = "height=500, width=500, scrollbars=yes, toolbar=no, location=no, status=yes";
	}
	window.open(URL, "picker", stats);
}

function pickDate(thisYear, thisMonth, thisDay, target) {
	eval("setSelectToValue(window.opener.document.imageForm." + target + "_month, thisMonth)");
	eval("setSelectToValue(window.opener.document.imageForm." + target + "_day, thisDay)");
	eval("setSelectToValue(window.opener.document.imageForm." + target + "_year, thisYear)");
	window.close();
}

function setSearchLessThan(thisProp) {
	eval("moreThan = document.imageForm." + thisProp + "_more");
	eval("lessThan = document.imageForm." + thisProp + "_less");
	if (lessThan.options[lessThan.options.selectedIndex].value == "") {
		setSelectToValue(lessThan, moreThan.options[moreThan.options.selectedIndex].value);
	}
}

function updateLoadingStatus(done, total, chunk) {
	if (done != -1) {
		var mult = total / 400;
		var completed = parseInt(done / mult);
		if (completed < 1) {
			completed = 1;
		}
		var cap = done + chunk;
		if (cap > total) {
			cap = total;
		}
		document.getElementById('progress_done').width = completed;
		document.getElementById('progress_left').width = 400 - completed;
		if (done < total) {
			var temp = js_processing_n_of_n.replace('__0', (done + 1));
			temp = temp.replace('__1', total);
			document.getElementById('status').innerHTML = temp;
		}
		else {
			document.getElementById('status').innerHTML = js_finished;
		}
	}
	else {
		document.getElementById('loading').style.display = 'none';
		document.getElementById('loaded').style.display = 'block';
		setFocus();
		showThumb();
	}
}