// Array of elements to exclude during check for empty form
var formElementsExcluded = new Array ("nr_records", "image", "limit_images");

function checkForm (form) {
	if (form) {
		for (var i = 0; i < form.elements.length; i++) {
			if (checkFormElement(form.elements[i])) {
				return;
			}
		}
		alert("No search criteria were given. Please enter at least one term!");
		return false;
	}
}
	
function checkFormElement (element) {
	if (formElementsExcluded && in_array(element.name, formElementsExcluded)) {
		return false;
	}
//	console.dir(element);
	if (element.type == 'checkbox' && element.checked) {
		return true;
	} else if ((element.type == 'text' || element.type == 'select-one') && element.value != '') {
		return true;
	}
	return false;
}


function popup (url, name, height, width) {
	newWindow = window.open(url, name, 'height='+height+',width='+width+',resizable=yes,scrollbars=yes');
	if (window.focus) {
		newWindow.focus();
	}
	return false;
}


function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/in_array
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '',
        strict = !! argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
    return false;
}

function showFullSize (id) {
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		    document.getElementById('fullsize').innerHTML = xmlhttp.responseText;
		}
	};
	resetThumbnailSelection(id);
	xmlhttp.open('GET', 'full_size_image_ajax.php?id=' + id, true);
	xmlhttp.send();
}

function resetThumbnailSelection (id) {
	var thumbs = document.getElementById('thumbsstrip').getElementsByTagName('img');
	for (var i = 0; i < thumbs.length; i++) {
		thumbs[i].className = "thumbdetail";
		if (thumbs[i].id == id) {
			thumbs[i].className = "thumbdetail selected";
		}
	}
}

function checkImagePopup () {
	if (document.getElementById('nrrecordspopup')
			&& document.getElementById('nrrecordspopup').value != 25 
			&& document.getElementById('imagepopup').value != '') {
		alert('When images are included in the search results, the maximum number of results per page is limited to 25.');
		document.getElementById('nrrecordspopup').value = 25;
	}
	if (document.getElementById('limit') 
			&& document.getElementById('limit').checked
			&& document.getElementById('imagepopup').value == '') {
		document.getElementById('limit').checked = false;
	}
}

function checkImageType () {
	if (document.getElementById('limit') && document.getElementById('imagepopup').value == '') {
		alert('Please first select the type of image you want to display.');
		return false;
	}
}

function changeImageType (currentImageType) {
	var alertText = 'Do you want to stop displaying images in the search results? ' +
		'If so, click OK, and this selection menu will be replaced with the option to display more than 25 results per page. ' +
		'Icons will still indicate if images are present for a taxon. ' +
		'You can reselect the display of images by performing a new search.\n\nClick Cancel to undo your selection.';
	if (document.getElementById('imagepopup').value == '') {
		if (!confirm(alertText)) {
			document.getElementById('imagepopup').value = currentImageType;
			return false;
		}
		// Reset image parameters
		document.results.image.value = '';
		document.results.limit_images.value = '';
	}
	document.results.submit();
}
