var submit_string = "submit";

// reload the form based on an event
function reloadForm(subaction) {
	if (document.getElementById('action')) {
		if (document.getElementById('action').value.indexOf(submit_string) != -1) {
			document.getElementById('action').value=document.getElementById('action').value.substring(submit_string.length, document.getElementById('action').value.length);
		}
	}
	if (document.getElementById('subaction')) {
		document.getElementById('subaction').value=subaction;
	}
	if (document.getElementById('form1')) {
		document.getElementById('form1').submit();
	}
	
	return false;
}

// reset the form (by clearing its action and submitting it)
function resetForm() {
	if (document.getElementById('action')) {
		document.getElementById('action').value="refresh";
	}
	if (document.getElementById('form1')) {
		document.getElementById('form1').submit();
	}
	
	return false;
}

// loads a new page when requested by the drop-down list of page numbers
function changePage(list, page, search) {
	// get the value from the list
	var start = list.options[list.selectedIndex].value;
	
	// compile it all into an address
	var url = page + "?start=" + start + "&search=" + search;
	
	// do the redirect
	window.location.href = url;
}

// replace all instances of a string in a string
function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );


    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }

    return str;
}

var indexes = new Array();
indexes["start"] = "null";
indexes["match"] = "null";
indexes["exclude"] = "null";
function addBlankPattern(index, area, prefix) {
	// back up the values, so that changes don't get lost
	var values = new Array();
	var i = 0;
	while (document.getElementById(prefix + "_patterns_" + i)) {
		values[i] = document.getElementById(prefix + "_patterns_" + i).value;
		i++;
	}
		
	// initiate the array values, if this is the first call (this saves javascript fudgery within the page itself)
	if (indexes[prefix] == "null") {
		indexes[prefix]=index;
	}
	
	// initial html to create a new pattern box
	var marker = "<!-- !!!next!!! -->";
	var code = '<div class="patterns"><div class="patternsheader">Pattern #$iplus:</div><input type="hidden" name="$prefix_id_patterns_$i" value="0"/><input type="text" class="widetext" name="$prefix_patterns_$i" id="$prefix_patterns_$i" value="" /></div>';

	// replace the relevant values
	code = replaceAll(code, "$iplus", (indexes[prefix]+1));		
	code = replaceAll(code, "$i", indexes[prefix]);		
	code = replaceAll(code, "$prefix", prefix);		
	
	// add it into the area
	if (document.getElementById(area)) {
		document.getElementById(area).innerHTML = document.getElementById(area).innerHTML.replace(marker, code + marker);
		
		// increase the index for next time...
		indexes[prefix]++;
	}
	
	// restore the values
	i = 0;
	while ((document.getElementById(prefix + "_patterns_" + i)) && (i < values.length)) {
		document.getElementById(prefix + "_patterns_" + i).value = values[i];
		i++;
	}
}

function openURLInNewWindow(title,name,url,width, height, additionalParams) {
	params = '';
	
	if (width > 0) {
		if (width < screen.width) {
			// if we're specifying the width and height, then centre it on the screen
			dialogLeft = (screen.width / 2) - (width / 2);
		} else {
			dialogLeft = 0;
			width = screen.width;
		}
	} else {
		// if no width and height are specified, then fill the screen
		dialogLeft = 0;
		width = screen.width;
	}
	
	if (height > 0) {
		if (height < screen.height) {
			// if we're specifying the width and height, then centre it on the screen
			dialogTop = (screen.height / 2) - (height / 2);
		} else {
			dialogTop = 0;
			height = screen.height - 100;
		}
	} else {
		// if no width and height are specified, then fill the screen (allowing space for taskbar)
		dialogTop = 0;
		height = screen.height - 100;
	}

	if ((additionalParams.indexOf('menubar') != -1) && (is.ie4up)) {
		// ie mucks up the height if we include a menu bar, so do some adjustment
		params = 'width=' + (width - 5) + ',height=' + (height - 23) + ',dependent,left=' + dialogLeft + ',top=' + dialogTop;
	} else {
		params = 'width=' + width + ',height=' + height + ',dependent,left=' + dialogLeft + ',top=' + dialogTop;
	}	

	if (additionalParams != '') {
		params = params + ',' + additionalParams
	}
	
	newWindow = window.open (url, '', params);
	newWindow.document.close();
	newWindow.focus();
}

// hides/shows a collection area
function toggleCollection(id, isChecked) {
	if (document.getElementById("collectionsinfo" + id)) {
		if (isChecked) {
			document.getElementById("collectionsinfo" + id).style.display = "block";
			// if the collection's match field doesn't currently have a value, add a default...
			if ((document.getElementById("action")) && ((document.getElementById("action").value == "submitnew") || (document.getElementById("action").value == "submitcontinue"))) {
				if ((document.getElementById("collections_matches_" + id)) && (document.getElementById("url")) && (document.getElementById("collections_matches_" + id).value=="")){
					document.getElementById("collections_matches_" + id).value = document.getElementById("url").value;
				}
			}
		} else {
			document.getElementById("collectionsinfo" + id).style.display = "none";
		}	
	}
}

// check the form data before sending an obituary request
function validateObituaryRequest() {
	var output = "";

	if ((document.getElementById("name")) && (document.getElementById("name").value == "")) {
		output += "Please supply your name\n";
	}
	if ((document.getElementById("phone")) && (document.getElementById("phone").value == "")) {
		output += "Please supply your phone number\n";
	}

	if (output != "") {
		output = "The following problems were found with your information:\n" + output;
		alert(output);
		return false;
	} else {
		return true;
	}
		
}